-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_utils.py
189 lines (152 loc) · 6.21 KB
/
gl_utils.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
from OpenGL.GL import *
import numpy as np
import cv2
def compile_shader(source, shader_type):
shader = glCreateShader(shader_type)
glShaderSource(shader, source)
glCompileShader(shader)
if not glGetShaderiv(shader, GL_COMPILE_STATUS):
error = glGetShaderInfoLog(shader).decode()
print('Shader compilation failed:', error)
glDeleteShader(shader)
return None
return shader
def create_shader_program(vertex_source, fragment_source):
vertex_shader = compile_shader(vertex_source, GL_VERTEX_SHADER)
fragment_shader = compile_shader(fragment_source, GL_FRAGMENT_SHADER)
if vertex_shader is None or fragment_shader is None:
return None
shader_program = glCreateProgram()
glAttachShader(shader_program, vertex_shader)
glAttachShader(shader_program, fragment_shader)
glLinkProgram(shader_program)
if not glGetProgramiv(shader_program, GL_LINK_STATUS):
error = glGetProgramInfoLog(shader_program).decode()
print('Shader program linking failed:', error)
glDeleteProgram(shader_program)
if vertex_shader:
glDeleteShader(vertex_shader)
if fragment_shader:
glDeleteShader(fragment_shader)
return None
glDetachShader(shader_program, vertex_shader)
glDetachShader(shader_program, fragment_shader)
glDeleteShader(vertex_shader)
glDeleteShader(fragment_shader)
return shader_program
def create_framebuffer(texture_id):
fbo_id = glGenFramebuffers(1)
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_id, 0)
if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
raise RuntimeError("Framebuffer is not complete")
glBindFramebuffer(GL_FRAMEBUFFER, 0)
return fbo_id
def create_texture(width, height):
texture_id = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture_id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, None)
glBindTexture(GL_TEXTURE_2D, 0)
return texture_id
def load_texture_from_cv_image(cv_image):
image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
textureID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, textureID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
height, width, channels = image.shape
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image)
# glGenerateMipmap(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)
return textureID
def setup_quad_vertices():
vertices = np.array([
# Positions # Texture Coords
-1.0, -1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0,
-1.0, 1.0, 0.0, 0.0,
1.0, 1.0, 1.0, 0.0
], dtype=np.float32)
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * vertices.itemsize, None)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * vertices.itemsize, ctypes.c_void_p(2 * vertices.itemsize))
glEnableVertexAttribArray(1)
return VAO
def setup_displace_vertices():
vertices = np.array([
# Positions # Texture Coords
-1.0, -1.0, 0.0, 0.0,
1.0, -1.0, 1.0, 0.0,
-1.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0
], dtype=np.float32)
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * vertices.itemsize, None)
glEnableVertexAttribArray(0)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * vertices.itemsize, ctypes.c_void_p(2 * vertices.itemsize))
glEnableVertexAttribArray(1)
return VAO
def setup_quilt_vertices():
vertices = np.array([
-1.0, -1.0, # Bottom left
1.0, -1.0, # Bottom right
-1.0, 1.0, # Top left
1.0, 1.0 # Top right
], dtype=np.float32)
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * vertices.itemsize, None)
glEnableVertexAttribArray(0)
return VAO
def create_grid_vao(width, height):
vertices = []
uvs = []
for y in range(height):
for x in range(width):
# Coordinates normalized between -1 and 1
x0 = x / width * 2 - 1
x1 = (x + 1) / width * 2 - 1
y0 = y / height * 2 - 1
y1 = (y + 1) / height * 2 - 1
quad_vertices = [
x0, y0, 0, x1, y0, 0, x1, y1, 0,
x0, y0, 0, x1, y1, 0, x0, y1, 0
]
vertices.extend(quad_vertices)
quad_uvs = [
0, 0, 1, 0, 1, 1,
0, 0, 1, 1, 0, 1
]
uvs.extend(quad_uvs)
vertices = np.array(vertices, dtype=np.float32)
uvs = np.array(uvs, dtype=np.float32)
vao = glGenVertexArrays(1)
glBindVertexArray(vao)
vertex_vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vertex_vbo)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(0)
uv_vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, uv_vbo)
glBufferData(GL_ARRAY_BUFFER, uvs.nbytes, uvs, GL_STATIC_DRAW)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(1)
glBindVertexArray(0)
return vao, vertex_vbo, uv_vbo