在glPushMatrix内外实现glulookat?

时间:2014-02-12 14:23:00

标签: c++ opengl

我知道在opengl中使用gluLookAt(...),glPushMatrix和其他基本转换内容的目的是什么。我被困在这些代码中。当我在设置opengl的适当要求后,在glPushMatrix()中实现glulookat(.....)。代码工作正常,在按键上,立方体通过适当的旋转渲染,但是当我在glPushMatrix()和glPopMatrix()之外实现gluLookAt(....)时,事情变得疯狂。多维数据集显示异常行为,最后得到 从屏幕上消失了。

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

1 个答案:

答案 0 :(得分:1)

gluLookAt使用查看矩阵对有效矩阵堆栈的当前最顶层元素进行多重处理,并用此替换最顶层的元素。

Push和Pop是标准的堆栈操作。 Push创建最顶层元素的副本并将其推送到堆栈顶部,pop将其删除。

因此,您在push-pop块中所做的任何更改都会通过pop操作恢复。但是在堆栈框架(push-pop)之外,变化将会累积。如果你在推送弹出之外的glLoadIdentity之前放置一个gluLookAt,那么它也可以正常工作,但是,因为你将矩阵重置为一个合理的值而不是工作在最重要的位置之上来自之前的渲染。