Why do reshape codes end in glMatrixMode(GL_MODELVIEW)? - opengl

When looking at the reshape examples in the redbook, I usually find something like:
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}
I understand that calling glMatrixMode(GL_PROJECTION); followed by glLoadIdentity(); resets the projection matrix but I don't understand why glMatrixMode(GL_MODELVIEW) is usually called at the end of reshape.
In this particular example glFrustum affects the projection matrix, right? Why is GL_MODELVIEW called later? Would it make a difference if the last call to glMatrixMode(GL_MODELVIEW) is omitted?

Most of your rendering code is going to affect GL_MODELVIEW, because it's what affects object translation and camera position. However, the resizing code works on GL_PROJECTION. The programmer probably assumes the current matrix is the modelview one in most of his code, and when a different one needs to be affected, he/she would change the matrix, update it, and then change the target back to GL_MODELVIEW so the rest of the code doesn't target the wrong matrix.
Generally, OpenGL functions affect whatever matrix is currently being targeted, which is why you change the matrix.

Related

OpenGL object not rotating

So, I've been trying to rotate a single object in an OpenGL/GLUT environment.
After going through several questions on SO and the like, I've written what appears to be correct code, but no dice. Does anyone know how to make this work?
PS: I've tried changing the GLMatrixmode to Projection, but that just shows a black screen. Same thing happens if I use glLoadIdentity().
Here's my rendering code
void display()
{
preProcessEvents();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(Camera::position.x, Camera::position.y, Camera::position.z,
Camera::position.x+Math::sind(Camera::rotationAngles.x)*Math::cosd(Camera::rotationAngles.y),
Camera::position.y+Math::cosd(Camera::rotationAngles.x),
Camera::position.z+Math::sind(Camera::rotationAngles.x)*Math::sind(Camera::rotationAngles.y),
0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-1, 0,-3);
glColor3f(0, 1, 0);
glVertex3f(0.0f, 2.0f,-3);
glColor3f(0, 0, 1);
glVertex3f(1.0f, 0.0f,-3);
glEnd();
glBindTexture(GL_TEXTURE_2D, tex->textureID);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glTexCoord2f(100, 100);
glVertex3f(100,0,100);
glTexCoord2f(-100, 100);
glVertex3f(-100,0,100);
glTexCoord2f(-100,-100);
glVertex3f(-100,0,-100);
glTexCoord2f(100,-100);
glVertex3f(100,0,-100);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
object1.draw();
glTranslatef(-10.0, 10.0, 0.0);
glBindTexture(GL_TEXTURE_2D, tex2->textureID);
gluQuadricTexture(quad,1);
gluSphere(quad,10,20,20);
glBindTexture(GL_TEXTURE_2D, 0);
//RELEVANT CODE STARTS HERE
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(190, 0.0, 0.0, 1.0);
glPopMatrix();
glutSwapBuffers();
}
Are you aware what glPushMatrix and glPopMatrix do? They save and restore the "current" matrix.
By enclosing your rotation in that and then doing no actual drawing operation before restoring the matrix the entire sequence of code beginning with //RELEVANT CODE STARTS HERE is completely pointless.
Even if you did not push/pop, your rotation would only be applied the next time you draw something. Logically you might think that would mean the next time you call display (...), but one of the first things you do in display (...) is replace the current matrix with an identity matrix (line 3).
In all honesty, you should consider abandoning whatever resource you are currently using to learn OpenGL. You are using deprecated functionality and missing a few fundamentals. A good OpenGL 3.0 tutorial will usually touch on the basics of transformation matrices.
As for why changing the matrix mode to projection produces a black screen, that is because the next time you call display (...), gluLookAt operates on the projection matrix. In effect, you wind up applying the camera transformation twice. You should really add glMatrixMode (GL_MODELVIEW) to the beginning of display (...) to avoid this.
You do the rotation (and reset it with glPopMatrix) after you draw, do the rotation code before the glBegin/glEnd calls.
Or just move to the shader based pipeline and manage you own transformation matrices.

openGL transformation and gluLookAt()

