forked from ivansipiran/grafica
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex_transformations_showcase.py
260 lines (189 loc) · 7.21 KB
/
ex_transformations_showcase.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
# coding=utf-8
"""Ilustrating different transformations"""
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy as np
import sys, os.path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import grafica.basic_shapes as bs
import grafica.easy_shaders as es
import grafica.transformations as tr
__author__ = "Daniel Calderon"
__license__ = "MIT"
# We will use 32 bits data, so an integer has 4 bytes
# 1 byte = 8 bits
SIZE_IN_BYTES = 4
# Transformation states that will operate over the shape
TR_STANDARD = 0
TR_ROTATE_ZP = 1
TR_ROTATE_ZM = 2
TR_TRANSLATE = 3
TR_UNIFORM_SCALE = 4
TR_NONUNIF_SCALE = 5
TR_REFLEX_Y = 6
TR_SHEARING_XY = 7
# Shapes
SP_TRIANGLE = 0
SP_QUAD = 1
SP_CUBE = 2
SP_CIRCLE = 3
# A class to store the application control
class Controller:
showTransform = TR_STANDARD
fillPolygon = True
shape = SP_TRIANGLE
animated = False
# we will use the global controller as communication with the callback function
controller = Controller()
def getTransform(showTransform, theta):
if showTransform == TR_STANDARD:
return tr.identity()
elif showTransform == TR_ROTATE_ZP:
return tr.rotationZ(theta)
elif showTransform == TR_ROTATE_ZM:
return tr.rotationZ(-theta)
elif showTransform == TR_TRANSLATE:
return tr.translate(0.3 * np.cos(theta), 0.3 * np.cos(theta), 0)
elif showTransform == TR_UNIFORM_SCALE:
return tr.uniformScale(0.7 + 0.5 * np.cos(theta))
elif showTransform == TR_NONUNIF_SCALE:
return tr.scale(
1.0 - 0.5 * np.cos(1.5 * theta),
1.0 + 0.5 * np.cos(2 * theta),
1.0)
elif showTransform == TR_REFLEX_Y:
return tr.scale(1,-1,1)
elif showTransform == TR_SHEARING_XY:
return tr.shearing(0.3 * np.cos(theta), 0, 0, 0, 0, 0)
else:
# This should NEVER happend
raise Exception()
def on_key(window, key, scancode, action, mods):
if action != glfw.PRESS:
return
global controller
if key == glfw.KEY_0:
print("No transformations applied")
controller.showTransform = TR_STANDARD
elif key == glfw.KEY_1:
print('Rotation Z+')
controller.showTransform = TR_ROTATE_ZP
elif key == glfw.KEY_2:
print('Rotation Z-')
controller.showTransform = TR_ROTATE_ZM
elif key == glfw.KEY_3:
print('Uniform Scaling')
controller.showTransform = TR_UNIFORM_SCALE
elif key == glfw.KEY_4:
print('Non-uniform Scaling')
controller.showTransform = TR_NONUNIF_SCALE
elif key == glfw.KEY_5:
print('Translation')
controller.showTransform = TR_TRANSLATE
elif key == glfw.KEY_6:
print('Shearing XY')
controller.showTransform = TR_SHEARING_XY
elif key == glfw.KEY_7:
print('Reflexion Y')
controller.showTransform = TR_REFLEX_Y
elif key == glfw.KEY_Q:
print('Showing triangle')
controller.shape = SP_TRIANGLE
elif key == glfw.KEY_W:
print('Showing quad')
controller.shape = SP_QUAD
elif key == glfw.KEY_E:
print('Showing cube')
controller.shape = SP_CUBE
elif key == glfw.KEY_R:
print('Showing circle')
controller.shape = SP_CIRCLE
elif key == glfw.KEY_A:
print('Toggling animated')
controller.animated = not controller.animated
elif 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. Try small numbers!')
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, "Basic Linear Transformations - Modern OpenGL", 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
pipeline = es.SimpleTransformShaderProgram()
glUseProgram(pipeline.shaderProgram)
# Setting up the clear screen color
glClearColor(0.15, 0.15, 0.15, 1.0)
# As we work in 3D, we need to check which part is in front,
# and which one is at the back
glEnable(GL_DEPTH_TEST)
# Creating shapes on GPU memory
shapeTriangle = bs.createRainbowTriangle()
gpuTriangle = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuTriangle)
gpuTriangle.fillBuffers(shapeTriangle.vertices, shapeTriangle.indices, GL_STATIC_DRAW)
shapeQuad = bs.createRainbowQuad()
gpuQuad = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuQuad)
gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW)
shapeCube = bs.createRainbowCube()
gpuCube = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuCube)
gpuCube.fillBuffers(shapeCube.vertices, shapeCube.indices, GL_STATIC_DRAW)
shapeCircle = bs.createRainbowCircle(20)
gpuCircle = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuCircle)
gpuCircle.fillBuffers(shapeCircle.vertices, shapeCircle.indices, GL_STATIC_DRAW)
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)
# Clearing the screen in both, color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
if controller.animated:
# Using the time as the theta parameter
theta = glfw.get_time()
else:
theta = np.pi / 6
transform = getTransform(controller.showTransform, theta)
if (controller.shape == SP_TRIANGLE):
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, transform)
pipeline.drawCall(gpuTriangle)
elif (controller.shape == SP_QUAD):
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, transform)
pipeline.drawCall(gpuQuad)
elif (controller.shape == SP_CUBE):
Rx = tr.rotationX(np.pi/3)
Ry = tr.rotationY(np.pi/3)
transform = tr.matmul([Ry, Rx, transform])
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, transform)
pipeline.drawCall(gpuCube)
elif (controller.shape == SP_CIRCLE):
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, transform)
pipeline.drawCall(gpuCircle)
else:
# This should never happen
raise Exception()
# Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen.
glfw.swap_buffers(window)
# freeing GPU memory
gpuTriangle.clear()
gpuQuad.clear()
gpuCube.clear()
gpuCircle.clear()
glfw.terminate()