2021-07-31

OprnGl in Python, moving camera in 3d [duplicate]

Am working on a simple 3d game to improve my python skills, however ran into a problem in moving the camera in 3d.

glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0) glRotatef(mouseMove[1]*0.1, 1.0, 0.0, 0.0)

when the above two operations are combined they create a warping effect. I think they need to be separated by poping/push the matrix to separate their effects on the player viewpoint. I've tried everything but have been unable to correct this. Full code below, if anyone can correct the obvious mistake I've made would be much appreciated. full code below.

while run:
    pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE or event.key == pygame.K_RETURN:
                run = False
            if event.key == pygame.K_PAUSE or event.key == pygame.K_p:
                paused = not paused
                pygame.mouse.set_pos(displayCenter) 
        if not paused: 
            if event.type == pygame.MOUSEMOTION:
                mouseMove = [event.pos[i] - displayCenter[i] for i in range(2)]
            pygame.mouse.set_pos(displayCenter)    

    if not paused:
        # get keys
        keypress = pygame.key.get_pressed()
        # mouseMove = pygame.mouse.get_rel()

        glLoadIdentity()

        up_down_angle += mouseMove[1]*0.1
        glRotatef(up_down_angle, 1.0, 0.0, 0.0)

        # init the view matrix
        glPushMatrix()
        glLoadIdentity()   

        # apply the movment 
        if keypress[pygame.K_w]:
            glTranslatef(0,0, 5)

        if keypress[pygame.K_s]:
            glTranslatef(0,0, -5)       

        if keypress[pygame.K_d]:
            glTranslatef(-5,0,0)

        if keypress[pygame.K_a]:
            glTranslatef(5,0,0)

        if keypress[pygame.K_c]:
            glTranslatef(0,5,0)

        if keypress[pygame.K_x]:
            glTranslatef(0,-5,0)
  

        # apply the left and right rotation
        glRotatef(mouseMove[0]*0.1, 0.0, 1.0, 0.0)
        glRotatef(mouseMove[1]*0.1, 1.0, 0.0, 0.0)

        # multiply the current matrix by the get the new view matrix and store the final
        glMultMatrixf(viewMatrix)
        viewMatrix = glGetFloatv(GL_MODELVIEW_MATRIX)

        # apply view matrix
        glPopMatrix()
        glMultMatrixf(viewMatrix)
 
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        draw_cross_hairs(screen)
        pygame.display.flip()
        pygame.time.wait(10)

pygame.quit()



from Recent Questions - Stack Overflow https://ift.tt/3zUnUSO
https://ift.tt/eA8V8J

No comments:

Post a Comment