forked from Rabbid76/graphics-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opengl_glfw_tutorial_xyz.py
274 lines (227 loc) · 10.6 KB
/
opengl_glfw_tutorial_xyz.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
import math
import glfw
import glm
import numpy as np
from OpenGL.GL import *
from OpenGL.GL.shaders import compileShader, compileProgram
class Param(object):
width = 640
height = 480
eye = glm.vec3(0, 0, 3) # 眼睛的位置(默认z轴的正方向)
target = glm.vec3(0, 0, 0) # 瞄准方向的参考点(默认在坐标原点)
up = glm.vec3(0, 1, 0) # 定义对观察者而言的上方(默认y轴的正方向)
near = 0.1
far = 100
radians = glm.radians(45)
projection = glm.perspective(radians, width / height, near, far)
view = glm.lookAt(eye, target, up)
pan_start = None
orbit_start = None
pivot_world = None
vertex_src = """
# version 330
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec3 a_color;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
out vec3 v_color;
void main()
{
gl_Position = projection * view * model * vec4(a_position, 1.0);
v_color = a_color;
}
""" # vec4(a_position, 1); 1指物体大小,越大越小
fragment_src = """
# version 330
in vec3 v_color;
out vec4 out_color;
void main()
{
out_color = vec4(v_color, 1.0);
}
"""
if not glfw.init():
print("Cannot initialize GLFW")
exit()
window = glfw.create_window(Param.width, Param.height, "Hello Triangle", None, None)
if not window:
glfw.terminate()
def depth(x, y):
depth_buffer = glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
depth_ = float(depth_buffer[0][0])
if depth_ == 1:
pt_drag = glm.vec3(0, 0, 0)
clip_pos = Param.projection * Param.view * glm.vec4(pt_drag, 1)
ndc_pos = glm.vec3(clip_pos) / clip_pos.w
if -1 < ndc_pos.z < 1:
depth_ = ndc_pos.z * 0.5 + 0.5
return depth_
def window_size_callback(w, width, height):
glViewport(0, 0, width, height)
Param.projection = glm.perspective(Param.radians, Param.width / Param.height, Param.near, Param.far)
def mouse_button_callback(w, button, action, mods):
x_pos, y_pos = glfw.get_cursor_pos(w)
y_pos = Param.height - y_pos
if button == glfw.MOUSE_BUTTON_RIGHT and action == glfw.PRESS:
Param.pan_start = glm.vec3(x_pos, y_pos, depth(x_pos, y_pos))
if button == glfw.MOUSE_BUTTON_LEFT and action == glfw.PRESS:
Param.orbit_start = glm.vec3(x_pos, y_pos, depth(x_pos, y_pos))
Param.pivot_world = glm.vec3(0, 0, 0)
def cursor_pos_callback(w, x_pos, y_pos):
y_pos = Param.height - y_pos
# get view matrix and viewport rectangle
view, inv_view = Param.view, glm.inverse(Param.view)
view_rect = glm.vec4(0, 0, Param.width, Param.height)
if glfw.get_mouse_button(w, glfw.MOUSE_BUTTON_RIGHT) == glfw.PRESS:
# get drag start and end
wnd_from = Param.pan_start
wnd_to = glm.vec3(x_pos, y_pos, Param.pan_start[2])
Param.pan_start = wnd_to
# get projection and window matrix
inv_proj = glm.inverse(Param.projection)
inv_wnd = glm.translate(glm.mat4(1), glm.vec3(-1, -1, -1))
inv_wnd = glm.scale(inv_wnd, glm.vec3(2 / view_rect[2], 2 / view_rect[3], 2))
inv_wnd = glm.translate(inv_wnd, glm.vec3(view_rect[0], view_rect[1], 0))
# calculate drag start and world coordinates
pt_h_world = [inv_view * inv_proj * inv_wnd * glm.vec4(*pt, 1) for pt in [wnd_from, wnd_to]]
pt_world = [glm.vec3(pt_h) / pt_h.w for pt_h in pt_h_world]
# calculate drag world translation
world_vec = pt_world[1] - pt_world[0]
# translate view position and update view matrix
inv_view = glm.translate(glm.mat4(1), world_vec * -1) * inv_view
view = glm.inverse(inv_view)
elif glfw.get_mouse_button(w, glfw.MOUSE_BUTTON_LEFT) == glfw.PRESS:
# get the drag start and end
wnd_from = Param.orbit_start
wnd_to = glm.vec3(x_pos, y_pos, Param.orbit_start[2])
Param.orbit_start = wnd_to
# calculate the pivot, rotation axis and angle
pivot_view = glm.vec3(view * glm.vec4(*Param.pivot_world, 1))
orbit_dir = wnd_to - wnd_from
# get the projection of the up vector to the view port
# TODO
# calculate the rotation components for the rotation around the view space x axis and the world up vector
orbit_dir_x = glm.vec2(0, 1)
orbit_vec_x = glm.vec2(0, orbit_dir.y)
orbit_dir_up = glm.vec2(1, 0)
orbit_vec_up = glm.vec2(orbit_dir.x, 0)
# calculate the rotation matrix around the view space x axis through the pivot
rot_pivot_x = glm.mat4(1)
if glm.length(orbit_vec_x) > 0.5:
axis_x = glm.vec3(-1, 0, 0)
angle_x = glm.dot(orbit_dir_x, glm.vec2(orbit_vec_x.x / (view_rect[2] - view_rect[0]),
orbit_vec_x.y / (view_rect[3] - view_rect[1]))) * math.pi
rot_mat_x = glm.rotate(glm.mat4(1), angle_x, axis_x)
rot_pivot_x = glm.translate(glm.mat4(1), pivot_view) * rot_mat_x * glm.translate(glm.mat4(1), -pivot_view)
# calculate the rotation matrix around the world space up vector through the pivot
rot_pivot_up = glm.mat4(1)
if glm.length(orbit_vec_up) > 0.5:
axis_up = glm.vec3(0, 1, 0)
angle_up = glm.dot(orbit_dir_up, glm.vec2(
orbit_vec_up.x / (view_rect[2] - view_rect[0]),
orbit_vec_up.y / (view_rect[3] - view_rect[1]))) * math.pi
rot_mat_up = glm.rotate(glm.mat4(1), angle_up, axis_up)
rot_pivot_up = glm.translate(
glm.mat4(1), Param.pivot_world) * rot_mat_up * glm.translate(glm.mat4(1), -Param.pivot_world)
# transform and update view matrix
view = rot_pivot_x * view * rot_pivot_up
Param.view = view
def scroll_callback(w, x_offset, y_offset):
x, y = glfw.get_cursor_pos(window)
y = Param.height - y
view_rect = glm.vec4(0, 0, Param.width, Param.height)
# get view, projection and window matrix
proj, inv_proj = Param.projection, glm.inverse(Param.projection)
view, inv_view = Param.view, glm.inverse(Param.view)
inv_wnd = glm.translate(glm.mat4(1), glm.vec3(-1, -1, -1))
inv_wnd = glm.scale(inv_wnd, glm.vec3(2 / view_rect[2], 2 / view_rect[3], 2))
inv_wnd = glm.translate(inv_wnd, glm.vec3(view_rect[0], view_rect[1], 0))
wnd = glm.inverse(inv_wnd)
# get world space position on view ray
pt_wnd = glm.vec3(x, y, 1.0)
# pt_world = glm.unProject(pt_wnd, view, proj, vp_rect)
pt_h_world = inv_view * inv_proj * inv_wnd * glm.vec4(*pt_wnd, 1)
pt_world = glm.vec3(pt_h_world) / pt_h_world.w
# get view position
eye = glm.vec3(inv_view[3])
# get "zoom" direction and amount
ray_cursor = glm.normalize(pt_world - eye)
# translate view position and update view matrix
inv_view = glm.translate(glm.mat4(1), ray_cursor * y_offset) * inv_view
# return new view matrix
Param.view = glm.inverse(inv_view)
glfw.make_context_current(window)
glfw.set_window_size_callback(window, window_size_callback)
glfw.set_mouse_button_callback(window, mouse_button_callback)
glfw.set_cursor_pos_callback(window, cursor_pos_callback)
glfw.set_scroll_callback(window, scroll_callback)
# (x,y,z,r,g,b) red 1.0, 0.0, 0.0, green 0.0, 1.0, 0.0, blue 0.0, 0.0, 1.0,
x = np.array([
1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
-1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
], dtype=np.float32)
y = np.array([
0.0, 1.0, 0.0, 0.0, 1.0, 0.0,
0.0, -1.0, 0.0, 0.0, 1.0, 0.0,
], dtype=np.float32)
z = np.array([
0.0, 0.0, 1.0, 0.9, 0.9, 0.0,
0.0, 0.0, -1.0, 0.9, 0.9, 0.6,
], dtype=np.float32)
# 声明3个数组的vbo,下面的均绑定在此vao中
vao = glGenVertexArrays(3)
# 声明两个数组的vbo,下面的均绑定在此vbo中
vbo = glGenBuffers(3)
glBindVertexArray(vao[0])
# 设置第一个vbo。即vbo[0]
glBindBuffer(GL_ARRAY_BUFFER, vbo[0])
glBufferData(GL_ARRAY_BUFFER, x.nbytes, x, GL_STATIC_DRAW)
glEnableVertexAttribArray(0) # 准备设置glsl。即vertex_src中的layout(location = 0)的属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, x.itemsize * 6, ctypes.c_void_p(0))
glEnableVertexAttribArray(1) # 准备设置glsl。即vertex_src中的layout(location = 1)的属性
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, x.itemsize * 6, ctypes.c_void_p(12))
glBindVertexArray(vao[1])
# 设置第二个vbo。即vbo[1]
glBindBuffer(GL_ARRAY_BUFFER, vbo[1])
glBufferData(GL_ARRAY_BUFFER, y.nbytes, y, GL_STATIC_DRAW)
glEnableVertexAttribArray(0) # 准备设置glsl。即vertex_src中的layout(location = 0)的属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, y.itemsize * 6, ctypes.c_void_p(0))
glEnableVertexAttribArray(1) # 准备设置glsl。即vertex_src中的layout(location = 1)的属性
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, y.itemsize * 6, ctypes.c_void_p(12))
glBindVertexArray(vao[2])
# 设置第三个vbo。即vbo[2]
glBindBuffer(GL_ARRAY_BUFFER, vbo[2])
glBufferData(GL_ARRAY_BUFFER, z.nbytes, z, GL_STATIC_DRAW)
glEnableVertexAttribArray(0) # 准备设置glsl。即vertex_src中的layout(location = 0)的属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, z.itemsize * 6, ctypes.c_void_p(0))
glEnableVertexAttribArray(1) # 准备设置glsl。即vertex_src中的layout(location = 1)的属性
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, z.itemsize * 6, ctypes.c_void_p(12))
vert_shader = glCreateShader(GL_VERTEX_SHADER)
program = compileProgram(compileShader(vertex_src, GL_VERTEX_SHADER),
compileShader(fragment_src, GL_FRAGMENT_SHADER))
glUseProgram(program)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glUniformMatrix4fv(glGetUniformLocation(program, 'projection'), 1, GL_FALSE, glm.value_ptr(Param.projection))
glUniformMatrix4fv(glGetUniformLocation(program, 'view'), 1, GL_FALSE, glm.value_ptr(Param.view))
# 设置模型坐标,和glUniformMatrix4fv不能写成一行,写成一行会无法展示,可能是因为用的指针地址
mat = glm.mat4()
glUniformMatrix4fv(glGetUniformLocation(program, 'model'), 1, GL_FALSE, glm.value_ptr(mat))
while not glfw.window_should_close(window):
glUniformMatrix4fv(glGetUniformLocation(program, 'projection'), 1, GL_FALSE,
glm.value_ptr(Param.projection))
glUniformMatrix4fv(glGetUniformLocation(program, 'view'), 1, GL_FALSE, glm.value_ptr(Param.view))
glClearColor(0.2, 0.3, 0.3, 1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBindVertexArray(vao[0])
# glDrawArrays(GL_TRIANGLES, 0, 4)
glDrawArrays(GL_LINE_STRIP, 0, len(x))
glBindVertexArray(vao[1])
glDrawArrays(GL_LINE_STRIP, 0, len(y))
glBindVertexArray(vao[2])
glDrawArrays(GL_LINE_STRIP, 0, len(z))
glfw.poll_events()
glfw.swap_buffers(window)
glfw.terminate()