Sign in to follow this  
datadawgx

simple glLoadIdentity() prob?

Recommended Posts

datadawgx    136
ok guys, help a newbie out. i'm having problems using glLoadIdentify() between drawing simple transformed objects. basically, I want to: 1 - draw a sphere in the middle of the screen 2 - draw a second sphere 1/2 way between the origin (upper left corner, in my case) and the middle of the screen (let's call that 1/2 middle) the first sphere draws fine. if i DONT use glLoadIdentity() afterwards, the second sphere draws at middle + 1/2 middle (as opposed to just 1/2 middle). but, if i call glLoadIdentity() between drawing the spheres, the second sphere doesn't draw at all. am i dont something wrong? here's some of the basic code. any help would be great!
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, window_width, window_height);		
gluPerspective(g_fov,(GLfloat)window_width/(GLfloat)window_height, g_near, g_far);
gluLookAt(g_camera->xPos, g_camera->yPos, g_camera->zPos,
             g_camera->xView, g_camera->yView, g_camera->zView,
             g_camera->xUp, g_camera->yUp, g_camera->zUp);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glColor3f(1.0f,1.0f,1.0f);
glTranslatef(mid_width,mid_height,mid_depth);
glutSolidSphere(30.0,100,100);

glLoadIdentity(); //the problem

glColor3f(1.0f,0.0f,1.0f);
glTranslatef(mid_width/2.0f,mid_height/2.0f,mid_depth/2.0f);
glutSolidSphere(30.0,100,100);

Share this post


Link to post
Share on other sites
Gorax    202
I made a few changes:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//you really only need to call glViewport() when the window is created and resized
gluPerspective(g_fov,(GLfloat)window_width/(GLfloat)window_height, g_near, g_far);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//moved the 'look at' function to the proper place:
gluLookAt(g_camera->xPos, g_camera->yPos, g_camera->zPos,
g_camera->xView, g_camera->yView, g_camera->zView,
g_camera->xUp, g_camera->yUp, g_camera->zUp);

glColor3f(1.0f,1.0f,1.0f);
//push and pop is better in situations like this
glPushMatrix();
glTranslatef(mid_width,mid_height,mid_depth);
glutSolidSphere(30.0,100,100);
glPopMatrix();

//no more loading the identity matrix, since it's not needed (push & pop)

glColor3f(1.0f,0.0f,1.0f);
//you might want to do the same for this (depending on what you're doing)
glTranslatef(mid_width/2.0f,mid_height/2.0f,mid_depth/2.0f);
glutSolidSphere(30.0,100,100);

Share this post


Link to post
Share on other sites
TomasH    360
The view transformation (gluLookAt in your case) should go on the modelview matrix. Something like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, window_width, window_height);
gluPerspective(g_fov,(GLfloat)window_width/(GLfloat)window_height, g_near, g_far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(g_camera->xPos, g_camera->yPos, g_camera->zPos,
g_camera->xView, g_camera->yView, g_camera->zView,
g_camera->xUp, g_camera->yUp, g_camera->zUp);
// For each object:
glPushMatrix();
// Do modelling transformation (glTranslate etc.)
glPopMatrix();


Help stamp out GL_PROJECTION abuse.

Share this post


Link to post
Share on other sites
datadawgx    136
and i am trying to understand the practicle use of glPopMatrix and glPushMatrix... I don't see what is actually happening when I use these commands.

Share this post


Link to post
Share on other sites
kburkhart84    3187
gluLookAt in simple language moves and rotates the camera, though in reality it is rotating and translating all the models that follow, which is why it is done in the modelview matrix. Unless you are doing some kind of special effect, the only time usually you need to change your perpective matrix is at initialization to set up the field of view. Also, you might change it to mix 2d and 3d views in the same frame, but that's about it. Also, unless you want it different, the glViewport only needs to be called once per frame as well. In reality, you shouldn't be doing any matrix switching unless you DO need to modify the other matrix instead of the modelview matrix(which in this case you don't). Now the push and pop commands. The glPushMatrix, takes the current matrix, pushes it down, and puts a copy on top of it. Then you can modify the copy(rotation, translation, etc...). Then the glPopMatrix brings back what you had before you modified it. This can be done several times(I think 32 for the modelview matrix, 2 for perspective and texture matrices). This is good for modeling objects in space relative to where and how other objects are placed. Say you have a moving solar system. The whole thing moves, but the planets stay the same relative to the sun. So you position the sun using variables to describe it(glTranslate(sunx, suny, sunz). Then you push and pop the matrix to position each planet using(glTranslate(5, 3, 1)) because since you know each planets position relative to the sun, you don't need a variable for it(though there are better programming practices). Then, even though the position of the sun changes and you keep the numbers for the posiitons of the planets constant, they will move with the sun.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this