Robin Thuran Malhotra
Robin Thuran Malhotra

Reputation: 594

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

Upvotes: 0

Views: 175

Answers (2)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

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.

Upvotes: 1

ratchet freak
ratchet freak

Reputation: 48196

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.

Upvotes: 1

Related Questions