forked from dantros/grafica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_quad.py
199 lines (142 loc) · 5.4 KB
/
ex_quad.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
# coding=utf-8
"""Drawing a Quad via a EBO"""
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
__author__ = "Daniel Calderon"
__license__ = "MIT"
# We will use 32 bits data, so floats and integers have 4 bytes
# 1 byte = 8 bits
SIZE_IN_BYTES = 4
# A class to store the application control
class Controller:
fillPolygon = True
# we will use the 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 createShaderProgram():
# Defining shaders for our pipeline
vertex_shader = """
#version 130
in vec3 position;
in vec3 color;
out vec3 fragColor;
void main()
{
fragColor = color;
gl_Position = vec4(position, 1.0f);
}
"""
fragment_shader = """
#version 130
in vec3 fragColor;
out vec4 outColor;
void main()
{
outColor = vec4(fragColor, 1.0f);
}
"""
# Assembling the shader program (pipeline) with both shaders
shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
return shaderProgram
def createQuad(shaderProgram):
# Defining locations and colors for each vertex of the shape
#####################################
vertexData = np.array([
# positions colors
-0.5, -0.5, 0.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.0, 1.0, 1.0, 1.0
# It is important to use 32 bits data
], dtype = np.float32)
# Defining connections among vertices
# We have a triangle every 3 indices specified
indices = np.array(
[0, 1, 2,
2, 3, 0], dtype= np.uint32)
size = len(indices)
# VAO, VBO and EBO and for the shape
#####################################
vao = glGenVertexArrays(1)
vbo = glGenBuffers(1)
ebo = glGenBuffers(1)
# Binding VBO and EBO to the VAO
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBindVertexArray(0)
# Setting up stride in the Vertex Attribute Object (VAO)
#####################################
glBindVertexArray(vao)
# Setting up the location of the attributes position and color from the VBO
# A vertex attribute has 3 integers for the position (each is 4 bytes),
# and 3 numbers to represent the color (each is 4 bytes),
# Henceforth, we have 3*4 + 3*4 = 24 bytes
position = glGetAttribLocation(shaderProgram, "position")
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 6 * SIZE_IN_BYTES, ctypes.c_void_p(0))
glEnableVertexAttribArray(position)
color = glGetAttribLocation(shaderProgram, "color")
glVertexAttribPointer(color, 3, GL_FLOAT, GL_FALSE, 6 * SIZE_IN_BYTES, ctypes.c_void_p(3 * SIZE_IN_BYTES))
glEnableVertexAttribArray(color)
# unbinding current vao
glBindVertexArray(0)
# Sending vertices and indices to GPU memory
#####################################
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, len(vertexData) * SIZE_IN_BYTES, vertexData, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, len(indices) * SIZE_IN_BYTES, indices, GL_STATIC_DRAW)
return vao, vbo, ebo, size
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
glfw.set_window_should_close(window, True)
width = 600
height = 600
window = glfw.create_window(width, height, "Drawing a quad via a EBO", 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)
# Creating our shader program and telling OpenGL to use it
shaderProgram = createShaderProgram()
glUseProgram(shaderProgram)
# Creating shapes on GPU memory
vao, vbo, ebo, size = createQuad(shaderProgram)
# Setting up the clear screen color
glClearColor(0.15, 0.15, 0.15, 1.0)
while not glfw.window_should_close(window):
# Using GLFW to check for input events
glfw.poll_events()
# Filling or not the shapes depending on the controller state
if (controller.fillPolygon):
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
else:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
# Clearing the screen in both, color and depth
glClear(GL_COLOR_BUFFER_BIT)
# Drawing the Quad as specified in the VAO with the active shader program
glBindVertexArray(vao)
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, None)
# Once the render is done, buffers are swapped, showing only the complete scene.
glfw.swap_buffers(window)
# freeing GPU memory
glDeleteBuffers(1, [ebo])
glDeleteBuffers(1, [vbo])
glDeleteVertexArrays(1, [vao])
glfw.terminate()