-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
131 lines (91 loc) · 3.95 KB
/
model.py
File metadata and controls
131 lines (91 loc) · 3.95 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
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.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.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.app.ctx.enable(flags=mgl.DEPTH_TEST | mgl.CULL_FACE)
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()
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 on_init(self):
# texture
self.texture = self.app.mesh.textures[self.tex_id]
self.program['u_texture_0'] = 0
self.texture.use()
# 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='flat_cube', tex_id=0, pos=(0, 0, 0), rot=(0, 0, 0), scale=(0.5,0.5,0.5)):
super().__init__(app, vao_name, tex_id, pos, rot, scale)
class MarkerCube(Cube):
def __init__(self, app, tex_id=3, pos=(0, 0, 0), rot=(0, 0, 0), scale=(0.51,0.51,0.51)):
super().__init__(app, "alpha_cube", tex_id, pos, rot, scale)
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.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.textures[self.tex_id]
self.program['u_texture_skybox'] = 0
self.texture.use(location=0)