-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.py
More file actions
398 lines (329 loc) · 11.8 KB
/
Copy pathGraphics.py
File metadata and controls
398 lines (329 loc) · 11.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
392
393
394
395
396
397
398
from PIL import Image
import urllib.request
import cv2
import numpy as np
import math
from pynput.keyboard import Key, Listener
from Matrix import Matrix
from Object import Object
from Vector3 import Vector3
from Vector2 import Vector2
from Triangle import Triangle
windowName = "Graphics"
window = cv2.namedWindow(windowName, 0)
windowWidth = 800
windowHeight = 800
centerX = int(windowWidth / 2)
centerY = int(windowHeight / 2)
pixelsPerUnit = 10
cameraPosition = Vector3()
cameraPosition.x = 0 * pixelsPerUnit
cameraPosition.y = 0 * pixelsPerUnit
cameraPosition.z = 0 * pixelsPerUnit
cameraRotation = Vector3()
cameraRotation.x = 20
cameraRotation.y = 0
cameraRotation.z = 20
cameraLookDirection = Vector3()
cameraLookDirection.y = 1
# camera default look direction is Vector3(0,1,0)
# z-axis is up, x-axis is right, y-axis is into the screen
def rotateAndScaleLocalCoord(coord: Vector3, rotation: Vector3, scale: Vector3):
localRotationMatrix = Matrix.getRotationMatrix(rotation)
scaleMatrix = Matrix.getScaleMatrix(scale)
scaledCoord = scaleMatrix.multiplyVector(coord)
locallyRotatedCoord = localRotationMatrix.multiplyVector(scaledCoord)
return locallyRotatedCoord
###
def rotateCoordinate(coord: Vector3, rotations: Vector3):
rotationMatrix = Matrix.getRotationMatrix(rotations)
return rotationMatrix.multiplyVector(coord)
###
def getScreenCoord(coordinate: Vector3) -> Vector3:
screenCoord = Vector3()
screenCoord.x = centerX + cameraPosition.x + (coordinate.x * pixelsPerUnit)
screenCoord.y = centerX + cameraPosition.z + (coordinate.z * pixelsPerUnit)
screenCoord.z = centerY - cameraPosition.y - (coordinate.y * pixelsPerUnit)
return screenCoord
###
def getScreenShapeTriangles(localShape: Object) -> list:
screenShapeTriangles = []
xPosition = localShape.m_position.x
yPosition = localShape.m_position.y
zPosition = localShape.m_position.z
updatedCameraLookDir = Matrix.getRotationMatrix(cameraRotation).multiplyVector(cameraLookDirection)
for i in range(int(len(localShape.m_localCoords) / 3)):
screenTriangle = Triangle()
screenCoords = [] # Vector3 list
worldCoords = [] # Vector3 list
for j in range(3):
worldRotationMatrix = Matrix.getRotationMatrix(cameraRotation)
coordVector = Vector3()
coordVector.x = localShape.m_localCoords[(3 * i) + j][0]
coordVector.y = localShape.m_localCoords[(3 * i) + j][1]
coordVector.z = localShape.m_localCoords[(3 * i) + j][2]
locallyRotatedScaledCoord = rotateAndScaleLocalCoord(coordVector, localShape.m_rotation, localShape.m_scale)
locallyPositionedCoord = Vector3()
locallyPositionedCoord.x = locallyRotatedScaledCoord.x + xPosition
locallyPositionedCoord.y = locallyRotatedScaledCoord.y + yPosition
locallyPositionedCoord.z = locallyRotatedScaledCoord.z + zPosition
worldRotatedScaledPositionedCoord = worldRotationMatrix.multiplyVector(locallyPositionedCoord)
worldCoords.append(worldRotatedScaledPositionedCoord)
screenCoords.append(getScreenCoord(worldRotatedScaledPositionedCoord))
#
screenTriangle.A = screenCoords[0]
screenTriangle.B = screenCoords[1]
screenTriangle.C = screenCoords[2]
AB = Vector3()
BC = Vector3()
AB = worldCoords[1].subtract(worldCoords[0])
BC = worldCoords[2].subtract(worldCoords[1])
screenTriangle.normal = AB.cross(BC).normalized()
if (screenTriangle.normal.dot(cameraLookDirection) > 0):
screenShapeTriangles.append(screenTriangle)
#
#
return screenShapeTriangles
###
def getPointsBetween(pointA: list, pointB: list) -> list:
firstToSecondInPixels = [ int(pointB[0] - pointA[0]), int(pointB[1] - (pointA[1])) ]
largerMovementX = False
largerMovementAmount = int(0)
ratio = 1
xDirection = 1
yDirection = 1
if (firstToSecondInPixels[0] < 0):
xDirection = -1
#
if (firstToSecondInPixels[1] < 0):
yDirection = -1
#
if abs(firstToSecondInPixels[0]) > abs(firstToSecondInPixels[1]):
largerMovementAmount = abs(firstToSecondInPixels[0])
if firstToSecondInPixels[0] != 0:
if (firstToSecondInPixels[1] != 0):
ratio = abs(firstToSecondInPixels[1] / firstToSecondInPixels[0])
else:
ratio = 0
#
else:
ratio = 0
#
largerMovementX = True
else:
largerMovementAmount = abs(firstToSecondInPixels[1])
if firstToSecondInPixels[1] != 0:
if (firstToSecondInPixels[0] != 0):
ratio = abs(firstToSecondInPixels[0] / firstToSecondInPixels[1])
else:
ratio = 0
#
else:
ratio = 0
#
#
smallerMovementTracker = ratio # makes sure we don't lose pixels because you can't divide real pixels
largerMovementTracker = 1
inBetweenPixels = []
for i in range(largerMovementAmount):
startingXPos = pointA[0]
startingYPos = pointA[1]
if (largerMovementX):
inBetweenPixels.append([ startingXPos + (largerMovementTracker * xDirection), (startingYPos + (smallerMovementTracker * yDirection))])
else:
inBetweenPixels.append([ startingXPos + (smallerMovementTracker * xDirection), (startingYPos + (largerMovementTracker * yDirection))])
#
smallerMovementTracker += ratio
largerMovementTracker += 1
#
return inBetweenPixels
###
def getAllEdgePointsTriangles(polygon: Object) -> list:
triangleEdges = []
for i in range(int(len(polygon) / 3)):
triangleEdges += getPointsBetween(polygon[3 * i], polygon[(3 * i) + 1])
triangleEdges += getPointsBetween(polygon[(3 * i) + 1], polygon[(3 * i) + 2])
triangleEdges += getPointsBetween(polygon[(3 * i) + 2], polygon[3 * i])
#
return triangleEdges
###
def getAllEdgePoints(segment: Object) -> list:
allPointsAlongEdges = []
for i in range(len(segment)):
allPointsAlongEdges += getPointsBetween(segment[i], segment[(i + 1) % (len(segment))])
#
return allPointsAlongEdges
###
def drawPolygon(polygon: Object, image):
trianglesOnScreen = getScreenShapeTriangles(polygon)
A = Vector2()
B = Vector2()
C = Vector2()
for i in range(int(len(trianglesOnScreen))):
A.x = trianglesOnScreen[i].A.x
A.y = trianglesOnScreen[i].A.y
B.x = trianglesOnScreen[i].B.x
B.y = trianglesOnScreen[i].B.y
C.x = trianglesOnScreen[i].C.x
C.y = trianglesOnScreen[i].C.y
drawFacePoints(A, B, C, image)
#
###
def drawSegments(segments: Object, image):
shapePointsOnScreen = getScreenShapeCoords(segments)
allPoints = shapePointsOnScreen + getAllEdgePoints(shapePointsOnScreen)
for coordinate in allPoints:
xCoord = coordinate[0]
yCoord = coordinate[1]
if (xCoord >= 0 and yCoord >= 0 and xCoord < windowWidth and yCoord < windowHeight):
image[xCoord, yCoord] = segments.m_color
#
#
###
def getMax(A: int, B: int) -> int:
if (A > B):
return A
else:
return B
###
def getMin(A: int, B: int) -> int:
if (A < B):
return A
else:
return B
###
def triangleArea(sideA: float, sideB: float, sideC: float) -> float:
semiperimeter = (sideA + sideB + sideC) / 2
return math.sqrt(int(semiperimeter * (semiperimeter - sideA) * (semiperimeter - sideB) * (semiperimeter - sideC)))
###
def pointInTriangle(P: Vector2, A: Vector2, B: Vector2, C: Vector2) -> bool:
AB = B.subtract(A).magnitude()
AC = C.subtract(A).magnitude()
BC = C.subtract(B).magnitude()
AP = P.subtract(A).magnitude()
BP = P.subtract(B).magnitude()
CP = P.subtract(C).magnitude()
totalArea = triangleArea(AB, AC, BC)
area1 = triangleArea(AB, BP, AP)
area2 = triangleArea(BC, BP, CP)
area3 = triangleArea(AC, AP, CP)
return (int(area1 + area2 + area3) >= int(totalArea) - 2 and int(area1 + area2 + area3) <= int(totalArea) + 2)
###
def drawFacePoints(A: Vector2, B: Vector2, C: Vector2, image) -> list:
facePoints = []
minX = getMin(getMin(A.x, B.x), C.x)
maxX = getMax(getMax(A.x, B.x), C.x)
minY = getMin(getMin(A.y, B.y), C.y)
maxY = getMax(getMax(A.y, B.y), C.y)
for w in range(int(maxX - minX)):
for h in range(int(maxY - minY)):
point = Vector2()
point.x = minX + w
point.y = minY + h
if (pointInTriangle(point, A, B, C)):
if (point.x >= 0 and point.y >= 0 and point.x < windowWidth and point.y < windowHeight):
image[point.x, point.y] = image2Pixels[point.x % 96,point.y % 96]
#
#
#
#
return facePoints
###
# Create shapes
box1 = Object()
box1.setBox()
box1.m_position = [ 10, 0, 0 ]
box2 = Object()
box2.setBox()
box2.m_position = [ 0, -10, 0 ]
box3 = Object()
box3.setBox()
box3.m_position = [ 0, 10, 0 ]
box4 = Object()
box4.setBox()
box4.m_position = [ -10, 0, 0 ]
polygon1 = Object()
polygon1.setDiamond()
polygon1.m_scale = [ 2, 2, 5 ]
polygon2 = Object()
polygon2.setDiamond()
polygon2.m_scale = [ 0.5, 0.5, 1.25 ]
polygon2.m_position = [ 5, 0, 0 ]
polygon2.m_rotation = [ 90, 0, 0 ]
imageBox = Object()
imageBox.setBox()
imageBox.m_scale.x = 10
imageBox.m_scale.y = 10
imageBox.m_scale.z = 10
imageBox.removeDuplicateEdges()
imageBox.m_color = (50,50,5)
xAxis = Object()
xAxis.m_rotation = cameraRotation
xAxis.m_localCoords = [ [ -10, 0, 0 ], [ 10, 0, 0 ] ]
xAxis.m_color = (50, 100, 100)
xAxis.m_b_isAxis = True
yAxis = Object()
yAxis.m_rotation = cameraRotation
yAxis.m_localCoords = [ [ 0, -20, 0 ], [ 0, 20, 0 ] ]
yAxis.m_color = (200, 200, 10)
yAxis.m_b_isAxis = True
zAxis = Object()
zAxis.m_rotation = cameraRotation
zAxis.m_localCoords = [ [ 0, 0, -100 ], [0, 0, 100 ] ]
zAxis.m_color = (100, 50, 100)
zAxis.m_b_isAxis = True
b_loopExited = False
image2 = Image.open("BrickWall.png")
image2Pixels = image2.load()
while b_loopExited == False:
image = Image.new( mode = "RGB", size = (windowWidth, windowHeight), color = (0, 0, 0, 100) )
pixels = image.load()
imageBox.m_textureData = image2Pixels
drawPolygon(imageBox, pixels)
# draw shapes
# drawPolygon(box1, pixels)
# drawPolygon(box2, pixels)
# drawPolygon(box3, pixels)
# drawPolygon(box4, pixels)
# drawPolygon(polygon1, pixels)
# drawPolygon(polygon2, pixels)
# draw axis
# drawSegments(zAxis, pixels)
# drawSegments(xAxis, pixels)
# drawSegments(yAxis, pixels)
# move objects
# cameraRotation.x += 1
cameraRotation.z += 10
# box1.addRotation([1,1,0])
# box2.addRotation([0,1,0])
# box3.addRotation([0,0,1])
# box4.addRotation([1,0,0])
# box3.m_rotation.x += 1
# box4.m_rotation.x += 1
# polygon1.addRotation([0,0,1])
# polygon1.m_position.z = 10 * math.sin(polygon1.m_rotation.z * 0.05)
# polygon2.addRotation([0,-30,0])
# polygon2.m_position.x = 10 * math.sin(polygon2.m_rotation.y * 0.001)
# polygon2.m_position.y = 10 * math.cos(polygon2.m_rotation.y * 0.001)
open_cv_image = np.array(image)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
cv2.imshow(windowName, open_cv_image)
# cv2.waitKey(50)
key = cv2.waitKey(50)
# if key == ord('w'):
# cameraRotation.x += 1
# #
# if key == ord('s'):
# cameraRotation.x -= 1
# #
# if key == ord('a'):
# cameraRotation.z += 1
# #
# if key == ord('d'):
# cameraRotation.z -= 1
# #
if key == ord('q'):
b_loopExited = True
#
##