-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
39 lines (27 loc) · 1.48 KB
/
Copy pathprogram.py
File metadata and controls
39 lines (27 loc) · 1.48 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
import numpy as np
class Program:
def __init__(self, ctx, camera, vertex_shader, fragment_shader):
# create a moderngl program from the provided shaders
self.program = ctx.program(vertex_shader=vertex_shader, fragment_shader=fragment_shader)
# get uniform locations for projection, model, and view matrices
self.projection_uniform = self.program['projection']
self.model_uniform = self.program['model']
self.view_uniform = self.program['view']
# set initial matrix uniforms
self.set_defaults(camera)
def set_defaults(self, camera):
# get and update the projection matrix from the camera
projection_matrix = camera.get_projection_matrix()
self.update_projection_matrix(projection_matrix)
# get and update the view matrix from the camera
view_matrix = camera.get_view_matrix()
self.update_view_matrix(view_matrix)
# initialize and update the model matrix to an identity matrix
model_matrix = np.identity(4, dtype=np.float32)
self.update_model_matrix(model_matrix)
def update_projection_matrix(self, projection_matrix):
self.projection_uniform.write(projection_matrix.astype('f4').tobytes())
def update_model_matrix(self, model_matrix):
self.model_uniform.write(model_matrix.astype('f4').tobytes())
def update_view_matrix(self, view_matrix):
self.view_uniform.write(view_matrix.astype('f4').tobytes())