Depth Test Problem

So, I’m an openGL newbie, but not a Java newbie. Here’s my problem: Cubes and Spheres work, but when I started making a Terrain object for my openGL engine, I ran into a problem which has completely stumped me. In this demo I’ve included a superimposed wooden crate which is drawing correctly (except for when it crosses the axes) to show how the terrain is supposed to be rotating.

Firstly, here’s the jar file: http://www.ageofinsanity.com/jogl/Test/Test.jar
(Arrow keys, Q, and W to rotate, no zoom implemented) - source files are included in the jar

Here’s the critical code:

TestRenderer.java - void init(GlDrawable drawable):


	// Prepare all geometry here but draw nothing
	gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
	gl.glEnable(GL.GL_DEPTH_TEST);
	gl.glDepthFunc(GL.GL_GEQUAL);
	gl.glFrontFace(GL.GL_CCW);
	gl.glEnable(GL.GL_CULL_FACE);
	gl.glCullFace(GL.GL_BACK);
	gl.glShadeModel(GL.GL_SMOOTH);

If I do glFrontFace with Clockwise instead of Counter-Clockwise, the whole terrain draws fine, except flipped horizontally (if you were just looking at the 2D texture. Also, the Normal points downward instead of upward. This wouldn’t be a concern if I was drawing a generated landmass, but in this case, Im not.

I realize that I could just flip the texture and heightmap horizontally and invert the Normal, but that’s just a workaround. I’d rather have the code work exactly the way I intend.

The other (bigger) problem I’m having is that objects closer than .5f (on the Z-axis) and farther than 1.0f (on the Z-axis) don’t get drawn. I have no idea why this is happening, especially considering I have tried gluPerspective over and over, and it hasn’t helped a bit.

Any help is greatly appreciated. I’ve been studying guides on here, as well as NeHe’s guide and this page. I’m completely out of ideas. Please help me out.

Keep in mind that when you change the face orientation to clockwise from counterclockwise that you’re seeing the model as it should be displayed instead of the back faces, so it will seem that it is flipped since the only time you can see the model with a counterclockwise orientation is from the back side.

You should look at the OpenGL red book to understand OpenGL’s coordinate system. I usually consider Y to be up, X to be to the “right” and Z to be coming out of the screen. Keeping that in mind you should be able to iterate through your texture and heightmap data and produce a correct triangle mesh. You may also want to consider computing per-vertex normals.

You should switch your matrix mode to GL_PROJECTION when calling gluPerspective and switch back to GL_MODELVIEW when moving your “camera” around. If you pass the appropriate parameters to gluPerspective for zNear and zFar your entire model should come into view. Keep in mind that you should fit zNear and zFar as closely as possible to your model’s size to get the best z-buffer precision. Otherwise you can get bad visual artifacts when rendering triangles near each other.

I’ve been referencing the “red book” quite a bit, actually. And I just learned about per-vertex normals. For now I’m not really concerned about Normals since I’m just dealing with how to draw correctly-rendered meshes.

[quote]You should switch your matrix mode to GL_PROJECTION when calling gluPerspective and switch back to GL_MODELVIEW when moving your “camera” around. If you pass the appropriate parameters to gluPerspective for zNear and zFar your entire model should come into view. Keep in mind that you should fit zNear and zFar as closely as possible to your model’s size to get the best z-buffer precision. Otherwise you can get bad visual artifacts when rendering triangles near each other.
[/quote]
I didn’t know that it was actually required that you change the matrix mode to use gluPerspective. That should hopefully fix my problem.

Thanks for your help.

It isn’t required that you switch matrix mode but it is helpful to be able to separate out the perspective portion of your view transformation from the portion which simply transforms the viewpoint to the correct location and orientation. You do need to set up a transformation matrix to set up your front and back clipping planes properly, for example using gluPerspective.

Well, I’ve changed the problem code area to this:

	gl.glEnable(GL.GL_DEPTH_TEST);
	gl.glDepthFunc(GL.GL_LEQUAL);
	gl.glEnable(GL.GL_CULL_FACE);
	gl.glCullFace(GL.GL_FRONT);
	gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
	gl.glShadeModel(GL.GL_SMOOTH);

With this, the objects draw correctly (because of the front face being culled instead of the back face.

The biggest problem now is that going positive on the Z axis now goes “into the screen”, and negative comes “out of the screen”. This is exactly opposite of how openGL is supposed to work, according to everything I have read on openGL. Consequently all my models will need to be changed, obviously, to take this into account, unless someone can explain how to switch this off while keeping the images drawing correctly. Another problem is that I had to flip my textures vertically (not horizontally, as previously mentioned) in order to keep them drawing correctly.

And as before, even with gluPerspective and suggestions I’ve gotten, the maximum draw depth is 1.0f and minimum is .5f.

It doesn’t look like your test program attempts to explicitly position the viewpoint or set up a perspective projection. Probably the first thing you should to is position the camera so it is pointing where you expect it to be. Try using gluLookAt to set up the modelview matrix to position your viewpoint at say (0, 0, 10) pointing at the origin and gluPerspective to set up the projection matrix with a zNear of 0.5 and a zFar of 20. From there you should be able to see your model and start figuring out the coordinate systems you need to work with.

OpenGL uses a right-hand coordinate system meaning that the cross product of the X and Y axes (in that order) yields the Z axis. From a practical standpoint this means that +Z is “out of the screen” if your camera viewpoint is set up as above.

OpenGL’s texture coordinates place (0, 0) at the lower-left of the image, while most window systems place it at the upper-left of the image. Again from a practical standpoint this means that you usually have to flip either your texture coordinates or image data vertically when reading in images to feed down to OpenGL.

[quote=“Ken Russell,post:6,topic:23849”]
Thank you, but understanding these two concepts is not my problem. I’m definitely not that much of a newbie. The fact is, openGL has decided to switch the negative and positive sides of the z-axis.

[quote=“Ken Russell,post:6,topic:23849”]
HOWEVER, this part helped a great deal! Thank you, both problems are now fixed.