Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Shockwave on January 20, 2008
-
If anyone has a simple explanation, please post it :) Please do not post a reply with jargon as I will not understand it! Thanks.
I would like to create an image in memory (in freebasic), for the sake of simplicity let's say I generate a 100*100 square and fill it with random coloured pixels;
Either one dimensional;
dim picture as uinteger ( 100 * 100 )
for l=0 to 100*100
b=int(rnd(1)*255)
picture(l) = rgb(b,b,b)
next
Or Two Dimensional;
dim picture as uinteger ( 100 , 100 )
for y=0 to 99
for x=0 to 99
b=int(rnd(1)*255)
picture(x,y) = rgb(b,b,b)
next
What would be the most straight forward way to put this image onto the screen using opengl?
I have tried to do some research myself, I found the command glreadpixels but is seems to read pixels from the framebuffer or accumulation buffer, this would not seem to suit me as I need to be able to read a picture in from another part of memory, my one or two dimensional array.
I do not necessarily want to use these as textures, just so that I can put a simple logo on the screen, which brings me to another question, and that is what is the best way to draw the picture once I have it's loading from memory sorted out.
It's going to be pretty essential to my plans for GL as I intend to write small intros that contain proceduraly generated graphics.
Thanks very much in advance and sorry for the nOOb question :)
-
It depends if you want scaling or not. If not,
http://www.deec.uc.pt/~jlobo/opengl/opengl/glDrawPixels.html
If you want scaling, the best way I think (some may argue) is to create a texture. Then draw two triangles filling the screen with the right texture co-ordinates. If thats too much jargon I could post some C, but I'm sure someone will post basic...
Be careful of bitmaps (black/white images). Windows OpenGL has some weird alignment issues. Using full colour images is very safe though.
-
Hey SHocky,
maybe this helps you. Have a look at the example 8-3 on
this page http://glprogramming.com/red/chapter08.html (http://glprogramming.com/red/chapter08.html).
Hope this helps ...
-
the simplest way is: glDrawPixels() => write from memory into the frame buffer. But it's the slowest method.
Otherwise, you need to use textures with glTexImage2D() to create the texture and glTexSubImage2D() to update the pixels in the texture in video memory
I hope this is not too cryptic..
-
I would probably go with using textures as it would allow scaling and rotation if needed, the texture dimensions should be powers of 2 but your image doesn't need to completely fill that. You could put other images into other areas of the same texture which could reduce state changes.
An example I can think of would be to have a font stored in a texture, there's a lot you could do with scrollers that way.
-
Wow, thanks for the quick replies :)
Hey SHocky,
maybe this helps you. Have a look at the example 8-3 on
this page http://glprogramming.com/red/chapter08.html (http://glprogramming.com/red/chapter08.html).
Hope this helps ...
Yep, I have the red book mate, I couldn't get my head around the chapter after reading it through a few times. Thanks for the tip though.
Thanks also to Stormbringer, Taj and Stonemonkey, I will try it with textures then. (I'll try glteximage2d) At the moment I still have not got a clue how to make it grab my memory block but I'll have a go and see if I can figure it out.
Thanks for the replies :)
-
glTexImage2D() creates a power of 2 texture (unless you are using extensions to create rectangular textures). However glTexSubImage2D() can update any portion of the texture and it's not mandatory to update a power-of-2 region => i.e. rectangular regions can be updated in the power-of-2 texture
-
in C the basic code would look like that:
// generate texture ids
glGenTextures(1,&logoId);
// select texture
glBindTexture(GL_TEXTURE_2D,logoId);
// Generate The Texture
glTexImage2D(
GL_TEXTURE_2D,
0,
4,
512,
512,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
NULL
);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // Linear Filtering
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexSubImage2D(
GL_TEXTURE_2D,
0,
0,0,
kLogoWidth,
kLogoHeight,
GL_RGB,
GL_UNSIGNED_BYTE,
logo
);
Obviously, kLogoWidth and kLogoHeight are constants and smaller than 512 (which is used in this example as a power-of-2 size for the texture)
logo is the buffer where your RGBA 8bit pixels are stored...
it should be pretty simple to write the same in FreeBasic I guess (although I never used FB...)
-
There's one way to do that here:
http://dbfinteractive.com/index.php?topic=1847.0
It was in C but you can recode it to freebasic easily.
-
Both of those examples will do it :)
Thanks so much :)
*Shockwave scatters Karmic dust into this thread*
I will post something when it's working :)
-
before you ask it.... remember that OpenGL is a state machine. This means that you get on the screen what the current state of OpenGL is set to produce. If you get weird results, 99% of the time, the current state is wrong in a way or another. This means the current texture is not the right one, or texture coordinates are not in a valid range, current color and blending mode are not correct, etc.
-
oh.. and before I forget it.. while the OpenGL default state should be the same on any machine/graphics card.... IT IS NOT! The implementation is driver/manufacturer dependent...
make sure that you set the correct state all the time to avoid comments from others, like "It's not working on my machine!"
;) happy coding!
-
Ah stormbringer, didn't know you could update parts of the texture like that, I use something like this although I've modified/simplified it a little for some clarity:
function upload_texture(wwidth as integer,height as integer,pixels_address as uinteger pointer)as integer pointer
'set up a pointer for the texture in video mem, it's what's used when binding the texture before drawing
dim vmem_pointer as integer pointer
vmem_pointer=callocate(len(integer))
glGenTextures(1, vmem_pointer)
glBindTexture(GL_TEXTURE_2D, *vmem_pointer)
'set some parameters for texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_linear)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_linear)
'create the texture in video mem and copy the pixel data
glteximage2d(gl_texture_2d,0,gl_rgba,wwidth,height,0,gl_rgba,gl_unsigned_byte,pixels_address)
'return the texture pointer
return vmem_pointer
end function
-
I am giving up for the evening, I am sure that the blend mode is wrong or something because my quad is not textured..
As this is all part of a larger intro I thought it easier to post the whole thing..
Maybe someone can spot my stupid mistake?
The Xor texture should be on the grey quad.
Thanks to Rbraz (I converted and slightly adapted your C source).
'
' GLENZE VECTOR INTRO WITH OPENGL
' BY SCHLOCKWAVE
'===============================================================================
OPTION STATIC
OPTION EXPLICIT
#INCLUDE "GL/GL.BI"
#INCLUDE "GL/GLU.BI"
#INCLUDE "WINDOWS.BI"
DIM SHARED AS INTEGER MOUSE
'===============================================================================
' LOGO XOR TEXTURE GENERATION;
'===============================================================================
DIM SHARED AS UINTEGER LOGOBUFFER ( 256 * 256 )
DIM SHARED TEX1 AS GLUINT
DIM AS UINTEGER X,Y,PIXEL
for y = 0 TO 255
for x = 0 TO 255
PIXEL = X XOR Y
LOGOBUFFER((y * 256)+X) = RGBA(PIXEL,PIXEL,PIXEL,255)
NEXT
NEXT
glGenTextures 1, @tex1
glBindTexture(GL_TEXTURE_2D, tex1)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR)
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 256, 256,GL_RGBA, GL_UNSIGNED_BYTE, @LOGOBUFFER(0))
'===============================================================================
' OPEN A SCREEN 1024 * 768;
'===============================================================================
SCREEN 20,,,3
'===============================================================================
' SET UP OPENGL;
'===============================================================================
GLVIEWPORT 0, 0, 1024, 768 '' Reset The Current Viewport
GLMATRIXMODE GL_PROJECTION '' Select The Projection Matrix
GLLOADIDENTITY
GLUPERSPECTIVE 90.0, 1024/768, 0.1, 50. '' Calculate The Aspect Ratio Of The Window
GLMATRIXMODE GL_MODELVIEW '' Select The Modelview Matrix
GLLOADIDENTITY '' Reset The Modelview Matrix
GLSHADEMODEL GL_SMOOTH '' Enable Smooth Shading
GLCLEARCOLOR 0.0, 0.0, 0.0, 0.00 '' Black Background
GLCLEARDEPTH 1.0 '' Depth Buffer Setup
GLENABLE GL_DEPTH_TEST '' Enables Depth Testing
GLDEPTHFUNC GL_LEQUAL '' The Type Of Depth Testing To Do
GLHINT GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST '' Really Nice Perspective Calculations
dim shared LightAmbient(0 to 3) as single => {0.00, 0.00, 0.00,0.0} '' Ambient Light is 20% white
dim shared LightDiffuse(0 to 3) as single => {0.5, 0.5, 0.5,0.5} '' Diffuse Light is white
dim shared LightPosition(0 to 2) as single =>{0.0, 0.0, 15.0 } '' Position is somewhat in front of screen
dim shared Lightdiffuse2(0 to 3) as single => {1.0, 1.0, 1.0,0.02} '' Diffuse Light is white
dim shared LightPosition2(0 to 2) as single =>{0.0, 10.0, 10.0 } '' Position is somewhat in front of screen
glLightfv( GL_LIGHT1, GL_AMBIENT, @LightAmbient(0)) '' Load Light-Parameters Into GL_LIGHT1
glLightfv( GL_LIGHT1, GL_DIFFUSE, @LightDiffuse(0))
glLightfv( GL_LIGHT1, GL_POSITION, @LightPosition(0))
glLightfv( GL_LIGHT2, GL_DIFFUSE , @Lightdiffuse2(0))
glLightfv( GL_LIGHT2, GL_POSITION, @LightPosition2(0))
gllightf (GL_LIGHT2,GL_SPOT_CUTOFF,60.0)
glEnable(GL_LIGHT1)
glEnable(GL_LIGHT2)
GLSHADEMODEL (GL_SMOOTH)
GLENABLE (GL_LIGHTING)
'===============================================================================
' SUBROUTINES;
'===============================================================================
DECLARE SUB GLENZECUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
DECLARE SUB DRAWGLENZE()
DECLARE SUB CUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
DECLARE SUB DRAWCUBE()
DECLARE SUB CUBE_OBJECTS()
DECLARE SUB COPPERS()
DECLARE SUB LOGO_TEXTURE()
'===============================================================================
' SETUP;
'===============================================================================
' MATERIALS;
DIM SHARED FACE1(0 to 3) AS SINGLE => {1.0, 0.2, 0.0,0.45} :' RED GLENZE FACES
DIM SHARED FACE2(0 to 3) AS SINGLE => {1.5, 1.2, 0.9,0.45} :' WHITE GLENZE FACES
DIM SHARED FACE3(0 to 3) AS SINGLE => {0.7, 0.5, 0.3,1.0} :' WHITE MATERIAL
DIM SHARED FACE4(0 to 3) AS SINGLE => {1.0, 0.1, 0.0,0.45} :' PURPLE FACES
' ROTATIONS;
DIM SHARED XR AS SINGLE : ' X ROTATION
DIM SHARED YR AS SINGLE : ' Y ROTATION
DIM SHARED ZR AS SINGLE : ' Z ROTATION
' HIDE MOUSE POINTER;
MOUSE=SHOWCURSOR(0)
GLENABLE(GL_MULTISAMPLE)
GLENABLE(GL_BLEND)
GLBLENDFUNC(GL_ONE_MINUS_CONSTANT_ALPHA,GL_dst_ALPHA)
GLENABLE (GL_CULL_FACE)
GLENABLE (GL_FOG)
GLHINT(GL_FOG_HINT,GL_NICEST)
GLFOGF(GL_FOG_DENSITY,0.1)
GLFOGF(GL_FOG_START, 5.0)
GLFOGF(GL_FOG_END, 20.0)
GLFOGI(GL_FOG_MODE,GL_EXP2)
GLENABLE(GL_TEXTURE_2D)
'===============================================================================
DIM D AS INTEGER
'===============================================================================
' MAIN LOOP
'===============================================================================
WHILE((GETASYNCKEYSTATE(VK_ESCAPE)<> -32767) )
GLFLUSH()
GLCLEAR GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
CUBE_OBJECTS()
LOGO_TEXTURE()
xr = xr + 0.03
yr = yr + 0.06
zr = zr + 0.07
FLIP
WEND
END
'===============================================================================
' DRAW THE "LOGO"
'===============================================================================
SUB LOGO_TEXTURE()
GLLOADIDENTITY
glTranslatef(0, -2, -6)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex1)
GLBEGIN GL_QUADS
glNormal3f 0.0f, 0.0f, 1.0f
glTexCoord2f 0.0f, 0.0f
glVertex3f -1.0f, -1.0f, 1.0f
glTexCoord2f 1.0f, 0.0f
glVertex3f 1.0f, -1.0f, 1.0f
glTexCoord2f 1.0f, 1.0f
glVertex3f 1.0f, 1.0f, 1.0f
glTexCoord2f 0.0f, 1.0f
glVertex3f -1.0f, 1.0f, 1.0f
GLEND
END SUB
SUB CUBE_OBJECTS()
DIM AS DOUBLE XXX,YYY,ZZZ,R2D,T
T=TIMER*33
R2D=(3.14/180)
DIM L AS INTEGER
CUBE (XR,0,ZR,0,0,-9)
FOR L=0 TO 359 STEP 40
XXX= 9*SIN((L+T)*R2D)
ZZZ=(9*COS((L+T)*R2D))-12
GLENZECUBE (XR,YR,ZR,XXX,YYY,ZZZ)
NEXT
END SUB
'===============================================================================
' CONTROL CODE TO CREATE A CUBE ANYWHERE AND AT ANY ROTATION
'===============================================================================
SUB CUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
GLLOADIDENTITY
glTranslatef(GPX, GPY, GPZ)
glRotatef(GRX, 1.0, 0.0, 0.0)
glRotatef(GRY, 0.0, 1.0, 0.0)
glRotatef(GRZ, 0.0, 0.0, 1.0)
GLCULLFACE(GL_BACK)
DRAWCUBE()
END SUB
'===============================================================================
' CONTROL CODE TO CREATE A GLENZE CUBE ANYWHERE AND AT ANY ROTATION
'===============================================================================
SUB GLENZECUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
GLLOADIDENTITY
glTranslatef(GPX, GPY, GPZ)
glRotatef(GRX, 1.0, 0.0, 0.0)
glRotatef(GRY, 0.0, 1.0, 0.0)
glRotatef(GRZ, 0.0, 0.0, 1.0)
GLCULLFACE(GL_FRONT)
DRAWGLENZE()
GLCULLFACE(GL_BACK)
DRAWGLENZE()
END SUB
SUB DRAWCUBE()
GLBEGIN GL_QUADS
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE3(0))
GLMATERIALFV (GL_FRONT,GL_SPECULAR,@FACE3(0))
' TOP
GLNORMAL3F 0,0,1
GLVERTEX3F 1, 1, 1
GLVERTEX3F -1, 1, 1
GLVERTEX3F -1,-1, 1
GLVERTEX3F 1,-1, 1
' BOTTOM
GLNORMAL3F 0,0,-1
GLVERTEX3F -1, 1, -1
GLVERTEX3F 1, 1, -1
GLVERTEX3F 1,-1, -1
GLVERTEX3F -1,-1, -1
' LEFT
GLNORMAL3F -1,0,0
GLVERTEX3F -1,-1,1
GLVERTEX3F -1, 1,1
GLVERTEX3F -1, 1,-1
GLVERTEX3F -1,-1,-1
' RIGHT
GLNORMAL3F 1,0,0
GLVERTEX3F 1, 1,-1
GLVERTEX3F 1, 1,1
GLVERTEX3F 1,-1,1
GLVERTEX3F 1,-1,-1
' FRONT
GLNORMAL3F 0,-1, 0
GLVERTEX3F -1,-1,-1
GLVERTEX3F 1,-1,-1
GLVERTEX3F 1,-1, 1
GLVERTEX3F -1,-1, 1
' BACK
GLNORMAL3F 0, 1, 0
GLVERTEX3F 1, 1, 1
GLVERTEX3F 1, 1,-1
GLVERTEX3F -1, 1,-1
GLVERTEX3F -1, 1, 1
GLEND
END SUB
SUB DRAWGLENZE()
GLBEGIN GL_TRIANGLES
'---------------------------------------------------------------------------
' TOP FACE
'---------------------------------------------------------------------------
GLCOLOR3F 1.0,0.0,0.0
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0,1,0
GLVERTEX3F 1, 1, 1
GLVERTEX3F 0, 1, 0
GLVERTEX3F -1, 1, 1
GLVERTEX3F 0, 1, 0
GLVERTEX3F 1, 1, -1
GLVERTEX3F -1, 1, -1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F -1,1,1
GLVERTEX3F 0,1,0
GLVERTEX3F -1,1,-1
GLVERTEX3F 0,1,0
GLVERTEX3F 1,1,1
GLVERTEX3F 1,1,-1
'---------------------------------------------------------------------------
' BOTTOM FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0,-1,0
GLVERTEX3F 0, -1, 0
GLVERTEX3F 1, -1, 1
GLVERTEX3F -1, -1, 1
GLVERTEX3F 0, -1, 0
GLVERTEX3F -1, -1, -1
GLVERTEX3F 1, -1, -1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F -1,-1,1
GLVERTEX3F -1,-1,-1
GLVERTEX3F 0,-1,0
GLVERTEX3F 1,-1,1
GLVERTEX3F 0,-1,0
GLVERTEX3F 1,-1,-1
'---------------------------------------------------------------------------
' LEFT FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLNORMAL3F -1, 0,0
GLVERTEX3F -1,-1, 1
GLVERTEX3F -1, 1, 1
GLVERTEX3F -1, 0, 0
GLVERTEX3F -1,-1,-1
GLVERTEX3F -1, 0, 0
GLVERTEX3F -1, 1,-1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLVERTEX3F -1, 0, 0
GLVERTEX3F -1, 1, 1
GLVERTEX3F -1, 1,-1
GLVERTEX3F -1, -1,-1
GLVERTEX3F -1, -1, 1
GLVERTEX3F -1, 0, 0
'---------------------------------------------------------------------------
' RIGHT FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLNORMAL3F 1 ,0,0
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1, 1, 1
GLVERTEX3F 1,-1, 1
GLVERTEX3F 1, 1,-1
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1,-1,-1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLVERTEX3F 1, 1,-1
GLVERTEX3F 1, 1, 1
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1, -1, 1
GLVERTEX3F 1, -1,-1
'---------------------------------------------------------------------------
' FRONT FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0,0,1
GLVERTEX3F -1,-1, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F -1, 1, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F 1,-1, 1
GLVERTEX3F 1, 1, 1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F -1,-1, 1
GLVERTEX3F 1,-1, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F 1, 1, 1
GLVERTEX3F -1, 1, 1
'---------------------------------------------------------------------------
' BACK FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0, 0, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F -1,-1, -1
GLVERTEX3F -1, 1, -1
GLVERTEX3F 1,-1, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F 1, 1, -1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F 1,-1, -1
GLVERTEX3F -1,-1, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F 1, 1, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F -1, 1, -1
GLEND
END SUB
-
Just to clarify a couple of things:
glTexImage2D() allocates the texture in the video memory and can be optionally initialized with some data. But glTexImage2D allocates a power-of-2 texture (unless you use extensions) and the buffer you initialize the texture with, must match the texture properties (i.e. width,height, color mode, etc)
glTexSubImage2D() updates any portion of the texture (at x,y position and width,height)
if the color mode/bitdepth of the buffer to be uploaded to the texture in video memory is different from the texture color mode/bitdepth, then the data gets converted on the fly. This can be slow, depending on the color mode/bitdepth of your buffer and target texture. Always make sure you work with compatible buffers (in terms of color mode & bitdepth) for maximum performance.
Then about vertices...
A vertex is made of:
Position
Texture coordinates
Normal
Vertex Color
The position is mandatory (otherwise no geometry is sent to the GL pipeline)
Texture coordinates are mandatory, if a texture is enabled (i.e. for a 2D texture, you must supply texture coordinates for U and V)
The nomal vector is mandatory if lighting is turned on. Otheriwise, it's useless.
The vertex color is mandatory also. Otherwise the current color is used. So if you clear the background with a black color and forget to set the color for your vertices, chances are that you will not see anything on screen. A call to glColorXX() before your glVertexXX() call will save you some headaches.
Remember that OGL is a state machine. When you give the vertices to the GL pipeline, what you do not set per vertex is set with the current value. In other words if you have something like this:
glColorXX()
glVertexXX()
glVertexXX()
..
all vertices will have the same color. If you want a specific color per vertex, you need to change the current color before calling the glVertexXX() function (which sends the current vertex definition to the GL pipeline).
I hope this helps a bit..
-
Nice work shockwave, the only thing wrong is that you've tried to upload the texture before you've initialised opengl.
-
Lol, how the hell didn't I spot that?!!
haha.
Working version;
'
' GLENZE VECTOR INTRO WITH OPENGL
' BY SCHLOCKWAVE
'===============================================================================
OPTION STATIC
OPTION EXPLICIT
#INCLUDE "GL/GL.BI"
#INCLUDE "GL/GLU.BI"
#INCLUDE "WINDOWS.BI"
DIM SHARED AS INTEGER MOUSE
'===============================================================================
' LOGO XOR TEXTURE GENERATION;
'===============================================================================
DIM SHARED AS UINTEGER LOGOBUFFER ( 256 * 256 )
DIM SHARED TEX1 AS GLUINT
DIM AS UINTEGER X,Y,PIXEL
for y = 0 TO 255
for x = 0 TO 255
PIXEL = X XOR Y
LOGOBUFFER((y * 256)+X) = RGBA(PIXEL,PIXEL,PIXEL,255)
NEXT
NEXT
'===============================================================================
' OPEN A SCREEN 1024 * 768;
'===============================================================================
SCREEN 20,,,3
'===============================================================================
' SET UP OPENGL;
'===============================================================================
GLVIEWPORT 0, 0, 1024, 768 '' Reset The Current Viewport
GLMATRIXMODE GL_PROJECTION '' Select The Projection Matrix
GLLOADIDENTITY
GLUPERSPECTIVE 90.0, 1024/768, 0.1, 50. '' Calculate The Aspect Ratio Of The Window
GLMATRIXMODE GL_MODELVIEW '' Select The Modelview Matrix
GLLOADIDENTITY '' Reset The Modelview Matrix
GLSHADEMODEL GL_SMOOTH '' Enable Smooth Shading
GLCLEARCOLOR 0.0, 0.0, 0.0, 0.00 '' Black Background
GLCLEARDEPTH 1.0 '' Depth Buffer Setup
GLENABLE GL_DEPTH_TEST '' Enables Depth Testing
GLDEPTHFUNC GL_LEQUAL '' The Type Of Depth Testing To Do
GLHINT GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST '' Really Nice Perspective Calculations
dim shared LightAmbient(0 to 3) as single => {0.00, 0.00, 0.00,0.0} '' Ambient Light is 20% white
dim shared LightDiffuse(0 to 3) as single => {0.5, 0.5, 0.5,0.5} '' Diffuse Light is white
dim shared LightPosition(0 to 2) as single =>{0.0, 0.0, 15.0 } '' Position is somewhat in front of screen
dim shared Lightdiffuse2(0 to 3) as single => {1.0, 1.0, 1.0,0.02} '' Diffuse Light is white
dim shared LightPosition2(0 to 2) as single =>{0.0, 10.0, 10.0 } '' Position is somewhat in front of screen
glLightfv( GL_LIGHT1, GL_AMBIENT, @LightAmbient(0)) '' Load Light-Parameters Into GL_LIGHT1
glLightfv( GL_LIGHT1, GL_DIFFUSE, @LightDiffuse(0))
glLightfv( GL_LIGHT1, GL_POSITION, @LightPosition(0))
glLightfv( GL_LIGHT2, GL_DIFFUSE , @Lightdiffuse2(0))
glLightfv( GL_LIGHT2, GL_POSITION, @LightPosition2(0))
gllightf (GL_LIGHT2,GL_SPOT_CUTOFF,60.0)
glEnable(GL_LIGHT1)
glEnable(GL_LIGHT2)
GLSHADEMODEL (GL_SMOOTH)
GLENABLE (GL_LIGHTING)
'===============================================================================
' SUBROUTINES;
'===============================================================================
DECLARE SUB GLENZECUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
DECLARE SUB DRAWGLENZE()
DECLARE SUB CUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
DECLARE SUB DRAWCUBE()
DECLARE SUB CUBE_OBJECTS()
DECLARE SUB COPPERS()
DECLARE SUB LOGO_TEXTURE()
'===============================================================================
' SETUP;
'===============================================================================
' MATERIALS;
DIM SHARED FACE1(0 to 3) AS SINGLE => {1.0, 0.2, 0.0,0.45} :' RED GLENZE FACES
DIM SHARED FACE2(0 to 3) AS SINGLE => {1.5, 1.2, 0.9,0.45} :' WHITE GLENZE FACES
DIM SHARED FACE3(0 to 3) AS SINGLE => {0.7, 0.5, 0.3,1.0} :' WHITE MATERIAL
DIM SHARED FACE4(0 to 3) AS SINGLE => {1.0, 0.1, 0.0,0.45} :' PURPLE FACES
' ROTATIONS;
DIM SHARED XR AS SINGLE : ' X ROTATION
DIM SHARED YR AS SINGLE : ' Y ROTATION
DIM SHARED ZR AS SINGLE : ' Z ROTATION
' HIDE MOUSE POINTER;
MOUSE=SHOWCURSOR(0)
GLENABLE(GL_MULTISAMPLE)
GLENABLE(GL_BLEND)
GLBLENDFUNC(GL_ONE_MINUS_CONSTANT_ALPHA,GL_dst_ALPHA)
GLENABLE (GL_CULL_FACE)
GLENABLE (GL_FOG)
GLHINT(GL_FOG_HINT,GL_NICEST)
GLFOGF(GL_FOG_DENSITY,0.1)
GLFOGF(GL_FOG_START, 5.0)
GLFOGF(GL_FOG_END, 20.0)
GLFOGI(GL_FOG_MODE,GL_EXP2)
GLENABLE(GL_TEXTURE_2D)
glGenTextures 1, @tex1
glBindTexture(GL_TEXTURE_2D, tex1)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR)
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 256, 256,GL_RGBA, GL_UNSIGNED_BYTE, @LOGOBUFFER(0))
'===============================================================================
DIM D AS INTEGER
'===============================================================================
' MAIN LOOP
'===============================================================================
WHILE((GETASYNCKEYSTATE(VK_ESCAPE)<> -32767) )
GLFLUSH()
GLCLEAR GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
CUBE_OBJECTS()
LOGO_TEXTURE()
xr = xr + 0.03
yr = yr + 0.06
zr = zr + 0.07
FLIP
WEND
END
'===============================================================================
' DRAW THE "LOGO"
'===============================================================================
SUB LOGO_TEXTURE()
GLLOADIDENTITY
glTranslatef(0, -2, -6)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex1)
GLBEGIN GL_QUADS
glNormal3f 0.0f, 0.0f, 1.0f
glTexCoord2f 0.0f, 0.0f
glVertex3f -1.0f, -1.0f, 1.0f
glTexCoord2f 1.0f, 0.0f
glVertex3f 1.0f, -1.0f, 1.0f
glTexCoord2f 1.0f, 1.0f
glVertex3f 1.0f, 1.0f, 1.0f
glTexCoord2f 0.0f, 1.0f
glVertex3f -1.0f, 1.0f, 1.0f
GLEND
END SUB
SUB CUBE_OBJECTS()
DIM AS DOUBLE XXX,YYY,ZZZ,R2D,T
T=TIMER*33
R2D=(3.14/180)
DIM L AS INTEGER
CUBE (XR,0,ZR,0,0,-9)
FOR L=0 TO 359 STEP 40
XXX= 9*SIN((L+T)*R2D)
ZZZ=(9*COS((L+T)*R2D))-12
GLENZECUBE (XR,YR,ZR,XXX,YYY,ZZZ)
NEXT
END SUB
'===============================================================================
' CONTROL CODE TO CREATE A CUBE ANYWHERE AND AT ANY ROTATION
'===============================================================================
SUB CUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
GLLOADIDENTITY
glTranslatef(GPX, GPY, GPZ)
glRotatef(GRX, 1.0, 0.0, 0.0)
glRotatef(GRY, 0.0, 1.0, 0.0)
glRotatef(GRZ, 0.0, 0.0, 1.0)
GLCULLFACE(GL_BACK)
DRAWCUBE()
END SUB
'===============================================================================
' CONTROL CODE TO CREATE A GLENZE CUBE ANYWHERE AND AT ANY ROTATION
'===============================================================================
SUB GLENZECUBE(BYVAL GRX AS SINGLE, BYVAL GRY AS SINGLE, BYVAL GRZ AS SINGLE , BYVAL GPX AS SINGLE, BYVAL GPY AS SINGLE, BYVAL GPZ AS SINGLE)
GLLOADIDENTITY
glTranslatef(GPX, GPY, GPZ)
glRotatef(GRX, 1.0, 0.0, 0.0)
glRotatef(GRY, 0.0, 1.0, 0.0)
glRotatef(GRZ, 0.0, 0.0, 1.0)
GLCULLFACE(GL_FRONT)
DRAWGLENZE()
GLCULLFACE(GL_BACK)
DRAWGLENZE()
END SUB
SUB DRAWCUBE()
GLBEGIN GL_QUADS
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE3(0))
GLMATERIALFV (GL_FRONT,GL_SPECULAR,@FACE3(0))
' TOP
GLNORMAL3F 0,0,1
GLVERTEX3F 1, 1, 1
GLVERTEX3F -1, 1, 1
GLVERTEX3F -1,-1, 1
GLVERTEX3F 1,-1, 1
' BOTTOM
GLNORMAL3F 0,0,-1
GLVERTEX3F -1, 1, -1
GLVERTEX3F 1, 1, -1
GLVERTEX3F 1,-1, -1
GLVERTEX3F -1,-1, -1
' LEFT
GLNORMAL3F -1,0,0
GLVERTEX3F -1,-1,1
GLVERTEX3F -1, 1,1
GLVERTEX3F -1, 1,-1
GLVERTEX3F -1,-1,-1
' RIGHT
GLNORMAL3F 1,0,0
GLVERTEX3F 1, 1,-1
GLVERTEX3F 1, 1,1
GLVERTEX3F 1,-1,1
GLVERTEX3F 1,-1,-1
' FRONT
GLNORMAL3F 0,-1, 0
GLVERTEX3F -1,-1,-1
GLVERTEX3F 1,-1,-1
GLVERTEX3F 1,-1, 1
GLVERTEX3F -1,-1, 1
' BACK
GLNORMAL3F 0, 1, 0
GLVERTEX3F 1, 1, 1
GLVERTEX3F 1, 1,-1
GLVERTEX3F -1, 1,-1
GLVERTEX3F -1, 1, 1
GLEND
END SUB
SUB DRAWGLENZE()
GLBEGIN GL_TRIANGLES
'---------------------------------------------------------------------------
' TOP FACE
'---------------------------------------------------------------------------
GLCOLOR3F 1.0,0.0,0.0
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0,1,0
GLVERTEX3F 1, 1, 1
GLVERTEX3F 0, 1, 0
GLVERTEX3F -1, 1, 1
GLVERTEX3F 0, 1, 0
GLVERTEX3F 1, 1, -1
GLVERTEX3F -1, 1, -1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F -1,1,1
GLVERTEX3F 0,1,0
GLVERTEX3F -1,1,-1
GLVERTEX3F 0,1,0
GLVERTEX3F 1,1,1
GLVERTEX3F 1,1,-1
'---------------------------------------------------------------------------
' BOTTOM FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0,-1,0
GLVERTEX3F 0, -1, 0
GLVERTEX3F 1, -1, 1
GLVERTEX3F -1, -1, 1
GLVERTEX3F 0, -1, 0
GLVERTEX3F -1, -1, -1
GLVERTEX3F 1, -1, -1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F -1,-1,1
GLVERTEX3F -1,-1,-1
GLVERTEX3F 0,-1,0
GLVERTEX3F 1,-1,1
GLVERTEX3F 0,-1,0
GLVERTEX3F 1,-1,-1
'---------------------------------------------------------------------------
' LEFT FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLNORMAL3F -1, 0,0
GLVERTEX3F -1,-1, 1
GLVERTEX3F -1, 1, 1
GLVERTEX3F -1, 0, 0
GLVERTEX3F -1,-1,-1
GLVERTEX3F -1, 0, 0
GLVERTEX3F -1, 1,-1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLVERTEX3F -1, 0, 0
GLVERTEX3F -1, 1, 1
GLVERTEX3F -1, 1,-1
GLVERTEX3F -1, -1,-1
GLVERTEX3F -1, -1, 1
GLVERTEX3F -1, 0, 0
'---------------------------------------------------------------------------
' RIGHT FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLNORMAL3F 1 ,0,0
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1, 1, 1
GLVERTEX3F 1,-1, 1
GLVERTEX3F 1, 1,-1
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1,-1,-1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLVERTEX3F 1, 1,-1
GLVERTEX3F 1, 1, 1
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1, 0, 0
GLVERTEX3F 1, -1, 1
GLVERTEX3F 1, -1,-1
'---------------------------------------------------------------------------
' FRONT FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0,0,1
GLVERTEX3F -1,-1, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F -1, 1, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F 1,-1, 1
GLVERTEX3F 1, 1, 1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F -1,-1, 1
GLVERTEX3F 1,-1, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F 0, 0, 1
GLVERTEX3F 1, 1, 1
GLVERTEX3F -1, 1, 1
'---------------------------------------------------------------------------
' BACK FACE
'---------------------------------------------------------------------------
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE1(0))
GLNORMAL3F 0, 0, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F -1,-1, -1
GLVERTEX3F -1, 1, -1
GLVERTEX3F 1,-1, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F 1, 1, -1
GLMATERIALFV (GL_FRONT,GL_AMBIENT_AND_DIFFUSE,@FACE2(0))
GLVERTEX3F 1,-1, -1
GLVERTEX3F -1,-1, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F 1, 1, -1
GLVERTEX3F 0, 0, -1
GLVERTEX3F -1, 1, -1
GLEND
END SUB
Thanks Stonemonkey :)
Exe also attached.
-
BTW... who is "SCHLOCKWAVE" ???? new member? :clap: :clap: :clap:
-
[edited] hehe, glad to see that you have find the damn "bug", anyway here the mini version:
option explicit
option static
'-------------------------------------
' Includes
'-------------------------------------
#include "GL/gl.bi"
#include "GL/glu.bi"
#include "windows.bi"
dim shared pfd as PIXELFORMATDESCRIPTOR
dim shared hdc as hDC
declare sub InitOGL()
DECLARE SUB LOGO_TEXTURE()
InitOGL()
'===============================================================================
' LOGO XOR TEXTURE GENERATION;
'===============================================================================
DIM SHARED AS UINTEGER LOGOBUFFER ( 256 * 256 )
DIM SHARED tex1 AS GLUINT
DIM AS UINTEGER X,Y,PIXEL
for y = 0 TO 255
for x = 0 TO 255
PIXEL = X XOR Y
LOGOBUFFER((y * 256)+X) = RGBA(PIXEL,PIXEL,PIXEL,0)
NEXT
NEXT
glGenTextures 1, @tex1
glBindTexture(GL_TEXTURE_2D, tex1)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR)
gluBuild2DMipmaps (GL_TEXTURE_2D, 3, 256, 256,GL_RGBA, GL_UNSIGNED_BYTE, @LOGOBUFFER(0))
'===============================================================================
' MAIN LOOP
'===============================================================================
while(GetAsyncKeyState(VK_ESCAPE)<>-32767)
glClear(GL_DEPTH_BUFFER_BIT + GL_COLOR_BUFFER_BIT )
LOGO_TEXTURE()
SwapBuffers(hDC)
wend
'===============================================================================
' Initialise OpenGL
'===============================================================================
sub InitOGL()
pfd.cColorBits = 32
pfd.cDepthBits = 32
pfd.dwFlags = PFD_SUPPORT_OPENGL + PFD_DOUBLEBUFFER
hDC = GetDC(CreateWindow("edit", 0,WS_POPUP+WS_VISIBLE+WS_MAXIMIZE,0, 0, 0 , 0, 0, 0, 0, 0))
SetPixelFormat ( hDC, ChoosePixelFormat ( hDC, @pfd) , @pfd )
wglMakeCurrent ( hDC, wglCreateContext(hDC) )
ShowCursor(FALSE)
glMatrixMode(GL_PROJECTION)
gluPerspective(45.0f,1024/768,0.1f,100.0f)
glMatrixMode(GL_MODELVIEW)
end sub
'===============================================================================
' DRAW THE "LOGO"
'===============================================================================
SUB LOGO_TEXTURE()
glLoadIdentity()
glTranslatef(0, 0, -6)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex1)
GLBEGIN GL_QUADS
glNormal3f 0.0f, 0.0f, 1.0f
glTexCoord2f 0.0, 0.0
glVertex3f -1.0f, -1.0f, 1.0f
glTexCoord2f 1.0, 0.0
glVertex3f 1.0f, -1.0f, 1.0f
glTexCoord2f 1.0, 1.0
glVertex3f 1.0f, 1.0f, 1.0f
glTexCoord2f 0.0, 1.0
glVertex3f -1.0f, 1.0f, 1.0f
GLEND
END SUB
-
7.5 kb, uncrunched.. hats off to you mate.