-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayableEllipsoid.py
More file actions
153 lines (126 loc) · 4.99 KB
/
DisplayableEllipsoid.py
File metadata and controls
153 lines (126 loc) · 4.99 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
147
148
149
150
151
152
153
"""
Define displayable Ellipsoid here.
"""
from Displayable import Displayable
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 DisplayableEllipsoid(Displayable):
vao = None
vbo = None
ebo = None
shaderProg = None
vertices = None # array to store vertices information
indices = None # stores triangle indices to vertices
radius_x = None
radius_y = None
radius_z = None
slices = None
stacks = None
color = None
def __init__(self, shaderProg,
radius_x=1,
radius_y=1,
radius_z=1,
slices=30,
stacks=30,
color=ColorType.ORANGE):
super(DisplayableEllipsoid, 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_x, radius_y, radius_z, slices, stacks, color)
def generate(self,
radius_x, radius_y, radius_z, slices, stacks,
color=None):
# Ellipsoid parametric equation:
# x = r_x * cos(u) * sin(v)
# y = r_y * sin(u) * sin(v)
# z = r_z * cos(v)
# u uses slices, v uses stacks
# Normal vector:
# n_x = cos(u) * sin(v) / r_x
# n_y = sin(u) * sin(v) / r_y
# n_z = cos(v) / r_z
# Store the ellipsoid's radius
self.radius_x = radius_x
self.radius_y = radius_y
self.radius_z = radius_z
# Make sure slices and stacks have at least 3 values
if slices < 3:
slices = 3
if stacks < 3:
stacks = 3
self.slices = slices
self.stacks = stacks
self.color = color
pi = np.pi
# slices_1 and stacks_1 need 1 more to ensure generate
# correct index sequences using np.linspace
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 through the ellipsoid's slices and stacks to generate vertices and indices
for i, phi in enumerate(np.linspace(-pi / 2, pi / 2, slices_1)):
for j, theta in enumerate(np.linspace(-pi, pi, stacks_1)):
# Compute the ellipsoid's coordinates and normal vector
vx = np.cos(phi) * np.cos(theta)
vy = np.cos(phi) * np.sin(theta)
vz = np.sin(phi)
x = radius_x * vx
y = radius_y * vy
z = radius_z * vz
nx = vx / radius_x
ny = vy / radius_y
nz = vz / radius_z
# Pre-compute the ellipsoid's texture indices for later
i_by_j = i * stacks_1 + j
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
# Store the vertex and index information for this slice and stack
self.vertices[i_by_j] = [x, y, z, nx, ny, nz, *color, j / stacks, i / slices]
self.indices[i_by_j] = [
i_by_j, ip1_by_j, i_by_jp1,
ip1_by_jp1, ip1_by_j, i_by_jp1]
# Flatten the index array to make it compatible with GLSL EBO definition.
self.indices = self.indices.flatten("C")
def draw(self):
self.vao.bind()
self.ebo.draw()
self.vao.unbind()
def initialize(self):
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()