-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplayableCylinder.py
180 lines (136 loc) · 5.75 KB
/
DisplayableCylinder.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
"""
Define displayable cube here. Current version only use VBO
First version in 10/20/2021
:author: micou(Zezhou Sun)
:version: 2021.1.1
"""
from Displayable import Displayable
from DisplayableDisk import DisplayableDisk
from GLBuffer import VAO, VBO, EBO
import numpy as np
import ColorType
import math
try:
import OpenGL
try:
import OpenGL.GL as gl
import OpenGL.GLU as glu
except ImportError:
from ctypes import util
orig_util_find_library = util.find_library
def new_util_find_library(name):
res = orig_util_find_library(name)
if res:
return res
return '/System/Library/Frameworks/' + name + '.framework/' + name
util.find_library = new_util_find_library
import OpenGL.GL as gl
import OpenGL.GLU as glu
except ImportError:
raise ImportError("Required dependency PyOpenGL not present")
class DisplayableCylinder(Displayable):
vao = None
vbo = None
ebo = None
shaderProg = None
vertices = None # array to store vertices information
indices = None # stores triangle indices to vertices
# stores current cube's information, read-only
length = None
width = None
height = None
color = None
end1 = None
end2 = None
def __init__(self, shaderProg, endRadius, height, slices=6, stacks=6, color=ColorType.BLUE):
super(DisplayableCylinder, self).__init__()
self.shaderProg = shaderProg
self.shaderProg.use()
self.vao = VAO()
self.vbo = VBO() # vbo can only be initiate with glProgram activated
self.ebo = EBO()
#self.end1 = DisplayableDisk(shaderProg, endRadius, 0, -1, slices, color)
self.generate(endRadius, height, slices, stacks, color)
#self.end2 = DisplayableDisk(shaderProg, endRadius, height, 1, slices, color)
# slices=segments in circles, stacks=segments in tube (height)
def generate(self, endRadius, height, slices=6, stacks=6, color=None):
# self.length = length
# self.width = width
self.height = height
self.color = color
pi = math.pi
num_divisions = (slices) * (stacks) # when given = 30
num_end_vertices = 2 * (slices + 2) # For disk
cylinder_vertices = np.zeros([num_divisions+1, num_divisions+1, 3])
cylinder_normals = np.zeros([num_divisions+1, num_divisions+1, 3])
# np.arrange(start, stop, step increment)
for i, current_h in enumerate(np.arange(0, height + height/stacks, height/stacks)):
for j, theta in enumerate(np.arange(-pi, pi + 2 * pi / slices, 2 * pi / slices)):
x = endRadius * math.cos(theta)
y = endRadius * math.sin(theta)
z = current_h
x_normal = math.cos(theta)
y_normal = math.sin(theta)
z_normal = 0
cylinder_vertices[i][j] = [x, y, z]
cylinder_normals[i][j] = [x_normal, y_normal, z_normal]
indices = []
cidx = 0
triangle_list = []
for i in range(stacks+1):
for j in range(slices+1):
triangle_list.append(np.array([
cylinder_vertices[i][j][0], cylinder_vertices[i][j][1], cylinder_vertices[i][j][2],
cylinder_normals[i][j][0], cylinder_normals[i][j][1], cylinder_normals[i][j][2], *color]))
if (i < stacks and j < slices):
indices.extend([
cidx, cidx + 1, cidx + 1 + slices,
cidx, cidx + slices, cidx + 1 + slices
])
elif (i == stacks and j < slices):
indices.extend([
cidx, cidx + 1, j + 1,
cidx, j, j + 1
])
elif (i < stacks and j == slices):
indices.extend([
cidx, cidx - j, cidx - (slices - j),
cidx, cidx + slices, cidx - slices + j
])
elif (i == stacks and j == slices):
indices.extend([
cidx, cidx - j, 0,
cidx, j, 0
])
cidx += 1
new_vl = np.stack(triangle_list)
self.vertices = np.zeros([len(new_vl), 11])
self.vertices[0:len(new_vl), 0:9] = new_vl
self.indices = np.asarray(indices)
#print(len(new_vl), len(self.indices))
# self.indices = np.zeros(0)
def draw(self):
self.vao.bind()
self.ebo.draw()
self.vao.unbind()
#self.end1.draw()
#self.end2.draw()
def initialize(self):
"""
Remember to bind VAO before this initialization. If VAO is not bind, program might throw an error
in systems that don't enable a default VAO after GLProgram compilation
"""
self.vao.bind()
self.vbo.setBuffer(self.vertices, 11)
self.ebo.setBuffer(self.indices)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexPos"),
stride=11, offset=0, attribSize=3)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexNormal"),
stride=11, offset=3, attribSize=3)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexColor"),
stride=11, offset=6, attribSize=3)
# TODO/BONUS 6.1 is at here, you need to set attribPointer for texture coordinates
# you should check the corresponding variable name in GLProgram and set the pointer
self.vao.unbind()
#self.end1.initialize()
#self.end2.initialize()