newsb
10-12-2011, 08:27 AM
Hi,
Below I have 3 newbie questions because I'm not very good at understanding modelview/projection matrices, which is relevant here... Even though I don't fully understand it, I've finally managed to put the following code together (doesn't compile here - the whole code ended up being relatively big, I hope you understand the part I include here anyway):
// based on OpenGL SuberBible "sphereworld" code...
-------------------------------
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glPushMatrix(); // <====== I HAVE A POP MATRIX IN THE BOTTOM!
frameCamera.ApplyCameraTransform();
// Position light before any other transformations
glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);
// Draw the ground
glColor3f(0.60f, .40f, .10f); // red/green/blue
DrawGround();
// Draw shadows first
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_STENCIL_TEST);
glPushMatrix();
glMultMatrixf(mShadowMatrix);
drawWindTurbine(true); // draw shadow=true <=== MY OWN STUFF
glPopMatrix();
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
// Draw inhabitants normally
drawWindTurbine(false); // draw shadow=false <=== MY OWN STUFF
// === MY OWN STUFF BELOW (ARE ALL THOSE PUSHES/POPS IN THE REST OF THE FUNCTION TO THE BOTTOM HERE, REALLY NECESSARY???) ===
//--
//setOrthographicProjection();
glMatrixMode(GL_PROJECTION); // switch to projection mode
glPushMatrix();
glLoadIdentity(); // reset matrix
//gluOrtho2D(left, right, bottom, top); // set a 2D orthographic projection
gluOrtho2D(0, 10, 0, 10); // set a 2D orthographic projection
glMatrixMode(GL_MODELVIEW); // switch back to modelview mode
// === BELOW: NOTICE THE INCOMPLETE "Your position is (rotation about y-axis): XXXXXXX deg." ===
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_CURRENT_BIT); // save, because color is changing below!
glPushAttrib(GL_ENABLE_BIT); // save, because color is changing below!
glDisable(GL_LIGHTING); // or else x-/y-/z- axis doesn't have same color...
char textWritten[] = "Your position is (rotation about y-axis): XXXXXXXX deg.";
glRasterPos2f(1, 9); // (int x, int y) arguments
int len = (int) strlen(textWritten);
for (int i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, textWritten[i]);
}
glPopAttrib(); // pop, because color was changed above!
glPopAttrib(); // pop, because color was changed above!
glPopMatrix();
//restorePerspectiveProjection();
glMatrixMode(GL_PROJECTION); // restore previous projection matrix
glPopMatrix();
glMatrixMode(GL_MODELVIEW); // get back to modelview mode
//--
glPopMatrix(); // <====== RELATED TO PUSH MATRIX IN THE BEGINNING!
// Do the buffer Swap
glutSwapBuffers();
}
-------------------------------
Questions:
1) I first managed to get the code above the section with "// === MY OWN STUFF BELOW (ARE ALL THOSE PUSHES/POPS NECESSARY??? ===" to work... But then I stole/found some code pieces from google and I inserted them below... The code piece produces a text in the upper left corner, no matter where I am in the world (x,y,z)... It's always fixed in upper left corner (before that, I had the code fixed to a 3D point in space so I could see the effect of projection when I got near/far away from the text). Could somebody perhaps please explain if I really need all those pushes/pops, because I think I have way too many now... I'm not sure I completely understand why it works...
2) Related to question 1: When I start the program, I think I start at 0,0,0... But once I've set all my objects I cannot figure out how to start at position (x,y,z) = 0,10,0 ? Really stupid/noobish question, I know... :-)
3) Notice the lines around:
char textWritten[] = "Your position is (rotation about y-axis): XXXXXXXX deg.";
In the initial xyz-coordinate system, x is right, y is up and z is from the screen towards myself. I have a glutSpecialFunc(specialKeys) and a glutKeyboardFunc(NormalKeys) section, that allows me to move around in this world using "w,a,s,d" + the 4 arrow keys. I would like to extract the z-vector of the matrix and calculate the orientation I'm facing, i.e. I need a number from 0-360 degrees. When I press left key I could go from perhaps, say 0 degrees (initially) to e.g. 315 degrees (NW). If I press right key, I would rotate (negatively) around the y-axis and I would face e.g. 45 deg (NE)... How to get this direction/angle?
Thanks for this great forum and your help!
p.s. this is not homework - I do it because it's interesting...
Below I have 3 newbie questions because I'm not very good at understanding modelview/projection matrices, which is relevant here... Even though I don't fully understand it, I've finally managed to put the following code together (doesn't compile here - the whole code ended up being relatively big, I hope you understand the part I include here anyway):
// based on OpenGL SuberBible "sphereworld" code...
-------------------------------
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glPushMatrix(); // <====== I HAVE A POP MATRIX IN THE BOTTOM!
frameCamera.ApplyCameraTransform();
// Position light before any other transformations
glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);
// Draw the ground
glColor3f(0.60f, .40f, .10f); // red/green/blue
DrawGround();
// Draw shadows first
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_STENCIL_TEST);
glPushMatrix();
glMultMatrixf(mShadowMatrix);
drawWindTurbine(true); // draw shadow=true <=== MY OWN STUFF
glPopMatrix();
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
// Draw inhabitants normally
drawWindTurbine(false); // draw shadow=false <=== MY OWN STUFF
// === MY OWN STUFF BELOW (ARE ALL THOSE PUSHES/POPS IN THE REST OF THE FUNCTION TO THE BOTTOM HERE, REALLY NECESSARY???) ===
//--
//setOrthographicProjection();
glMatrixMode(GL_PROJECTION); // switch to projection mode
glPushMatrix();
glLoadIdentity(); // reset matrix
//gluOrtho2D(left, right, bottom, top); // set a 2D orthographic projection
gluOrtho2D(0, 10, 0, 10); // set a 2D orthographic projection
glMatrixMode(GL_MODELVIEW); // switch back to modelview mode
// === BELOW: NOTICE THE INCOMPLETE "Your position is (rotation about y-axis): XXXXXXX deg." ===
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_CURRENT_BIT); // save, because color is changing below!
glPushAttrib(GL_ENABLE_BIT); // save, because color is changing below!
glDisable(GL_LIGHTING); // or else x-/y-/z- axis doesn't have same color...
char textWritten[] = "Your position is (rotation about y-axis): XXXXXXXX deg.";
glRasterPos2f(1, 9); // (int x, int y) arguments
int len = (int) strlen(textWritten);
for (int i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, textWritten[i]);
}
glPopAttrib(); // pop, because color was changed above!
glPopAttrib(); // pop, because color was changed above!
glPopMatrix();
//restorePerspectiveProjection();
glMatrixMode(GL_PROJECTION); // restore previous projection matrix
glPopMatrix();
glMatrixMode(GL_MODELVIEW); // get back to modelview mode
//--
glPopMatrix(); // <====== RELATED TO PUSH MATRIX IN THE BEGINNING!
// Do the buffer Swap
glutSwapBuffers();
}
-------------------------------
Questions:
1) I first managed to get the code above the section with "// === MY OWN STUFF BELOW (ARE ALL THOSE PUSHES/POPS NECESSARY??? ===" to work... But then I stole/found some code pieces from google and I inserted them below... The code piece produces a text in the upper left corner, no matter where I am in the world (x,y,z)... It's always fixed in upper left corner (before that, I had the code fixed to a 3D point in space so I could see the effect of projection when I got near/far away from the text). Could somebody perhaps please explain if I really need all those pushes/pops, because I think I have way too many now... I'm not sure I completely understand why it works...
2) Related to question 1: When I start the program, I think I start at 0,0,0... But once I've set all my objects I cannot figure out how to start at position (x,y,z) = 0,10,0 ? Really stupid/noobish question, I know... :-)
3) Notice the lines around:
char textWritten[] = "Your position is (rotation about y-axis): XXXXXXXX deg.";
In the initial xyz-coordinate system, x is right, y is up and z is from the screen towards myself. I have a glutSpecialFunc(specialKeys) and a glutKeyboardFunc(NormalKeys) section, that allows me to move around in this world using "w,a,s,d" + the 4 arrow keys. I would like to extract the z-vector of the matrix and calculate the orientation I'm facing, i.e. I need a number from 0-360 degrees. When I press left key I could go from perhaps, say 0 degrees (initially) to e.g. 315 degrees (NW). If I press right key, I would rotate (negatively) around the y-axis and I would face e.g. 45 deg (NE)... How to get this direction/angle?
Thanks for this great forum and your help!
p.s. this is not homework - I do it because it's interesting...