Ortho mode and Depth

Started by
3 comments, last by Daishim 18 years, 4 months ago
I've been digging for days to try to find an answer to my problem and have come up with nothing. What I'm trying to do is render a screen sized quad in orthographic mode at a certain depth which is greater than 1 to perform certain effects on the image. So here is my OpenGL/Glut setup code:

glutInit(argc, argv);
glutInitWindowSize(Width, Height);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(argv[0]);
glutDisplayFunc(Display);

glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST); 

/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
glEnable(GL_LIGHT0);

However, when I try to render anything in orthographic mode beyond 1 on the z-axis it is clipped out. Here is my render code:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glOrtho(0, 1280, 0, 1024, -1.0, 100.0);

glLoadIdentity();

glBindTexture(GL_TEXTURE_2D, texture[1]);

glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-1.0f,-1.0f, 1.0f);		// Bottom Left

	glTexCoord2f(1.0f, 0.0f);
	glVertex3f( 1.0f,-1.0f, 1.0f);		// Bottom Right

	glTexCoord2f(1.0f, 1.0f); 
	glVertex3f( 1.0f, 1.0f, 1.0f);	        // Top Right

	glTexCoord2f(0.0f, 1.0f); 
	glVertex3f(-1.0f, 1.0f, 1.0f);		// Top Left
glEnd();

glutSwapBuffers();



So, for example, with this code, if I were to change glVertex3f(X, Y, 1.0f) to glVertex(X, Y, 3.0f), the quad will no longer be displayed.

I know only that which I know, but I do not know what I know.
Advertisement
You have glLoadIdentity right after glOrtho, restoring any changes made by glOrtho. And you should, unless you have a very good reason not to, always put the projection matrix in, that's right, the projection matrix stack, and object/viewpoint transforms goes into the modelview matrix.

In the display function.
glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(...)glMatrixMode(GL_MODELVIEW);glLoadIdentity();


edit: Oh, and your quad will probably only show up as a single pixel in the bottom left corner. That's because the viewport ranges from [0, 1280] in X and [0, 1024] in Y, but the quad you're drawing only ranges from [-1, 1] in X and Y. That's a very small quad in the lower left corner.
I implemented the matrix setup like you said Brother Bob, and that will eliminate some headaches with keeping track of coordinates, however, it did not help the depth/z-axis issue any. If I render beyond 1.0 on the z-axis, it is clipped.

I know only that which I know, but I do not know what I know.
The problem is the way the Z-axis is pointing and what the near and far clip planes parameters really mean. The near/far value is the distance to the near/far clip plane, and the Z-axis is pointing out from the screen. That means, near=10 means the distance from the viewpoint to the near clip plane is 10 units. But it's 10 units INTO the screen, where the negative Z is pointing, so the near plane is actually located at Z=-10. Negative distances are used to specify a clip plane "behind" the viewpoint.

Anyways, this means your Z-range is currently [1, -100], 1 being nearest and -100 furtherst, and therefore Z=3 is outside the viewport. You must negate the values you really want, to describe it in a simple way.
Ok, duh. I knew that... it was a long day. Ok, so now rendering past -1 (world space) or 1 (clip space) now works. Now my ultimate accomplishment with this implementation is to manipulate depth values of texels via a fragment program. Now that my orthographic projection is working correctly, I re-implemented the Cg fragment program. This question is probably a little out of place for this forum, but I'll throw it out there since it is an extension of my original question.

So, I have a very simple fragment program that I want to use to manipulate depth values of texels. It is a Cg program as follows:

struct frag_out{     float4 Color : COLOR;     float Depth : DEPTH;};frag_out main(sampler2D inTex: TEXTURE0, in float2 InTexCoord: TEXCOORD0){     frag_out OutFrag;	     OutFrag.Color = tex2D(inTex, InTexCoord);     OutFrag.Depth = 1.0;     return OutFrag;}


If I set the depth from [0 to 1) the fragments are rendered appropriately. However, if the depth is set outside that range [0, 1), the fragments are not rendered. I'm using the latest fragment profile available on the card I'm working on, which is fp30.

I know only that which I know, but I do not know what I know.

This topic is closed to new replies.

Advertisement