forked from dantros/grafica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_scene_graph_3dcars.py
198 lines (143 loc) · 6.03 KB
/
ex_scene_graph_3dcars.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
# coding=utf-8
"""Drawing 3D cars via scene graph"""
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.transformations as tr
import grafica.basic_shapes as bs
import grafica.scene_graph as sg
import grafica.easy_shaders as es
import grafica.performance_monitor as pm
__author__ = "Daniel Calderon"
__license__ = "MIT"
# A class to store the application control
class Controller:
def __init__(self):
self.fillPolygon = True
self.showAxis = 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.fillPolygon = not controller.fillPolygon
elif key == glfw.KEY_LEFT_CONTROL:
controller.showAxis = not controller.showAxis
elif key == glfw.KEY_ESCAPE:
glfw.set_window_should_close(window, True)
else:
print('Unknown key')
def createCar(pipeline, r, g, b):
# Creating shapes on GPU memory
blackCube = bs.createColorCube(0,0,0)
gpuBlackCube = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuBlackCube)
gpuBlackCube.fillBuffers(blackCube.vertices, blackCube.indices, GL_STATIC_DRAW)
chasisCube = bs.createColorCube(r,g,b)
gpuChasisCube = es.GPUShape().initBuffers()
pipeline.setupVAO(gpuChasisCube)
gpuChasisCube.fillBuffers(chasisCube.vertices, chasisCube.indices, GL_STATIC_DRAW)
# Cheating a single wheel
wheel = sg.SceneGraphNode("wheel")
wheel.transform = tr.scale(0.2, 0.8, 0.2)
wheel.childs += [gpuBlackCube]
wheelRotation = sg.SceneGraphNode("wheelRotation")
wheelRotation.childs += [wheel]
# Instanciating 2 wheels, for the front and back parts
frontWheel = sg.SceneGraphNode("frontWheel")
frontWheel.transform = tr.translate(0.3,0,-0.3)
frontWheel.childs += [wheelRotation]
backWheel = sg.SceneGraphNode("backWheel")
backWheel.transform = tr.translate(-0.3,0,-0.3)
backWheel.childs += [wheelRotation]
# Creating the chasis of the car
chasis = sg.SceneGraphNode("chasis")
chasis.transform = tr.scale(1,0.7,0.5)
chasis.childs += [gpuChasisCube]
# All pieces together
car = sg.SceneGraphNode("car")
car.childs += [chasis]
car.childs += [frontWheel]
car.childs += [backWheel]
return car
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
glfw.set_window_should_close(window, True)
width = 600
height = 600
title = "3D cars via scene graph"
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)
# Assembling the shader program (pipeline) with both shaders
mvpPipeline = es.SimpleModelViewProjectionShaderProgram()
# Telling OpenGL to use our shader program
glUseProgram(mvpPipeline.shaderProgram)
# Setting up the clear screen color
glClearColor(0.85, 0.85, 0.85, 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
cpuAxis = bs.createAxis(7)
gpuAxis = es.GPUShape().initBuffers()
mvpPipeline.setupVAO(gpuAxis)
gpuAxis.fillBuffers(cpuAxis.vertices, cpuAxis.indices, GL_STATIC_DRAW)
redCarNode = createCar(mvpPipeline, 1, 0, 0)
blueCarNode = createCar(mvpPipeline, 0, 0, 1)
blueCarNode.transform = np.matmul(tr.rotationZ(-np.pi/4), tr.translate(3.0,0,0.5))
# Using the same view and projection matrices in the whole application
projection = tr.perspective(45, float(width)/float(height), 0.1, 100)
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection)
view = tr.lookAt(
np.array([5,5,7]),
np.array([0,0,0]),
np.array([0,0,1])
)
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "view"), 1, GL_TRUE, view)
perfMonitor = pm.PerformanceMonitor(glfw.get_time(), 0.5)
# glfw will swap buffers as soon as possible
glfw.swap_interval(0)
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()
# 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)
if controller.showAxis:
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity())
mvpPipeline.drawCall(gpuAxis, GL_LINES)
# Moving the red car and rotating its wheels
redCarNode.transform = tr.translate(3 * np.sin( glfw.get_time() ),0,0.5)
redWheelRotationNode = sg.findNode(redCarNode, "wheelRotation")
redWheelRotationNode.transform = tr.rotationY(-10 * glfw.get_time())
# Uncomment to print the red car position on every iteration
#print(sg.findPosition(redCarNode, "car"))
# Drawing the Car
sg.drawSceneGraphNode(redCarNode, mvpPipeline, "model")
sg.drawSceneGraphNode(blueCarNode, mvpPipeline, "model")
# Once the render is done, buffers are swapped, showing only the complete scene.
glfw.swap_buffers(window)
# freeing GPU memory
gpuAxis.clear()
redCarNode.clear()
blueCarNode.clear()
glfw.terminate()