I have a studying project which represents simple 3D scene. I want to draw sphere in some non-origin point. Later I'm going implement this as separate function or method.
I'm setting point of view using gluLookAt() then I'm transforming model-view matrix using glTranslatef() with little offset and drawing sphere. Unfortunately, the sphere isn't shown. Am I right with model-view matrix approaching?
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1, 0 ,1, 0, 0, 0, 0, 1, 0);
glColor3b(197, 96, 63);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.1, 0, 0);
glutWireSphere(0.2, 20, 10);
glPopMatrix();
glFlush();
}
void reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho ((float)w/(float)h, (float)-w/(float)h, -1, 1, 0.8, 100);
glMatrixMode(GL_MODELVIEW);
}
No, you aren't.
gluLookAt(1, 0 ,1, 0, 0, 0, 0, 1, 0);
glColor3b(197, 96, 63);
glPushMatrix();
glLoadIdentity(); // why it should be there?
By zeroing the view matrix there, you are drawing your object relatively to the origin coordinates, not taking your glLookAt into account. The call to it is effectively ignored. It should be coded as:
Set up the "camera matrix"
Push the matrix on the stack
Translate to the object's position
Draw the object
Pop and go back to 2.
So if you want to set up hypothetical "camera", you have to combine positions of objects with the camera matrix itself.
Your approach doesn't look that unreasonable. The problem is here:
glPushMatrix();
glLoadIdentity();
glTranslatef(0.1, 0, 0);
The pushing (and later popping) is a good idea, but by setting the matrix to identity before the translation, you loose any transformations done before, in particular the viewing transformations established with gluLookAt. So just remove this glLoadIdentity to properly concatenate the individual transformations.
Always keep in mind that all the matrix transformation functions, like glTranslate, glOrtho, or gluLookat always modify the currently selected (with glMatrixMode) matrix and don't just replace it. This is also the reason why you do a glLoadIdentity before the calls to glOrtho and gluLookAt.

How to determine a full-window rectangle in OpenGL

That's problem: I'm currently in OpenGL eye-space, now I want make a black rectangle to cover all window's area. How can I determine a exactly X, Y, Z position to do this?
Update
Or can someone tell me how can I determine the X, Y (top-left) of window when we have a Z value?
You can try to calculate a rectangle that fits the camera exactly and go from there. To do that, you would need to take into account the projection matrix and calculate an inverse.
However, there is a simpler method. All you have to do is to change the camera temporarily, so that you know exactly how to draw the rectangle. A simple enough camera is the default orthogonal camera, with simple limits(from -1 to 1). The following does that:
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glBegin(GL_QUAD);
glVertex2f(-1.f, -1.f);
glVertex2f(1.f, -1.f);
glVertex2f(1.f, 1.f);
glVertex2f(-1.f, 1.f);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

OpenGL Camera position and skybox

Been integrating this camera tutorial http://www.swiftless.com/tutorials/opengl/camera2.html and having a bit of trouble centering the camera in the skybox.
Using this code below makes my camera inside the box:
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.0, 1.0, -1.0*(GLfloat)h/(GLfloat)w,
1.0*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-1.0*(GLfloat)w/(GLfloat)h,
1.0*(GLfloat)w/(GLfloat)h, -1.0, 1.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
To draw the skybox, I followed this tutorial: http://sidvind.com/wiki/Skybox_tutorial
I've been trying to translate objects closer to the camera, but didn't work as I expected. Now I'm not sure what I need to do.
Appreciate any help.
First: Don'y apply the projection in the reshape handler. Otherwise simple things appear impossible (like doing a skybox). Second: For a skybox to work you must use the very same projection like for the rendering of the rest of the scene. What you should change is the translation of the modelview to 0, yet keeping the camera orientation.
You can do this by setting the last column of the modelview matrix to (0,0,0,1).
So this makes your rendering code like this:
void render_skybox()
{
push_modelview();
set_modelview_column(3, 0, 0, 1);
draw_skybox();
pop_modelview();
}
void render()
{
set_viewport();
set_projection();
apply_camera_transform();
render_skybox();
render_scene();
}

Is there a difference between gluLookAt() and glFrustum()?

My instructor says they can be used interchangeably to affect the projection matrix. He argues that gluLookAt() is preferably used because of its 'relative simplicity due to its ability to define the viewing angle'. Is this true? I've seen code examples using both gluLookAt() and glFrustum() and I'm wondering why would the programmmer mix them.
Like in the cube.c example of the redbook appears:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
**//why isn't this a call to glFrustum?**
glScalef(1.0, 2.0, 1.0);
glutWireCube(1.0);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
//why isn't this a call a call to gluLookAt()?
glMatrixMode(GL_MODELVIEW);
}
Both glFrustum and gluLookAt perform simple matrix multiplication. Check the man pages for equations for those matrices:
gluLookAt
glFrustum
Both of those can be replaced by a glMultMatrix* call.
The most important difference is that glFrustum is used most of the time to establish a perspective projection matrix (used internally by gluPerspective), and gluLookAt is a convenience method for specifying model-view matrices (usually: implementing a camera).
gluLookAt affects the ModelView matrix whereas, glFrustum/gluPerspective affect the Projection matrix. These are entirely two different matrices in the pipeline. gluLookAt is a utility method which is a combination of glRotate and glTranslate calls to move the "camera" to the appropriate point in space and point it to the desired location. Whereas, glFrustum/gluPerspective are methods which define a perspective viewing volume. Check out for more details in the OpenGL Red Book.