-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gui.py
119 lines (85 loc) · 3.31 KB
/
Gui.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
import numpy as np
from OpenGL.GL import *
import OpenGL.GL.shaders as shaders
from OpenGL.arrays.numbers import NumberHandler as NH # used to pass number references to pyopengl
FLOATS_PER_POSITION = 2
FLOATS_PER_UV = 2
FLOATS_PER_VERTEX = FLOATS_PER_POSITION + FLOATS_PER_UV
class Gui:
def __init__(self):
vertShader = shaders.compileShader("""
#version 460
layout (location = 0) in vec2 i_pos;
layout (location = 1) in vec2 i_uv;
out vec2 uv;
void main() {
uv = i_uv;
gl_Position = vec4(i_pos, 0., 1.);
}
""", GL_VERTEX_SHADER)
fragShader = shaders.compileShader("""
#version 460
in vec2 uv;
out vec4 fragColor;
void main() {
// fragColor = vec4(uv, 0., 1.);
fragColor = vec4(vec3(1.), 1.);
}
""", GL_FRAGMENT_SHADER)
self.prog = shaders.compileProgram(vertShader, fragShader, validate=False)
# Create Vertex Array Object:
self.vao = GLuint(-1)
glCreateVertexArrays(1, NH().dataPointer(self.vao))
# Create vertex buffer:
self.vbo = GLuint(-1)
glCreateBuffers(1, NH().dataPointer(self.vbo))
# Create vertex buffer:
self.ebo = GLuint(-1)
glCreateBuffers(1, NH().dataPointer(self.ebo))
# link vao, vbo and ebo
glVertexArrayVertexBuffer(self.vao, 0, self.vbo, 0, FLOATS_PER_VERTEX * 4) # vao, bindIndex, buffer, offset, stride
glVertexArrayElementBuffer(self.vao, self.ebo) # vao, buffer
# configure vertex attributes:
glEnableVertexArrayAttrib(self.vao, 0)
glVertexArrayAttribFormat(self.vao, 0, FLOATS_PER_POSITION, GL_FLOAT, False, (0) * 4) # vao, attribIndex, size, type, normalized, offset
glVertexArrayAttribBinding(self.vao, 0, 0)
glVertexArrayAttribFormat(self.vao, 1, FLOATS_PER_UV, GL_FLOAT, False, (FLOATS_PER_POSITION) * 4) # vao, attribIndex, size, type, normalized, offset
glVertexArrayAttribBinding(self.vao, 1, 0) # vao, attribIndex, bindingIndex
glEnableVertexArrayAttrib(self.vao, 1) # vao, attribIndex
viewport = glGetIntegerv(GL_VIEWPORT) # [x0, y0, x1, y1]
px = 1. / viewport[2]
py = 1. / viewport[3]
self.vertices = np.array([
-px * 16, py * 3, 0., 0., # horizontal
px * 16, py * 3, 1., 0.,
-px * 16, -py * 3, 0., 1.,
px * 16, -py * 3, 1., 1.,
-px * 3, py * 16, 0., 0., # vertical
px * 3, py * 16, 1., 0.,
-px * 3, -py * 16, 0., 1.,
px * 3, -py * 16, 1., 1.,
], dtype=np.float32)
self.indices = np.array([
0, 1, 3, 0, 3, 2, # horizontal
4, 5, 7, 4, 7, 6, # vertical
], dtype=np.uint32)
glNamedBufferStorage(self.vbo, len(self.vertices) * 4, self.vertices, GL_DYNAMIC_STORAGE_BIT)
glNamedBufferStorage(self.ebo, len(self.indices) * 4, self.indices, GL_DYNAMIC_STORAGE_BIT)
def draw(self):
viewport = glGetIntegerv(GL_VIEWPORT) # [x0, y0, x1, y1]
px = 1. / viewport[2]
py = 1. / viewport[3]
# self.vertices = np.array([
# -px * 6, py * 2, 0., 0.,
# px * 6, py * 2, 1., 0.,
# -px * 6, -py * 2, 0., 1.,
# px * 6, -py * 2, 1., 1.,
# ], dtype=np.float32)
# self.indices = np.array([
# 0, 1, 3, 0, 3, 2,
# ], dtype=np.uint32)
# glNamedBufferStorage(self.vbo, len(self.vertices) * 4, self.vertices, GL_DYNAMIC_STORAGE_BIT)
# glNamedBufferStorage(self.ebo, len(self.indices) * 4, self.indices, GL_DYNAMIC_STORAGE_BIT)
glUseProgram(self.prog)
glBindVertexArray(self.vao)
glDrawElements(GL_TRIANGLES, len(self.indices), GL_UNSIGNED_INT, None)