forked from dantros/grafica
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ex_projections.py
208 lines (146 loc) · 6.43 KB
/
ex_projections.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
# coding=utf-8
"""Projections example"""
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"
PROJECTION_ORTHOGRAPHIC = 0
PROJECTION_FRUSTUM = 1
PROJECTION_PERSPECTIVE = 2
# A class to store the application control
class Controller:
def __init__(self):
self.fillPolygon = True
self.projection = PROJECTION_ORTHOGRAPHIC
# 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_1:
print('Orthographic projection')
controller.projection = PROJECTION_ORTHOGRAPHIC
elif key == glfw.KEY_2:
print('Frustum projection')
controller.projection = PROJECTION_FRUSTUM
elif key == glfw.KEY_3:
print('Perspective projection')
controller.projection = PROJECTION_PERSPECTIVE
elif key == glfw.KEY_ESCAPE:
glfw.set_window_should_close(window, True)
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, "Projections Demo", 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)
# Assembling the shader program
pipeline = es.SimpleModelViewProjectionShaderProgram()
# Telling OpenGL to use our shader program
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)
# Convenience function to ease initialization
def createGPUShape(shape):
gpuShape = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuShape)
gpuShape.fillBuffers(shape.vertices, shape.indices, GL_STATIC_DRAW)
return gpuShape
# Creating shapes on GPU memory
gpuAxis = createGPUShape(bs.createAxis(7))
gpuRedCube = createGPUShape(bs.createColorCube(1,0,0))
gpuGreenCube = createGPUShape(bs.createColorCube(0,1,0))
gpuBlueCube = createGPUShape(bs.createColorCube(0,0,1))
gpuYellowCube = createGPUShape(bs.createColorCube(1,1,0))
gpuCyanCube = createGPUShape(bs.createColorCube(0,1,1))
gpuPurpleCube = createGPUShape(bs.createColorCube(1,0,1))
gpuRainbowCube = createGPUShape(bs.createRainbowCube())
t0 = glfw.get_time()
camera_theta = np.pi/4
while not glfw.window_should_close(window):
# Using GLFW to check for input events
glfw.poll_events()
# Getting the time difference from the previous iteration
t1 = glfw.get_time()
dt = t1 - t0
t0 = t1
if (glfw.get_key(window, glfw.KEY_LEFT) == glfw.PRESS):
camera_theta -= 2 * dt
if (glfw.get_key(window, glfw.KEY_RIGHT) == glfw.PRESS):
camera_theta += 2* dt
# Setting up the view transform
camX = 10 * np.sin(camera_theta)
camY = 10 * np.cos(camera_theta)
viewPos = np.array([camX, camY, 10])
view = tr.lookAt(
viewPos,
np.array([0,0,0]),
np.array([0,0,1])
)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE, view)
# Setting up the projection transform
if controller.projection == PROJECTION_ORTHOGRAPHIC:
projection = tr.ortho(-8, 8, -8, 8, 0.1, 100)
elif controller.projection == PROJECTION_FRUSTUM:
projection = tr.frustum(-5, 5, -5, 5, 9, 100)
elif controller.projection == PROJECTION_PERSPECTIVE:
projection = tr.perspective(60, float(width)/float(height), 0.1, 100)
else:
raise Exception()
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "projection"), 1, GL_TRUE, projection)
# Clearing the screen in both, color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# 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)
# Drawing shapes with different model transformations
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(5,0,0))
pipeline.drawCall(gpuRedCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(-5,0,0))
pipeline.drawCall(gpuGreenCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,5,0))
pipeline.drawCall(gpuBlueCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,-5,0))
pipeline.drawCall(gpuYellowCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,0,5))
pipeline.drawCall(gpuCyanCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.translate(0,0,-5))
pipeline.drawCall(gpuPurpleCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity())
pipeline.drawCall(gpuRainbowCube)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity())
pipeline.drawCall(gpuAxis, GL_LINES)
# Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen.
glfw.swap_buffers(window)
# freeing GPU memory
gpuAxis.clear()
gpuRedCube.clear()
gpuGreenCube.clear()
gpuBlueCube.clear()
gpuYellowCube.clear()
gpuCyanCube.clear()
gpuPurpleCube.clear()
gpuRainbowCube.clear()
glfw.terminate()