forked from dantros/grafica
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathex_lighting.py
264 lines (195 loc) · 8.98 KB
/
ex_lighting.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# coding=utf-8
"""Showing lighting effects: Flat, Gauraud and Phong"""
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.easy_shaders as es
import grafica.lighting_shaders as ls
__author__ = "Daniel Calderon"
__license__ = "MIT"
LIGHT_FLAT = 0
LIGHT_GOURAUD = 1
LIGHT_PHONG = 2
SHAPE_RED_CUBE = 0
SHAPE_GREEN_CUBE = 1
SHAPE_BLUE_CUBE = 2
SHAPE_YELLOW_CUBE = 3
SHAPE_RAINBOW_CUBE = 4
# A class to store the application control
class Controller:
def __init__(self):
self.fillPolygon = True
self.showAxis = True
self.lightingModel = LIGHT_FLAT
self.shape = SHAPE_RED_CUBE
# 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_Q:
controller.lightingModel = LIGHT_FLAT
elif key == glfw.KEY_W:
controller.lightingModel = LIGHT_GOURAUD
elif key == glfw.KEY_E:
controller.lightingModel = LIGHT_PHONG
elif key == glfw.KEY_1:
controller.shape = SHAPE_RED_CUBE
elif key == glfw.KEY_2:
controller.shape = SHAPE_GREEN_CUBE
elif key == glfw.KEY_3:
controller.shape = SHAPE_BLUE_CUBE
elif key == glfw.KEY_4:
controller.shape = SHAPE_YELLOW_CUBE
elif key == glfw.KEY_5:
controller.shape = SHAPE_RAINBOW_CUBE
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, "Lighting 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)
# Different shader programs for different lighting strategies
flatPipeline = ls.SimpleFlatShaderProgram()
gouraudPipeline = ls.SimpleGouraudShaderProgram()
phongPipeline = ls.SimplePhongShaderProgram()
# This shader program does not consider lighting
mvpPipeline = es.SimpleModelViewProjectionShaderProgram()
# 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)
# Convenience function to ease initialization
def createGPUShape(pipeline, 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(mvpPipeline, bs.createAxis(4))
# Note: the vertex attribute layout (stride) is the same for the 3 lighting pipelines in
# this case: flatPipeline, gouraudPipeline and phongPipeline. Hence, the VAO setup can
# be the same.
gpuRedCube = createGPUShape(gouraudPipeline, bs.createColorNormalsCube(1,0,0))
gpuGreenCube = createGPUShape(gouraudPipeline, bs.createColorNormalsCube(0,1,0))
gpuBlueCube = createGPUShape(gouraudPipeline, bs.createColorNormalsCube(0,0,1))
gpuYellowCube = createGPUShape(gouraudPipeline, bs.createColorNormalsCube(1,1,0))
gpuRainbowCube = createGPUShape(gouraudPipeline, bs.createRainbowNormalsCube())
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
projection = tr.ortho(-1, 1, -1, 1, 0.1, 100)
projection = tr.perspective(45, float(width)/float(height), 0.1, 100)
camX = 3 * np.sin(camera_theta)
camY = 3 * np.cos(camera_theta)
viewPos = np.array([camX,camY,2])
view = tr.lookAt(
viewPos,
np.array([0,0,0]),
np.array([0,0,1])
)
rotation_theta = glfw.get_time()
axis = np.array([1,-1,1])
#axis = np.array([0,0,1])
axis = axis / np.linalg.norm(axis)
model = tr.rotationA(rotation_theta, axis)
model = tr.identity()
# 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)
# The axis is drawn without lighting effects
if controller.showAxis:
glUseProgram(mvpPipeline.shaderProgram)
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection)
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "view"), 1, GL_TRUE, view)
glUniformMatrix4fv(glGetUniformLocation(mvpPipeline.shaderProgram, "model"), 1, GL_TRUE, tr.identity())
mvpPipeline.drawCall(gpuAxis, GL_LINES)
# Selecting the shape to display
if controller.shape == SHAPE_RED_CUBE:
gpuShape = gpuRedCube
elif controller.shape == SHAPE_GREEN_CUBE:
gpuShape = gpuGreenCube
elif controller.shape == SHAPE_BLUE_CUBE:
gpuShape = gpuBlueCube
elif controller.shape == SHAPE_YELLOW_CUBE:
gpuShape = gpuYellowCube
elif controller.shape == SHAPE_RAINBOW_CUBE:
gpuShape = gpuRainbowCube
else:
raise Exception()
# Selecting the lighting shader program
if controller.lightingModel == LIGHT_FLAT:
lightingPipeline = flatPipeline
elif controller.lightingModel == LIGHT_GOURAUD:
lightingPipeline = gouraudPipeline
elif controller.lightingModel == LIGHT_PHONG:
lightingPipeline = phongPipeline
else:
raise Exception()
glUseProgram(lightingPipeline.shaderProgram)
# Setting all uniform shader variables
# White light in all components: ambient, diffuse and specular.
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "La"), 1.0, 1.0, 1.0)
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "Ld"), 1.0, 1.0, 1.0)
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "Ls"), 1.0, 1.0, 1.0)
# Object is barely visible at only ambient. Diffuse behavior is slightly red. Sparkles are white
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "Ka"), 0.2, 0.2, 0.2)
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "Kd"), 0.9, 0.5, 0.5)
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "Ks"), 1.0, 1.0, 1.0)
# TO DO: Explore different parameter combinations to understand their effect!
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "lightPosition"), -5, -5, 5)
glUniform3f(glGetUniformLocation(lightingPipeline.shaderProgram, "viewPosition"), viewPos[0], viewPos[1], viewPos[2])
glUniform1ui(glGetUniformLocation(lightingPipeline.shaderProgram, "shininess"), 100)
glUniform1f(glGetUniformLocation(lightingPipeline.shaderProgram, "constantAttenuation"), 0.0001)
glUniform1f(glGetUniformLocation(lightingPipeline.shaderProgram, "linearAttenuation"), 0.03)
glUniform1f(glGetUniformLocation(lightingPipeline.shaderProgram, "quadraticAttenuation"), 0.01)
glUniformMatrix4fv(glGetUniformLocation(lightingPipeline.shaderProgram, "projection"), 1, GL_TRUE, projection)
glUniformMatrix4fv(glGetUniformLocation(lightingPipeline.shaderProgram, "view"), 1, GL_TRUE, view)
glUniformMatrix4fv(glGetUniformLocation(lightingPipeline.shaderProgram, "model"), 1, GL_TRUE, model)
# Drawing
lightingPipeline.drawCall(gpuShape)
# 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()
gpuRainbowCube.clear()
glfw.terminate()