-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-hellolightingcol.py
executable file
·284 lines (211 loc) · 8.87 KB
/
07-hellolightingcol.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/Users/marc/miniconda3/bin/python3
import glfw
from OpenGL.GL import *
from OpenGL.GLU import *
import glm
import math
import ctypes
print("""Based on examples from the learnopengl tutorial
use keys QWASDZ to move around and mouse to point camera.
use key i to display info.
""")
def error_callback(errnum, descr):
print("Called GLFW Error Callback", err, descr)
def framebuffer_size_callback(window, width, height):
# make sure the viewport matches the new window dimensions; note that width and
# height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height)
camera.setRatio( width/height )
# process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
# ---------------------------------------------------------------------------------------------------------
import mycamera
camera = mycamera.Camera( position = glm.vec3( 1.3, -0.2, 6.2 ),
front = glm.vec3( -0.1, 0, -1.0 ) )
ipressed = False
def processInput(window):
# global rotate, rpressed
global cameraPos, cameraFront, ipressed
if glfw.get_key(window, glfw.KEY_ESCAPE) == glfw.PRESS:
glfw.set_window_should_close(window, True)
if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS:
camera.processKeyboard( mycamera.FORWARD, deltaTime )
if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS:
camera.processKeyboard( mycamera.BACKWARD, deltaTime )
if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS:
camera.processKeyboard( mycamera.LEFT, deltaTime )
if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS:
camera.processKeyboard( mycamera.RIGHT, deltaTime )
if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
camera.processKeyboard( mycamera.UP, deltaTime )
if glfw.get_key(window, glfw.KEY_Z) == glfw.PRESS:
camera.processKeyboard( mycamera.DOWN, deltaTime )
if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS:
if not ipressed:
print( f"lastX: {lastX:3.2f}, lastY: {lastY:3.2f}")
print( f"fps: {1/deltaTime:3.1f}")
print( camera )
ipressed = True
if glfw.get_key(window, glfw.KEY_I) == glfw.RELEASE:
ipressed = False
camera.step(deltaTime)
def mousebutton_callback( window, button, action, mods ):
# mouse_pos = glfw.get_cursor_pos(window)
# glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_HIDDEN)
# glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_NORMAL)
if action == glfw.PRESS and button == glfw.MOUSE_BUTTON_1:
camera.move( mycamera.FORWARD )
if action == glfw.RELEASE and button == glfw.MOUSE_BUTTON_1:
camera.move( None )
if action == glfw.PRESS and button == glfw.MOUSE_BUTTON_2:
camera.move( mycamera.BACKWARD )
if action == glfw.RELEASE and button == glfw.MOUSE_BUTTON_2:
camera.move( None )
# track_mouse = None
lastX = 400
lastY = 300
firstMouse = True
def mouse_callback(window, xpos, ypos):
global lastX, lastY, firstMouse
if(firstMouse): # initially set to true
lastX = xpos
lastY = ypos
firstMouse = False
xoffset = xpos - lastX
yoffset = lastY - ypos # reversed since y-coordinates range from bottom to top
lastX = xpos
lastY = ypos
camera.processMouseMovement( xoffset, yoffset )
def scroll_callback(window, xoffset, yoffset):
camera.processMouseScroll(yoffset)
width = 800
height = 600
# Initialize the glfw library
if not glfw.init():
print("Failed to init glfw")
else:
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
window = glfw.create_window(width, height, "LearnOpenGL", None, None)
if not window:
print("Failed to create GLFW window")
glfw.terminate()
glfw.make_context_current(window)
glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
glfw.set_input_mode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)
glfw.set_cursor_pos_callback(window, mouse_callback)
glfw.set_scroll_callback(window, scroll_callback)
glfw.set_mouse_button_callback(window, mousebutton_callback)
glfw.set_error_callback(error_callback);
## Load, compile, link shaders
import myshader
shaders = myshader.shader( "shaders/hellolightingcol.vert", "shaders/hellolightingcol.frag")
shaders.linkShaders()
lightshader = myshader.shader( "shaders/hellolightingcol.vert", "shaders/hellolight.frag")
lightshader.linkShaders()
# ## Textures
import mytexture
t1 = mytexture.texture('resources/wall.jpg', GL_TEXTURE0)
# t2 = mytexture.texture('awesomeface.png', GL_TEXTURE1)
## Scene
import mycube
import numpy as np
myvertices = mycube.vertices
cubePositions = [
#glm.vec3( 0.0, 0.0, 0.0),
glm.vec3( 2.0, 5.0, -15.0),
glm.vec3(-1.5, -2.2, -2.5),
glm.vec3(-3.8, -2.0, -12.3),
glm.vec3( 2.4, -0.4, -3.5),
glm.vec3(-1.7, 3.0, -7.5),
glm.vec3( 1.3, -2.0, -2.5),
glm.vec3( 1.5, 2.0, -2.5),
glm.vec3( 1.5, 0.2, -1.5),
glm.vec3(-1.3, 1.0, -1.5)
]
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, myvertices, GL_STATIC_DRAW)
# bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
## position of the attrib array, must match the shader
location = 0
glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 5*4, None) #3 * 4, 0)
glEnableVertexAttribArray(location)
## position of the attrib array, must match the shader
location = 1
glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 5*4, ctypes.c_void_p(3*4)) #3 * 4, 0)
glEnableVertexAttribArray(location)
# glBindBuffer(GL_ARRAY_BUFFER, 0)
# glBindVertexArray(0)
lightVAO = glGenVertexArrays(1)
glBindVertexArray(lightVAO)
# we only need to bind to the VBO, the container's VBO's data already contains the data.
glBindBuffer(GL_ARRAY_BUFFER, VBO)
# set the vertex attributes (only position data for our lamp)
location = 0
glVertexAttribPointer(location, 3, GL_FLOAT, GL_FALSE, 5*4, None)
glEnableVertexAttribArray(location);
# uncomment this call to draw in wireframe polygons.
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
# render loop
# -----------
glClearColor(0.9, 0.7, 0.7, 1.0)
deltaTime = 0.0
lastFrame = 0.0
glEnable(GL_DEPTH_TEST)
while not glfw.window_should_close(window):
# input
processInput(window)
currentFrameTime = glfw.get_time()*1.0
deltaTime = currentFrameTime - lastFrame
lastFrame = currentFrameTime
# render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# radius = 10.0
# camX = math.sin(timeValue) * radius
# camZ = math.cos(timeValue) * radius
# view = glm.lookAt(glm.vec3(camX, 0.0, camZ), glm.vec3(0.0, 0.0, 0.0), glm.vec3(0.0, 1.0, 0.0))
#
projection = camera.getProjectionMatrix()
# glm.ortho(0.0, 800.0, 0.0, 600.0, 0.1, 100.0)
view = camera.getViewMatrix()
shaders.use();
shaders.setUniform3f("objectColor", 1.0, 0.5, 0.31)
shaders.setUniform3f("lightColor", 1.0, 1.0, 1.0)
shaders.setUniformMatrix4fv("view", glm.value_ptr( view ))
shaders.setUniformMatrix4fv("projection", glm.value_ptr( projection ))
glBindVertexArray(VAO)
for i in range(len(cubePositions)):
model = glm.mat4(1.0)
model = glm.translate(model, cubePositions[i]);
# model = glm.rotate(model, timeValue * glm.radians(-55.0), glm.vec3(1.0, 0.5, 0.0))
angle = 20.0 * i
model = glm.rotate(model, glm.radians(angle), glm.vec3(1.0, 0.3, 0.5));
shaders.setUniformMatrix4fv("model", glm.value_ptr( model ))
# glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
glDrawArrays(GL_TRIANGLES, 0, 36)
lightshader.use();
lightshader.setUniform3f("lightColor", 1.0, 1.0, 1.0)
lightshader.setUniformMatrix4fv("view", glm.value_ptr( view ))
lightshader.setUniformMatrix4fv("projection", glm.value_ptr( projection ))
model = glm.mat4(1.0)
sc = .3*math.sin(currentFrameTime/2.0)
model = glm.scale(model, glm.vec3(sc, sc, sc))
lightshader.setUniformMatrix4fv("model", glm.value_ptr( model ))
glBindVertexArray(lightVAO)
glDrawArrays(GL_TRIANGLES, 0, 36)
# glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
# -------------------------------------------------------------------------------
glfw.swap_buffers(window)
glfw.poll_events()
glBindVertexArray(0) # no need to unbind it every time
# optional: de-allocate all resources once they've outlived their purpose:
# ------------------------------------------------------------------------
glDeleteVertexArrays(1, [VAO, lightVAO])
glDeleteBuffers(1, [VBO])
# glfw: terminate, clearing all previously allocated GLFW resources.
# ------------------------------------------------------------------
glfw.terminate()