def test3(): def compute_normals(sc): out = len(sc.points) * [ [.0, .0, .0] ] triangle_normals = len(sc.faces) * [ [.0, .0, .0] ] def hash(p): return .11234 * p[0] + .35678 * p[1] + .67257 * p[2] from collections import defaultdict pt_table = defaultdict(list) for i, t in enumerate(sc.faces): p1 = sc.points[t[0]] p2 = sc.points[t[1]] p3 = sc.points[t[2]] pt_table[hash(p1)].append( (i, p1, t[0]) ) pt_table[hash(p2)].append( (i, p2, t[1]) ) pt_table[hash(p3)].append( (i, p3, t[2]) ) normal = vcross(sub(p2, p1), sub(p3, p1)) normal = vnorm(normal) triangle_normals[i] = normal for key, value in pt_table.iteritems(): # we assume no collisions in the hash point_index = value[0][2] first_point = value[0][1] # compute the normal of each triangles around # TODO should be done just once for each triangle in pre-process normals = [] for t_index, p, _ in value: assert p == first_point normals.append(triangle_normals[t_index]) N = ( sum(n[0] for n in normals) / len(normals), sum(n[1] for n in normals) / len(normals), sum(n[2] for n in normals) / len(normals) ) # print N out[point_index] = N return out scene = load(sys.argv[1]) with benchmark('compute normals'): scene.normals = compute_normals(scene) scene.write(sys.argv[2])
def test3(): def compute_normals(sc): out = len(sc.points) * [[.0, .0, .0]] triangle_normals = len(sc.faces) * [[.0, .0, .0]] def hash(p): return .11234 * p[0] + .35678 * p[1] + .67257 * p[2] from collections import defaultdict pt_table = defaultdict(list) for i, t in enumerate(sc.faces): p1 = sc.points[t[0]] p2 = sc.points[t[1]] p3 = sc.points[t[2]] pt_table[hash(p1)].append((i, p1, t[0])) pt_table[hash(p2)].append((i, p2, t[1])) pt_table[hash(p3)].append((i, p3, t[2])) normal = vcross(sub(p2, p1), sub(p3, p1)) normal = vnorm(normal) triangle_normals[i] = normal for key, value in pt_table.iteritems(): # we assume no collisions in the hash point_index = value[0][2] first_point = value[0][1] # compute the normal of each triangles around # TODO should be done just once for each triangle in pre-process normals = [] for t_index, p, _ in value: assert p == first_point normals.append(triangle_normals[t_index]) N = (sum(n[0] for n in normals) / len(normals), sum(n[1] for n in normals) / len(normals), sum(n[2] for n in normals) / len(normals)) # print N out[point_index] = N return out scene = load(sys.argv[1]) with benchmark('compute normals'): scene.normals = compute_normals(scene) scene.write(sys.argv[2])
def test5(): scene = load(sys.argv[1]) from geom_ops import compute_normals with benchmark('compute normals'): scene.normals, scene.faces_normals = compute_normals(scene) scene.write(sys.argv[2])
def setup_vbo(self): glInitVertexBufferObjectARB() self.VBO_vertex = int(glGenBuffersARB(1)) # Get A Valid Name self.VBO_normal = int(glGenBuffersARB(1)) # Get A Valid Name # Load The Data sc = self.scene self.vbo_array_size = len(sc.faces) * 3 vertices = Numeric.zeros ( (self.vbo_array_size, 3), 'f') if self.do_lighting: normals = Numeric.zeros ( (self.vbo_array_size, 3), 'f') if not sc.normals: print 'compute normals' from geom_ops import compute_normals sc.normals, sc.faces_normals = compute_normals(sc) i = 0 for j in xrange(len(sc.faces)): t = sc.faces[j] p1 = sc.points[t[0]] p2 = sc.points[t[1]] p3 = sc.points[t[2]] vertices [i, 0] = p1[0] vertices [i, 1] = p1[1] vertices [i, 2] = p1[2] vertices [i+1, 0] = p2[0] vertices [i+1, 1] = p2[1] vertices [i+1, 2] = p2[2] vertices [i+2, 0] = p3[0] vertices [i+2, 1] = p3[1] vertices [i+2, 2] = p3[2] # print p1, p2, p3 if self.do_lighting: n = sc.faces_normals[j] n1 = sc.normals[n[0]] n2 = sc.normals[n[1]] n3 = sc.normals[n[2]] normals [i, 0] = n1[0] normals [i, 1] = n1[1] normals [i, 2] = n1[2] normals [i+1, 0] = n2[0] normals [i+1, 1] = n2[1] normals [i+1, 2] = n2[2] normals [i+2, 0] = n3[0] normals [i+2, 1] = n3[1] normals [i+2, 2] = n3[2] i += 3 glBindBufferARB( GL_ARRAY_BUFFER_ARB, self.VBO_vertex ) glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertices, GL_STATIC_DRAW_ARB ); if self.do_lighting: glBindBufferARB( GL_ARRAY_BUFFER_ARB, self.VBO_normal ) glBufferDataARB( GL_ARRAY_BUFFER_ARB, normals, GL_STATIC_DRAW_ARB ); if self.gpu_info: print glGetString (GL_VENDOR) print glGetString (GL_RENDERER) print glGetString (GL_VERSION) Extensions = glGetString (GL_EXTENSIONS) for ext in Extensions.split(): print ext return self.VBO_vertex, self.VBO_normal
def setup_vbo(self): glInitVertexBufferObjectARB() self.VBO_vertex = int(glGenBuffersARB(1)) # Get A Valid Name self.VBO_normal = int(glGenBuffersARB(1)) # Get A Valid Name # Load The Data sc = self.scene self.vbo_array_size = len(sc.faces) * 3 vertices = Numeric.zeros((self.vbo_array_size, 3), 'f') if self.do_lighting: normals = Numeric.zeros((self.vbo_array_size, 3), 'f') if not sc.normals: print 'compute normals' from geom_ops import compute_normals sc.normals, sc.faces_normals = compute_normals(sc) i = 0 for j in xrange(len(sc.faces)): t = sc.faces[j] p1 = sc.points[t[0]] p2 = sc.points[t[1]] p3 = sc.points[t[2]] vertices[i, 0] = p1[0] vertices[i, 1] = p1[1] vertices[i, 2] = p1[2] vertices[i + 1, 0] = p2[0] vertices[i + 1, 1] = p2[1] vertices[i + 1, 2] = p2[2] vertices[i + 2, 0] = p3[0] vertices[i + 2, 1] = p3[1] vertices[i + 2, 2] = p3[2] # print p1, p2, p3 if self.do_lighting: n = sc.faces_normals[j] n1 = sc.normals[n[0]] n2 = sc.normals[n[1]] n3 = sc.normals[n[2]] normals[i, 0] = n1[0] normals[i, 1] = n1[1] normals[i, 2] = n1[2] normals[i + 1, 0] = n2[0] normals[i + 1, 1] = n2[1] normals[i + 1, 2] = n2[2] normals[i + 2, 0] = n3[0] normals[i + 2, 1] = n3[1] normals[i + 2, 2] = n3[2] i += 3 glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.VBO_vertex) glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertices, GL_STATIC_DRAW_ARB) if self.do_lighting: glBindBufferARB(GL_ARRAY_BUFFER_ARB, self.VBO_normal) glBufferDataARB(GL_ARRAY_BUFFER_ARB, normals, GL_STATIC_DRAW_ARB) if self.gpu_info: print glGetString(GL_VENDOR) print glGetString(GL_RENDERER) print glGetString(GL_VERSION) Extensions = glGetString(GL_EXTENSIONS) for ext in Extensions.split(): print ext return self.VBO_vertex, self.VBO_normal