YoonaAI commited on
Commit
bd58d25
·
1 Parent(s): 5bbd1a6

Create renderer/gl/normal_render.py

Browse files
Files changed (1) hide show
  1. lib/renderer/gl/normal_render.py +97 -0
lib/renderer/gl/normal_render.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
+ MIT License
3
+
4
+ Copyright (c) 2019 Shunsuke Saito, Zeng Huang, and Ryota Natsume
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ '''
24
+ import numpy as np
25
+ import math
26
+
27
+ from .framework import *
28
+ from .norm_render import NormRender
29
+
30
+
31
+ class NormalRender(NormRender):
32
+ def __init__(self, width=1600, height=1200, name='Normal Renderer'):
33
+ NormRender.__init__(self,
34
+ width,
35
+ height,
36
+ name,
37
+ program_files=['normal.vs', 'normal.fs'])
38
+
39
+ self.norm_buffer = glGenBuffers(1)
40
+
41
+ self.norm_data = None
42
+
43
+ def set_normal_mesh(self, vertices, faces, norms, face_normals):
44
+ NormRender.set_mesh(self, vertices, faces)
45
+
46
+ self.norm_data = norms[face_normals.reshape([-1])]
47
+
48
+ glBindBuffer(GL_ARRAY_BUFFER, self.norm_buffer)
49
+ glBufferData(GL_ARRAY_BUFFER, self.norm_data, GL_STATIC_DRAW)
50
+
51
+ glBindBuffer(GL_ARRAY_BUFFER, 0)
52
+
53
+ def euler_to_rot_mat(self, r_x, r_y, r_z):
54
+ R_x = np.array([[1, 0, 0], [0, math.cos(r_x), -math.sin(r_x)],
55
+ [0, math.sin(r_x), math.cos(r_x)]])
56
+
57
+ R_y = np.array([[math.cos(r_y), 0, math.sin(r_y)], [0, 1, 0],
58
+ [-math.sin(r_y), 0, math.cos(r_y)]])
59
+
60
+ R_z = np.array([[math.cos(r_z), -math.sin(r_z), 0],
61
+ [math.sin(r_z), math.cos(r_z), 0], [0, 0, 1]])
62
+
63
+ R = np.dot(R_z, np.dot(R_y, R_x))
64
+
65
+ return R
66
+
67
+ def draw(self):
68
+ self.draw_init()
69
+
70
+ glUseProgram(self.program)
71
+ glUniformMatrix4fv(self.model_mat_unif, 1, GL_FALSE,
72
+ self.model_view_matrix.transpose())
73
+ glUniformMatrix4fv(self.persp_mat_unif, 1, GL_FALSE,
74
+ self.projection_matrix.transpose())
75
+
76
+ # Handle vertex buffer
77
+ glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer)
78
+
79
+ glEnableVertexAttribArray(0)
80
+ glVertexAttribPointer(0, self.vertex_dim, GL_DOUBLE, GL_FALSE, 0, None)
81
+
82
+ # Handle normal buffer
83
+ glBindBuffer(GL_ARRAY_BUFFER, self.norm_buffer)
84
+
85
+ glEnableVertexAttribArray(1)
86
+ glVertexAttribPointer(1, 3, GL_DOUBLE, GL_FALSE, 0, None)
87
+
88
+ glDrawArrays(GL_TRIANGLES, 0, self.n_vertices)
89
+
90
+ glDisableVertexAttribArray(1)
91
+ glDisableVertexAttribArray(0)
92
+
93
+ glBindBuffer(GL_ARRAY_BUFFER, 0)
94
+
95
+ glUseProgram(0)
96
+
97
+ self.draw_end()