forked from IntelligentMOtionlab/SNU_ComputerGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.py
158 lines (130 loc) · 5.22 KB
/
primitives.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
import pyglet
from pyglet import window, app, shapes
from pyglet.math import Mat4, Vec3, Vec4
import math
from pyglet.gl import *
import shader
class CustomGroup(pyglet.graphics.Group):
'''
To draw multiple 3D shapes in Pyglet, you should make a group for an object.
'''
def __init__(self, transform_mat: Mat4, order):
super().__init__(order)
'''
Create shader program for each shape
'''
self.shader_program = shader.create_program(
shader.vertex_source_default, shader.fragment_source_default
)
self.transform_mat = transform_mat
self.indexed_vertices_list = None
self.shader_program.use()
def set_state(self):
self.shader_program.use()
model = self.transform_mat
self.shader_program['model'] = model
def unset_state(self):
self.shader_program.stop()
def __eq__(self, other):
return (self.__class__ is other.__class__ and
self.order == other.order and
self.parent == other.parent)
def __hash__(self):
return hash((self.order))
class Cube:
'''
default structure of cube
'''
def __init__(self, scale=1.0):
self.vertices = [-0.5, -0.5, 0.5,
0.5, -0.5, 0.5,
0.5, 0.5, 0.5,
-0.5, 0.5, 0.5,
-0.5, -0.5, -0.5,
0.5, -0.5, -0.5,
0.5,0.5,-0.5,
-0.5,0.5,-0.5]
self.vertices = [scale[idx%3] * x for idx, x in enumerate(self.vertices)]
self.indices = [0, 1, 2, 2, 3, 0,
4, 7, 6, 6, 5, 4,
4, 5, 1, 1, 0, 4,
6, 7, 3, 3, 2, 6,
5, 6, 2, 2, 1, 5,
7, 4, 0, 0, 3, 7]
self.colors = (255, 0, 0,255,
0, 255, 0,255,
0, 0,255,255,
255,255,255,255,
255, 0, 0,255,
0, 255, 0,255,
0, 0,255,255,
255,255,255,255)
class Sphere:
'''
default structure of sphere
'''
def __init__(self, stacks, slices, scale=1.0):
num_triangles = 2 * slices * (stacks - 1)
self.vertices = []
self.indices = []
self.colors = ()
for i in range(stacks):
phi0 = 0.5 * math.pi - (i * math.pi) / stacks
phi1 = 0.5 * math.pi - ((i + 1) * math.pi) / stacks
coord_v0 = 1.0 - float(i) / stacks
coord_v1 = 1.0 - float(i + 1) / stacks
y0 = scale * math.sin(phi0)
r0 = scale * math.cos(phi0)
y1 = scale * math.sin(phi1)
r1 = scale * math.cos(phi1)
y2 = y1
y3 = y0
for j in range(slices):
theta0 = (j * 2 * math.pi) / slices
theta1 = ((j + 1) * 2 * math.pi) / slices
coord_u0 = float(j) / slices
coord_u1 = float(j + 1) / slices
x0 = r0 * math.cos(theta0)
z0 = r0 * math.sin(-theta0)
u0 = coord_u0
v0 = coord_v0
x1 = r1 * math.cos(theta0)
z1 = r1 * math.sin(-theta0)
u1 = coord_u0
v1 = coord_v1
x2 = r1 * math.cos(theta1)
z2 = r1 * math.sin(-theta1)
u2 = coord_u1
v2 = coord_v1
x3 = r0 * math.cos(theta1)
z3 = r0 * math.sin(-theta1)
u3 = coord_u1
v3 = coord_v0
if (i != stacks - 1):
self.vertices.append(x0)
self.vertices.append(y0)
self.vertices.append(z0)
self.vertices.append(x1)
self.vertices.append(y1)
self.vertices.append(z1)
self.vertices.append(x2)
self.vertices.append(y2)
self.vertices.append(z2)
self.colors += (int(math.cos(phi0) * 255),int(math.cos(theta0) * 255),int(math.sin(phi0)*255),255)
self.colors += (int(math.cos(phi0) * 255),int(math.cos(theta0) * 255),int(math.sin(phi0)*255),255)
self.colors += (int(math.cos(phi0) * 255),int(math.cos(theta0) * 255),int(math.sin(phi0)*255),255)
if (i != 0):
self.vertices.append(x2)
self.vertices.append(y2)
self.vertices.append(z2)
self.vertices.append(x3)
self.vertices.append(y3)
self.vertices.append(z3)
self.vertices.append(x0)
self.vertices.append(y0)
self.vertices.append(z0)
self.colors += (int(math.cos(phi0) * 255),int(math.cos(theta0) * 255),int(math.sin(phi0)*255),255)
self.colors += (int(math.cos(phi0) * 255),int(math.cos(theta0) * 255),int(math.sin(phi0)*255),255)
self.colors += (int(math.cos(phi0) * 255),int(math.cos(theta0) * 255),int(math.sin(phi0)*255),255)
for i in range(num_triangles*3):
self.indices.append(i)