Page 1 of 1
gluPerspective problems
Posted: Sun May 09, 2010 12:13 pm
by dandymcgee
This draws a red quad:
Code: Select all
glEnable( GL_DEPTH_TEST );
glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, -300.0f, 300.0f); //ONLY DIFFERENT LINE OF CODE
glMatrixMode( GL_MODELVIEW );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glBegin( GL_QUADS );
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( -20.0f, -20.0f, 10.0f );
glVertex3f( 20.0f, -20.0f, 10.0f );
glVertex3f( 20.0f, 20.0f, 10.0f );
glVertex3f( -20.0f, 20.0f, 10.0f );
glEnd();
This doesn't:
Code: Select all
glEnable( GL_DEPTH_TEST );
glViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0f, (GLfloat)SCREEN_WIDTH / (GLfloat)SCREEN_HEIGHT, 0.5f, 500.0f ); //ONLY DIFFERENT LINE OF CODE
glMatrixMode( GL_MODELVIEW );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glBegin( GL_QUADS );
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( -20.0f, -20.0f, 10.0f );
glVertex3f( 20.0f, -20.0f, 10.0f );
glVertex3f( 20.0f, 20.0f, 10.0f );
glVertex3f( -20.0f, 20.0f, 10.0f );
glEnd();
I can't for the life of me seem to figure out what I'm doing wrong. Why isn't perspective view working?
Re: gluPerspective problems
Posted: Sun May 09, 2010 12:50 pm
by qpHalcy0n
You're actually rendering outside the view volume, which is what both the perspective and orthographic projection define. If you're using GLU, it generates a right handed coordinate frame and the OpenGL pipeline is setup for a right handed system. In this system, the camera looks along (0, 0, -1) in absence of a view transform.
If you flip the vertices' "z" around to the negative size towards -infinity, then they should show up.
Re: gluPerspective problems
Posted: Sun May 09, 2010 1:57 pm
by dandymcgee
qpHalcy0n wrote:You're actually rendering outside the view volume, which is what both the perspective and orthographic projection define. If you're using GLU, it generates a right handed coordinate frame and the OpenGL pipeline is setup for a right handed system. In this system, the camera looks along (0, 0, -1) in absence of a view transform.
If you flip the vertices' "z" around to the negative size towards -infinity, then they should show up.
I'm not sure what you mean. I tried changing the vertices' z values to -10.0f and still see nothing.
Re: gluPerspective problems
Posted: Sun May 09, 2010 2:11 pm
by GroundUpEngine
Add this before the render, to look in the direction of the Quad
Code: Select all
gluLookAt(0, 0, -50,
0, 0, 0,
0, 1, 0);
Re: gluPerspective problems
Posted: Sun May 09, 2010 2:15 pm
by dandymcgee
GroundUpEngine wrote:Add this before the render, to look in the direction of the Quad
Code: Select all
gluLookAt(0, 0, -50,
0, 0, 0,
0, 1, 0);
Still nothing... I don't understand?!
Re: gluPerspective problems
Posted: Sun May 09, 2010 2:17 pm
by qpHalcy0n
Then one of two things is going on.
You've either got something sneaky going on in code that is not posted or you need to clean your build and rebuild the entire project with that fix. That is most certainly the fix for THAT particular problem. You have to understand that a perspective projection scales the values so that it does not preserve linearity. So as the z approaches negative infinity, the object will appear to be smaller and smaller, anything behind the view volume will be clipped out.
Also, the fact that you don't have a camera doesn't really mean much, so adding a view transform won't really do anything.
Re: gluPerspective problems
Posted: Sun May 09, 2010 6:56 pm
by dandymcgee
I've posted the entirety of my OpenGL code, and a clean rebuild yields the same error. It works just as suspected in ortho. I'm dunno what's wrong with the way I'm setting up the perspective viewing.
EDIT: There WAS a single line of code that was relevant but not posted and that was a glTranslate call. Apparently translations in perspective mode have a FAR greater effect than in ortho (the way I have everything else set up).
gluLookAt(0, 0, -50, 0, 0, 0, 0, 1, 0); fixed my problem. Thanks for the help!
Re: gluPerspective problems
Posted: Sun May 09, 2010 7:52 pm
by qpHalcy0n
The reason this is the case is because, like I said, perspective projections do not preserve linearity. This is why they're called "perspective" projections. If you've got an orthographic camera, no matter how close or how far away from the object, it will visibly appear the same no matter what. This is not the case for perspective projections where the object is scaled inversely with depth. This is why the translation (which I presume was along Z) had no effect whatsoever with your orthographic projection.
In all reality, adding gluLookAt does fixes your problem, but in a way that you did not expect it to. There's two ways to skin a cat :] . All this is doing is moving the camera closer to the far clip plane. With an identity modelview matrix you're effectively rendering directly into the camera's view frustum which in this case would be at the origin. (0, 0, 0) looking down Z (0, 0, -1). So when the vertex comes in w/ a "z" of 9.0, its trivially rejected because its impossible for it to be in the frustum. By making it increasingly negative you're making it smaller.
So another way to look at what's happening is this: How do you make something appear smaller? Two ways.
1) Throw the object farther away. A basketball up on your face appears huge compared to one 100 yards away.
2) Move YOURSELF closer to the object.
These are actually the same thing so by making the vertices' "z" more negative you've accomplished the same task. By adding a translation on top of that you've potentially negated this effect.
Re: gluPerspective problems
Posted: Sun May 09, 2010 8:17 pm
by dandymcgee
qpHalcy0n wrote:
So another way to look at what's happening is this: How do you make something appear smaller? Two ways.
...
2) Move YOURSELF closer to the object.
Wouldn't that make the object appear bigger?
qpHalcy0n wrote:
These are actually the same thing so by making the vertices' "z" more negative you've accomplished the same task. By adding a translation on top of that you've potentially negated this effect.
Actually, I was translating the object out of the frustum along the x axis. But in any case it was causing it to go out of view.
The main source of confusion was in ortho the screen was 600 arbitrary GL units wide, whereas in perspective it was 2 units wide. Therefore, translating along x by 10 had a VERY different effect.
Re: gluPerspective problems
Posted: Sun May 09, 2010 8:37 pm
by qpHalcy0n
Whoops, yes move FARTHER away :]
Gotcha. It depends on where the object's "z" is in relation to the near and far clip planes as to how much of an effect a translation laterally will actually have on the final location. If you're very close to near, it'll have a huge effect. If you're very close to the far clip plane it will have minimal effect.
Re: gluPerspective problems
Posted: Sun May 09, 2010 10:01 pm
by dandymcgee
Re: gluPerspective problems
Posted: Mon May 10, 2010 3:31 pm
by eatcomics
Your one box gets cut off at the time, but I think you know why so yeah, pretty cool :D