【问题标题】:OpenGl Rotation and TranslationOpenGL旋转和平移
【发布时间】:2011-07-03 22:08:42
【问题描述】:

我正在阅读一系列 NeHe OpenGK 教程。 Tutorial #9 做了一些花哨的事情;我什么都懂,除了我认为是整个教程的支柱的两件事。

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

如果有人告诉我引擎盖下的机制,我将非常感激。

【问题讨论】:

标签: opengl rotation translation 3d


【解决方案1】:

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

倾斜可以让您从另一个角度看到星星,而不仅仅是“正上方”。

另一件事是他首先倾斜屏幕,然后将其旋转一个星角,然后立即执行相反的操作。这是什么技术?

那是因为他想围绕选定平面(在本例中为 Y 平面)旋转星形,但是(!)他还希望纹理四边形面向观察者。假设他将其旋转 90 度,如果是这样,您只会看到(就像他在教程中所说的那样)一条“粗”线。

考虑这些 cmets:

// 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);

【讨论】:

  • 您的回答非常有帮助,并且您在此示例中正确解释了旋转和平移的复杂行为,例如先旋转然后反转。但是在我看来,在本教程中,简单的事情变得复杂了,就像我们可以通过以下几行获得相同的效果 glTranslatef(0.0f,0.0f,zoom); glRotatef(star[loop].angle,0.0f,0.0f,1.0f); glTranslatef(star[loop].dist,0.0f,0.0f);而不是所有首先旋转然后反向旋转的线。欢迎评论。
  • 有很多方法可以处理平移和旋转,这主要是个人喜好。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多