无法理解gluLookat

时间:2016-03-20 17:50:38

标签: c opengl

我是openGL的新手,我在使用openGL的gluLookat函数时遇到了一些问题。我已将原点设置在过剩窗口的左上角。我将相机放在原点并尝试查看我在原点创建的立方体并将其平移到屏幕中心。

int w = glutGet(GLUT_SCREEN_WIDTH);
int h = glutGet(GLUT_SCREEN_HEIGHT);
windowWidth = w * 2 / 3;
windowHeight = h * 2 / 3;

 glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, windowWidth, windowHeight, 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (0.0, 0.0, 0.0, windowWidth/2, windowHeight/2, 0.0, 0.0, 0.0, 1.0); //Placing camera at the origin.

    glPushMatrix(); // Set current matrix on the stack
      glTranslatef (windowWidth/2, windowHeight/2, 0.0); // Translating it to the center of the screen
      glutSolidSphere(5, 20,20); // creating the solid cube of radius 5
    glPopMatrix(); // Pop the old matrix without the transformations.
  glFlush(); 

 I am not able to view the solid cube on the screen. What is wrong with this piece of code. 

1 个答案:

答案 0 :(得分:2)

您在ortho和lookAt中使用窗口尺寸会产生误导,特别是对您自己而言。在ortho中你似乎想要制作一个与窗口大小相同的观察体积,这没关系。但是你希望你的相机在原点并朝向窗口的中心看,但是它并不是真正朝向窗口的中心,而是到达与尺寸的一半相同距离的点窗口。这并没有多大意义,因为当您调整窗口大小时,球体将移动得更远(展开窗口)或更近(缩小窗口)到相机。

我们假设窗口的尺寸为640 x 480.这意味着球体将以( 320, 240, 0 )为中心,距离相机400个单位。因此,为了让您看到对象,您的远值需要高一点。

要回答"错误的部分",在glOrtho中,第6个参数(zfar)太小,因此您的球体将远远超出观看体积。

gluLookAt来电没有问题。就像我说的那样,它做的事与你想要的不同。

真正的问题不是你的代码,而是你对代码和OpenGL函数的理解。因此,我建议您搜索查看体积,正交投影,透视投影和视图矩阵。

此外,学习现代OpenGL并完全放弃旧的OpenGL功能也不会让你受伤。

相关问题