简体   繁体   中英

Can't Move Foward with Camera in OpenGL

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM