-
Notifications
You must be signed in to change notification settings - Fork 5
/
Camera.py
157 lines (115 loc) · 3.33 KB
/
Camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from math import *
from ZMath import *
class Camera:
def __init__(self):
self.maxPitchRate = 5
self.maxYawRate = 5
self.maxRollRate = 5
self.pitchAngle = 0
self.yawAngle = 0
self.rollAngle = 0
self.maxVelocity = 5
self.forward = 0
self.strafe = 0
self.up = 0
self.pitch = Quaternion()
self.yaw = Quaternion()
self.roll = Quaternion()
self.position = Vector3()
self.forwardVector = Vector3()
self.strafeVector = Vector3()
self.upVector = Vector3()
def setPosition(self,pos):
self.position = Vector3(pos)
self.position.z = -self.position.z
def setPerspective(self):
self.pitch = Quaternion(radians(self.pitchAngle),Vector3(1,0,0))
self.yaw = Quaternion(radians(self.yawAngle),Vector3(0,1,0))
self.roll = Quaternion(radians(self.rollAngle),Vector3(0,0,1))
quat = self.pitch*self.yaw*self.roll
#glRotated(degrees(quat.x), 1.0, 0.0, 0.0);
#glRotated(degrees(quat.y), 0.0, 1.0, 0.0);
#glRotated(degrees(quat.z), 0.0, 0.0, 1.0);
self.viewMatrix = quat.toMatrix()
glMultMatrixf(self.viewMatrix)
quat = self.yaw*self.pitch*self.roll
mat = quat.toMatrix()
self.strafeVector.x = mat[0][0]
self.strafeVector.y = mat[0][1]
self.strafeVector.z = mat[0][2]
self.forwardVector.x = mat[2][0]
self.forwardVector.y = mat[2][1]
self.forwardVector.z = mat[2][2]
#self.upVector.x = mat[1][0]
#self.upVector.y = mat[1][1]
#self.upVector.z = mat[1][2]
self.upVector.y = 1
self.strafeVector *= self.strafe
self.forwardVector *= self.forward
self.upVector *= self.up
self.position += self.forwardVector + self.strafeVector + self.upVector
#self.position.z = -self.position.z
glTranslatef(-self.position.x,-self.position.y,self.position.z)
def Roll(self,angle):
if abs(angle) < abs(self.maxRollRate):
self.rollAngle += angle
else:
if angle < 0:
self.rollAngle -= self.maxRollRate
else:
self.rollAngle += self.maxRollRate
self.rollAngle%=360
def Yaw(self,angle):
if abs(angle) < abs(self.maxYawRate):
self.yawAngle += angle
else:
if angle < 0:
self.yawAngle -= self.maxYawRate
else:
self.yawAngle += self.maxYawRate
self.yawAngle%=360
def Pitch(self,angle):
if abs(angle) < abs(self.maxPitchRate):
self.pitchAngle += angle
else:
if angle < 0:
self.pitchAngle -= self.maxPitchRate
else:
self.pitchAngle += self.maxPitchRate
self.pitchAngle%=360
def MoveZ(self,velocity):
if abs(velocity) < abs(self.maxVelocity):
self.forward = velocity
else:
if velocity<0:
self.forward = -self.maxVelocity
else:
self.forward = self.maxVelocity
def MoveX(self,velocity):
if abs(velocity) < abs(self.maxVelocity):
self.strafe = velocity
else:
if velocity<0:
self.strafe = -self.maxVelocity
else:
self.strafe = self.maxVelocity
def MoveY(self,velocity):
if abs(velocity) < abs(self.maxVelocity):
self.up = velocity
else:
if velocity<0:
self.up = -self.maxVelocity
else:
self.up = self.maxVelocity
def getMouseRay(self,x,y):
model = glGetDoublev(GL_MODELVIEW_MATRIX)
proj = glGetDoublev(GL_PROJECTION_MATRIX)
vp = glGetIntegerv(GL_VIEWPORT)
near = gluUnProject(x,vp[3]-y,0,model,proj,vp)
far = gluUnProject(x,vp[3]-y,1,model,proj,vp)
return Vector3(near),Vector3(far)
def reset(self):
self.__init__()