Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does glLoadIdentity have to be called after every call to glMatrixMode?

Tags:

c

opengl

I am reading a book on OpenGL, SDL, and have read some of the OpenGL documentation. I have also read this post: What does glLoadIdentity() do in OpenGL?

I am grasping what glLoadIdentity() does, and why it is used in a general sense (to return the current matrix back to its original state). What I do not know is why I am returning it to its original state. For instance:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

I am not exactly sure why glLoadIdentity() has to be called after each call to glMatrixMode.

like image 468
LunchMarble Avatar asked Aug 04 '11 23:08

LunchMarble


1 Answers

When to call glMatrixMode() -- 1st answer tries to explain:

glLoadIdentity() is typically called immediately after a matrix mode change so you are starting "fresh" if you will. Matrix transforms such as the gluPerspective(), glOrtho(), glFrustum(), glRotate(), glMultMatrix(), glTranslate() are cumulative operations because they aggregate to allow you to describe complex 3D world space transforms or to describe your OpenGL viewing volume. Example: if I want a cube translated in the +X direction then rotated around the Z axis I issue a glRotate() followed by a glTranslate().

glLoadIdentity() wipes out the matrix (of the current matrix mode) with the identity matrix so following a gluPerspective() by glLoadIdentity() is equivalent to a single call to glLoadIdentity(). In other words, that sequence is nonsensical.

like image 197
hari Avatar answered Nov 19 '22 19:11

hari