2016-03-20 79 views
-2

我是openGL的新手,我面临着openGL的gluLookat函数的一些问题。我已经在glut窗口的左上角设置了原点。我将相机放置在原点处并试图查看我在原点创建的实心立方体,并将其转换为屏幕中心。无法理解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. 
+0

这看起来像C,而不是C++。不要为不同的语言添加标签。 – Olaf

+0

我的不好!感谢编辑:) –

回答

2

您在ortho和lookAt中使用的窗口尺寸是误导性的,特别是对您自己。在正常情况下,您似乎希望将观看音量设置为窗口大小,没关系。但是,然后你想让你的相机在原点,并朝向窗口的中心看,但它不是真正朝向窗口的中心看,而是到一个点的距离是一半的尺寸窗口。这并没有多大意义,因为在调整窗口大小时,球体会移动得更远(扩大窗口)或更近(缩小窗口)到相机。

我们假设该窗口的尺寸为640 x 480.这意味着该球体的中心位置为(320, 240, 0),与您的相机距离为400单位。所以你的价值远远高于为了让你看到你的对象。


要回答“什么是错的一部分”,在glOrtho第6个参数(zfar)太小,所以你的球会以及观察空间之外。

调用gluLookAt没有问题。正如我所说,正在做一些与你想要的不同的事情。

真正的问题不在于你的代码,而在于你理解你的代码和OpenGL函数。因此,我建议您搜索观看音量,正交投影,透视投影和视图矩阵。

此外,学习现代OpenGL并完全放弃旧的OpenGL功能并不会伤害您。