我是 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.
最佳答案
您在 ortho 和 LookAt 中使用的窗口尺寸具有误导性,尤其是对您自己而言。在正交中,您似乎想要将观察体积设置为窗口的大小,这没关系。但是,您希望相机位于原点并朝窗口的中心看,但它并不是真正朝窗口的中心看,而是看向与一半尺寸相同距离的点窗口的。这实际上没有多大意义,因为当您调整窗口大小时,球体将远离相机(扩大窗口)或更近(缩小窗口)。
假设窗口的尺寸为 640 x 480。这意味着球体将以 ( 320, 240, 0 )
为中心,距相机 400 个单位。因此,您的远值需要比该值高一点才能看到您的对象。
要回答“哪里出了问题”,在 glOrtho
中,第 6 个参数 (zfar) 太小,因此您的球体将远远超出查看体积。
gluLookAt
调用没有问题。只是,正如我所说,它所做的事情与您的预期不同。
真正的问题不是你的代码,而是你对代码和 OpenGL 函数的理解。所以我建议你搜索查看体积、正交投影、透视投影和 View 矩阵。
此外,学习现代 OpenGL 并完全放弃旧的 OpenGL 功能也不会有什么坏处。
关于c - 无法理解 gluLookat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36117309/