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

## Recommended Posts

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);
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);

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

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 on other sites
nevermind. it was my perspective angle distorting the sphere out of view

##### Share on other sites

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 resizedgluPerspective(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 thisglPushMatrix();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 on other sites
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 on other sites
what makes using gluLookAt to transform MODEL instead of perspective correct?

##### Share on other sites
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 on other sites
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 on other sites
thanks kburk that helped a lot! i get it now

• 10
• 18
• 14
• 18
• 15