def transform_wavefront_file(src_file_path, dest_file_path, transform_matrix): dest_dir = path.dirname(dest_file_path) if (not path.exists(dest_dir)): os.makedirs(dest_dir) src_file = open(src_file_path, 'r') dest_file = open(dest_file_path, 'w') # Keep a separated non-translation matrix to use on the mesh's vertex normal non_translation_matrix = Matrix44.from_matrix33(transform_matrix.matrix33) # print("non_translation_matrix: {}".format(non_translation_matrix)) # Parse each lines in the original mesh file for line in src_file: line_args = line.split() if len(line_args): type = line_args[0] # Transform each vertex if (type == 'v'): src_vertex = pyrr.Vector4([float(line_args[1]), float(line_args[2]), float(line_args[3]), 1.0]) dest_vertex = transform_matrix * src_vertex dest_file.write("v {:.6f} {:.6f} {:.6f}\n".format(dest_vertex.x, dest_vertex.y, dest_vertex.z)) continue # Transform each vertex normal of the mesh elif (type == 'vn'): src_vertex = pyrr.Vector4([float(line_args[1]), float(line_args[2]), float(line_args[3]), 1.0]) dest_vertex = non_translation_matrix * src_vertex dest_vertex = pyrr.vector.normalize([dest_vertex.x, dest_vertex.y, dest_vertex.z]) dest_file.write("vn {:.6f} {:.6f} {:.6f}\n".format(dest_vertex[0], dest_vertex[1], dest_vertex[2])) continue dest_file.write(line) src_file.close() dest_file.close()
def render(self, batch_size): warp = np.empty( (batch_size, self.height, self.width, 2), dtype=np.float32) for i in range(batch_size): translation = Matrix44.from_translation(( np.random.uniform(self.x_low, self.x_high), np.random.uniform(self.y_low, self.y_high), 0.0 )) rotation = Matrix44.from_matrix33( self.rand_rotation_matrix(self.deflection) ) view = Matrix44.look_at( (0.0, 0.0, np.random.uniform(self.close, self.far)), (0.0, 0.0, 0.0), (0.0, 1.0, 0.0), ) projection = Matrix44.perspective_projection( 45.0, self.width / self.height, 0.1, 1000.0 ) # TODO: translation or rotation first? transform = projection * view * translation * rotation self.fbo.use() self.fbo.clear() self.mvp.write(transform.astype('f4').tobytes()) self.vao.render() framebuffer = self.fbo.read(components=2, floats=True) warp[i] = np.frombuffer(framebuffer, dtype=np.float32).reshape( (self.height, self.width, 2))[::-1] return warp
def render(self, world_coords, colors, triangles, projection, rotation, translation, out_shape=(224, 224)): # Matrices and Uniforms # fov_rad = 2 * np.arctan(120 / focal_length) # (film_size / 2) / focal_length # fov_deg = fov_rad * 180 / np.pi # projection_mat = Matrix44.perspective_projection(fov_deg, 1.0, 1000, 1200) translation_mat = Matrix44.from_translation(translation) projection_mat = Matrix44.from_matrix33(projection.T) rotation_mat = Matrix44.from_matrix33(rotation) translation = self.prog['translation'] projection = self.prog['projection'] rotation = self.prog['rotation'] translation.write(translation_mat.astype('f4').tobytes()) projection.write(projection_mat.astype('f4').tobytes()) rotation.write(rotation_mat.astype('f4').tobytes()) # vertex array and buffer (binding the mesh) vbo = self.ctx.buffer(get_vertex_data(world_coords, colors, triangles)) vao = self.ctx.simple_vertex_array(self.prog, vbo, 'in_vert', 'in_color') # frame buffer setup rbo = self.ctx.renderbuffer(out_shape, components=4, dtype='f4') dbo = self.ctx.depth_renderbuffer(out_shape) fbo = self.ctx.framebuffer([rbo], dbo) fbo.use() fbo.clear(0.0, 0.0, 0.0, 0.0, depth=1.0) # render vao.render() data = np.fliplr(np.frombuffer(fbo.read(components=4), dtype=np.dtype('u1')).reshape((out_shape[1], out_shape[0], 4))) # release memory fbo.release() rbo.release() dbo.release() vbo.release() vao.release() return data
def render(self, obj, va, col, bright=0): """Render the obj and its images in bounded 3D space.""" vsqr = self.visibility**2 rotation = Matrix44.from_matrix33(obj.rot) i, j, k = map(sign, self.pos - obj.pos) for position in product(*zip(obj.pos, obj.pos + [i * 12, j * 12, k * 9])): if sum((self.pos - position)**2) > vsqr: continue model = rotation @ Matrix44.from_translation(position) self.prog['model'].write(model.astype(np.float32).tobytes()) self.prog['color'].write(color('Background').tobytes()) va.render(moderngl.LINES) self.prog['color'].write(color(col, bright).tobytes()) va.render(moderngl.TRIANGLES)