-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgldemo.py
executable file
·131 lines (106 loc) · 3.58 KB
/
gldemo.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
import math
import time
import numpy as np
import pygame as pg
import OpenGL
from OpenGL import GL
from OpenGL.GL import *
from OpenGL.GL import shaders
from OpenGL.arrays.vbo import VBO
import glm
W, H = 640, 480
VERT_SHADER = b"""
#version 330 core
layout (location = 0) in vec3 aPosition;
layout (location = 1) in vec3 aColor;
out vec3 Color;
uniform mat4 projection;
uniform mat4 modelview;
void main() {
gl_Position = projection * modelview * vec4(aPosition, 1.0);
Color = aColor;
}
"""
FRAG_SHADER = b"""
#version 330 core
out vec4 FragColor;
in vec3 Color;
void main() {
FragColor = vec4(Color, 1.0);
}
"""
Vertex = np.dtype([('position', np.float32, 3),
('color', np.float32, 3)])
def offsetof(dtype, field):
if isinstance(field, int):
field = dtype.names[field]
return dtype.fields[field][1]
vertices = np.array([
([-0.5, -0.43, 0.], [1., 0., 0.]),
([ 0.5, -0.43, 0.], [0., 1., 0.]),
([ 0.0, 0.43, 0.], [0., 0., 1.]),
], dtype=Vertex)
indices = np.array([0, 1, 2], np.uint32)
def main():
pg.init()
pg.display.gl_set_attribute(pg.GL_CONTEXT_PROFILE_MASK,
pg.GL_CONTEXT_PROFILE_CORE)
pg.display.gl_set_attribute(pg.GL_CONTEXT_MAJOR_VERSION, 3)
pg.display.gl_set_attribute(pg.GL_CONTEXT_MINOR_VERSION, 3)
screen = pg.display.set_mode(
(W, H), pg.OPENGL | pg.DOUBLEBUF | pg.RESIZABLE)
vertShader = shaders.compileShader(VERT_SHADER, GL_VERTEX_SHADER)
fragShader = shaders.compileShader(FRAG_SHADER, GL_FRAGMENT_SHADER)
shader = shaders.compileProgram(vertShader, fragShader)
glUseProgram(shader)
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
vbo = VBO(vertices, GL_STATIC_DRAW)
ebo = VBO(indices, GL_STATIC_DRAW, GL_ELEMENT_ARRAY_BUFFER)
with vbo, ebo:
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)
glVertexAttribPointer(0, 3, GL_FLOAT, False, Vertex.itemsize,
vbo + offsetof(Vertex, 'position'))
glVertexAttribPointer(1, 3, GL_FLOAT, False, Vertex.itemsize,
vbo + offsetof(Vertex, 'color'))
glBindVertexArray(0)
clock = pg.time.Clock()
while True:
clock.tick(30)
glClearColor(.5, .5, .5, 1.)
glClear(GL_COLOR_BUFFER_BIT)
x, y, w, h = glGetInteger(GL_VIEWPORT)
projection = glm.perspective(math.pi/4, w/h, .1, 10)
glUniformMatrix4fv(glGetUniformLocation(shader, "projection"),
1, False, glm.value_ptr(projection))
view = glm.lookAt(
glm.vec3(0, 0, 2), glm.vec3(0, 0, 0), glm.vec3(0, 1, 0))
t = pg.time.get_ticks()/1000
modelview = glm.rotate(view, t*math.pi/2, [0, 1, 0])
glUniformMatrix4fv(glGetUniformLocation(shader, "modelview"),
1, False, glm.value_ptr(modelview))
glBindVertexArray(vao)
#glDrawArrays(GL_TRIANGLES, 0, len(vertices))
glDrawElements(GL_TRIANGLES, len(indices), GL_UNSIGNED_INT, ebo)
pg.display.flip()
if not process_events():
break
def process_events():
for e in pg.event.get():
if e.type == pg.QUIT:
return False
elif e.type == pg.KEYDOWN:
if e.key == pg.K_ESCAPE:
return False
if e.key == pg.K_F4 and e.mod & pg.KMOD_ALT:
return False
elif e.type == pg.VIDEORESIZE:
glViewport(0, 0, e.w, e.h)
return True
if __name__ == '__main__':
try:
main()
finally:
pg.display.quit()