-
Notifications
You must be signed in to change notification settings - Fork 36
/
model.py
157 lines (112 loc) · 4.83 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
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): ...
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):
self.update()
self.vao.render()
class ExtendedBaseModel(BaseModel):
def __init__(self, app, vao_name, tex_id, pos, rot, scale):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
self.on_init()
def update(self):
self.texture.use(location=0)
self.program['camPos'].write(self.camera.position)
self.program['m_view'].write(self.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)
# 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):
self.m_model = self.get_model_matrix()
super().update()
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 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):
self.program['m_view'].write(glm.mat4(glm.mat3(self.camera.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):
m_view = glm.mat4(glm.mat3(self.camera.m_view))
self.program['m_invProjView'].write(glm.inverse(self.camera.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)