-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuni maths rotation example with import graphics.py
159 lines (102 loc) · 4.56 KB
/
uni maths rotation example with import graphics.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
import pygame
import math
import time
WIDTH = 1280
HEIGHT = 720
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
gameDisplay.fill(black)
orthogonalProjection = [[1, 0, 0], [0, 1, 0]]
teapotVertices = [];
faceOrder = [];
with open('teapot.obj', 'r') as teapot:
for line in teapot:
strippedLine = line.strip()
components = strippedLine.split()
if(len(components) > 3):
if(components[0] == "v"):
teapotVertices.append((float(components[1]), float(components[2]), float(components[3])))
else:
faceOrder.append((int(components[1]), int(components[2]), int(components[3])))
del components
del strippedLine
del line
# Define center at (0,0)
def rotateVertexByX(vertex, theta):
return (vertex[0], (math.cos(theta) * vertex[1]) - (math.sin(theta) * vertex[2]), (math.cos(theta) * vertex[2]) + (math.sin(theta) * vertex[1]))
def rotateVertexByY(vertex, theta):
return ((math.cos(theta) * vertex[0]) + (math.sin(theta) * vertex[2]), vertex[1], (math.cos(theta) * vertex[2]) - (math.sin(theta) * vertex[0]))
def rotateVertexByZ(vertex, theta):
return ((math.cos(theta) * vertex[0]) - (math.sin(theta) * vertex[1]), (math.cos(theta) * vertex[1]) + (math.sin(theta) * vertex[0]), vertex[2])
def scaleVertex(vertex, scaleFactor):
return (vertex[0] * scaleFactor, vertex[1] * scaleFactor, vertex[2] * scaleFactor)
def scaleVertexByX(vertex, scaleFactor):
return (vertex[0] * scaleFactor, vertex[1], vertex[2])
def scaleVertexByY(vertex, scaleFactor):
return (vertex[0], vertex[1] * scaleFactor, vertex[2])
def scaleVertexByZ(vertex, scaleFactor):
return (vertex[0], vertex[1], vertex[2] * scaleFactor)
def translate(vertex, vector):
return (vertex[0] + vector[0], vertex[1] + vector[1], vertex[2] + vector[2])
def project(vertex):
return (int(vertex[0] * 50 + (WIDTH / 2)), int(vertex[1] * 50 + (HEIGHT / 2)))
def render(vertices, connectionOrder, scaleFactor):
projectedRepresentation = []
for vertex in vertices:
vertex = scaleVertex(vertex, abs(scaleFactor))
projectedRepresentation.append(project(vertex));
del vertex
gameDisplay.fill(black)
for i in range(len(connectionOrder)):
for j in range(len(connectionOrder[i])):
pygame.draw.line(gameDisplay, green, projectedRepresentation[i], projectedRepresentation[connectionOrder[i][j] - 1])
del projectedRepresentation
pygame.display.update()
def render2(vertices, faces):
projectedRepresentation = []
for vertex in vertices:
projectedRepresentation.append(project(vertex));
gameDisplay.fill(black)
for i in range(len(faces)):
if len(projectedRepresentation) >= i + 1:
pygame.draw.line(gameDisplay, green, projectedRepresentation[faces[i][0]], projectedRepresentation[faces[i][1]])
pygame.draw.line(gameDisplay, green, projectedRepresentation[faces[i][1]], projectedRepresentation[faces[i][2]])
pygame.draw.line(gameDisplay, green, projectedRepresentation[faces[i][0]], projectedRepresentation[faces[i][2]])
del projectedRepresentation
pygame.display.update()
vertices = [(-1, 1, 1), (1, 1, 1), (1, -1, 1), (-1, -1, 1), (-1, 1, -1), (1, 1, -1), (1, -1, -1), (-1, -1, -1), (0, 0, 2), (0, 0, -2)];
connectionOrder = [[2, 4, 5, 9], [3, 6, 9], [4, 7, 9], [8, 9], [6, 8, 10], [7, 10], [8, 10], [10]];
deltaTime = 0
HZ = 60
timePerFrame = 1 / 60
scaleFactor = 1
increasing = True
upperScaleBound = 2
lowerScaleBound = -2
for i in range(len(vertices)):
vertices[i] = scaleVertexByX(vertices[i], 3);
vertices[i] = scaleVertexByY(vertices[i], 3);
while True:
ogTime = time.time()
if(increasing and scaleFactor < upperScaleBound):
scaleFactor += 0.005
else:
increasing = False
if(not increasing and scaleFactor > lowerScaleBound):
scaleFactor -= 0.005
else:
increasing = True
for i in range(len(teapotVertices)):
teapotVertices[i] = rotateVertexByX(teapotVertices[i], 1 * (math.pi / 180));
teapotVertices[i] = rotateVertexByY(teapotVertices[i], 1 * (math.pi / 180));
teapotVertices[i] = rotateVertexByZ(teapotVertices[i], 1 * (math.pi / 180));
render2(teapotVertices, faceOrder)
finalTime = time.time()
deltaTime = finalTime - ogTime;
if(timePerFrame - deltaTime > 0):
time.sleep(timePerFrame - deltaTime)