-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project#1.py
400 lines (310 loc) · 12.5 KB
/
Project#1.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from OpenGL.GL import *
from glfw.GLFW import *
import glm
import ctypes
import numpy as np
global cameraUp
global cameraRight
global cameraPan
global cameraPos
global cameraOrigin
global cameraDirection
global world_up
cameraUp = glm.vec3(0,1,0)
cameraRight = glm.vec3(0,0,0)
cameraDirection = glm.vec3(0,0,0)
cameraPan = glm.vec3(0,0,0)
cameraPos = glm.vec3(0,0,0)
cameraOrigin = glm.vec3(0,0,0)
world_up = glm.vec3(0,1,0)
lastX = 0
lastY = 0
direction_x = 0.5000000000000001
direction_y = 0.7071067811865475
direction_z = 0.5
distance = 0
yaw = 45
token = 1
pitch = 45
zoom = 0.0
chk = 1
g_vertex_shader_src = '''
#version 330 core
layout (location = 0) in vec3 vin_pos;
layout (location = 1) in vec3 vin_color;
out vec4 vout_color;
uniform mat4 MVP;
void main()
{
// 3D points in homogeneous coordinates
vec4 p3D_in_hcoord = vec4(vin_pos.xyz, 1.0);
gl_Position = MVP * p3D_in_hcoord;
vout_color = vec4(vin_color, 1.);
}
'''
g_fragment_shader_src = '''
#version 330 core
in vec4 vout_color;
out vec4 FragColor;
void main()
{
FragColor = vout_color;
}
'''
def load_shaders(vertex_shader_source, fragment_shader_source):
# build and compile our shader program
# ------------------------------------
# vertex shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER) # create an empty shader object
glShaderSource(vertex_shader, vertex_shader_source) # provide shader source codeㅅ
glCompileShader(vertex_shader) # compile the shader object
# check for shader compile errors
success = glGetShaderiv(vertex_shader, GL_COMPILE_STATUS)
if (not success):
infoLog = glGetShaderInfoLog(vertex_shader)
print("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" + infoLog.decode())
# fragment shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER) # create an empty shader object
glShaderSource(fragment_shader, fragment_shader_source) # provide shader source code
glCompileShader(fragment_shader) # compile the shader object
# check for shader compile errors
success = glGetShaderiv(fragment_shader, GL_COMPILE_STATUS)
if (not success):
infoLog = glGetShaderInfoLog(fragment_shader)
print("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" + infoLog.decode())
# link shaders
shader_program = glCreateProgram() # create an empty program object
glAttachShader(shader_program, vertex_shader) # attach the shader objects to the program object
glAttachShader(shader_program, fragment_shader)
glLinkProgram(shader_program) # link the program object
# check for linking errors
success = glGetProgramiv(shader_program, GL_LINK_STATUS)
if (not success):
infoLog = glGetProgramInfoLog(shader_program)
print("ERROR::SHADER::PROGRAM::LINKING_FAILED\n" + infoLog.decode())
glDeleteShader(vertex_shader)
glDeleteShader(fragment_shader)
return shader_program # return the shader program
def prepare_vao_triangle():
# prepare vertex data (in main memory)
vertices = glm.array(glm.float32,
# position # color
0.1, 0.0, 0.0, 1.0, 1.0, 1.0, # v0
0.0, 0.1, 0.0, 1.0, 1.0, 1.0, # v1
0.0, 0.0, 0.1, 1.0, 1.0, 1.0, # v2
)
# create and activate VAO (vertex array object)
VAO = glGenVertexArrays(1) # create a vertex array object ID and store it to VAO variable
glBindVertexArray(VAO) # activate VAO
# create and activate VBO (vertex buffer object)
VBO = glGenBuffers(1) # create a buffer object ID and store it to VBO variable
glBindBuffer(GL_ARRAY_BUFFER, VBO) # activate VBO as a vertex buffer object
# copy vertex data to VBO
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices.ptr, GL_STATIC_DRAW) # allocate GPU memory for and copy vertex data to the currently bound vertex buffer
# configure vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * glm.sizeof(glm.float32), None)
glEnableVertexAttribArray(0)
# configure vertex colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * glm.sizeof(glm.float32), ctypes.c_void_p(3*glm.sizeof(glm.float32)))
glEnableVertexAttribArray(1)
return VAO
def prepare_vao_grid():
# prepare vertex data (in main memory)
vertices = glm.array(glm.float32,
# position # color
-0.1, 0.0, 0.3, 1.0, 1.0, 1.0, #1
-0.1, 0.0, -0.3, 1.0, 1.0, 1.0,
0.0, 0.0, 0.3, 1.0, 1.0, 1.0, #2
0.0, 0.0, -0.3, 1.0, 1.0, 1.0,
0.1, 0.0, 0.3, 1.0, 1.0, 1.0, #3
0.1, 0.0, -0.3, 1.0, 1.0, 1.0,
-0.2, 0.0, 0.3, 1.0, 1.0, 1.0, #4
-0.2, 0.0, -0.3, 1.0, 1.0, 1.0,
0.2, 0.0, 0.3, 1.0, 1.0, 1.0, #5
0.2, 0.0, -0.3, 1.0, 1.0, 1.0,
0.3, 0.0, 0.3, 1.0, 1.0, 1.0, #6
0.3, 0.0, -0.3, 1.0, 1.0, 1.0,
-0.3, 0.0, 0.3, 1.0, 1.0, 1.0, #7
-0.3, 0.0, -0.3, 1.0, 1.0, 1.0,
0.3, 0.0, 0.0, 1.0, 1.0, 1.0, #1
-0.3, 0.0, 0.0, 1.0, 1.0, 1.0,
0.3, 0.0, -0.1, 1.0, 1.0, 1.0, #2
-0.3, 0.0, -0.1, 1.0, 1.0, 1.0,
0.3, 0.0, 0.1, 1.0, 1.0, 1.0, #3
-0.3, 0.0, 0.1, 1.0, 1.0, 1.0,
0.3, 0.0, 0.2, 1.0, 1.0, 1.0, #4
-0.3, 0.0, 0.2, 1.0, 1.0, 1.0,
0.3, 0.0, -0.2, 1.0, 1.0, 1.0, #5
-0.3, 0.0, -0.2, 1.0, 1.0, 1.0,
0.3, 0.0, 0.3, 1.0, 1.0, 1.0, #6
-0.3, 0.0, 0.3, 1.0, 1.0, 1.0,
0.3, 0.0, -0.3, 1.0, 1.0, 1.0, #7
-0.3, 0.0, -0.3, 1.0, 1.0, 1.0,
)
# create and activate VAO (vertex array object)
VAO = glGenVertexArrays(1) # create a vertex array object ID and store it to VAO variable
glBindVertexArray(VAO) # activate VAO
# create and activate VBO (vertex buffer object)
VBO = glGenBuffers(1) # create a buffer object ID and store it to VBO variable
glBindBuffer(GL_ARRAY_BUFFER, VBO) # activate VBO as a vertex buffer object
# copy vertex data to VBO
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices.ptr, GL_STATIC_DRAW) # allocate GPU memory for and copy vertex data to the currently bound vertex buffer
# configure vertex positions
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * glm.sizeof(glm.float32), None)
glEnableVertexAttribArray(0)
# configure vertex colors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * glm.sizeof(glm.float32), ctypes.c_void_p(3*glm.sizeof(glm.float32)))
glEnableVertexAttribArray(1)
return VAO
def cursor_callback_orbit(window, xpos, ypos):
global lastX, lastY, yaw, pitch, direction_x, direction_y, direction_z, world_up, distance, token
# store offset
xoffset = xpos - lastX
yoffset = lastY - ypos
# store last pos to calculate offset
lastX = xpos
lastY = ypos
# control speed of orbit
sensitivity = 0.3
xoffset = xoffset * sensitivity
yoffset = yoffset * sensitivity
# control azimuth and elevation
pitch = pitch + yoffset
# exception handling
if np.cos(np.radians(pitch)) > 0:
world_up = glm.vec3(0,1,0)
yaw = yaw + xoffset
else:
world_up = glm.vec3(0,-1,0)
yaw = yaw - xoffset
# calculate camera pos
direction_x = distance * np.cos(np.radians(pitch)) * np.cos(np.radians(yaw))
direction_y = distance * np.sin(np.radians(pitch))
direction_z = distance * np.cos(np.radians(pitch)) * np.sin(np.radians(yaw))
def cursor_callback_pan(window, xpos, ypos):
global lastX, lastY, cameraRight, cameraUp, cameraPan
# store offset
xoffset = -(xpos - lastX)
yoffset = -(lastY - ypos)
# store last pos to calculate offset
lastX = xpos
lastY = ypos
# control speed of orbit
sensitivity = 0.01
xoffset = xoffset * sensitivity
yoffset = yoffset * sensitivity
# calculate camerapan
cameraPan = cameraPan + (cameraRight * xoffset) + (cameraUp * yoffset)
def cursor_callback_wait(window, xpos, ypos):
return None
def scroll_callback_zoom(window, xoffset, yoffset):
global zoom, direction_x, direction_y, direction_z, distance, cameraOrigin, cameraPan, cameraPos, cameraDirection
zoom = -yoffset * 0.01
# store past pos to control max zoom-in
past_x = direction_x
past_y = direction_y
past_z = direction_z
# calculate zoom
direction_x = direction_x + zoom * cameraDirection.x
direction_y = direction_y + zoom * cameraDirection.y
direction_z = direction_z + zoom * cameraDirection.z
# control max zoom-in
cameraPos = glm.vec3(direction_x,direction_y,direction_z)
distance = glm.distance(cameraPos + cameraPan, cameraOrigin + cameraPan)
if distance < 0.2:
direction_x = past_x
direction_y = past_y
direction_z = past_z
def button_callback(window, button, action, mod):
global lastX,lastY
if button == GLFW_MOUSE_BUTTON_LEFT:
if action==GLFW_PRESS:
lastX,lastY = glfwGetCursorPos(window)
glfwSetCursorPosCallback(window, cursor_callback_orbit)
elif action == GLFW_RELEASE:
glfwSetCursorPosCallback(window, cursor_callback_wait)
elif button == GLFW_MOUSE_BUTTON_RIGHT:
if action==GLFW_PRESS:
lastX, lastY = glfwGetCursorPos(window)
glfwSetCursorPosCallback(window, cursor_callback_pan)
elif action == GLFW_RELEASE:
glfwSetCursorPosCallback(window, cursor_callback_wait)
def key_callback(window, key, scancode, action, mods):
global chk
if key==GLFW_KEY_ESCAPE and action==GLFW_PRESS:
glfwSetWindowShouldClose(window, GLFW_TRUE)
elif key == GLFW_KEY_V:
if action == GLFW_RELEASE:
chk = -chk
def main():
global cameraPos, cameraOrigin, cameraDirection, cameraRight, cameraUp, cameraPan, direction_x, direction_y, direction_z, distance
# initialize glfw
if not glfwInit():
return
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3) # OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE) # Do not allow legacy OpenGl API calls
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE) # for macOS
# create a window and OpenGL context
window = glfwCreateWindow(800, 800, '2019030991_Project#1', None, None)
if not window:
glfwTerminate()
return
glfwMakeContextCurrent(window)
# load shaders
shader_program = load_shaders(g_vertex_shader_src, g_fragment_shader_src)
# get uniform locations
MVP_loc = glGetUniformLocation(shader_program, 'MVP')
# keycallback function
glfwSetKeyCallback(window, key_callback)
glfwSetMouseButtonCallback(window, button_callback)
glfwSetScrollCallback(window, scroll_callback_zoom)
# prepare vao
# vao_triangle = prepare_vao_triangle()
vao_frame = prepare_vao_grid()
# loop until the user closes the window
while not glfwWindowShouldClose(window):
# render
# enable depth test (we'll see details later)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_DEPTH_TEST)
glUseProgram(shader_program)
# projection matrix
# use orthogonal projection
if chk == -1:
P = glm.ortho(-0.5,0.5,-0.5,0.5,-10,10)
# use perspective projection
if chk == 1:
P = glm.perspective(45, 800.0 / 800.0, 0.1, 100.0)
# camera position / camera vector
cameraOrigin = glm.vec3(0, 0, 0)
cameraPos = glm.vec3(direction_x,direction_y,direction_z)
cameraRight = glm.normalize(glm.cross(world_up, cameraPos))
cameraUp = glm.cross(cameraPos,cameraRight)
cameraDirection = glm.normalize(cameraPos - cameraOrigin)
# camera distance
distance = glm.distance(cameraPos + cameraPan, cameraOrigin + cameraPan)
# view matrix
V = glm.lookAt(cameraPos + cameraPan, cameraOrigin + cameraPan, cameraUp)
# current frame: P*V*I (now this is the world frame)
I = glm.mat4()
MVP = P * V * I
glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm.value_ptr(MVP))
M = glm.translate(glm.vec3(0,0,0))
# current frame: P*V*M
MVP = P * V * M
glUniformMatrix4fv(MVP_loc, 1, GL_FALSE, glm.value_ptr(MVP))
# draw grid on xz plane
glBindVertexArray(vao_frame)
glDrawArrays(GL_LINES, 0, 28)
# draw triangle
# glBindVertexArray(vao_triangle)
# glDrawArrays(GL_TRIANGLES, 0, 3)
# swap front and back buffers
glfwSwapBuffers(window)
# poll events
glfwPollEvents()
# terminate glfw
glfwTerminate()
if __name__ == "__main__":
main()