-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_texture_cube.py
116 lines (83 loc) · 3.6 KB
/
ex_texture_cube.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
import glfw
from OpenGL.GL import *
import numpy as np
import sys
import transformations2 as tr2
import basic_shapes as bs
import easy_shaders as es
# A class to store the application control
class Controller:
def __init__(self):
self.fillPolygon = True
# 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:
sys.exit()
else:
print('Unknown key')
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
sys.exit()
width = 600
height = 600
window = glfw.create_window(width, height, "Cube with texture", None, None)
if not window:
glfw.terminate()
sys.exit()
glfw.make_context_current(window)
# Connecting the callback function 'on_key' to handle keyboard events
glfw.set_key_callback(window, on_key)
# Creating shader programs for textures and for colores
textureShaderProgram = es.SimpleTextureModelViewProjectionShaderProgram()
colorShaderProgram = es.SimpleModelViewProjectionShaderProgram()
# 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
gpuTextureCube = es.toGPUShape(bs.createTextureCube("img/bricks.png"), GL_REPEAT, GL_LINEAR)
gpuAxis = es.toGPUShape(bs.createAxis(100))
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 | GL_DEPTH_BUFFER_BIT)
projection = tr2.ortho(-1, 1, -1, 1, 0.1, 100)
view = tr2.lookAt(
np.array([10, 10, 5]),
np.array([0, 0, 0]),
np.array([0, 0, 1])
)
theta = glfw.get_time()
axis = np.array([1, -1, 1])
axis = axis / np.linalg.norm(axis)
model = tr2.rotationA(theta, axis)
# Telling OpenGL to use our shader program
glUseProgram(colorShaderProgram.shaderProgram)
glUniformMatrix4fv(glGetUniformLocation(colorShaderProgram.shaderProgram, "projection"), 1, GL_TRUE, projection)
glUniformMatrix4fv(glGetUniformLocation(colorShaderProgram.shaderProgram, "view"), 1, GL_TRUE, view)
glUniformMatrix4fv(glGetUniformLocation(colorShaderProgram.shaderProgram, "model"), 1, GL_TRUE, tr2.identity())
colorShaderProgram.drawShape(gpuAxis, GL_LINES)
# Drawing the Cube
glUseProgram(textureShaderProgram.shaderProgram)
glUniformMatrix4fv(glGetUniformLocation(textureShaderProgram.shaderProgram, "projection"), 1, GL_TRUE,
projection)
glUniformMatrix4fv(glGetUniformLocation(textureShaderProgram.shaderProgram, "view"), 1, GL_TRUE, view)
glUniformMatrix4fv(glGetUniformLocation(textureShaderProgram.shaderProgram, "model"), 1, GL_TRUE, model)
textureShaderProgram.drawShape(gpuTextureCube)
# Once the drawing is rendered, buffers are swap so an uncomplete drawing is never seen.
glfw.swap_buffers(window)
glfw.terminate()