2011-07-03 285 views
1

我正在浏览一系列NeHe OpenGK教程。 Tutorial #9做一些花哨的东西;我明白了一切,除了两件事我认为是整个教程的骨干。OpenGl旋转和翻译

DrawGlScene函数中,我没有理解下面这行。

glRotatef(tilt,1.0f,0.0f,0.0f); // Tilt The View (Using The Value In 'tilt') 

我明白那行代码的作用,它在教程中也很清楚地提到。但我不明白他为什么要倾斜屏幕。

另一件事是他首先倾斜屏幕,然后用星形角度旋转屏幕,然后立即旋转屏幕。那是什么技术?什么需要倾斜?当星星面向用户时,旋转星星。

glRotatef(star[loop].angle,0.0f,1.0f,0.0f); // Rotate To The Current Stars Angle 
glTranslatef(star[loop].dist,0.0f,0.0f); // Move Forward On The X Plane 

glRotatef(-star[loop].angle,0.0f,1.0f,0.0f); // Cancel The Current Stars Angle 
glRotatef(-tilt,1.0f,0.0f,0.0f);    // Cancel The Screen Tilt 

如果有些机构告诉我机制正在进行,我将非常感激。

+0

你可能想看看一个相关的问题http://stackoverflow.com/questions/6565630/rotate-5-circle-problem – whoplisp

回答

1

我不明白他为什么要倾斜屏幕。

倾斜让你看到另一个角度的星星,而不仅仅是“正上方”。

另一件事是他首先倾斜屏幕,然后以星形角度旋转,然后立即旋转屏幕。那是什么技术?

这是因为他想围绕选定平面(在这种情况下是Y平面)旋转恒星,但是(!)他也希望有纹理的四边形面对观看者。让我们假设他将它旋转90度,如果是这样,你只会看到(如他在教程中所述)一条“粗”线。

考虑这些评论:

// Rotate the current drawing by the specified angle on the Y axis 
// in order to get it to rotate. 
glRotatef(star[loop].angle, 0.0f, 1.0f, 0.0f); 

// Rotating around the object's origin is not going to make 
// any visible effects, especially since the star object itself is in 2D. 
// In order to move around in your current projection, a glRotatef() 
// call does rotate the star, but not in terms of moving it "around" 
// on the screen. 
// Therefore, use the star's distance to move it out from the center. 
glTranslatef(star[loop].dist, 0.0f, 0.0f); 

// We've moved the star out from the center, with the specified 
// distance in star's distance. With the first glRotatef() 
// call in mind, the 2D star is not 100 % facing 
// the viewer. Therefore, face the star towards the screen using 
// the negative angle value. 
glRotatef(-star[loop].angle, 0.0f, 1.0f, 0.0f); 

// Cancel the tilt on the X axis. 
glRotatef(-tilt, 1.0f, 0.0f, 0.0f); 
+0

你的回答是非常有益的,你已经正确地解释了这个例子中的旋转和平移的复杂行为,就像第一次旋转然后扭转那样。然而在我看来,在本教程简单的事情已经变得复杂就像我们可以通过下面的行获得同样的效果\t \t \t \t \t \t \t \t 的glTranslatef(0.0F,0.0F,缩放); glRotatef(star [loop] .angle,0.0f,0.0f,1.0f);的glTranslatef(星[循环] .dist,0.0F,0.0F);而不是先旋转的所有线条,然后再旋转。欢迎评论。 –

+0

处理翻译和旋转的方法很多,主要是个人偏好。 :-) – Wroclai