def calc_normals(self): normals = np.zeros((len(self.array_buffer), 3), dtype="float32") #empty array rights size fv = self.array_buffer[self.element_array_buffer,0:3] #expand faces with x,y,z values for each vertex #cross product of two edges of triangles fn = np.cross(fv[:,1] - fv[:,0], fv[:,2] - fv[:,0]) fn = Utility.normalize_v3(fn) normals[self.element_array_buffer[:,0]] += fn #add up all normal vectors for a vertex normals[self.element_array_buffer[:,1]] += fn normals[self.element_array_buffer[:,2]] += fn return Utility.normalize_v3(normals)
def calc_normals(self): normals = np.zeros((len(self.array_buffer), 3), dtype="float32") #empty array rights size fv = self.array_buffer[self.element_array_buffer,0:3] #expand faces with x,y,z values for each vertex #cross product of two edges of triangles self.element_normals = np.cross(fv[:,1] - fv[:,0], fv[:,2] - fv[:,0]) self.element_normals = Utility.normalize_v3(self.element_normals) normals[self.element_array_buffer[:,0]] += self.element_normals #add up all normal vectors for a vertex normals[self.element_array_buffer[:,1]] += self.element_normals normals[self.element_array_buffer[:,2]] += self.element_normals return Utility.normalize_v3(normals)
def __init__(self, shape, pts, texcoords, faces, normals=None, smooth=True): """Generate a vertex buffer to hold data and indices. If no normals are provided then these are generated. Arguments: *shape* Shape object that this Buffer is a child of *pts* array of vertices tuples i.e. [(x0,y0,z0), (x1,y1,z1),...] *texcoords* array of texture (uv) coordinates tuples i.e. [(u0,v0), (u1,v1),...] *faces* array of indices (of pts array) defining triangles i.e. [(a0,b0,c0), (a1,b1,c1),...] Keyword arguments: *normals* array of vector component tuples defining normals at each vertex i.e. [(x0,y0,z0), (x1,y1,z1),...] *smooth* if calculating normals then average normals for all faces meeting at this vertex, otherwise just use first (for speed). """ super(Buffer, self).__init__() # Uniform variables all in one array! self.unib = (c_float * 12)(0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0) """ pass to shader array of vec3 uniform variables: ===== ============================ ==== == vec3 description python ----- ---------------------------- ------- index from to ===== ============================ ==== == 0 ntile, shiny, blend 0 2 1 material 3 5 2 umult, vmult, point_size 6 8 3 u_off, v_off (only 2 used) 9 10 ===== ============================ ==== == """ #self.shape = shape self.textures = [] pts = np.array(pts, dtype=float) texcoords = np.array(texcoords, dtype=float) faces = np.array(faces, dtype=int) if normals == None: #i.e. normals will only be generated if explictly None LOGGER.debug('Calculating normals ...') normals = np.zeros(pts.shape, dtype=float) #empty array rights size fv = pts[faces] #expand faces with x,y,z values for each vertex #cross product of two edges of triangles fn = np.cross(fv[:, 1] - fv[:, 0], fv[:, 2] - fv[:, 0]) fn = Utility.normalize_v3(fn) normals[faces[:, 0]] += fn #add up all normal vectors for a vertex normals[faces[:, 1]] += fn normals[faces[:, 2]] += fn normals = Utility.normalize_v3(normals) else: normals = np.array(normals) # keep a copy for speeding up the collision testing of ElevationMap self.vertices = pts self.normals = normals self.tex_coords = texcoords self.indices = faces self.material = (0.5, 0.5, 0.5, 1.0) self.__pack_data()
def __init__(self, shape, pts, texcoords, faces, normals=None, smooth=True): """Generate a vertex buffer to hold data and indices. If no normals are provided then these are generated. Arguments: *shape* Shape object that this Buffer is a child of *pts* array of vertices tuples i.e. [(x0,y0,z0), (x1,y1,z1),...] *texcoords* array of texture (uv) coordinates tuples i.e. [(u0,v0), (u1,v1),...] *faces* array of indices (of pts array) defining triangles i.e. [(a0,b0,c0), (a1,b1,c1),...] Keyword arguments: *normals* array of vector component tuples defining normals at each vertex i.e. [(x0,y0,z0), (x1,y1,z1),...] *smooth* if calculating normals then average normals for all faces meeting at this vertex, otherwise just use first (for speed). """ super(Buffer, self).__init__() # Uniform variables all in one array! self.unib = (c_float * 12)(0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0) """ pass to shader array of vec3 uniform variables: ===== ============================ ==== == vec3 description python ----- ---------------------------- ------- index from to ===== ============================ ==== == 0 ntile, shiny, blend 0 2 1 material 3 5 2 umult, vmult, point_size 6 8 3 u_off, v_off (only 2 used) 9 10 ===== ============================ ==== == """ #self.shape = shape self.textures = [] pts = np.array(pts) texcoords = np.array(texcoords) faces = np.array(faces) if normals == None: #i.e. normals will only be generated if explictly None LOGGER.debug('Calculating normals ...') normals = np.zeros(pts.shape, dtype=pts.dtype) #empty array rights size fv = pts[faces] #expand faces with x,y,z values for each vertex #cross product of two edges of triangles fn = np.cross(fv[:][:][:,1] - fv[:][:][:,0], fv[:][:][:,2] - fv[:][:][:,0]) fn = Utility.normalize_v3(fn) normals[faces[:,0]] += fn #add up all normal vectors for a vertex normals[faces[:,1]] += fn normals[faces[:,2]] += fn Utility.normalize_v3(normals) else: normals = np.array(normals) # keep a copy for speeding up the collision testing of ElevationMap self.vertices = pts self.normals = normals self.tex_coords = texcoords self.indices = faces self.material = (0.5, 0.5, 0.5, 1.0) # Pack points,normals and texcoords into tuples and convert to ctype floats. n_verts = len(pts) if len(texcoords) != n_verts: if len(normals) != n_verts: self.N_BYTES = 12 # only use pts self.array_buffer = c_floats(pts.reshape(-1).tolist()) else: self.N_BYTES = 24 # use pts and normals self.array_buffer = c_floats(np.concatenate((pts, normals), axis=1).reshape(-1).tolist()) else: self.N_BYTES = 32 # use all three NB doesn't check that normals are there self.array_buffer = c_floats(np.concatenate((pts, normals, texcoords), axis=1).reshape(-1).tolist()) self.ntris = len(faces) self.element_array_buffer = c_shorts(faces.reshape(-1))