-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_shaders.py
437 lines (318 loc) · 13.4 KB
/
easy_shaders.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# coding=utf-8
"""
Daniel Calderon, CC3501, 2019-2
Simple Shaders
"""
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
from PIL import Image
import basic_shapes as bs
# We will use 32 bits data, so we have 4 bytes
# 1 byte = 8 bits
SIZE_IN_BYTES = 4
# A simple class container to reference a shape on GPU memory
class GPUShape:
def __init__(self):
self.vao = 0
self.vbo = 0
self.ebo = 0
self.texture = 0
self.size = 0
def textureSimpleSetup(texture, imgName, wrapMode, filterMode):
# wrapMode: GL_REPEAT, GL_CLAMP_TO_EDGE
# filterMode: GL_LINEAR, GL_NEAREST
glBindTexture(GL_TEXTURE_2D, texture)
# texture wrapping params
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode)
# texture filtering params
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterMode)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterMode)
image = Image.open(imgName)
img_data = np.array(list(image.getdata()), np.uint8)
if image.mode == "RGB":
internalFormat = GL_RGB
format = GL_RGB
elif image.mode == "RGBA":
internalFormat = GL_RGBA
format = GL_RGBA
else:
print("Image mode not supported.")
raise Exception()
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, image.size[0], image.size[1], 0, format, GL_UNSIGNED_BYTE, img_data)
def toGPUShape(shape, wrapMode=None, filterMode=None):
assert isinstance(shape, bs.Shape)
vertexData = np.array(shape.vertices, dtype=np.float32)
indices = np.array(shape.indices, dtype=np.uint32)
# Here the new shape will be stored
gpuShape = GPUShape()
gpuShape.size = len(shape.indices)
gpuShape.vao = glGenVertexArrays(1)
gpuShape.vbo = glGenBuffers(1)
gpuShape.ebo = glGenBuffers(1)
# Vertex data must be attached to a Vertex Buffer Object (VBO)
glBindBuffer(GL_ARRAY_BUFFER, gpuShape.vbo)
glBufferData(GL_ARRAY_BUFFER, len(vertexData) * SIZE_IN_BYTES, vertexData, GL_STATIC_DRAW)
# Connections among vertices are stored in the Elements Buffer Object (EBO)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gpuShape.ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(indices) * SIZE_IN_BYTES, indices, GL_STATIC_DRAW)
if shape.textureFileName != None:
assert wrapMode != None and filterMode != None
gpuShape.texture = glGenTextures(1)
textureSimpleSetup(gpuShape.texture, shape.textureFileName, wrapMode, filterMode)
return gpuShape
class SimpleShaderProgram:
def __init__(self):
vertex_shader = """
#version 130
in vec3 position;
in vec3 color;
out vec3 newColor;
void main()
{
gl_Position = vec4(position, 1.0f);
newColor = color;
}
"""
fragment_shader = """
#version 130
in vec3 newColor;
out vec4 outColor;
void main()
{
outColor = vec4(newColor, 1.0f);
}
"""
self.shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
def drawShape(self, shape, mode=GL_TRIANGLES):
assert isinstance(shape, GPUShape)
# Binding the proper buffers
glBindVertexArray(shape.vao)
glBindBuffer(GL_ARRAY_BUFFER, shape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ebo)
# 3d vertices + rgb color specification => 3*4 + 3*4 = 24 bytes
position = glGetAttribLocation(self.shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(self.shaderProgram, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(color)
# Render the active element buffer with the active shader program
glDrawElements(mode, shape.size, GL_UNSIGNED_INT, None)
class SimpleTextureShaderProgram:
def __init__(self):
vertex_shader = """
#version 130
in vec3 position;
in vec2 texCoords;
out vec2 outTexCoords;
void main()
{
gl_Position = vec4(position, 1.0f);
outTexCoords = texCoords;
}
"""
fragment_shader = """
#version 130
in vec2 outTexCoords;
out vec4 outColor;
uniform sampler2D samplerTex;
void main()
{
outColor = texture(samplerTex, outTexCoords);
}
"""
self.shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
def drawShape(self, shape, mode=GL_TRIANGLES):
assert isinstance(shape, GPUShape)
# Binding the proper buffers
glBindVertexArray(shape.vao)
glBindBuffer(GL_ARRAY_BUFFER, shape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ebo)
glBindTexture(GL_TEXTURE_2D, shape.texture)
# 3d vertices + 2d texture coordinates => 3*4 + 2*4 = 20 bytes
position = glGetAttribLocation(self.shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
texCoords = glGetAttribLocation(self.shaderProgram, "texCoords")
glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
glEnableVertexAttribArray(texCoords)
# Render the active element buffer with the active shader program
glDrawElements(mode, shape.size, GL_UNSIGNED_INT, None)
class SimpleTransformShaderProgram:
def __init__(self):
vertex_shader = """
#version 130
uniform mat4 transform;
in vec3 position;
in vec3 color;
out vec3 newColor;
void main()
{
gl_Position = transform * vec4(position, 1.0f);
newColor = color;
}
"""
fragment_shader = """
#version 130
in vec3 newColor;
out vec4 outColor;
void main()
{
outColor = vec4(newColor, 1.0f);
}
"""
self.shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, OpenGL.GL.GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, OpenGL.GL.GL_FRAGMENT_SHADER))
def drawShape(self, shape, mode=GL_TRIANGLES):
assert isinstance(shape, GPUShape)
# Binding the proper buffers
glBindVertexArray(shape.vao)
glBindBuffer(GL_ARRAY_BUFFER, shape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ebo)
# 3d vertices + rgb color specification => 3*4 + 3*4 = 24 bytes
position = glGetAttribLocation(self.shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(self.shaderProgram, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(color)
# Render the active element buffer with the active shader program
glDrawElements(mode, shape.size, GL_UNSIGNED_INT, None)
class SimpleTextureTransformShaderProgram:
def __init__(self):
vertex_shader = """
#version 130
uniform mat4 transform;
in vec3 position;
in vec2 texCoords;
out vec2 outTexCoords;
void main()
{
gl_Position = transform * vec4(position, 1.0f);
outTexCoords = texCoords;
}
"""
fragment_shader = """
#version 130
in vec2 outTexCoords;
out vec4 outColor;
uniform sampler2D samplerTex;
void main()
{
outColor = texture(samplerTex, outTexCoords);
}
"""
self.shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
def drawShape(self, shape, mode=GL_TRIANGLES):
assert isinstance(shape, GPUShape)
# Binding the proper buffers
glBindVertexArray(shape.vao)
glBindBuffer(GL_ARRAY_BUFFER, shape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ebo)
glBindTexture(GL_TEXTURE_2D, shape.texture)
# 3d vertices + 2d texture coordinates => 3*4 + 2*4 = 20 bytes
position = glGetAttribLocation(self.shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
texCoords = glGetAttribLocation(self.shaderProgram, "texCoords")
glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
glEnableVertexAttribArray(texCoords)
# Render the active element buffer with the active shader program
glDrawElements(mode, shape.size, GL_UNSIGNED_INT, None)
class SimpleModelViewProjectionShaderProgram:
def __init__(self):
vertex_shader = """
#version 130
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
in vec3 position;
in vec3 color;
out vec3 newColor;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
newColor = color;
}
"""
fragment_shader = """
#version 130
in vec3 newColor;
out vec4 outColor;
void main()
{
outColor = vec4(newColor, 1.0f);
}
"""
self.shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, OpenGL.GL.GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, OpenGL.GL.GL_FRAGMENT_SHADER))
def drawShape(self, shape, mode=GL_TRIANGLES):
assert isinstance(shape, GPUShape)
# Binding the proper buffers
glBindVertexArray(shape.vao)
glBindBuffer(GL_ARRAY_BUFFER, shape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ebo)
# 3d vertices + rgb color specification => 3*4 + 3*4 = 24 bytes
position = glGetAttribLocation(self.shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(self.shaderProgram, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 24, ctypes.c_void_p(12))
glEnableVertexAttribArray(color)
# Render the active element buffer with the active shader program
glDrawElements(mode, shape.size, GL_UNSIGNED_INT, None)
class SimpleTextureModelViewProjectionShaderProgram:
def __init__(self):
vertex_shader = """
#version 130
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
in vec3 position;
in vec2 texCoords;
out vec2 outTexCoords;
void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
outTexCoords = texCoords;
}
"""
fragment_shader = """
#version 130
uniform sampler2D samplerTex;
in vec2 outTexCoords;
out vec4 outColor;
void main()
{
outColor = texture(samplerTex, outTexCoords);
}
"""
self.shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, OpenGL.GL.GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, OpenGL.GL.GL_FRAGMENT_SHADER))
def drawShape(self, shape, mode=GL_TRIANGLES):
assert isinstance(shape, GPUShape)
# Binding the proper buffers
glBindVertexArray(shape.vao)
glBindBuffer(GL_ARRAY_BUFFER, shape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shape.ebo)
glBindTexture(GL_TEXTURE_2D, shape.texture)
# 3d vertices + 2d texture coordinates => 3*4 + 2*4 = 20 bytes
position = glGetAttribLocation(self.shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
texCoords = glGetAttribLocation(self.shaderProgram, "texCoords")
glVertexAttribPointer(texCoords, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
glEnableVertexAttribArray(texCoords)
# Render the active element buffer with the active shader program
glDrawElements(mode, shape.size, GL_UNSIGNED_INT, None)