-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
192 lines (156 loc) · 6.93 KB
/
model.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
import moderngl as mgl
import numpy as np
import glm
class BaseModel:
def __init__(self, app, vao_name, tex_id, pos=(0, 0, 0), rot=(0, 0, 0), scale=(1, 1, 1)):
self.app = app
self.pos = pos
self.vao_name = vao_name
self.rot = glm.vec3([glm.radians(a) for a in rot])
self.scale = scale
self.m_model = self.get_model_matrix()
self.tex_id = tex_id
self.vao = app.mesh.vao.vaos[vao_name]
self.program = self.vao.program
self.camera = self.app.camera
def update(self, *args, **kwargs): ...
def get_model_matrix(self):
m_model = glm.mat4()
# translate
m_model = glm.translate(m_model, self.pos)
# rotate
m_model = glm.rotate(m_model, self.rot.z, glm.vec3(0, 0, 1))
m_model = glm.rotate(m_model, self.rot.y, glm.vec3(0, 1, 0))
m_model = glm.rotate(m_model, self.rot.x, glm.vec3(1, 0, 0))
# scale
m_model = glm.scale(m_model, self.scale)
return m_model
def render(self, cam):
self.update(cam)
self.vao.render()
class ExtendedBaseModel(BaseModel):
def __init__(self, app, vao_name, tex_id, pos, rot, scale, use_shadow=True):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
self.use_shadow = use_shadow
self.on_init()
def update(self, cam=None):
camera = cam if cam else self.camera
self.texture.use(location=0)
self.program['camPos'].write(camera.position)
self.program['m_view'].write(camera.m_view)
self.program['m_model'].write(self.m_model)
def update_shadow(self):
self.shadow_program['m_model'].write(self.m_model)
def render_shadow(self):
self.update_shadow()
self.shadow_vao.render()
def on_init(self):
self.program['m_view_light'].write(self.app.light.m_view_light)
# resolution
self.program['u_resolution'].write(glm.vec2(self.app.WIN_SIZE))
# depth texture
self.depth_texture = self.app.mesh.texture.textures['depth_texture']
self.program['shadowMap'] = 1
self.depth_texture.use(location=1)
if self.use_shadow:
# shadow
self.shadow_vao = self.app.mesh.vao.vaos['shadow_' + self.vao_name]
self.shadow_program = self.shadow_vao.program
self.shadow_program['m_proj'].write(self.camera.m_proj)
self.shadow_program['m_view_light'].write(self.app.light.m_view_light)
self.shadow_program['m_model'].write(self.m_model)
# texture
self.texture = self.app.mesh.texture.textures[self.tex_id]
self.program['u_texture_0'] = 0
self.texture.use(location=0)
# mvp
self.program['m_proj'].write(self.camera.m_proj)
self.program['m_view'].write(self.camera.m_view)
self.program['m_model'].write(self.m_model)
# light
self.program['light.position'].write(self.app.light.position)
self.program['light.Ia'].write(self.app.light.Ia)
self.program['light.Id'].write(self.app.light.Id)
self.program['light.Is'].write(self.app.light.Is)
class Cube(ExtendedBaseModel):
def __init__(self, app, vao_name='cube', tex_id=0, pos=(0, 0, 0), rot=(0, 0, 0), scale=(1, 1, 1)):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
class MovingCube(Cube):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def update(self, cam):
self.m_model = self.get_model_matrix()
super().update(cam)
class Cat(ExtendedBaseModel):
def __init__(self, app, vao_name='cat', tex_id='cat',
pos=(0, 0, 0), rot=(-90, 0, 0), scale=(1, 1, 1)):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
class ShinyBase(ExtendedBaseModel):
def __init__(self, app, vao_name='shiny_cube', tex_id='skybox',
pos=(0, 0, 0), rot=(0, 0, 0), scale=(1, 1, 1), hint=(0, 0, 0), tex_location=0):
self.hint = hint
self.tex_location = tex_location
super().__init__(app, vao_name, tex_id, pos, rot, scale, use_shadow=False)
def update(self, cam):
self.texture.use(location=self.tex_location)
self.program['camPos'].write(cam.position)
self.program['m_view'].write(cam.m_view)
self.program['m_model'].write(self.m_model)
def on_init(self):
# texture
self.texture = self.app.mesh.texture.textures[self.tex_id]
self.program['environment'] = self.tex_location
self.texture.use(location=self.tex_location)
# mvp
self.program['m_proj'].write(self.camera.m_proj)
self.program['m_view'].write(self.camera.m_view)
self.program['m_model'].write(self.m_model)
# cube mapping
self.program['hint'].write(glm.vec3(self.hint))
self.program['camPos'].write(glm.vec3(self.camera.position))
class ShinyCube(ShinyBase):
def __init__(self, app, vao_name='shiny_cube', tex_id='env_cube',
pos=(0, 0, 0), rot=(0, 0, 0), scale=(1, 1, 1)):
super().__init__(app, vao_name, tex_id, pos, rot, scale, hint=(0.2, 0.2, 0.1), tex_location=1)
def update(self, cam):
super().update(cam)
self.m_model = self.get_model_matrix()
class ShinyCat(ShinyBase):
def __init__(self, app, vao_name='shiny_cat', tex_id='skybox',
pos=(0, 0, 0), rot=(-90, 0, 0), scale=(1, 1, 1)):
super().__init__(app, vao_name, tex_id, pos, rot, scale, hint=(0, 0, 0), tex_location=0)
pass
class SkyBox(BaseModel):
def __init__(self, app, vao_name='skybox', tex_id='skybox',
pos=(0, 0, 0), rot=(0, 0, 0), scale=(1, 1, 1)):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
self.on_init()
def update(self, cam):
self.program['m_view'].write(glm.mat4(glm.mat3(cam.m_view)))
def on_init(self):
# texture
self.texture = self.app.mesh.texture.textures[self.tex_id]
self.program['u_texture_skybox'] = 0
self.texture.use(location=0)
# mvp
self.program['m_proj'].write(self.camera.m_proj)
self.program['m_view'].write(glm.mat4(glm.mat3(self.camera.m_view)))
class AdvancedSkyBox(BaseModel):
def __init__(self, app, vao_name='advanced_skybox', tex_id='skybox',
pos=(0, 0, 0), rot=(0, 0, 0), scale=(1, 1, 1)):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
self.on_init()
def update(self, cam):
m_view = glm.mat4(glm.mat3(cam.m_view))
self.program['m_invProjView'].write(glm.inverse(cam.m_proj * m_view))
def on_init(self):
# texture
self.texture = self.app.mesh.texture.textures[self.tex_id]
self.program['u_texture_skybox'] = 0
self.texture.use(location=0)
#
# def render(self, cam):
# self.app.ctx.disable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE)
# self.update(cam)
# self.vao.render()
# self.app.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE)