-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_view.py
180 lines (146 loc) · 6.92 KB
/
mesh_view.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
import glfw
import sys
import graphics_math as gm
import numpy as np
import pyigl as igl
import traceback
from OpenGL.GL import *
from OpenGL.arrays import ArrayDatatype
from iglhelpers import *
from shader_program import *
from glfw_controller import *
class MeshModel(GlfwModel):
def __init__(self, mesh_path):
self.mesh_path = mesh_path
def initialize(self):
self.vertices = igl.eigen.MatrixXd()
self.faces = igl.eigen.MatrixXi()
try:
if not igl.read_triangle_mesh(self.mesh_path, self.vertices,
self.faces):
print("failed to read mesh\n")
except:
traceback.print_exc(file=sys.stdout)
sys.exit(-1)
self.face_normals = igl.eigen.MatrixXd()
igl.per_face_normals(self.vertices, self.faces, self.face_normals)
self.vertex_normals = igl.eigen.MatrixXd()
igl.per_vertex_normals(self.vertices, self.faces,
igl.PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA,
self.vertex_normals)
self.vertex_data = e2p(self.vertices).astype(
dtype=np.float32, order='C')
self.index_data = e2p(self.faces).astype(dtype=np.uint32, order='C')
self.face_normal_data = e2p(self.face_normals).astype(
dtype=np.float32, order='C')
self.vertex_normal_data = e2p(self.vertex_normals).astype(
dtype=np.float32, order='C')
self.num_faces = self.index_data.shape[0]
self.num_vertices = self.vertex_data.shape[0]
self.center = np.mean(self.vertex_data, axis=0)
self.max_vals = np.max(self.vertex_data, axis=0)
self.min_vals = np.min(self.vertex_data, axis=0)
self.extents = self.max_vals - self.min_vals
print("min = %s, max = %s, extents = %s" % (self.min_vals, self.max_vals,
self.extents))
self.vertex_data = (self.vertex_data - self.center)/ self.extents
self.vertex_byte_count = ArrayDatatype.arrayByteCount(self.vertex_data)
self.vertex_normal_byte_count = ArrayDatatype.arrayByteCount(
self.vertex_normal_data)
self.index_byte_count = ArrayDatatype.arrayByteCount(self.index_data)
class MeshView(GlfwView):
def __init__(self, fragment_shader_path, vertex_shader_path):
self.fragment_shader_path = fragment_shader_path
self.vertex_shader_path = vertex_shader_path
def set_camera(self, eye, at, up, fov, near, far):
self.eye = np.transpose([eye])
self.at = np.transpose([at])
self.up = np.transpose([up])
self.fov = fov
self.near = near
self.far = far
def set_light_position(self, position):
self.light_position = np.array(position)
def update_vbos(self):
glBindVertexArray(self.vao_id)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo_id[0])
glBufferData(GL_ARRAY_BUFFER, self.model.vertex_byte_count,
self.model.vertex_data, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.vbo_id[1])
glBufferData(GL_ARRAY_BUFFER, self.model.vertex_normal_byte_count,
self.model.vertex_normal_data, GL_STATIC_DRAW)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.vbo_id[2])
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.model.index_byte_count,
self.model.index_data, GL_STATIC_DRAW)
def set_hints(self):
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)
def initialize(self):
self.set_hints()
# Load shaders.
self.fragment = open(self.fragment_shader_path, 'r').read()
self.vertex = open(self.vertex_shader_path, 'r').read()
# Compile shaders and link program.
self.program = ShaderProgram(
fragment=self.fragment, vertex=self.vertex)
glUseProgram(self.program.program_id)
fragment_color_location = glGetFragDataLocation(
self.program.program_id, "fragment_color")
# Generate VAOs.
self.vao_id = glGenVertexArrays(1)
glBindVertexArray(self.vao_id)
# Generate VBOs.
self.vbo_id = glGenBuffers(3)
# Setup the vertex data in VBO.
self.vertex_location = self.program.attribute_location(
'vertex_position')
glBindBuffer(GL_ARRAY_BUFFER, self.vbo_id[0])
glVertexAttribPointer(self.vertex_location, 3,
GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(self.vertex_location)
# Setup the normal data in VBO.
self.vertex_normal_location = self.program.attribute_location(
'vertex_normal')
glBindBuffer(GL_ARRAY_BUFFER, self.vbo_id[1])
glVertexAttribPointer(self.vertex_normal_location, 3,
GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(self.vertex_normal_location)
# Setup the indices data VBO.
self.model_location = self.program.uniform_location('model')
self.view_location = self.program.uniform_location('view')
self.projection_location = self.program.uniform_location('projection')
self.light_position_location = self.program.uniform_location(
'light_position')
self.update_vbos()
def render(self, width, height):
glViewport(0, 0, width, height)
glClearColor(0.0, 0.0, 0.0, 0.0)
glEnable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glDepthFunc(GL_LESS)
aspect = float(width) / float(height)
projection_matrix = gm.perspective(
self.fov, aspect, self.near, self.far)
model_matrix = np.eye(4)
view_matrix = gm.lookat(self.eye, self.at, self.up)
# Specify program to be used
glUseProgram(self.program.program_id)
model_matrix_py = model_matrix.transpose().flatten().tolist()
glUniformMatrix4fv(self.model_location, 1, GL_FALSE,
(GLfloat * 16)(*model_matrix_py))
view_matrix_py = view_matrix.transpose().flatten().tolist()
glUniformMatrix4fv(self.view_location, 1, GL_FALSE,
(GLfloat * 16)(*view_matrix_py))
projection_matrix_py = projection_matrix.transpose().flatten().tolist()
glUniformMatrix4fv(self.projection_location, 1, GL_FALSE,
(GLfloat * 16)(*projection_matrix_py))
light_position_py = self.light_position.tolist()
glUniform3fv(self.light_position_location, 1,
(GLfloat * 3)(*light_position_py))
# Bind to VAO.
glBindVertexArray(self.vao_id)
# Draw the triangles.
glDrawElements(GL_TRIANGLES, self.model.num_faces *
3, GL_UNSIGNED_INT, None)