-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShapes.py
240 lines (206 loc) · 8.54 KB
/
Shapes.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
"""
Implements four geometric primitives (Sphere, Cube, Cone, Cylinder) using the DisplayableMesh class
Modified by Daniel Scrivener 07/2022
"""
from time import time_ns
from typing import Union, List, Tuple
from collada import *
from DisplayableMesh import DisplayableMesh
from Component import Component
import GLUtility
import ColorType
import numpy as np
from GLProgram import GLProgram
from Point import Point
def getVertexData(filename):
colladaData = Collada(filename)
# generate vertices from model data
# also generate indices
geo = colladaData.geometries[0]
tridata = geo.primitives[0]
trilist = list(tridata)
# construct vertex list
vertices = np.array([])
for vert in tridata.vertex:
vert = np.concatenate((vert, [0.,0.,0.]), axis=0) # empty normals
vert = np.concatenate((vert, [0.,0.,0.]), axis=0) # color
vert = np.concatenate((vert, [0.,0.]), axis=0) # empty UV
vertices = np.append(vertices, vert)
# construct indices
indices = np.array([])
for tri in trilist:
indices = np.append(indices, tri.indices)
return (vertices, indices)
class Shape(Component):
vertexData = None
indexData = None
mesh = None
def __init__(self,
position: Point,
shaderProg: GLProgram,
scale: Union[List[float], Tuple[float, float, float], np.ndarray],
vertexData,
indexData,
color: ColorType.ColorType = ColorType.YELLOW):
"""
:param position: location of the object
:type position: Point
:param shaderProg: compiled shader program
:type shaderProg: GLProgram
:param scale: set of three scale factors to be applied to each vertex
:type scale: list or tuple
:param pathname: .dae file to import
:type pathname: string
:param color: vertex color to be applied uniformly
:type color: ColorType
:param limb: sets the rotation behavior of the object. if true, rotations happen "at the joint" \
rather than the object's center
:type limb: boolean
"""
self.mesh = DisplayableMesh(shaderProg, scale, vertexData, indexData, color)
super(Shape, self).__init__(position, self.mesh)
class Cone(Shape):
pathname = "assets/cone0.dae"
pathnameLP = "assets/coneLP.dae"
data = getVertexData(pathname)
dataLP = getVertexData(pathnameLP)
vertices = data[0]
verticesLP = dataLP[0]
indices = data[1]
indicesLP = dataLP[1]
def __init__(self,
position: Point,
shaderProg: GLProgram,
scale: Union[List[float], Tuple[float, float, float], np.ndarray],
color: ColorType.ColorType = ColorType.YELLOW,
lowPoly=True):
"""
:param position: location of the object
:type position: Point
:param shaderProg: compiled shader program
:type shaderProg: GLProgram
:param scale: set of three scale factors to be applied to each vertex
:type scale: list or tuple
:param color: vertex color to be applied uniformly
:type color: ColorType
"""
if lowPoly:
super(Cone, self).__init__(position, shaderProg, scale, self.verticesLP.copy(), self.indicesLP.copy(), color)
else:
super(Cone, self).__init__(position, shaderProg, scale, self.vertices.copy(), self.indices.copy(), color)
# translate object by -z extent of the new component so that rotations occur @ the joint
# rather than around the object's true center
glutility = GLUtility.GLUtility()
tIn = glutility.translate(0, 0, scale[2], False)
tOut = glutility.translate(0, 0, -scale[2], False)
self.inRotation = tIn
self.outRotation = tOut
class Cube(Shape):
pathname = "assets/cube0.dae"
data = getVertexData(pathname)
vertices = data[0]
indices = data[1]
def __init__(self,
position: Point,
shaderProg: GLProgram,
scale: Union[List[float], Tuple[float, float, float], np.ndarray],
color: ColorType.ColorType = ColorType.RED):
"""
:param position: location of the object
:type position: Point
:param shaderProg: compiled shader program
:type shaderProg: GLProgram
:param scale: set of three scale factors to be applied to each vertex
:type scale: list or tuple
:param color: vertex color to be applied uniformly
:type color: ColorType
"""
super(Cube, self).__init__(position, shaderProg, scale, self.vertices.copy(), self.indices.copy(), color)
# translate object by -z extent of the new component so that rotations occur @ the joint
# rather than around the object's true center
glutility = GLUtility.GLUtility()
tIn = glutility.translate(0, 0, scale[2] / 2, False)
tOut = glutility.translate(0, 0, -scale[2] / 2, False)
self.inRotation = tIn
self.outRotation = tOut
class Cylinder(Shape):
pathname = "assets/cylinder0.dae"
pathnameLP = "assets/cylinderLP.dae"
data = getVertexData(pathname)
dataLP = getVertexData(pathnameLP)
vertices = data[0]
verticesLP = dataLP[0]
indices = data[1]
indicesLP = dataLP[1]
def __init__(self,
position: Point,
shaderProg: GLProgram,
scale: Union[List[float], Tuple[float, float, float], np.ndarray],
color: ColorType.ColorType = ColorType.GREEN,
lowPoly=True):
"""
:param position: location of the object
:type position: Point
:param shaderProg: compiled shader program
:type shaderProg: GLProgram
:param scale: set of three scale factors to be applied to each vertex
:type scale: list or tuple
:param color: vertex color to be applied uniformly
:type color: ColorType
"""
if lowPoly:
super(Cylinder, self).__init__(position, shaderProg, scale, self.verticesLP.copy(), self.indicesLP.copy(), color)
else:
super(Cylinder, self).__init__(position, shaderProg, scale, self.vertices.copy(), self.indices.copy(), color)
# translate object by -z extent of the new component so that rotations occur @ the joint
# rather than around the object's true center
glutility = GLUtility.GLUtility()
tIn = glutility.translate(0, 0, scale[2], False)
tOut = glutility.translate(0, 0, -scale[2], False)
self.inRotation = tIn
self.outRotation = tOut
class Sphere(Shape):
pathname = "assets/sphere0.dae"
pathnameLP = "assets/sphereLP.dae"
data = getVertexData(pathname)
dataLP = getVertexData(pathnameLP)
vertices = data[0]
verticesLP = dataLP[0]
indices = data[1]
indicesLP = dataLP[1]
def __init__(self,
position: Point,
shaderProg: GLProgram,
scale: Union[List[float], Tuple[float, float, float], np.ndarray],
color: ColorType.ColorType = ColorType.BLUE,
limb=True,
lowPoly=True):
"""
:param position: location of the object
:type position: Point
:param shaderProg: compiled shader program
:type shaderProg: GLProgram
:param scale: set of three scale factors to be applied to each vertex
:type scale: list or tuple
:param color: vertex color to be applied uniformly
:type color: ColorType
:param limb: sets the rotation behavior of the object. if true, rotations happen "at the joint" \
rather than the object's center.
Set this to False for eyes or other ball joints.
:type limb: boolean
"""
if lowPoly:
super(Sphere, self).__init__(position, shaderProg, scale, self.verticesLP.copy(), self.indicesLP.copy(), color)
else:
super(Sphere, self).__init__(position, shaderProg, scale, self.vertices.copy(), self.indices.copy(), color)
# translate object by -z extent of the new component so that rotations occur @ the joint
# rather than around the object's true center
glutility = GLUtility.GLUtility()
if limb:
tIn = glutility.translate(0, 0, scale[2], False)
tOut = glutility.translate(0, 0, -scale[2], False)
else:
tIn = np.identity(4)
tOut = np.identity(4)
self.inRotation = tIn
self.outRotation = tOut