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
glRotateandglTranslateconsider looking intogluLookAt, which allows you to set things up in a camera-centered way, so to speak.By the way,
gluLookAtis 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.