-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.py
274 lines (258 loc) · 10 KB
/
shape.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
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
class drawObject:
def __init__(self):
self.indexCoord2f = (
(0.0, 0.0),
(0.0, 1.0),
(1.0, 1.0),
(1.0, 0.0),
(0.5, 1.0),
)
def set_vertices(self, x, y, z): # for cube, box
self.vertices = [
[x, -y, -z],
[x, y, -z],
[-x, y, -z],
[-x, -y, -z],
[x, -y, z],
[x, y, z],
[-x, -y, z],
[-x, y, z],
]
self.edges = (
(0, 1),
(0, 3),
(0, 4),
(2, 1),
(2, 3),
(2, 7),
(6, 3),
(6, 4),
(6, 7),
(5, 1),
(5, 4),
(5, 7)
)
self.surfaces = (
(0, 1, 2, 3),
(3, 2, 7, 6),
(6, 7, 5, 4),
(4, 5, 1, 0),
(1, 5, 7, 2),
(4, 0, 3, 6),
)
def init_pyramid(self, size, height):
half_size = size * 0.5
self.verPy = [
[0.0, height, 0.0],
[-half_size, 0.0, half_size],
[half_size, 0.0, half_size],
[-half_size, 0.0, -half_size],
[half_size, 0.0, -half_size],
]
self.indexPy = [
[0, 1, 2], # Front face
[0, 3, 1], # Left face
[0, 3, 4], # Back face
[0, 4, 2], # Right face
[2, 4, 3, 1], # Bottom face
]
def make_cube(self, size):
self.set_vertices(size, size, size)
if self.typeDraw == "points":
glBegin(GL_POINTS)
for edge in self.edges:
for vertex in edge:
glVertex3fv(self.vertices[vertex])
elif self.typeDraw == "lines":
glBegin(GL_LINES)
for edge in self.edges:
for vertex in edge:
glVertex3fv(self.vertices[vertex])
else:
glBegin(GL_QUADS)
if self.toggleTextures:
for surface in self.surfaces:
for ia, vertex in enumerate(surface):
glTexCoord2fv(self.indexCoord2f[ia])
glVertex3fv(self.vertices[vertex])
else:
for surface in self.surfaces:
for vertex in surface:
glVertex3fv(self.vertices[vertex])
glEnd()
def make_box(self, length, width, height):
self.set_vertices(length, width, height)
if self.typeDraw == "points":
glBegin(GL_POINTS)
for edge in self.edges:
for vertex in edge:
glVertex3fv(self.vertices[vertex])
elif self.typeDraw == "lines":
glBegin(GL_LINES)
for edge in self.edges:
for vertex in edge:
glVertex3fv(self.vertices[vertex])
else:
glBegin(GL_QUADS)
if self.toggleTextures:
for surface in self.surfaces:
for ia, vertex in enumerate(surface):
glTexCoord2fv(self.indexCoord2f[ia])
glVertex3fv(self.vertices[vertex])
else:
for surface in self.surfaces:
for vertex in surface:
glVertex3fv(self.vertices[vertex])
glEnd()
def make_sphere(self, radius):
if self.typeDraw == "points":
print("cc")
obj = gluNewQuadric()
gluQuadricDrawStyle(obj, GLU_POINT)
gluSphere(obj, radius, 32, 32)
elif self.typeDraw == "lines":
glutWireSphere(radius, 32, 32)
else:
if self.toggleTextures:
quadratic_obj = gluNewQuadric()
gluQuadricTexture(quadratic_obj, GL_TRUE)
gluSphere(quadratic_obj, radius, 32, 32)
else:
glutSolidSphere(radius, 32, 32)
def make_light_source_shape(self):
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL)
glutSolidSphere(0.2, 32, 32)
def make_cone(self, base_rad, length):
if self.typeDraw == "points":
obj = gluNewQuadric()
gluQuadricDrawStyle(obj, GLU_POINT)
gluCylinder(obj, base_rad, 0.0, length, 32, 32)
elif self.typeDraw == "lines":
glutWireCone(base_rad, length, 32, 32)
else:
if self.toggleTextures:
quadratic_obj = gluNewQuadric()
gluQuadricTexture(quadratic_obj, GL_TRUE)
gluCylinder(quadratic_obj, base_rad, 0.0, length, 32, 32)
else:
glutSolidCone(base_rad, length, 32, 32)
def make_cylinder(self, radius, length):
#Hinh Tru
if self.typeDraw == "points":
obj = gluNewQuadric()
gluQuadricDrawStyle(obj, GLU_POINT)
gluCylinder(obj, radius, radius, length, 32, 32)
elif self.typeDraw == "lines":
glutWireCylinder(radius, length, 32, 32)
else:
if self.toggleTextures:
quadratic_obj = gluNewQuadric()
gluQuadricTexture(quadratic_obj, GL_TRUE)
gluCylinder(quadratic_obj, radius, radius, length, 32, 32)
else:
glutSolidCylinder(radius, length, 32, 32)
def make_torus(self, innerRadius, outerRadious):
if self.typeDraw == "points":
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)
glutSolidTorus(innerRadius, outerRadious, 32, 32)
elif self.typeDraw == "lines":
glutWireTorus(innerRadius, outerRadious, 32, 32)
else:
if self.toggleTextures:
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR)
glEnable(GL_TEXTURE_GEN_S)
glEnable(GL_TEXTURE_GEN_T)
glutSolidTorus(innerRadius, outerRadious, 32, 32)
glDisable(GL_TEXTURE_GEN_S)
glDisable(GL_TEXTURE_GEN_T)
else:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glutSolidTorus(innerRadius, outerRadious, 32, 32)
def make_teapot(self, size):
if self.typeDraw == "points":
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)
glutSolidTeapot(size)
elif self.typeDraw == "lines":
glutWireTeapot(size)
else:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glutSolidTeapot(size)
def make_truncatedCone(self, base_rad, top_rad, length):
#Non cut
if self.typeDraw == "points":
obj = gluNewQuadric()
gluQuadricDrawStyle(obj, GLU_POINT)
gluCylinder(obj, base_rad, top_rad, length, 32, 32)
elif self.typeDraw == "lines":
obj = gluNewQuadric()
gluQuadricDrawStyle(obj, GLU_LINE)
gluCylinder(obj, base_rad, top_rad, length, 32, 32)
else:
if self.toggleTextures:
quadratic_obj = gluNewQuadric()
gluQuadricTexture(quadratic_obj, GL_TRUE)
gluCylinder(quadratic_obj, base_rad, top_rad, length, 32, 32)
else:
quadratic_obj = gluNewQuadric()
gluCylinder(quadratic_obj, base_rad, top_rad, length, 32, 32)
def make_pyramid(self, size, height):
self.init_pyramid(size, height)
if self.typeDraw == "points":
glBegin(GL_POINTS)
for ia in range(len(self.indexPy)-1):
for ib in self.indexPy[ia]:
glVertex3fv(self.verPy[ib])
glEnd()
glBegin(GL_POINTS)
for ia in self.indexPy[-1]:
glVertex3fv(self.verPy[ia])
glEnd()
elif self.typeDraw == "lines":
glBegin(GL_LINES)
for ia in range(len(self.indexPy)-1):
for ib in self.indexPy[ia]:
glVertex3fv(self.verPy[ib])
glEnd()
glBegin(GL_LINE_LOOP)
for ia in self.indexPy[-1]:
glVertex3fv(self.verPy[ia])
glEnd()
else:
glBegin(GL_TRIANGLES)
for ia in range(len(self.indexPy)-1):
tempInt = 0
for ib in self.indexPy[ia]:
if ib == 0:
glTexCoord2fv(self.indexCoord2f[-1])
else:
glTexCoord2fv(self.indexCoord2f[3]) if tempInt % 2 ==0 \
else glTexCoord2fv(self.indexCoord2f[0])
tempInt += 1
glVertex3fv(self.verPy[ib])
glEnd()
glBegin(GL_QUADS)
for id, ia in enumerate(self.indexPy[-1]):
glTexCoord2fv(self.indexCoord2f[id])
glVertex3fv(self.verPy[ia])
glEnd()
def make_flat(self):
glPushMatrix()
# glTranslatef(0 , 0, -5.0)
# ambien = [1.0, 1.0, 0.0, 1.0]
# diff_use = [1.0, 1.0, 0.0, 1.0]
# glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambien)
# glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff_use)
glColor4f(0.5,0.5,0.5,1)
glRotatef(60,-1,0,0)
glBegin(GL_QUADS)
glVertex3f(-10,-10,-5)
glVertex3f(10,-10, -5)
glVertex3f(15,20, -5)
glVertex3f(-15,20, -5)
glEnd()
glColor4f(1,1,1,1)
glPopMatrix()