-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeta3d.py
More file actions
391 lines (296 loc) · 10.8 KB
/
theta3d.py
File metadata and controls
391 lines (296 loc) · 10.8 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import pygame
import math
import sys
#the Vec3 class
class Vec3:
def __init__(self,x=0,y=0,z=0):
self.x=x
self.y=y
self.z=z
#add and sub methods:
def __add__(self,other):
return Vec3(self.x+other.x,self.y+other.y,self.z+other.z)
def __sub__(self, other):
return Vec3(self.x-other.x, self.y-other.y, self.z-other.z)
#scalar mul and div methods:
def __mul__(self,scalar):
return Vec3(self.x*scalar,self.y*scalar,self.z*scalar)
def __truediv__(self,scalar):
if scalar==0:
scalar=1
return Vec3(self.x/scalar,self.y/scalar,self.z/scalar)
#length methods:
def length(self):
return math.sqrt(self.x**2 + self.y**2 + self.z**2)
def lengthSqrd(self):
return self.x**2 + self.y**2 + self.z**2
#normalize method:
def normalize(self):
l=self.length()
if l==0:
return Vec3(0,0,0)
return self/l
#dot production:
def dot(self,other):
return self.x * other.x + self.y * other.y + self.z * other.z
#coss production:
def cross(self, other):
return Vec3(
self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x
)
#utility methods:
def toList(self):
return [self.x,self.y,self.z]
def toTuple(self):
return (self.x,self.y,self.z)
def __repr__(self):
return f"Vec3({self.x}, {self.y}, {self.z})"
def __getitem__(self,index):
coords = self.toList()
return coords[index]
#the Mat4 (4x4 matirx) class
class Mat4:
def __init__(self,matrix=None):
self.identityM =[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
self.zeroM =[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
if matrix == None:
self.matrix=self.zeroM
elif (len(matrix) != 4) and (len(matrix[0] != 4)):
self.matrix=matrix[:]
raise ValueError("Mat4 must have 4 list elements (which contain 4 elements)")
else:
self.matrix=matrix
#acsess helpers:
def __getitem__(self,index):
row, col = index
return self.matrix[row][col]
def __setitem__(self,index,value):
row,col = index
self.matrix[row][col]=value
#identity and zero method:
def identity(self):
return Mat4(matrix=self.identityM)
def zero(self):
return Mat4(matrix=self.zeroM)
#operation methods:
def __mul__(self,other):
if isinstance(other, Mat4):
output=Mat4()
for row in range(4):
for col in range(4):
sum=0
for k in range(4):
sum+=self[row,k]*other[k,col]
output[row,col] = sum
return output
elif isinstance(other, Vec4):
x = self[0, 0] * other.x + self[0, 1] * other.y + self[0, 2] * other.z + self[0, 3] * other.w
y = self[1, 0] * other.x + self[1, 1] * other.y + self[1, 2] * other.z + self[1, 3] * other.w
z = self[2, 0] * other.x + self[2, 1] * other.y + self[2, 2] * other.z + self[2, 3] * other.w
w = self[3, 0] * other.x + self[3, 1] * other.y + self[3, 2] * other.z + self[3, 3] * other.w
return Vec4(x,y,z,w)
else:
raise TypeError("Unsupported operand for Mat4 * {}".format(type(other)))
def translate(self,tx,ty,tz):
output = self.identity()
output[0,3]=tx
output[1,3]=ty
output[2,3]=tz
return output
def scale(self,sx,sy,sz):
output=self.identity()
output[0,0]=sx
output[1,1]=sy
output[2,2]=sz
return output
#angle is in radians
def rotateX(self,angle):
output = self.identity()
output[1,1]=math.cos(angle)
output[1,2]=-math.sin(angle)
output[2,1]=math.sin(angle)
output[2,2]=math.cos(angle)
return output
def rotateY(self,angle):
output=self.identity()
output[0,0]=math.cos(angle)
output[0,2]=math.sin(angle)
output[2,0]=-math.sin(angle)
output[2,2]=math.cos(angle)
return output
def rotateZ(self,angle):
output=self.identity()
output[0,0]=math.cos(angle)
output[0,1]=-math.sin(angle)
output[1,0]=math.sin(angle)
output[1,1]=math.cos(angle)
return output
def transpose(self):
output=Mat4()
for i in range(4):
for j in range(4):
output[j,i]=self[i,j]
return output
#provided by chatgpt
def inverseAffine(self):
# Step 1: Extract rotation (upper-left 3x3)
R = [[self[i, j] for j in range(3)] for i in range(3)]
# Step 2: Transpose rotation (Rᵀ = inverse of rotation)
R_T = [[R[j][i] for j in range(3)] for i in range(3)]
# Step 3: Extract translation vector
T = [self[i, 3] for i in range(3)]
# Step 4: Compute inverse translation = -Rᵀ * T
T_inv = [
-sum(R_T[i][j] * T[j] for j in range(3)) for i in range(3)
]
# Step 5: Build the inverse matrix
inv = self.identity()
for i in range(3):
for j in range(3):
inv[i, j] = R_T[i][j]
inv[i, 3] = T_inv[i]
return inv
#veiw matrices:
#eye,target, and up are all Vec3 objects. Also provided by chatgpt, but uses my Vec3 calss
@staticmethod
def lookAt(eye,target,up):
z=(eye-target).normalize()
x=up.cross(z).normalize()
y=z.cross(x)
tx=-x.dot(eye)
ty=-y.dot(eye)
tz=-z.dot(eye)
return Mat4(matrix=[[x.x,x.y,x.z,tx],
[y.x,y.y,y.z,ty],
[z.x,z.y,z.z,tz],
[0,0,0,1]])
#also chatgpt. Example use: proj = Mat4.perspective(fov=math.radians(60), aspect=16/9, near=0.1, far=100.0)
@staticmethod
def perspective(fov,aspect,near,far):
f=1/math.tan(fov/2)
nf=1/(near-far)
return Mat4([[f/aspect,0,0,0],
[0,f,0,0],
[0,0,(far+near)*nf,2*far*near*nf],
[0,0,-1,0]])
#position is a Vec3 object
class Vertex:
def __init__(self,position,color=(255,255,255)):
self.position=position
self.color=color
#v1,v2, and v3 are all vertex objects
class Triangle:
def __init__(self,v1,v2,v3):
self.v1=v1
self.v2=v2
self.v3=v3
#easy acsess:
def getVertexList(self):
return [self.v1,self.v2,self.v3]
def extractVectors(self):
output=[]
for vert in self.getVertexList():
output.append(vert.position)
return output
class Vec4:
def __init__(self,x,y,z,w=1):
self.x=x
self.y=y
self.z=z
self.w=w
#easy acsess:
def toVec3(self):
if self.w!=0:
return Vec3(self.x/self.w, self.y/self.w, self.z/self.w)
else:
return Vec3(self.x,self.y,self.z)
def toList(self):
return [self.x,self.y,self.z,self.w]
def toTuple(self):
return (self.x,self.y,self.z,self.w)
def __getitem__(self,index):
output=0
if (0<index) and (index<4):
output = self.toList()[index]
else:
raise IndexError("index must be from 0 through 3 inclusive")
return output
def __setitem__(self,index,value):
if (0<index) and (index<4):
if index==0:
self.x=value
if index==1:
self.y=value
if index==2:
self.z
if index==3:
self.w
else:
raise IndexError("index must be from 0 through 3 inclusive")
#operations:
def __mul__(self,scalar):
return Vec4(self.x*scalar, self.y*scalar, self.z*scalar, self.w*scalar)
def __add__(self,other):
return Vec4(self.x+other.x, self.y+other.y, self.z+other.z, self.w+other.w)
def __repr__(self):
return f"Vec4({self.x},{self.y},{self.z},{self.w})"
class Renderer:
def __init__(self, screenWidth,screenHeight):
self.width=screenWidth
self.height=screenHeight
#triangle is a triangle object
def projectTriangle(self,triangle,mvpMatrix):
vec3s=triangle.extractVectors()
vec4s=[]
for vec3 in vec3s:
x=vec3.x
y=vec3.y
z=vec3.z
vec4=Vec4(x,y,z,1.0)
vec4s.append(vec4)
clipSpaceVecs=[]
for vec4 in vec4s:
transformed=mvpMatrix*vec4
clipSpaceVecs.append(transformed)
ndcCoords=[]
for v in clipSpaceVecs:
ndc=v.toVec3()
ndcCoords.append(ndc)
screenCoords=[]
for ndc in ndcCoords:
screenX=(ndc.x+1)*.5*self.width
screenY=(1-ndc.y)*.5*self.height #y is fliped
screenCoords.append((screenX,screenY))
return screenCoords
def drawTriangle(self,surface,triangle,mvpMatrix,color=(255,255,255)):
screenCoords=self.projectTriangle(triangle,mvpMatrix)
#print(screenCoords)
intCoords=[]
for (x,y) in screenCoords:
intCoord=(round(x),round(y))
intCoords.append(intCoord)
pygame.draw.polygon(surface,color,intCoords)
def computeTriangleDepth(self, triangle, mvpMatrix):
vec3s = triangle.extractVectors()
avg_z = 0
for v in vec3s:
vec4 = Vec4(v.x, v.y, v.z, 1.0)
transformed = mvpMatrix * vec4
# Convert to NDC
if transformed.w != 0:
ndc_z = transformed.z / transformed.w
else:
ndc_z = transformed.z
avg_z += ndc_z
return avg_z / 3
def renderTriangles(self, surface, triangleList, mvpMatrix):
# Compute depth for each triangle
sortedTriangles = sorted(
triangleList,
key=lambda tri: self.computeTriangleDepth(tri, mvpMatrix),
reverse=True # farthest first
)
for tri in sortedTriangles:
self.drawTriangle(surface, tri, mvpMatrix, color=tri.v1.color) # Or another color logic