   Camera View
  I'm working on an exercise which consists of a static camera view, a single bone animation (defined at the origin) and text. I am having some difficulties getting the correct camera view, but I know each draw process works, just not when everything is enabled. My Pseudocode is below:InitNextScene():   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();ENDSpawnCamera():   glTranslate(0,0,-50);ENDDrawAnimation():   //Note: The animated object is centered at the originglPushMatrix();glLoadIdentity();SetupAnimation();   glBindTexture();for k=1:NTriangles   glBegin( GL_TRIANGLES );   glNormal3fv();glTexCoord2fv();glVertex3fv();   glEnd    glPopMatrix();ENDDrawText():   glPushMatrix();//Note: Calculate the Ortho view such that the origin//is at the center of the screen;glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-width/2,width/2,-height/2,height/2);glTranslatef(0.0f,0.0f,0.5f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();   DrawString("Hello");    glPopMatrix();ENDIs there something obviously wrong here?   
Quote:Original post by clarkm2Is there something obviously wrong here?Well, it looks like you are losing your "camera" since you call glLoadIdentity() after glPushMatrix() when drawing (Remember, there is no camera in OpenGL, it's all just a transform, so if you get rid of the ModelView matrix you get rid of your camera). I also do not see you setting the projection matrix back to something acceptable for 3D after drawing text. Actually, that's hard to tell since you left out the overall rendering flow logic, but what you want to do is push the projection matrix before glOrtho and pop it after you're done drawing text.   ;Quote:Original post by lightbringerWell, it looks like you are losing your "camera" since you call glLoadIdentity() after glPushMatrix() when drawing (Remember, there is no camera in OpenGL, it's all just a transform, so if you get rid of the ModelView matrix you get rid of your camera).Calling glLoadIdentity should not affect the matrix stack, so why would the "camera" view get lost? When I call glPopMatrix at the end of each draw function, I should get the camera view back, correct? I am aware that there is no camera, so I am trying to emulate one in software.Quote:Original post by lightbringerI also do not see you setting the projection matrix back to something acceptable for 3D after drawing text. Actually, that's hard to tell since you left out the overall rendering flow logic, but what you want to do is push the projection matrix before glOrtho and pop it after you're done drawing text.The drawText function would be the final call and the process would then loop back to the function InitNextScene(), where the Projection matrix and glLoadIdentity matrix is set.When everything is enabled, I don't see the text and the animation looks very far away and appears as two moving pixels. I try to zoom in using the spawncamera function and it doesn't work.;Quote:Original post by clarkm2Calling glLoadIdentity should not affect the matrix stack, so why would the "camera" view get lost? When I call glPopMatrix at the end of each draw function, I should get the camera view back, correct? I am aware that there is no camera, so I am trying to emulate one in software.Because the matrix used for drawing is the current one, not the one in the back of the stack. Those other matrices are just for backup and aren't used for anything. Your current modelview matrix must include the camera transform (or more accurately, if I remember the concepts correctly, the inverse of the world transform of your camera object). The correct order for these operations is: 1) load identity for modelview matrix2) apply camera transforms3) push modelview matrix4) apply transforms for 1st object5) draw 1st object6) pop modelview matrix7) push modelview matrix8) apply transforms for 2nd object9) wash, rinse, repeat...Another way of thinking about it is: to simulate a camera, transform the entire scene by the inverse of the camera. That's why you need to keep this for every object and not discard it by calling glLoadIdentity()Quote:Original post by clarkm2The drawText function would be the final call and the process would then loop back to the function InitNextScene(), where the Projection matrix and glLoadIdentity matrix is set.This is not what is happening in your pseudocode though. The only thing you are currently doing there is setting the modelview matrix to identity. Are you calling glMatrixMode(GL_PROJECTION); gluPerspective() or something similar in your actual InitNextScene() ?Quote:Original post by clarkm2When everything is enabled, I don't see the text and the animation looks very far away and appears as two moving pixels. I try to zoom in using the spawncamera function and it doesn't work.So the spawncamera is for zooming, or for setting the camera in general? I can't be sure from your description, but it does sound like the problem is partially related to not resetting perspective mode correctly.   ;Yes one thing you made me realize is that I was not going back into PROJECTION_MODE after drawing text. I recently added this in, so my orginal code didn't not have this. I'm going to walk through the code again and try to debug this.The spawncamera function only places the camera view at a defined (x,y,z) coordinate while looking in the -z direction. The next step is zooming and moving around. I'm trying to load animations and objects duing run-time using the keyboard, so each draw function should have a defined template (push/pop), which does not screw up the camera view.;  I managed to get the animation to display, but once I draw the animation the text on the screen disappears. Here is my updated Pseudocode:InitScene():   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);glLoadIdentity();ENDSpawnCamera():   glTranslate(0,0,-75);ENDDrawAnimation(): //Note: The animated object is centered at the originglPushMatrix();    SetupAnimation();   glBindTexture();for k=1:NTriangles   glBegin( GL_TRIANGLES );   glNormal3fv();glTexCoord2fv();glVertex3fv();   glEnd    glPopMatrix();ENDDrawText():   glPushMatrix();//Note: Calculate the Ortho view such that the origin//is at the center of the screen;glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho(-width/2,width/2,-height/2,height/2,0,1000);glMatrixMode(GL_MODELVIEW);glLoadIdentity();   DrawString("Hello");glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();   glPopMatrix();ENDGoTo InitScene()I'm pretty confused about the proper way to switch between orthographic and perspective while preserving the camera's view as define in Spawncamera().Also if I enable 3D movement in the world, it seems I have to update the ortho 2D plane position on the z axis, so that the text is always shown (assuming small values of "far").   ;  Try disabling depth testing when drawing the text.There is usually no relation between drawing stuff in orthographic perspective and moving the camera around. You should never have to adjust z-position because of camera. DrawAnimation, if you only call it once per frame to draw all your stuff, should be something like:DrawAnimation():   glMatrixMode(GL_MODELVIEW);   glLoadIdentity();   SpawnCamera();   // stuff below here done once per object   //Note: The animated object is centered at the originglPushMatrix();    SetupAnimation();   glBindTexture();for k=1:NTriangles   glBegin( GL_TRIANGLES );   glNormal3fv();glTexCoord2fv();glVertex3fv();   glEnd    glPopMatrix();END;  I tried disabling depth testing and it didn't seem to help. I moved the text so that it overlapped with the animation, and I was able to see the text. The only problem is that the text color is black.This problem was solved by doing the following:1)Removing the glLoadIdenity function after each glPushMatrix call.2)Switching back to projection mode after drawing text in Orthographic mode.3)Disabling 2D texturing while drawing text (reason why the text was black).Thanks for the help!   