-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.py
302 lines (241 loc) · 9.48 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#! /usr/bin/env python
from PyQt5.QtGui import (QMatrix4x4, QVector3D, QVector4D)
import math
class Camera(object):
"""docstring for Camera"""
def __init__(self, position=QVector3D(0, 0, 1),
lookAt=QVector3D(0, 0, 0),
up=QVector3D(0, 1, 0),
fov = 90):
super(Camera, self).__init__()
self._pos = position
self._lookAt = lookAt
self._dir = (lookAt - self._pos).normalized()
# self._dir = direction.normalized()
self._up = up
self._fov = fov
self._right = QVector3D.crossProduct(self.direction, self.position)
self._center = self.position + self.direction
self._projectionMatrix = QMatrix4x4()
self._modelViewMatrix = QMatrix4x4()
self._normalMatrix = QMatrix4x4()
def setOrthographic(self, left=-1.0, right=1.0, bottom=-1.0, top=1.0, near=0.1, far=20.0):
self.projectionMatrix.setToIdentity()
self.projectionMatrix.ortho( left, right, bottom, top, near, far)
def setPerspective(self, fov=90, ratio=100, near=1.0, far=2.0):
self.fov = fov
self._projectionMatrix.setToIdentity()
self._projectionMatrix.perspective(fov, ratio, near, far)
def setFrustum(self, left=-1, right=1, bottom=-1, top=1, near=1, far=2.0):
self._projectionMatrix.setToIdentity()
self._projectionMatrix.frustum(left, right, bottom, top, near, far)
def setProjectionMatrixToIdentity(self):
self._projectionMatrix.setToIdentity()
def setModelViewMatrixToIdentityt(self):
self._modelViewMatrix.setToIdentity()
def lookAtCenter(self):
self.lookAtTarget(QVector3D(0, 0, 0))
def lookAtTarget(self, target):
self._modelViewMatrix.setToIdentity()
self._modelViewMatrix.lookAt(self._pos, target, self._up)
def moveUp(self):
self._modelViewMatrix.setToIdentity()
self.position += QVector3D(0.0, 0.1, 0.0)
self.direction += QVector3D(0.0, 0.1, 0.0)
def moveDown(self):
self._modelViewMatrix.setToIdentity()
self.position -= QVector3D(0.0, 0.1, 0.0)
self.direction -= QVector3D(0.0, 0.1, 0.0)
def moveRight(self):
self._modelViewMatrix.setToIdentity()
self.position += QVector3D(0.1, 0.0, 0.0)
self.direction += QVector3D(0.1, 0.0, 0.0)
def moveLeft(self):
self._modelViewMatrix.setToIdentity()
self.position -= QVector3D(0.1, 0.0, 0.0)
self.direction -= QVector3D(0.1, 0.0, 0.0)
def moveForward(self):
self._modelViewMatrix.setToIdentity()
self.position -= QVector3D(0.0, 0.0, 0.1)
self.direction -= QVector3D(0.0, 0.0, 0.1)
def moveBackward(self):
self._modelViewMatrix.setToIdentity()
self.position += QVector3D(0.0, 0.0, 0.1)
self.direction += QVector3D(0.0, 0.0, 0.1)
def rotate(self, xangle, yangle, zangle):
self._modelViewMatrix.rotate(xangle, 1, 0, 0)
self._modelViewMatrix.rotate(yangle, 0, 1, 0)
self._modelViewMatrix.rotate(zangle, 0, 0, 1)
def rotate(self, mat):
self.position = mat * self.position
def zoom(self, increment):
self.fov -= increment
def translate(self, x, y, z):
T = QMatrix4x4()
T.translate(QVector3D(x, y, z))
self.position = T * self.position
del T
def mouseRay(self, xin, yin, width, height):
near = -1.0
far = 0.0
rayBegin = self._rayDirection(xin, yin, width, height, near)
rayEnd = self._rayDirection(xin, yin, width, height, far)
return rayBegin, rayEnd
def _rayDirection(self, xin, yin, width, height, plane):
x, y, z = self.devicePortCoordinates(xin, yin, width, height)
clipCoord = QVector4D(x, y, plane, 1.0)
eyeCoord = self._eyeSpace(clipCoord)
ray = self._worldCoord(eyeCoord)
return ray
def _eyeSpace(self, clipCoord):
invertedProjectionMatrix = self.projectionMatrix.inverted()[0]
eye = invertedProjectionMatrix * clipCoord
return eye / eye.w()
def _worldCoord(self, eyeCoord):
invertedViewMatrix = self.modelViewMatrix.inverted()[0]
worldCoord = invertedViewMatrix * eyeCoord
return worldCoord / worldCoord.w()
def mouseWorld(self, xin, yin, width, height):
mouse = self._mouseDirection(xin, yin, width, height, -1.0)
return mouse
def _mouseDirection(self, xin, yin, width, height, plane):
x, y, z = self.devicePortCoordinates(xin, yin, width, height)
clipCoord = QVector4D(x, y, plane, 0.0)
eyeCoord = self._mouseEyeSpace(clipCoord)
ray = self._mouseWorldCoord(eyeCoord)
return ray
def _mouseEyeSpace(self, clipCoord):
invertedProjectionMatrix = self.projectionMatrix.inverted()[0]
eye = invertedProjectionMatrix * clipCoord
return QVector4D(eye.x(), eye.y(), -1.0, 0.0)
def _mouseWorldCoord(self, eyeCoord):
invertedViewMatrix = self.modelViewMatrix.inverted()[0]
worldCoord = invertedViewMatrix * eyeCoord
return QVector3D(worldCoord.x(), worldCoord.y(), worldCoord.z())
@property
def position(self):
return self._pos
@position.setter
def position(self, position):
self._pos = position
@property
def direction(self):
self._dir = self._lookAt - self._pos
return self._dir.normalized()
@direction.setter
def direction(self, direction):
self._dir = direction
@property
def fov(self):
return self._fov
@fov.setter
def fov(self, fov):
if 180.0 > fov > 5.0:
self._fov = fov
@property
def up(self):
self._up = QVector3D.crossProduct(self.direction, self._right)
return self._up.normalized()
@up.setter
def up(self, up):
self._up = up
@property
def center(self):
self._center = self.position + self.direction
return self._center
@property
def right(self):
self._right = QVector3D.crossProduct(self._up, self.direction)
return self._right
@property
def projectionMatrix(self):
return self._projectionMatrix
@projectionMatrix.setter
def projectionMatrix(self, projectionMatrix):
self._projectionMatrix.setToIdentity()
self._projectionMatrix = projectionMatrix
@property
def modelViewMatrix(self):
return self._modelViewMatrix
@modelViewMatrix.setter
def modelViewMatrix(self, modelViewMatrix):
self._modelViewMatrix.setToIdentity()
self._modelViewMatrix = modelViewMatrix
@property
def normalMatrix(self):
self._normalMatrix = self.modelViewMatrix.inverted()[0].transposed()
return self._normalMatrix
@normalMatrix.setter
def normalMatix(self, normalMat):
self._normalMatrix = normalMat
def __str__(self):
return 'position:{}\ndirection: {}\nup:{}\n'.format(self._pos, self._dir, self._up)
@staticmethod
def devicePortCoordinates(x, y, width, height):
# bring mouse to device coordinates to opengl coordinates ranges [-1:1,xy]
x -= width / 2
y -= height / 2
y /= (height / 2)
x /= (width / 2)
return x, y, -1.0
class Ray:
"""generate a ray = e + td"""
def __init__(self, origin, direction):
self.e = origin
self.d = direction
def __str__(self):
return "e = {}\nd = {}".format(self.e, self.d)
# @staticmethod
def devicePortCoordinatesRayTracing(self, x, y, width, height):
# xratio = 1.0
# yratio = 1.0
# if width > height:
# xratio = width / height
# yratio = 1.0
# elif width < height:
# xratio = 1.0
# yratio = width / height
pixelScreenX = (x + 0.5) / width
pixelScreenY = (y + 0.5) / height
mx = (2 * pixelScreenX - 1) * math.tan(self._fov / 2 * math.pi / 180) # 0.5 so that it passes through the center
my = (1 - 2 * pixelScreenY) * math.tan(self._fov / 2 * math.pi / 180)
return mx, my
def _makeCoordianteSystem(self):
pass
def rayCast(self, i, j, width, height):
# Eye Coordinate System
origin = self.position
n = (origin - QVector3D(0, 0, 0)).normalized()
u = QVector3D.crossProduct(self._up, n).normalized()
v = QVector3D.crossProduct(n, u).normalized()
# Image Plane setup
planeCenter = origin + n
d = (origin - planeCenter).length()
aspectRatio = width / height
H = math.tan(aspectRatio * self.fov) * 2 * d
W = H * aspectRatio
C = origin - n * d
L = C - u * W / 2 - v * H/2
pixelWidth = W / width
pixelHeight = H / height
s = L + u * i * pixelWidth + v * j * pixelHeight
print(s)
return Camera.Ray(origin, (s - origin).normalized())
def rayCastBook(self):
pass
def rayCastStack(self, i, j, width, height):
direction = self.direction
right = QVector3D.crossProduct(direction, self._up).normalized()
up = QVector3D.crossProduct(direction, right) * -1.0
center = self.position + direction
print(up)
# normalizedI = (i/width)- 0.5
# normalizedJ = (j /height) - 0.5
s = center + i * right + j * up
# print((self.position - center).length())
return Camera.Ray(self.position, (s - self.position))
if __name__ == '__main__':
camera = Camera()
print(camera)
camera.rotate(60, 60, 60)
print(camera)