-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengl.py
68 lines (58 loc) · 1.69 KB
/
opengl.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
"""As of yet an unused module."""
import math
import numpy as np
import sdl2
import moderngl
import moderngl_window
from moderngl_window.conf import settings
# (math.sin(time) + 1.0) / 2,
# (math.sin(time + 2) + 1.0) / 2,
# (math.sin(time + 3) + 1.0) / 2,
settings.WINDOW['class'] = 'moderngl_window.context.sdl2.Window'
settings.WINDOW['gl_version'] = (4, 5)
window = moderngl_window.create_window_from_settings()
ctx = window.ctx
vertices = np.array([
# x, y, red, green, blue
0.0, 1.0, 1.0, 0.1, 0.3,
-1.0, -1.0, 0.5, 0.5, 0.3,
1.0, -1.0, 0.1, 1.0, 0.3,
], dtype='f4')
vbo = ctx.buffer(vertices)
prog = ctx.program(
vertex_shader='''
#version 330
in vec2 in_vert;
in vec3 in_color;
out vec3 v_color; // Goes to the fragment shader
void main() {
gl_Position = vec4(in_vert, 0.0, 1.0);
v_color = in_color;
}
''',
fragment_shader='''
#version 330
in vec3 v_color;
out vec4 f_color;
void main() {
// We're not interested in changing the alpha value
f_color = vec4(v_color, 1.0);
}
''',
)
# We control the 'in_vert' and `in_color' variables
vao = ctx.vertex_array(
prog,
[
# Map in_vert to the first 2 floats
# Map in_color to the next 3 floats
(vbo, '2f 3f', 'in_vert', 'in_color')
],
)
def render(window):
#window.ctx.clear(0.5,0.2,0.1)
vao.render()
while not window.is_closing:
window.clear()
render(window)
window.swap_buffers()