OpenGL Simple OpenGL Setup Sequence Oddity

This topic is 4093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

G'day! I am wondering why this works:
	glEnable(GL_TEXTURE_2D);
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glOrtho(0, 100, 100, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

But this doesn't:
	glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glClearColor(0, 0, 0, 1);
glOrtho(0, 100, 100, 0, -1, 1);

I am drawing white boxes, and using the latter causes them to not appear. Why is this so?

Share on other sites
In the first example, the projection matrix is set to an orthographic projection with range [0, 100] on X and Y axis, and the modelview matrix is the identity matrix.

The second example leaves the projection matrix untouched, and sets the modelview matrix to the same orthographic projection matrix as above but then immediately resets it to the identity matrix.

All matrix operations modifies the active matrix stack. So in the second example, the orthographic projection matrix ends up in the modelview matrix stack.

Share on other sites
Ohhhhhhhh.....

Thank you so much. I get it now. I had a strange misconception that by calling glMatrixMode() I was doing something like "enabling" the matrix... So, by calling glMatrixMode(), I am setting which matrix I will affecting when I use functions like glOrtho() and glLoadIdentity(), right?

Thanks for that!

Share on other sites
Yes, OpenGL is designed as a state machine.

You set modes and those modes stay set until they are changed.

Like if you bind a texture, that texture will stay bound until another texture is bound or textures are disabled. It is the same with Matrices, you set the mode, do your stuff, and set another mode.

Share on other sites
Okay, thanks a lot!

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 37
• 12
• 12
• 11
• Forum Statistics

• Total Topics
631413
• Total Posts
2999946
×

Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!