-
Notifications
You must be signed in to change notification settings - Fork 46
/
ex_binding_textures.py
239 lines (173 loc) · 7.12 KB
/
ex_binding_textures.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
# coding=utf-8
"""Using 2 different textures in the same Fragment Shader"""
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
import sys
import os.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import grafica.transformations as tr
import grafica.basic_shapes as bs
import grafica.easy_shaders as es
from grafica.assets_path import getAssetPath
from grafica.gpu_shape import GPUShape, SIZE_IN_BYTES
__author__ = "Sebastián Olmos"
__license__ = "MIT"
# We extend the functionality of a GPUShape with an additional texture.
class TexGPUShape(GPUShape):
def __init__(self):
"""VAO, VBO, EBO and texture handlers to GPU memory"""
super().__init__()
self.texture2 = None
def __str__(self):
return super().__str__() + " tex=" + str(self.texture2)
def clear(self):
"""Freeing GPU memory"""
super().clear()
if self.texture2 != None:
glDeleteTextures(1, [self.texture2])
# Shader that handles two textures
class DoubleTextureTransformShaderProgram:
def __init__(self):
vertex_shader = """
#version 330
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 330
// gl_FragCoord contains the (x,y) fragment coordinates of the window.
// We also set the origin to the upper left corner
layout(origin_upper_left) in vec4 gl_FragCoord;
in vec2 outTexCoords;
out vec4 outColor;
uniform sampler2D upTexture;
uniform sampler2D downTexture;
uniform float mousePosY;
void main()
{
vec4 finalColor;
if ( gl_FragCoord.y > mousePosY){
finalColor = texture(downTexture, outTexCoords);
}
else {
finalColor = texture(upTexture, outTexCoords);
}
outColor = finalColor;
}
"""
# Compiling our shader program
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 setupVAO(self, gpuShape):
glBindVertexArray(gpuShape.vao)
glBindBuffer(GL_ARRAY_BUFFER, gpuShape.vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gpuShape.ebo)
# 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(3 * SIZE_IN_BYTES))
glEnableVertexAttribArray(texCoords)
# Unbinding current vao
glBindVertexArray(0)
def drawCall(self, gpuShape, mode=GL_TRIANGLES):
assert isinstance(gpuShape, TexGPUShape)
glBindVertexArray(gpuShape.vao)
# Binding the first texture
glActiveTexture(GL_TEXTURE0 + 0)
glBindTexture(GL_TEXTURE_2D, gpuShape.texture)
# Binding the second texture
glActiveTexture(GL_TEXTURE0 + 1)
glBindTexture(GL_TEXTURE_2D, gpuShape.texture2)
glDrawElements(mode, gpuShape.size, GL_UNSIGNED_INT, None)
# Unbind the current VAO
glBindVertexArray(0)
# A class to store the application control
class Controller:
def __init__(self):
self.fillPolygon = True
self.mousePos = (0.0, 0.0)
# global controller as communication with the callback function
controller = Controller()
def on_key(window, key, scancode, action, mods):
if action != glfw.PRESS:
return
global controller
if key == glfw.KEY_SPACE:
controller.fillPolygon = not controller.fillPolygon
elif key == glfw.KEY_ESCAPE:
glfw.set_window_should_close(window, True)
else:
print('Unknown key')
def cursor_pos_callback(window, x, y):
global controller
controller.mousePos = (x,y)
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
sys.exit(1)
width = 600
height = 600
window = glfw.create_window(width, height, "Double binding", None, None)
if not window:
glfw.terminate()
glfw.set_window_should_close(window, True)
glfw.make_context_current(window)
# Connecting the callback function 'on_key' to handle keyboard events
glfw.set_key_callback(window, on_key)
glfw.set_cursor_pos_callback(window, cursor_pos_callback)
# A simple shader program with position and texture coordinates as inputs.
pipeline = DoubleTextureTransformShaderProgram()
# Telling OpenGL to use our shader program
# Setting up the clear screen color
glClearColor(0.25, 0.25, 0.25, 1.0)
# Creating shapes on GPU memory
shape = bs.createTextureQuad(1, 1)
gpuShape = TexGPUShape().initBuffers()
pipeline.setupVAO(gpuShape)
gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW)
gpuShape.texture = es.textureSimpleSetup(
getAssetPath("torres-del-paine-sq.jpg"), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR)
gpuShape.texture2 = es.textureSimpleSetup(
getAssetPath("red_woodpecker.jpg"), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, GL_LINEAR, GL_LINEAR)
currentMousePos = [width/2, height/2]
while not glfw.window_should_close(window):
# Using GLFW to check for input events
glfw.poll_events()
if (controller.fillPolygon):
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
else:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
theta = 0.3 * np.sin(glfw.get_time())
# Clearing the screen in both, color and depth
glClear(GL_COLOR_BUFFER_BIT)
glUseProgram(pipeline.shaderProgram)
# Drawing the shapes
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE,
np.matmul(
tr.shearing(0,theta,0,0,0,0),
tr.uniformScale(1.5)
)
)
# Binding samplers to both texture units
glUniform1i(glGetUniformLocation(pipeline.shaderProgram, "upTexture"), 0)
glUniform1i(glGetUniformLocation(pipeline.shaderProgram, "downTexture"), 1)
# Sending the mouse vertical location to our shader
glUniform1f(glGetUniformLocation(pipeline.shaderProgram, "mousePosY"), controller.mousePos[1])
pipeline.drawCall(gpuShape)
# Once the render is done, buffers are swapped, showing only the complete scene.
glfw.swap_buffers(window)
# freeing GPU memory
gpuShape.clear()
glfw.terminate()