-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGLutils.py
More file actions
77 lines (62 loc) · 3.04 KB
/
OpenGLutils.py
File metadata and controls
77 lines (62 loc) · 3.04 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
from Mesh import Mesh
import numpy as np
from vispy import gloo
def pass_mesh_to_opengl(mesh : Mesh,
attribute_position_name='a_position', attribute_color_name='a_color'):
"""
Pass the mesh data to OpenGL for rendering.
:param vertices: Array of vertex positions
:param faces: Array of vertex indices defining the faces
:param vertex_colors: Array of RGB color per vertex
:param attribute_position_name: Name of the shader attribute for the vertex positions
:param attribute_color_name: Name of the shader attribute for the vertex colors
:return: OpenGL Buffer objects
"""
# Collate vertex data (position and opt. color).
# we need to explicitly specify the data types (float32), as well as the names
# for the variables the vertex data and th
vertices = mesh.vertices
maxas = vertices.max(axis=0)
factor = vertices.max(axis=0)[0] - vertices.min(axis=0)[0]
factor = 40/factor
vertices *=factor
# import matplotlib.pyplot as plt
# fig = plt.figure()
# ax = fig.add_subplot(projection='3d')
# ax.scatter(vertices[:,0],vertices[:,1],vertices[:,2])
# plt.show()
faces =mesh.faces
vertex_colors = mesh.vertex_colors
vertices_type = [(attribute_position_name, np.float32, 3)]
if vertex_colors is not None:
vertices_type += [(attribute_color_name, np.float32, 3)]
vertex_data = np.asarray(list(zip(vertices, vertex_colors)), vertices_type)
else:
vertex_data = np.asarray(vertices, vertices_type)
# Buffers
vertex_buffer = gloo.VertexBuffer(vertex_data)
index_buffer = gloo.IndexBuffer(faces.flatten().astype(np.uint32))
return vertex_buffer, index_buffer
def compute_mv_and_mvp(model_matrix, projection_matrix,
camera_translation_vector, camera_rotation_matrix):
"""
Compute the MV and MVP matrices for OpenGL.
:param model_matrix: 4x4 model matrix
:param projection_matrix: 4x4 projection matrix
:param camera_translation_vector: 3x1 translation vector for the camera
:param camera_rotation_matrix: 3x3 rotation matrix for the camera
:return: 4x4 MV matrix, 4x4 MVP matrix
"""
yz_flip = np.eye(4, dtype=np.float32)
yz_flip[1, 1], yz_flip[2, 2] = -1, -1
# View matrix (defining the camera pose):
view_matrix = np.eye(4, dtype=np.float32)
view_matrix[:3, 3] = np.squeeze(camera_translation_vector)
view_matrix[:3, :3] = camera_rotation_matrix
# Converting it to OpenGL coordinate system:
view_matrix = yz_flip.dot(view_matrix).T
# Model-view matrix (projecting from object space to camera space):
mv_matrix = np.dot(model_matrix, view_matrix)
# Model-view-projection matrix (projecting from object space to image space):
mvp_matrix = np.dot(mv_matrix, projection_matrix)
return mv_matrix, mvp_matrix