sam
sam

Reputation: 123

OpenGL, how to rotate objects independent of each other?

My code so far

void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
gluLookAt(  camera[0][0],       camera[0][1],       camera[0][2],
            camera[1][0],       camera[1][1],       camera[1][2],
            camera[2][0],       camera[2][1],       camera[2][2]            );          //Set the point looking at


glRotatef(cubeRot[0], 1.0f, 0.0f, 0.0f);    //rotate on x axis
glRotatef(cubeRot[1], 0.0f, 1.0f, 0.0f);    //rotate on y axis
glRotatef(cubeRot[2], 0.0f, 0.0f, 1.0f);    //rotate on z axis

switch ( Rendermode ) { //different render mode

    case 'f':
            //Draw object I want to rotate
    break;

    case 'v':
            //Draw object I want to rotate          
    break;

    case 'e':
            //Draw object I want to rotate
    break;

glLoadIdentity();

}

//Draw object I DO NOT want to rotate

glutSwapBuffers ( ); // Swap The Buffers To Not Be Left With A Clear Screen
}

However at the moment all my objects rotate at the same time, how can I rotate the object I've noted to want to rotate while leaving the ones I've noted to not want to rotate still?

Upvotes: 2

Views: 3473

Answers (2)

Berke Cagkan Toptas
Berke Cagkan Toptas

Reputation: 1034

As @karlphillip states you need to put your rotation in to glPush/glPop matrix encapsulation. I think this code should work :

void display ( void )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
gluLookAt(  camera[0][0],       camera[0][1],       camera[0][2],
            camera[1][0],       camera[1][1],       camera[1][2],
            camera[2][0],       camera[2][1],       camera[2][2]            );          //Set the point looking at

glPushMatrix();  //-------------------- Encapsulate Rotation ----------------
glRotatef(cubeRot[0], 1.0f, 0.0f, 0.0f);    //rotate on x axis
glRotatef(cubeRot[1], 0.0f, 1.0f, 0.0f);    //rotate on y axis
glRotatef(cubeRot[2], 0.0f, 0.0f, 1.0f);    //rotate on z axis

switch ( Rendermode ) { //different render mode

    case 'f':
            //Draw object I want to rotate
    break;

    case 'v':
            //Draw object I want to rotate          
    break;

    case 'e':
            //Draw object I want to rotate
    break;   
}
glPopMatrix(); //------------------- Encapsulate Rotation Ends -------

//Draw object I DO NOT want to rotate

glutSwapBuffers ( ); // Swap The Buffers To Not Be Left With A Clear Screen
}

Upvotes: 2

karlphillip
karlphillip

Reputation: 93410

Encapsulate the linear transformations with glPush/PopMatrix() and draw the object. These functions save/restore the current state of the matrix, so they don't affect other drawings.

glPushMatrix();
glRotatef(...); 
// glTranslatef(...), 
//glScalef(...);
drawObject1();
glPopMatrix();

glPushMatrix();
glRotatef(...); 
// glTranslatef(...), 
//glScalef(...);
drawObject2();
glPopMatrix();

Upvotes: 2

Related Questions