-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.py
110 lines (89 loc) · 3.5 KB
/
render.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
import pyglet
from pyglet import window, app, shapes
from pyglet.window import mouse,key
from pyglet.graphics.shader import Shader, ShaderProgram
from pyglet.gl import GL_TRIANGLES
from pyglet.math import Mat4, Vec3
from pyglet.gl import *
import shader
from primitives import CustomGroup
class RenderWindow(pyglet.window.Window):
'''
inherits pyglet.window.Window which is the default render window of Pyglet
'''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.batch = pyglet.graphics.Batch()
'''
View (camera) parameters
'''
self.cam_eye = Vec3(0,2,4)
self.cam_target = Vec3(0,0,0)
self.cam_vup = Vec3(0,1,0)
self.view_mat = None
'''
Projection parameters
'''
self.z_near = 0.1
self.z_far = 100
self.fov = 60
self.proj_mat = None
self.shapes = []
self.setup()
self.animate = False
def setup(self) -> None:
self.set_minimum_size(width = 400, height = 300)
self.set_mouse_visible(True)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
# 1. Create a view matrix
self.view_mat = Mat4.look_at(
self.cam_eye, target=self.cam_target, up=self.cam_vup)
# 2. Create a projection matrix
self.proj_mat = Mat4.perspective_projection(
aspect = self.width/self.height,
z_near=self.z_near,
z_far=self.z_far,
fov = self.fov)
def on_draw(self) -> None:
self.clear()
self.batch.draw()
def update(self,dt) -> None:
view_proj = self.proj_mat @ self.view_mat
for i, shape in enumerate(self.shapes):
'''
Update position/orientation in the scene. In the current setting,
shapes created later rotate faster while positions are not changed.
'''
if self.animate:
rotate_angle = dt
rotate_axis = Vec3(0,0,1)
rotate_mat = Mat4.from_rotation(angle = rotate_angle, vector = rotate_axis)
shape.transform_mat @= rotate_mat
# # Example) You can control the vertices of shape.
# shape.indexed_vertices_list.vertices[0] += 0.5 * dt
'''
Update view and projection matrix. There exist only one view and projection matrix
in the program, so we just assign the same matrices for all the shapes
'''
shape.shader_program['view_proj'] = view_proj
def on_resize(self, width, height):
glViewport(0, 0, *self.get_framebuffer_size())
self.proj_mat = Mat4.perspective_projection(
aspect = width/height, z_near=self.z_near, z_far=self.z_far, fov = self.fov)
return pyglet.event.EVENT_HANDLED
def add_shape(self, transform, vertice, indice, color):
'''
Assign a group for each shape
'''
shape = CustomGroup(transform, len(self.shapes))
shape.indexed_vertices_list = shape.shader_program.vertex_list_indexed(len(vertice)//3, GL_TRIANGLES,
batch = self.batch,
group = shape,
indices = indice,
vertices = ('f', vertice),
colors = ('Bn', color))
self.shapes.append(shape)
def run(self):
pyglet.clock.schedule_interval(self.update, 1/60)
pyglet.app.run()