forked from ivansipiran/grafica
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex_4shapes.py
167 lines (125 loc) · 4.87 KB
/
ex_4shapes.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
# coding=utf-8
"""Drawing 4 shapes with different transformations"""
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.basic_shapes as bs
import grafica.easy_shaders as es
import grafica.transformations as tr
import grafica.performance_monitor as pm
__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
# A class to store the application control
class Controller:
def __init__(self):
self.fillPolygon = True
# we will use the global controller as communication with the callback function
controller = Controller()
# This function will be executed whenever a key is pressed or released
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')
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
glfw.set_window_should_close(window, True)
# Creating a glfw window
width = 600
height = 600
title = "Displaying multiple shapes - Modern OpenGL"
window = glfw.create_window(width, height, title, 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
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)
perfMonitor = pm.PerformanceMonitor(glfw.get_time(), 0.5)
# glfw will swap buffers as soon as possible
glfw.swap_interval(0)
# Application loop
while not glfw.window_should_close(window):
# Measuring performance
perfMonitor.update(glfw.get_time())
glfw.set_window_title(window, title + str(perfMonitor))
# 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
glClear(GL_COLOR_BUFFER_BIT)
# Using the time as the theta parameter
theta = glfw.get_time()
# Triangle
triangleTransform = tr.matmul([
tr.translate(0.5, 0.5, 0),
tr.rotationZ(2 * theta),
tr.uniformScale(0.5)
])
# Updating the transform attribute
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, triangleTransform)
# Drawing function
pipeline.drawCall(gpuTriangle)
# Another instance of the triangle
triangleTransform2 = tr.matmul([
tr.translate(-0.5, 0.5, 0),
tr.scale(
0.5 + 0.2 * np.cos(1.5 * theta),
0.5 + 0.2 * np.sin(2 * theta),
0)
])
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, triangleTransform2)
pipeline.drawCall(gpuTriangle)
# Quad
quadTransform = tr.matmul([
tr.translate(-0.5, -0.5, 0),
tr.rotationZ(-theta),
tr.uniformScale(0.7)
])
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, quadTransform)
pipeline.drawCall(gpuQuad)
# Another instance of the Quad
quadTransform2 = tr.matmul([
tr.translate(0.5, -0.5, 0),
tr.shearing(0.3 * np.cos(theta), 0, 0, 0, 0, 0),
tr.uniformScale(0.7)
])
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "transform"), 1, GL_TRUE, quadTransform2)
pipeline.drawCall(gpuQuad)
# 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()
glfw.terminate()