【问题标题】:Saving and restoring the Projection and ModelView matrices保存和恢复 Projection 和 ModelView 矩阵
【发布时间】:2012-03-10 23:28:16
【问题描述】:

我正在尝试编写在渲染动画和渲染平视显示器之间循环的代码。动画有效,但是我在动画的投影矩阵和 HUD 的另一个正交投影矩阵之间切换时遇到了困难。

主循环运行如下:

init();
while (isAnimationRunning) {
    drawAnimation();
    drawHUD();
}

而表面的初始化是:

void init(){
    glEnableClientState(GL10.GL_VERTEX_ARRAY);
    glClearColor(1, 1, 1, 1);
    glMatrixMode(GL11.GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, windowWidth, windowHeight);
    GLU.gluPerspective(gl11, 45.0f, windowWidth / windowHeight, 0.1f, 100.0f);
}

以及动画渲染的代码

void drawAnimation() {
        glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL10.GL_MODELVIEW);
        glLoadIdentity();
        GLU.gluLookAt(gl11, x, y, zoom, x, y, 0, 0, 1, 0);
            drawAnimationTextures();
}

以及HUD渲染的代码

void drawHUD(){
        glMatrixMode(GL10.GL_MODELVIEW);
        glPushMatrix();

            // IF THIS LINE IS REMOVED THE ANIMATION DISPLAYS BUT NOT THE HUD
        glLoadIdentity(); 

        glClear(GL10.GL_COLOR_BUFFER_BIT);
        GLU.gluLookAt(gl11, 0, 0, 2, 0, 0, 0, 0, 1, 0);

        glMatrixMode(GL10.GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        GLU.gluPerspective(gl11, 45, aspectRatio, 0.1f, 100.0f);
        glOrthof(-aspectRatio, aspectRatio, -1, 1, -4, 4);

        drawHUDTexture();

    glPopMatrix();
        glMatrixMode(GL10.GL_MODELVIEW);
        glPopMatrix();
}

执行此代码时屏幕为空白。任何想法可能是什么问题?

/////////////////////

void drawTexture(float x, float y, float z, float sizeX, float sizeY,...){
            glEnable(GL10.GL_TEXTURE_2D);
            glPushMatrix();
            bindTexture(texture);
            glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            glFrontFace(GL10.GL_CW);
            glColor4f(RGB[0], RGB[1], RGB[2], alpha);
            glTranslatef(x, y, z);
            glScalef(sizeX, sizeY, sizeZ);
            glVertexPointer(3, GL10.GL_FLOAT, 0, squareVertexBuffer);
            glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
            glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
            glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            glPopMatrix();
}

【问题讨论】:

  • 这是整数除法吗? GLU.gluPerspective(gl11, 45.0f, windowWidth / windowHeight, 0.1f, 100.0f);drawTexture()函数的内容是什么?
  • 嗨。不是除法截断,但我之前在 gluPespective 中遇到过这个问题。 drawTexture() 贴在上面。感谢您查看此内容。

标签: android opengl-es


【解决方案1】:

为什么要在两个例程中清除帧缓冲区?您应该只在顶层循环中调用它一次。如果在注释掉 loadidentity 时出现动画但仍然有 glclear 我很困惑。

看起来 hud 覆盖了整个屏幕,让它半透明来检查一下,它可能太大了,你只能看到它的一小部分。

另外,您的 hud 代码正在设置透视图,然后是正交视图 - 您想要哪个?

【讨论】:

  • clearColor() 没关系,但同时调用 gluPerspective() 是问题所在。谢谢你。
猜你喜欢
  • 2012-06-11
  • 2011-08-28
  • 1970-01-01
  • 2013-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-14
相关资源
最近更新 更多