-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayableSphere.py
More file actions
146 lines (118 loc) · 4.98 KB
/
Copy pathDisplayableSphere.py
File metadata and controls
146 lines (118 loc) · 4.98 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
"""
Define displayable cube here. Current version only use VBO
First version in 10/20/2021
:author: micou(Zezhou Sun)
:version: 2021.1.1
"""
from typing import Optional
from Displayable import Displayable
from GLBuffer import VAO, VBO, EBO
import numpy as np
import ColorType as Ct
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 DisplayableSphere(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
radius = None
slices = None
stacks = None
color = None
def __init__(self, shaderProg,
radius=1,
slices=30,
stacks=30,
color=Ct.BLUE):
super(DisplayableSphere, self).__init__()
self.shaderProg = shaderProg
self.shaderProg.use()
self.vao = VAO()
self.vbo = VBO() # vbo can only be initiated with glProgram activated
self.ebo = EBO()
self.generate(radius, slices, stacks, color)
def generate(self,
radius: float,
slices: int,
stacks: int,
color: Optional[Ct.ColorType] = None):
# If the number of slices or stacks is less than 3, set it to 3
if slices < 3:
slices = 3
if stacks < 3:
stacks = 3
# Set the class parameters
self.radius = radius
self.slices = slices
self.stacks = stacks
self.color = color
pi = math.pi
# Calculate the number of vertices and indices based on the number of slices and stacks
slices_1 = slices + 1
stacks_1 = stacks + 1
self.vertices = np.zeros((slices_1 * stacks_1, 11))
self.indices = np.zeros((slices_1 * stacks_1, 6), dtype=np.uint32)
# Loop over the slices and stacks, calculating the x, y, and z coordinates
# of each vertex and the corresponding normal vector
for i, phi in enumerate(np.linspace(-pi / 2, pi / 2, slices_1)):
for j, theta in enumerate(np.linspace(-pi, pi, stacks_1)):
x = radius * math.cos(phi) * math.cos(theta)
y = radius * math.cos(phi) * math.sin(theta)
z = radius * math.sin(phi)
nx = math.cos(phi) * math.cos(theta)
ny = math.cos(phi) * math.sin(theta)
nz = math.sin(phi)
# Encoding vertex order using C array order
i_by_j = i * stacks_1 + j
self.vertices[i_by_j] = [x, y, z, nx, ny, nz, *color, j / stacks, i / slices]
# I've combined the two loops here so that the number of loops can be reduced.
ip1_by_j = (i + 1) % slices_1 * stacks_1 + j
i_by_jp1 = i * stacks_1 + (j + 1) % stacks_1
ip1_by_jp1 = (i + 1) % slices_1 * stacks_1 + (j + 1) % stacks_1
# Set the indices in the correct order for CCW winding
self.indices[i_by_j] = [
i_by_j, ip1_by_j, i_by_jp1,
ip1_by_jp1, ip1_by_j, i_by_jp1]
self.indices = self.indices.flatten("C")
def draw(self):
self.vao.bind()
self.ebo.draw()
self.vao.unbind()
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)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexTexture"),
stride=11, offset=9, attribSize=2)
self.vao.unbind()