OpenGL 中相机无法向前移动

发布于 2024-11-24 03:39:24 字数 734 浏览 1 评论 0原文

我正在尝试 pyOpenGL,但无法让相机正常工作。

def Draw():

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     glMatrixMode(GL_MODELVIEW)
     glLoadIdentity()

     glRotatef(roty,1,0,0)
     glRotatef(rotx,0,1,0)
     glTranslate(0,0,eyez)

     glPushMatrix()

     glTranslate(0,0,-80)

     glBegin(GL_QUADS)
     glColor(0,1,0)
     glVertex3f(-50,-5,10)
     glVertex3f(-50,50,10)
     glVertex3f(50,50,10)
     glVertex3f(50,-5,10)

     glColor(1,0,0)
     glVertex3f(-50,-5,10)
     glVertex3f(-50,50,10)
     glVertex3f(-50,50,70)
     glVertex3f(-50,-5,70)
     glEnd()

     glPopMatrix()

     glutSwapBuffers()

这些旋转效果很好,但我无法沿着相机面向的方向向前移动。当我修改代码以便能够以这种方式向前移动时,场景将无法正确旋转。如果我倒退得足够远,场景将会旋转一圈。

I'm experimenting with pyOpenGL and I've had trouble getting the camera to work properly.

def Draw():

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
     glMatrixMode(GL_MODELVIEW)
     glLoadIdentity()

     glRotatef(roty,1,0,0)
     glRotatef(rotx,0,1,0)
     glTranslate(0,0,eyez)

     glPushMatrix()

     glTranslate(0,0,-80)

     glBegin(GL_QUADS)
     glColor(0,1,0)
     glVertex3f(-50,-5,10)
     glVertex3f(-50,50,10)
     glVertex3f(50,50,10)
     glVertex3f(50,-5,10)

     glColor(1,0,0)
     glVertex3f(-50,-5,10)
     glVertex3f(-50,50,10)
     glVertex3f(-50,50,70)
     glVertex3f(-50,-5,70)
     glEnd()

     glPopMatrix()

     glutSwapBuffers()

These rotations work great but I am not able to move forward in the direction the camera is facing. When I have modified the code to be able to move forward in this way the scene will not rotate correctly. What will happen is that the scene will rotate in a circle if I back up far enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七秒鱼° 2024-12-01 03:39:24

如果您正在编写一个具有相机移动(我想也是旋转)概念的应用程序,那么而不是使用 glRotateglTranslate< /code> 考虑研究一下 gluLookAt,可以这么说,它允许您以以相机为中心的方式进行设置。

顺便说一下,gluLookAt 已被弃用,尽管有些人说他们“非常怀念它”。如果您希望远离这个已弃用的实用函数,请参阅此函数的源代码并滚动您的CPU上有自己的矩阵。

If you are writing an application that has the concept of the camera moving (and rotating too I suppose) then rather than operating on the object with glRotate and glTranslate consider looking into gluLookAt, which allows you to set things up in a camera-centered way, so to speak.

By the way, gluLookAt is deprecated, even though some people have said they "miss it a lot." If you wish to stay away from this deprecated utility function, see the source of this function and roll your own matrix on the CPU.

独留℉清风醉 2024-12-01 03:39:24

要前进,您必须首先确定前进方向。您需要获得一个指向相机面向方向的 3D 矢量。一种方法是将向量 (0, 0, -1) 乘以相机矩阵的旋转部分的逆。一般来说,有更简单的方法可以做到这一点,但它们都取决于您如何计算相机矩阵。

一旦你有了一个指向相机正面方向的单位向量,这就很容易了。只需将其乘以您想要沿该方向移动的距离,并将其添加到相机的当前位置即可。

请注意,如果您使用 gluLookAt,您还需要同时偏移观察点。否则,你的相机会移动,但它会尝试面对世界空间中一个不动的点。

To move forward, you must first determine where forward is. You need to get a 3D vector that points in the direction the camera faces. One way to do that is to multiply the vector (0, 0, -1) by the inverse of the rotation portion of the camera matrix. Generally, there are easier ways to do it, but they all depend on how you're computing your camera matrix.

Once you have a unit vector pointing in the direction of the camera's facing, it's easy. Just multiply this by how far along that direction you want to move and add it into the camera's current position.

Note that if you use gluLookAt, you will also need to offset the lookat point at the same time. Otherwise, your camera will move, but it will try to face an unmoving point in world space.

无法回应 2024-12-01 03:39:24

虽然我最习惯在 JOGL 中编程而不是任何其他 OpenGL 绑定,但我建议您使用以下检查点:

*)您是否在正确的函数中设置(对我来说,它是 JOGL GLEventListener 的 reshape(int x, int y, int width, int height) 方法)、视口和截锥体?

=>对于视口(用于 OpenGL 绘图的帧区域),通常只需调用 glViewport(x,y,width,height) =>为 GL 绘图分配所有 Canvas 区域

=>对于视锥体(相机视野),您必须首先切换到投影矩阵(glMatrixMode(GL_PROJECTION)),然后设置您的视锥体,然后切换回 GL_MODELVIEW 矩阵,以便为您的绘图做好准备。我建议您为了定义平截头体,调用 gluPerspective(45, (float) width/height, 0.001f, 1000f)。 45 表示 45 度半角,宽度/高度表示屏幕比例,0.001f 表示距眼睛(或屏幕)0.001f 的近截平面,1000f 表示距眼睛 1000f 的远截平面。

*)您是否考虑过在绘图函数中使用对 gluLookAt 的调用,正如 Ray Toal 所建议的那样?我建议你调用 gluLookAt(

0,0,1, // 相机设置在 (0,0,1),这样它就会看到 0,0,0 点

0,0,0, // 相机正在瞄准at (0,0,0)

0,1,0) // 相机的向上向量为 (0,1,0) (所以,Oy 是向上向量...逻辑)

希望这可以帮助你

Although I am most used to program in JOGL than any other OpenGL binding, I suggest you the following checkpoint :

*) Have you set, in the correct function (for me it is JOGL GLEventListener's reshape(int x, int y, int width, int height) method), the viewport and the frustum ?

=> For the viewport (the frame area used for the OpenGL drawing), you typically just call glViewport(x,y,width,height) => allocating all your Canvas zone for your GL drawings

=> For the frustum (the camera field of view), you must first switch to Projection matrix (glMatrixMode(GL_PROJECTION)), then set your frustum, then switch back to GL_MODELVIEW matrix in order to be prepared for your drawings . I suggest you, in order to define the frustum, to call gluPerspective(45, (float) width/height, 0.001f, 1000f). 45 for 45 degrees of half angle, width/height for giving the screen ratio, 0.001f for giving a near truncating plane 0.001f away from your eye (or the screen), 1000f for giving a far truncating plane 1000f away from your eye.

*) Have you thought of using, in your drawing function, a call to gluLookAt, as suggest you Ray Toal ? I suggest you to call gluLookAt(

0,0,1, // Camera is set at (0,0,1), so that it will see the 0,0,0 point

0,0,0, // camera is aiming at (0,0,0)

0,1,0) // camera has (0,1,0) for its up vector (so, Oy is the up vector ... logic)

Hope this can help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文