Quad is not drawn with a z value above 1

BlitzMax Forums/OpenGL Module/Quad is not drawn with a z value above 1

Gabriel(Posted 2005) [#1]
I've written a simple program which draws an OpenGL quad on the screen at 0,0,0. It fills the entire screen ( which makes sense to me ) and if I move it 1 unit left or right, up or down, it moves accordingly ( I can see black background to the right or left, bottom or top respectively. ) But if I move it beyond 1.0 on the z axis, it doesn't seem to be drawn.

So I'm wondering if this is because the camera range is very low by default and I haven't increased it. I found a page that said it defaulted to 0 for near and 1 for far ( which is exactly the limit ) and thought that would fix it. So I added the glDepthRange(0,1000) call to fix that, but that made no difference.

So I don't know if it's a perspective thing or what. Bear in mind, I have no OpenGL books and I'm trying to figure this stuff out from a bunch of websites and the modules, so it's a bit tricky. I'm only really playing with OpenGL to force myself to pick up all the new BMax features ( OOP, pointers, etc ) but I can't really go on until I can draw something beyond z=1.

I'd prefer not to post all the code as I've written it all as a library and there's a lot of stuff which is not really necessary. So I've snipped what I think are the only important bits.

OpenGL Initialization code :

bglCreateContext(SW,SH,SD,HZ,Flags)
		
		glShadeModel(GL_SMOOTH)
		glClearColor(0.0,0.0,0.0,0.0)
		glDepthRange(0,1000)
				
		glClearDepth(1.0)          ' Depth Buffer Setup
		glEnable(GL_DEPTH_TEST)    ' Enables Depth Testing
		glDepthFunc(GL_LEQUAL)     ' The Type Of Depth Test To Do
		
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)     ' USE THE BEST PERSPECTIVE CALCULATIONS
		



Quad Render bit:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
		
			
		' RENDER QUADS
		glBegin(GL_QUADS)
		
		For M=EachIn MeshList
			If M.Quads=True
				For Q=EachIn M.QuadList
					glColor3f(1.0,1.0,1.0)
					glVertex3f(M.X+Q.V1.X,M.Y+Q.V1.Y,M.Z+Q.V1.Z)
					glVertex3f(M.X+Q.V2.X,M.Y+Q.V2.Y,M.Z+Q.V2.Z)
					glVertex3f(M.X+Q.V3.X,M.Y+Q.V3.Y,M.Z+Q.V3.Z)
					glVertex3f(M.X+Q.V4.X,M.Y+Q.V4.Y,M.Z+Q.V4.Z)
				Next
			End If
		Next
		
		glEnd()



MeshList is a TList of meshes. Each Mesh has a QuadList. A Quad has four vertices, which are numbered 1-4 in the order they should be draw. As you can see, it totals the global position of the mesh and the local position of the vertex within that mesh and draws it. This works fine with z ranges between 0 and 1, and affecting the x and y positions works too, so I'm pretty sure there's no problem with any of this.

I'm guessing it's something else I should be doing when I initialize OpenGL, but I have no idea what else there is.


ozak(Posted 2005) [#2]
Did you set up the perspective projection?

' Setup projection
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, Float(ScreenWidth) / Float(ScreenHeight), 1.0, 1000.0)
glMatrixMode(GL_MODELVIEW)



Bremer(Posted 2005) [#3]
Not sure if it applies, but isn't plus values the z-axis towards the viewer and minus values away from the center.



This would mean that unless you translate the viewpoint away from the center, objects moved too far out on the plus range of the z-axis will not be seen.

[edit] you can use glTranslatef 0.0,0.0,0.0 to move the viewpoint.


Gabriel(Posted 2005) [#4]
Beautiful, thanks guys. Having made both those adjustments, it now works perfectly. I completely follow the inverted z axis by comparison to Blitz, and now I can adjust my move and position methods to allow me to use it the Blitz way.

I have no idea what any of the perspective projection stuff means, but now that I have the commands and constants, I can figure all that out from my OpenGL bookmarks. Thanks again.

EDIT: OK, I think I understand all the projection stuff now too.

glMatrixMode(GL_PROJECTION) ' This simply means that subsequent commands apply to the projection matrix and not the modelview matrix, which is the default )

glLoadIdentity() ' This resets the default values for this matrix.

gluPerspective(45.0,Float(SW)/Float(SH),1.0,1000.0)  ' This sets a 45 degree perspective and near and far clipping values of 1 and 1000.

glMatrixMode(GL_MODELVIEW)  ' And this puts us back into the modelview matrix ( which is the one needed for object manipulation. )



I think I have all that right now.


Gabriel(Posted 2005) [#5]
Ok, new problem. Looking at the OpenGL commands, I decided it would make more sense to use the translate command to set the entity position and then use the local vertex coords with the draw commands.

But when I add glTranslatef(x,y,z) in the draw section, it does nothing. It makes absolutely no difference what values I use, the result is the same. I tried resetting the matrix with glLoadIdentity() both at the start and end of the drawing operations ( and separately ) but nothing seems to make any difference at all. Any ideas?


N(Posted 2005) [#6]
I fix0red it.


Gabriel(Posted 2005) [#7]
Yup, Noel fix0red it. I was using glTranslatef() between a GL_Begin and GL_End pair, which is not allowed. Moving it outside that got it working just fine.


boomboommax(Posted 2005) [#8]
triangles would be better to render with, rather than quads


AntonyWells(Posted 2005) [#9]
an incorrection. less data to send to the gpu = faster to render.


Jams(Posted 2005) [#10]
when you render quads it's converted to triangles in the GPU anyway = slower to render


Bot Builder(Posted 2005) [#11]
No, it's converted by the driver usually. So, just as fast, and some cards might actually have the capability and dont convert it.


AntonyWells(Posted 2005) [#12]
it's faster because it's less data to send down the bus.

consider 4,000 quads. = 4,000 4,000 vertex definitions. the rest filled in on the card or the gpu as bot mentions above. compared to 4,000 x 6...(Wait..i was never any good at maths...)