简体   繁体   中英

OpenGL: Camera theta and zooming

Studying the code of another 3D GL game, I decided to try to create a very simple, walk around kind of thing.

Everything SEEMS to work OK, but only on one axis. When walking forward and backward one direction, all is well. Though, if you turn 90 degrees, and start to walk forward / backward, backward will function as forward and forward as backward.

Here is the ground drawing code:

def drawground(self):
        glPushMatrix()

        glTranslate(-1, -1, -3)
        glRotate(90, True, False, False)

        glBegin(GL_QUADS)
        glColor(0.6, 0.6, 0.6)
        glVertex(0, 0)
        glVertex(0, self.winmain.world.xlen)
        glVertex(self.winmain.world.ylen, self.winmain.world.xlen)
        glVertex(self.winmain.world.ylen, 0)
        glEnd()

        glPopMatrix() 

Moving code:

if pygame.key.get_pressed()[K_w]:
                self.campos[0] -= 0.005*math.sin(self.camtheta * 3.14159 / 180)
                self.campos[1] -= 0.005*math.cos(self.camtheta * 3.14159 / 180)

if pygame.key.get_pressed()[K_s]:
                self.campos[0] += 0.005*math.sin(self.camtheta * 3.14159 / 180)
                self.campos[1] += 0.005*math.cos(self.camtheta * 3.14159 / 180)

And, camera theta rotation:

if event.type == MOUSEMOTION:
                    self.camtheta -= 2 * event.rel[0] #Subtracting 2 * the relative moved pixels from the last position.

What could cause the problem of this?

You haven't put much of details, but in any case, I would suggest you look into an amazing glu API called gluLookAt(..) . It takes 9 parameters, 3 for location, 3 for direction, and 3 for orientation.

Import GLU:

from OpenGL.GLU import *

and say:

gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ)

For a walkaround kind of thing, upX,upY, upZ will be 0,1,0.

Documentation here:
http://pyopengl.sourceforge.net/documentation/manual-3.0/gluLookAt.html

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