| 92 | return viewMatrix |
| 93 | |
| 94 | class CameraControls3D: |
| 95 | def __init__(self): |
| 96 | self.zoomSpeed = 1.0 |
| 97 | self.translateSpeed = 1.0 |
| 98 | self.rotateSpeed = 1.0 |
| 99 | self.reset() |
| 100 | |
| 101 | def __rotationMatrixX(self, a): |
| 102 | return np.array([ |
| 103 | [1, 0, 0], |
| 104 | [0, np.cos(a), -np.sin(a)], |
| 105 | [0, np.sin(a), np.cos(a)] |
| 106 | ]) |
| 107 | |
| 108 | def __rotationMatrixZ(self, a): |
| 109 | return np.array([ |
| 110 | [np.cos(a), -np.sin(a), 0], |
| 111 | [np.sin(a), np.cos(a), 0], |
| 112 | [0, 0, 1] |
| 113 | ]) |
| 114 | |
| 115 | def reset(self): |
| 116 | self.lastMousePos = None |
| 117 | self.draggingLeft = False |
| 118 | self.draggingRight = False |
| 119 | self.camera_pos = np.array([0.0, 0.0, 0.0]) |
| 120 | self.yaw = 0 |
| 121 | self.pitch = 0 |
| 122 | self.zoom = 1.0 |
| 123 | |
| 124 | def update(self, event): |
| 125 | if event.type == pygame.MOUSEBUTTONDOWN: |
| 126 | if event.button == 1: # Left mouse button |
| 127 | self.draggingLeft = True |
| 128 | elif event.button == 3: # Right mouse button |
| 129 | self.draggingRight = True |
| 130 | elif event.button == 4: # Scroll up |
| 131 | self.camera_pos[2] -= 0.25 * self.zoomSpeed |
| 132 | elif event.button == 5: # Scroll down |
| 133 | self.camera_pos[2] += 0.25 * self.zoomSpeed |
| 134 | self.lastMousePos = pygame.mouse.get_pos() |
| 135 | elif event.type == pygame.MOUSEBUTTONUP: |
| 136 | if event.button == 1: |
| 137 | self.draggingLeft = False |
| 138 | elif event.button == 3: |
| 139 | self.draggingRight = False |
| 140 | elif event.type == pygame.MOUSEMOTION: |
| 141 | if self.draggingRight: |
| 142 | # Drag to move |
| 143 | mouse_pos = pygame.mouse.get_pos() |
| 144 | dx = mouse_pos[0] - self.lastMousePos[0] |
| 145 | dy = mouse_pos[1] - self.lastMousePos[1] |
| 146 | self.camera_pos[0] += 0.01 * dx * self.translateSpeed |
| 147 | self.camera_pos[1] += 0.01 * dy * self.translateSpeed |
| 148 | elif self.draggingLeft: |
| 149 | # Drag to rotate (yaw and pitch) |
| 150 | mouse_pos = pygame.mouse.get_pos() |
| 151 | dx = mouse_pos[0] - self.lastMousePos[0] |