generated from CodersSquad/cg-booting-up
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path06_multiple_objects.py
107 lines (77 loc) · 2.72 KB
/
06_multiple_objects.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
import math
import os
import sys
import glm
import moderngl
import numpy as np
import pygame
os.environ['SDL_WINDOWS_DPI_AWARENESS'] = 'permonitorv2'
pygame.init()
pygame.display.set_mode((800, 800), flags=pygame.OPENGL | pygame.DOUBLEBUF, vsync=True)
class TriangleGeometry:
def __init__(self):
self.ctx = moderngl.get_context()
vertices = np.array([
0.0, 0.4, 0.0,
-0.4, -0.3, 0.0,
0.4, -0.3, 0.0,
])
self.vbo = self.ctx.buffer(vertices.astype('f4').tobytes())
def vertex_array(self, program):
return self.ctx.vertex_array(program, [(self.vbo, '3f', 'in_vertex')])
class Mesh:
def __init__(self, program, geometry):
self.ctx = moderngl.get_context()
self.vao = geometry.vertex_array(program)
def render(self, position, color, scale):
self.vao.program['position'] = position
self.vao.program['color'] = color
self.vao.program['scale'] = scale
self.vao.render()
class Scene:
def __init__(self):
self.ctx = moderngl.get_context()
self.program = self.ctx.program(
vertex_shader='''
#version 330 core
uniform mat4 camera;
uniform vec3 position;
uniform float scale;
layout (location = 0) in vec3 in_vertex;
void main() {
gl_Position = camera * vec4(position + in_vertex * scale, 1.0);
}
''',
fragment_shader='''
#version 330 core
uniform vec3 color;
layout (location = 0) out vec4 out_color;
void main() {
out_color = vec4(color, 1.0);
}
''',
)
self.triangle_geometry = TriangleGeometry()
self.triangle = Mesh(self.program, self.triangle_geometry)
def camera_matrix(self):
now = pygame.time.get_ticks() / 1000.0
eye = (math.cos(now), math.sin(now), 0.5)
proj = glm.perspective(45.0, 1.0, 0.1, 1000.0)
look = glm.lookAt(eye, (0.0, 0.0, 0.0), (0.0, 0.0, 1.0))
return proj * look
def render(self):
camera = self.camera_matrix()
self.ctx.clear()
self.ctx.enable(self.ctx.DEPTH_TEST)
self.program['camera'].write(camera)
self.triangle.render((-0.2, 0.0, 0.0), (1.0, 0.0, 0.0), 0.2)
self.triangle.render((0.0, 0.0, 0.0), (0.0, 1.0, 0.0), 0.2)
self.triangle.render((0.2, 0.0, 0.0), (0.0, 0.0, 1.0), 0.2)
scene = Scene()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
scene.render()
pygame.display.flip()