-
Notifications
You must be signed in to change notification settings - Fork 18
/
glut_opengl_draw_elements.py
237 lines (183 loc) · 8.55 KB
/
glut_opengl_draw_elements.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Using offsets into the buffer in PyOpenGL calls
# https://stackoverflow.com/questions/6431103/using-offsets-into-the-buffer-in-pyopengl-calls
import os
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GL.shaders import *
from OpenGL.arrays import *
import numpy
import ctypes
class MyWindow:
__caption = 'OpenGL Window'
__vp_size = [800, 600]
__vp_valid = False
__glut_wnd = None
__glsl_vert = """
#version 450 core
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec4 a_col;
out vec4 v_color;
void main()
{
v_color = a_col;
gl_Position = vec4(a_pos.xyz, 1.0);
}
"""
__glsl_frag = """
#version 450 core
out vec4 frag_color;
in vec4 v_color;
void main()
{
frag_color = v_color;
}
"""
__program = None
__vao = None
__vbo = None
def __init__(self, w, h):
self.__vp_size = [w, h]
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(self.__vp_size[0], self.__vp_size[1])
__glut_wnd = glutCreateWindow(self.__caption)
self.__program = compileProgram(
compileShader( self.__glsl_vert, GL_VERTEX_SHADER ),
compileShader( self.__glsl_frag, GL_FRAGMENT_SHADER ),
)
self.___attrib = { a : glGetAttribLocation (self.__program, a) for a in ['a_pos','a_col'] }
print(self.___attrib)
vertex_attributes = numpy.array([
# x y z R G B A
-0.5, -0.5, 0, 1, 0, 0, 1,
0.5, -0.5, 0, 1, 1, 0, 1,
0.5, 0.5, 0, 0, 0, 1, 1,
-0.5, 0.5, 0, 0, 1, 0, 1
], dtype=numpy.float32)
indices = [0, 1, 2, 0, 2, 3]
self.indexArray = (ctypes.c_uint32 * 6)(*indices)
#self.indexArray = numpy.array(indices, dtype=numpy.uint32)
self.__vbo = glGenBuffers(2)
glBindBuffer(GL_ARRAY_BUFFER, self.__vbo[0])
glBufferData(GL_ARRAY_BUFFER, vertex_attributes, GL_STATIC_DRAW)
self.__vao = glGenVertexArrays(1)
glBindVertexArray(self.__vao)
glVertexAttribPointer(0, 3, GL_FLOAT, False, 7*vertex_attributes.itemsize, None)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 4, GL_FLOAT, False, 7*vertex_attributes.itemsize, ctypes.c_void_p(3*vertex_attributes.itemsize))
glEnableVertexAttribArray(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.indexArray, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
self.__darw_indirect_bo = glGenBuffers(2)
"""
typedef struct {
uint count;
uint instanceCount;
uint firstIndex;
uint baseVertex;
uint baseInstance;
} DrawElementsIndirectCommand;
"""
indirect_1 = [6, 1, 0, 0, 0]
indirect_array_1 = (ctypes.c_uint32 * len(indirect_1))(*indirect_1)
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, self.__darw_indirect_bo[0])
glBufferData(GL_DRAW_INDIRECT_BUFFER, indirect_array_1, GL_STATIC_DRAW)
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0)
indirect_2 = [3, 1, 0, 0, 0, 3, 1, 3, 0, 0]
indirect_array_2 = (ctypes.c_uint32 * len(indirect_2))(*indirect_2)
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, self.__darw_indirect_bo[1])
glBufferData(GL_DRAW_INDIRECT_BUFFER, indirect_array_2, GL_STATIC_DRAW)
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0)
glUseProgram(self.__program)
glutReshapeFunc(self.__reshape)
glutDisplayFunc(self.__mainloop)
def run(self):
glutMainLoop()
def __reshape(self, w, h):
self.__vp_valid = False
def __mainloop(self):
if not self.__vp_valid:
self.__vp_size = [glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT)]
self.__vp_valid = True
glViewport(0, 0, self.__vp_size[0], self.__vp_size[1])
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
index_case = 12
if index_case == 0:
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, self.indexArray)
elif index_case == 1:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
elif index_case == 2:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, ctypes.c_void_p(3 * 4))
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
elif index_case == 3:
indexArray1 = (ctypes.c_uint32 * 3)(0, 1, 2)
indexArray2 = (ctypes.c_uint32 * 3)(0, 2, 3)
#indexArray1 = numpy.array([0, 1, 2], dtype=numpy.uint32)
#indexArray2 = numpy.array([0, 2, 3], dtype=numpy.uint32)
#offset = numpy.array([3, 3], dtype=numpy.uint32)
counts = [3, 3]
indexPtr = (ctypes.c_void_p * 2)(ctypes.addressof(indexArray1), ctypes.addressof(indexArray2))
#indexPtr = numpy.array([indexArray1.ctypes.data, indexArray2.ctypes.data], dtype=numpy.intp)
glMultiDrawElements(GL_TRIANGLES, counts, GL_UNSIGNED_INT, indexPtr, 2)
elif index_case == 4:
indexArray1 = (GLuint * 3)(0, 1, 2)
indexArray2 = (GLuint * 3)(0, 2, 3)
counts = [3, 3]
indexPtr = (GLvoidp * 2)(ctypes.addressof(indexArray1), ctypes.addressof(indexArray2))
glMultiDrawElements(GL_TRIANGLES, counts, GL_UNSIGNED_INT, indexPtr, 2)
elif index_case == 5:
counts = [3, 3]
indexPtr = (ctypes.c_void_p * 2)(0, 3 * 4)
#indexPtr = numpy.array([0, 3 * 4], dtype=numpy.intp)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glMultiDrawElements(GL_TRIANGLES, counts, GL_UNSIGNED_INT, indexPtr, 2)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
elif index_case == 6:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElementsBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None, 0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
if index_case == 7:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None, 1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
if index_case == 8:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElementsInstancedBaseVertex(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None, 1, 0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
if index_case == 9:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElementsInstancedBaseInstance(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None, 1, 0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
if index_case == 10:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None, 1, 0, 0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
elif index_case == 11:
# GLAPI/glDrawElementsIndirect
# https://www.khronos.org/opengl/wiki/GLAPI/glDrawElementsIndirect
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, self.__darw_indirect_bo[0])
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, None)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0)
elif index_case == 12:
# GLAPI/glMultiDrawElementsIndirect
# https://www.khronos.org/opengl/wiki/GLAPI/glMultiDrawElementsIndirect
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, self.__darw_indirect_bo[1])
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__vbo[1])
glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, None, 2, 4*5)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0)
elif index_case == 13:
first = [0, 1]
counts = [3, 3]
glMultiDrawArrays(GL_TRIANGLES, first, counts, 2)
glutSwapBuffers()
glutPostRedisplay()
window = MyWindow(800, 600)
window.run()