gluPerspective problems

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

gluPerspective problems

Post 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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: gluPerspective problems

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: gluPerspective problems

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: gluPerspective problems

Post 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);
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: gluPerspective problems

Post 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?!
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: gluPerspective problems

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: gluPerspective problems

Post 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!
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: gluPerspective problems

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: gluPerspective problems

Post 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? :shock:
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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: gluPerspective problems

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: gluPerspective problems

Post by dandymcgee »

In case you were wondering, this is what I've made so far:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: gluPerspective problems

Post by eatcomics »

Your one box gets cut off at the time, but I think you know why so yeah, pretty cool :D
Image
Post Reply