C - OpenGL中的深度效果

时间:2016-04-17 20:47:30

标签: c opengl

我在线阅读本教程:http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/。更准确地说是关于投影矩阵的章节。

我正在尝试将我所阅读的内容应用于我所绘制的特定部分。但是我没有看到任何不同。我误解了什么,或者我只是错误地应用它?

我实际上正在画2间房子。我想创建这种效果:http://www.opengl-tutorial.org/assets/images/tuto-3-matrix/homogeneous.png

我的代码:

       void rescale()
       {
         glViewport(0,0,500,500);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glOrtho(-6.500, 6.500, -6.500, 6.500, -6.00, 12.00);
        }

        void setup()
        {
        glClearColor(1.0, 1.0, 1.0, 1.0); 
    glEnable(GL_DEPTH_TEST);
         }

       void draw()
        {

    glPushMatrix();
          //draw something in the orthogonal projection
    glPopMatrix();

    glPushMatrix();
    gluPerspective(0.0, 1, -12, 30);  //perspective projection
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        glTranslatef(-1, 0.5, -5);
        glColor3f(1.0, 0.0, 0.0);
        drawVertexCube();
    glPopMatrix();

    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        glTranslatef(-1, 0.5, -40);
        glColor3f(0, 1.0, 0.0);
            drawVertexCube();
    glPopMatrix();
   glPopMatrix();

    glFlush(); 
        }

结果如下:

enter image description here

如您所见,没有(可见)结果。我想更多地强调这种效果。

1 个答案:

答案 0 :(得分:0)

您的gluPerspective来电无效:

gluPerspective(0.0, 1, -12, 30);

nearfar参数必须都是正数,否则命令将仅生成GL错误并且不会修改当前矩阵,因此您的正投影仍在使用中。您的glPushMatrix() / glPopMatrix()来电无效,因为您从未将glOrtho()调用包含在此类块中,因此您永远不会恢复到尚未应用该矩阵的状态。此外。 glMatrixMode()glLoadIdentity()之间的glOrtho似乎也很奇怪。

您还应该知道除了glLoad*之外的所有GL矩阵函数都将乘以当前矩阵一个新矩阵。由于你仍然使用了ortho矩阵,你将得到两个矩阵的乘积,这将完全搞砸你的结果。

最后,您应该知道现代OpenGL的核心配置文件中所有 GL矩阵strack功能已弃用已完全删除 。如果你现在正在学习OpenGL,你应该考虑在OpenGL中学习“现代”的方式(基本上已经有十年了)。

相关问题