Repository navigation
Expand file tree
/
Copy pathview.py
More file actions
164 lines (126 loc) · 5.59 KB
/
Copy pathview.py
File metadata and controls
164 lines (126 loc) · 5.59 KB
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
import numpy as np
import glfw
import sys
import random
import imgui
from imgui.integrations.glfw import GlfwRenderer
from OpenGL.GL import *
import grafica.transformations as tr
import grafica.lighting_shaders as ls
import grafica.easy_shaders as es
import grafica.basic_shapes as bs
import grafica.scene_graph as sg
from controller import Controller
from model import Cube, create_gpu, AllModel
from utils import *
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
glfw.set_window_should_close(window, True)
controller = Controller()
width = 1200
height = 800
window = glfw.create_window(width, height, "Scene Graph Node", None, None)
if not window:
glfw.terminate()
glfw.set_window_should_close(window, True)
print("Could not initialize Window")
exit(1)
glfw.make_context_current(window)
# Creating our shader program and telling OpenGL to use it
pipeline = ls.SimplePhongShaderProgram()
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)
# Enabling transparencies
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Creating shapes on GPU memory
initial_cube = Cube(pipeline)
all_model = AllModel(initial_cube)
controller.set_shape(all_model) # todo change with many shapes
# initiliaze imgui context (see documentation)
imgui.create_context()
impl = GlfwRenderer(window)
# Connecting the callback function 'on_key' to handle keyboard events
glfw.set_key_callback(window, controller.on_key)
# Connecting the callback function 'cursor_pos_callback' to handle mouse events
glfw.set_cursor_pos_callback(window, controller.cursor_pos_callback)
# glfw will swap buffers as soon as possible
glfw.swap_interval(0) # TODO: buscar por qué
locationX = 0.0
locationY = 0.0
locationZ = 0.0
scaleX = 0.5
scaleY = 0.5
scaleZ = 0.5
angleX = 0.4
angleY = -0.6
angleZ = 0.4
color = (0.5, 0.5, 0.5)
# initial setup: ilumination
La, Ld, Ls, Ka, Kd, Ks, lightPos, shininess, constantAttenuation, linearAttenuation, quadraticAttenuation = \
setUpLightsDefault(pipeline)
# initial setup: view position
viewPos = controller.eye[0], controller.eye[1], controller.eye[2]
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)
impl.process_inputs()
imgui.new_frame()
# 3D transformation
locationX, locationY, locationZ, scaleX, scaleY, scaleZ, angleX, angleY, angleZ, color= \
controller.transformGuiOverlay(locationX, locationY, locationZ, scaleX, scaleY, scaleZ, angleX, angleY, angleZ, color, pipeline)
# ilumination
La, Ld, Ls, Ka, Kd, Ks, lightPos, viewPos, shininess, constantAttenuation, linearAttenuation, quadraticAttenuation =\
controller.lightGuiOverlay(La, Ld, Ls, Ka, Kd, Ks, lightPos, viewPos, shininess, constantAttenuation, linearAttenuation, quadraticAttenuation)
setUpLights(pipeline, La, Ld, Ls, Ka, Kd, Ks, lightPos, viewPos, shininess, constantAttenuation, linearAttenuation, quadraticAttenuation)
controller.sceneGraphGuiOverlay(pipeline)
imgui.render()
# Setting uniforms and drawing the Quad
rotationMatrixXY = np.matmul(
tr.rotationY(angleY),
tr.rotationX(angleX),
)
rotationMatrixXYZ = np.matmul(
tr.rotationZ(angleZ),
rotationMatrixXY
)
rotationAndScale = np.matmul(
rotationMatrixXYZ,
tr.scale(scaleX, scaleY, scaleZ)
)
transformMatrix = np.matmul(
tr.translate(locationX, locationY, locationZ),
rotationAndScale
)
modulationColor = color[0], color[1], color[2]
glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "modulationColor"),
*modulationColor)
view = tr.lookAt(controller.eye, controller.at, controller.up)
glUniform3f(glGetUniformLocation(pipeline.shaderProgram, "viewPosition"), controller.eye[0], controller.eye[1], controller.eye[2])
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "view"), 1, GL_TRUE, view)
glUniformMatrix4fv(glGetUniformLocation(pipeline.shaderProgram, "projection"), 1, GL_TRUE, controller.projection)
# Setting up the model
all_model.draw(pipeline, transformMatrix, controller.name_node_selected) #"cube_0"
# Saving lights setup
controller.save_setup_lights(La, Ld, Ls, Ka, Kd, Ks, lightPos, viewPos, shininess, constantAttenuation, linearAttenuation, quadraticAttenuation, modulationColor)
# Drawing the imgui texture over our drawing
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
impl.render(imgui.get_draw_data())
# Once the render is done, buffers are swapped, showing only the complete scene.
glfw.swap_buffers(window)
# freeing GPU memory
controller.clear()
impl.shutdown()
glfw.terminate()