forked from dantros/grafica
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ex_quad_controlled.py
125 lines (87 loc) · 3.17 KB
/
ex_quad_controlled.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
# coding=utf-8
"""Controlling the movement of a quad"""
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.transformations as tr
import grafica.basic_shapes as bs
import grafica.easy_shaders as es
__author__ = "Daniel Calderon"
__license__ = "MIT"
# A class to store the application control
class Controller:
x = 0.0
y = 0.0
theta = 0.0
rotate = 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.rotate = not controller.rotate
elif key == glfw.KEY_LEFT:
controller.x -= 0.1
elif key == glfw.KEY_RIGHT:
controller.x += 0.1
elif key == glfw.KEY_UP:
controller.y += 0.1
elif key == glfw.KEY_DOWN:
controller.y -= 0.1
elif key == glfw.KEY_ESCAPE:
glfw.set_window_should_close(window, True)
else:
print('Unknown key')
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, "Mind Controlled Quad", 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)
# Creating shapes on GPU memory
shapeQuad = bs.createRainbowQuad()
gpuQuad = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuQuad)
gpuQuad.fillBuffers(shapeQuad.vertices, shapeQuad.indices, GL_STATIC_DRAW)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
t0 = glfw.get_time()
while not glfw.window_should_close(window):
# Getting the time difference from the previous iteration
t1 = glfw.get_time()
dt = t1 - t0
t0 = t1
# Using GLFW to check for input events
glfw.poll_events()
# Clearing the screen in both, color and depth
glClear(GL_COLOR_BUFFER_BIT)
# theta is modified an amount proportional to the time spent in a loop iteration
if controller.rotate:
controller.theta += dt
# Drawing the Quad with the given transformation
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, np.matmul(
tr.translate(controller.x, controller.y, 0.0),
tr.rotationZ(controller.theta)
))
pipeline.drawCall(gpuQuad)
# Once the render is done, buffers are swapped, showing only the complete scene.
glfw.swap_buffers(window)
# freeing GPU memory
gpuQuad.clear()
glfw.terminate()