Abhaya Singh Pandey
Abhaya Singh Pandey

Reputation: 67

implemenatation of glulookat inside and outside the glPushMatrix?

I knew what the purpose of using gluLookAt(...) , glPushMatrix and other basic transformational stuff in opengl. I am stuck in these piece of code. When i implement the glulookat(.....) inside the glPushMatrix() after setting the appropriate requirement for the opengl. The code works fine and on the keypress the cube gets rendered with appropriate rotation but when i implement the gluLookAt(....) outside the glPushMatrix() and glPopMatrix(), things got crazy. The cube shows the abnormal behaviour and finally it gets disappeared from the screen.

gluLookAt(0.0f, 0.0f, 400.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);          
glPushMatrix();

//gluLookAt(0.0f, 0.0f, 400.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);       
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);


glBegin(GL_QUADS);

    // Front Face
    // White
    glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)255);
    glVertex3f(50.0f,50.0f,50.0f);

    // Yellow
    glColor3ub((GLubyte) 255, (GLubyte)255, (GLubyte)0);
    glVertex3f(50.0f,-50.0f,50.0f);

    // Red
    glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)0);
    glVertex3f(-50.0f,-50.0f,50.0f);

    // Magenta
    glColor3ub((GLubyte) 255, (GLubyte)0, (GLubyte)255);
    glVertex3f(-50.0f,50.0f,50.0f);


// Back Face
    // Cyan
    glColor3f(0.0f, 1.0f, 1.0f);
    glVertex3f(50.0f,50.0f,-50.0f);

    // Green
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(50.0f,-50.0f,-50.0f);

    // Black
    glColor3f(0.0f, 0.0f, 0.0f);
    glVertex3f(-50.0f,-50.0f,-50.0f);

    // Blue
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-50.0f,50.0f,-50.0f);

// Top Face
    // Cyan
    glColor3f(0.0f, 1.0f, 1.0f);
    glVertex3f(50.0f,50.0f,-50.0f);

    // White
    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(50.0f,50.0f,50.0f);

    // Magenta
    glColor3f(1.0f, 0.0f, 1.0f);
    glVertex3f(-50.0f,50.0f,50.0f);

    // Blue
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-50.0f,50.0f,-50.0f);

// Bottom Face
    // Green
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(50.0f,-50.0f,-50.0f);

    // Yellow
    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex3f(50.0f,-50.0f,50.0f);

    // Red
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-50.0f,-50.0f,50.0f);

    // Black
    glColor3f(0.0f, 0.0f, 0.0f);
    glVertex3f(-50.0f,-50.0f,-50.0f);

// Left face
    // White
    glColor3f(1.0f, 1.0f, 1.0f);
    glVertex3f(50.0f,50.0f,50.0f);

    // Cyan
    glColor3f(0.0f, 1.0f, 1.0f);
    glVertex3f(50.0f,50.0f,-50.0f);

    // Green
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(50.0f,-50.0f,-50.0f);

    // Yellow
    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex3f(50.0f,-50.0f,50.0f);

// Right face
    // Magenta
    glColor3f(1.0f, 0.0f, 1.0f);
    glVertex3f(-50.0f,50.0f,50.0f);

    // Blue
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-50.0f,50.0f,-50.0f);

    // Black
    glColor3f(0.0f, 0.0f, 0.0f);
    glVertex3f(-50.0f,-50.0f,-50.0f);

    // Red
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-50.0f,-50.0f,50.0f);
glEnd();

glPopMatrix();

Upvotes: 1

Views: 268

Answers (1)

datenwolf
datenwolf

Reputation: 162164

gluLookAt mulitplies the currently topmost element of the active matrix stack with a look-at matrix and replaces the topmost element with this.

Push and Pop are standard stack operations. Push creates a copy of the topmost element and pushes it on the top of the stack, pop removes it.

So any changes you do within a push-pop block get reverted with the pop operation. But outside of a stack frame (push-pop) the changes will accumulate. If you put a glLoadIdentity before the gluLookAt outside of the push-pop, it will work as well, but that is, because you reset the matrix to a sane value instead of working on top of what's been there from the previous rendering.

Upvotes: 1

Related Questions