def __init__(self, models_cad_files, samples=1, vertex_tmp_store_folder='.', clamp=False): self._samples = samples self._context = gu.OffscreenContext() # FBO W, H = Renderer.MAX_FBO_WIDTH, Renderer.MAX_FBO_HEIGHT self._fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB8, W, H), GL_COLOR_ATTACHMENT1: gu.Texture(GL_TEXTURE_2D, 1, GL_R32F, W, H), GL_DEPTH_ATTACHMENT: gu.Renderbuffer(GL_DEPTH_COMPONENT32F, W, H) }) self._fbo_depth = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB8, W, H), GL_COLOR_ATTACHMENT1: gu.Texture(GL_TEXTURE_2D, 1, GL_R32F, W, H), GL_DEPTH_ATTACHMENT: gu.Renderbuffer(GL_DEPTH_COMPONENT32F, W, H) }) glNamedFramebufferDrawBuffers( self._fbo.id, 2, np.array((GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1), dtype=np.uint32)) glNamedFramebufferDrawBuffers( self._fbo_depth.id, 2, np.array((GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1), dtype=np.uint32)) if self._samples > 1: self._render_fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.TextureMultisample(self._samples, GL_RGB8, W, H, True), GL_COLOR_ATTACHMENT1: gu.TextureMultisample(self._samples, GL_R32F, W, H, True), GL_DEPTH_STENCIL_ATTACHMENT: gu.RenderbufferMultisample(self._samples, GL_DEPTH32F_STENCIL8, W, H) }) glNamedFramebufferDrawBuffers( self._render_fbo.id, 2, np.array((GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1), dtype=np.uint32)) self._fbo.bind() # VAO attributes = gu.geo.load_meshes_sixd(models_cad_files, vertex_tmp_store_folder, recalculate_normals=False) vertices = [] indices = [] for vertex, normal, color, faces in attributes: indices.append(faces.flatten()) vertices.append( np.hstack((vertex, normal, color / 255.0)).flatten()) indices = np.hstack(indices).astype(np.uint32) vertices = np.hstack(vertices).astype(np.float32) vao = gu.VAO( { (gu.Vertexbuffer(vertices), 0, 9 * 4): [(0, 3, GL_FLOAT, GL_FALSE, 0 * 4), (1, 3, GL_FLOAT, GL_FALSE, 3 * 4), (2, 3, GL_FLOAT, GL_FALSE, 6 * 4)] }, gu.EBO(indices)) vao.bind() # IBO vertex_count = [np.prod(vert[3].shape) for vert in attributes] instance_count = np.ones(len(attributes)) first_index = [ sum(vertex_count[:i]) for i in xrange(len(vertex_count)) ] vertex_sizes = [vert[0].shape[0] for vert in attributes] base_vertex = [ sum(vertex_sizes[:i]) for i in xrange(len(vertex_sizes)) ] base_instance = np.zeros(len(attributes)) ibo = gu.IBO(vertex_count, instance_count, first_index, base_vertex, base_instance) ibo.bind() gu.Shader.shader_folder = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'shader') # if clamp: # shader = gu.Shader('depth_shader_phong.vs', 'depth_shader_phong_clamped.frag') # else: shader = gu.Shader('depth_shader_phong.vs', 'depth_shader_phong.frag') shader.compile_and_use() self._scene_buffer = gu.ShaderStorage(0, gu.Camera().data, True) self._scene_buffer.bind() glEnable(GL_DEPTH_TEST) glClearColor(0.0, 0.0, 0.0, 1.0)
def __init__(self, models_cad_files, samples, W, H, vertex_tmp_store_folder='.', debug_mode=False): self.W, self.H = W, H self._context = gu.OffscreenContext() self._samples = 1 self._rgb_tex = gu.Texture(GL_TEXTURE_2D, 1, GL_RGB32F, self.W, self.H) self._rgb_tex.setFilter(GL_NEAREST, GL_NEAREST) self._rgb_tex.setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE) self._edge_tex = gu.Texture(GL_TEXTURE_2D, 1, GL_RGB32F, self.W, self.H) self._edge_tex.setFilter(GL_NEAREST, GL_NEAREST) self._edge_tex.setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE) # TEXTURE BUFFER handles = [self._rgb_tex.makeResident(), self._edge_tex.makeResident()] self._material_buffer = gu.ShaderStorage( 2, np.array(handles, dtype=np.uint64), False) self._material_buffer.bind() self._fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: self._rgb_tex, GL_DEPTH_ATTACHMENT: gu.Renderbuffer(GL_DEPTH_COMPONENT24, self.W, self.H) }) # FOR GRADIENTS if debug_mode: self._gradient_fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.Texture(GL_TEXTURE_2D, 1, GL_R32F, self.W, self.H), GL_COLOR_ATTACHMENT1: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB32F, self.W, self.H), GL_COLOR_ATTACHMENT2: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB32F, self.W, self.H) }) glNamedFramebufferDrawBuffers( self._gradient_fbo.id, 3, np.array((GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2), dtype=np.uint32)) else: self._gradient_fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB32F, self.W, self.H) }) # VAO vert_norms = gu.geo.load_meshes(models_cad_files, vertex_tmp_store_folder, recalculate_normals=True) vertices = np.empty(0, dtype=np.float32) self.min_vert = {} self.max_vert = {} for obj_id, vert_norm in enumerate(vert_norms): vertices = np.hstack( (vertices, np.hstack( (vert_norm[0], vert_norm[1])).reshape(-1))) # if obj==obj_id: self.min_vert[obj_id] = np.min(vert_norm[0], axis=0) self.max_vert[obj_id] = np.max(vert_norm[0], axis=0) print self.min_vert, self.max_vert vao = gu.VAO({ (gu.Vertexbuffer(vertices), 0, 6 * 4): [(0, 3, GL_FLOAT, GL_FALSE, 0 * 4), (1, 3, GL_FLOAT, GL_FALSE, 3 * 4)] }) vao.bind() # IBO sizes = [vert[0].shape[0] for vert in vert_norms] offsets = [sum(sizes[:i]) for i in xrange(len(sizes))] ibo = gu.IBO(sizes, np.ones(len(vert_norms)), offsets, np.zeros(len(vert_norms))) ibo.bind() gu.Shader.shader_folder = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'shader') # self.scene_shader = gu.Shader('common_shader.vs', 'common_shader.frag') # self.scene_shader.compile() # self.edge_shader = gu.Shader('screen.vs', 'edge_shader.frag') # self.edge_shader.compile_and_use() # glUniform1i(1, 1 if debug_mode else 0) # self.outline_shader = gu.Shader('screen.vs', 'edge_shader_lineout.frag') # self.outline_shader.compile() # self.screen_shader = gu.Shader('screen.vs', 'screen.frag') # self.screen_shader.compile() self.line_shader = gu.Shader('line.vs', 'line.frag') self.line_shader.compile() self._scene_buffer = gu.ShaderStorage(0, gu.Camera().data, True) self._scene_buffer.bind() glClearColor(0.0, 0.0, 0.0, 1.0) self.camera = gu.Camera() self.debug_mode = debug_mode glLineWidth(3)
texture_index = 0 vertices = np.array([-0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5], dtype=np.float32) tex_coords = np.array([0, 0, 1, 0, 0, 1, 1, 1], dtype=np.float32) colors = np.array([255, 0, 0, 0, 255, 0, 0, 0, 255, 0, 255, 255], dtype=np.uint8) indices = np.array([0, 1, 2, 2, 1, 3], dtype=np.uint8) ibo_data = np.array([6, 1, 0, 0, 0], dtype=np.uint32) ibo = gu.Buffer(ibo_data) color_buffer = gu.Buffer(colors, GL_DYNAMIC_STORAGE_BIT|GL_MAP_WRITE_BIT|GL_MAP_PERSISTENT_BIT) vao = gu.VAO( { (gu.Buffer(vertices), 0, 2*4): [(0, 2, GL_FLOAT, GL_FALSE, 0)], (color_buffer, 0, 3): [(1, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0)], (gu.Buffer(tex_coords), 0, 2*4): [(2, 2, GL_FLOAT, GL_FALSE, 0)] }, gu.Buffer(indices) ) vao.bind() ibo.bind(GL_DRAW_INDIRECT_BUFFER) path = '/home/dimitri/Desktop/kittens-cat-cat-puppy-rush-45170.jpeg' import glob paths = glob.glob('/home/dimitri/Pictures/*.png')[:2] tex = gu.build_textures(paths) texture_buffer = gu.build_texture_buffer(*tex)
def __init__(self, models_cad_files, samples=1, vertex_tmp_store_folder='.', vertex_scale=1.): self._samples = samples self._context = gu.OffscreenContext() # FBO W, H = Renderer.MAX_FBO_WIDTH, Renderer.MAX_FBO_HEIGHT self._fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB8, W, H), GL_COLOR_ATTACHMENT1: gu.Texture(GL_TEXTURE_2D, 1, GL_R32F, W, H), GL_DEPTH_ATTACHMENT: gu.Renderbuffer(GL_DEPTH_COMPONENT32F, W, H) }) self._fbo_depth = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.Texture(GL_TEXTURE_2D, 1, GL_RGB8, W, H), GL_COLOR_ATTACHMENT1: gu.Texture(GL_TEXTURE_2D, 1, GL_R32F, W, H), GL_DEPTH_ATTACHMENT: gu.Renderbuffer(GL_DEPTH_COMPONENT32F, W, H) }) glNamedFramebufferDrawBuffers( self._fbo.id, 2, np.array((GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1), dtype=np.uint32)) glNamedFramebufferDrawBuffers( self._fbo_depth.id, 2, np.array((GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1), dtype=np.uint32)) if self._samples > 1: self._render_fbo = gu.Framebuffer({ GL_COLOR_ATTACHMENT0: gu.TextureMultisample(self._samples, GL_RGB8, W, H, True), GL_DEPTH_ATTACHMENT: gu.RenderbufferMultisample(self._samples, GL_DEPTH_COMPONENT32F, W, H) }) # VAO vert_norms = gu.geo.load_meshes(models_cad_files, vertex_tmp_store_folder, recalculate_normals=True) vertices = np.empty(0, dtype=np.float32) for vert_norm in vert_norms: _verts = vert_norm[0] * vertex_scale vertices = np.hstack((vertices, np.hstack( (_verts, vert_norm[1])).reshape(-1))) vao = gu.VAO({ (gu.Vertexbuffer(vertices), 0, 6 * 4): [(0, 3, GL_FLOAT, GL_FALSE, 0 * 4), (1, 3, GL_FLOAT, GL_FALSE, 3 * 4)] }) sizes = [vert[0].shape[0] for vert in vert_norms] offsets = [sum(sizes[:i]) for i in xrange(len(sizes))] ibo = gu.IBO(sizes, np.ones(len(vert_norms)), offsets, np.zeros(len(vert_norms))) gu.Shader.shader_folder = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'shader') shader = gu.Shader('cad_shader.vs', 'cad_shader.frag') shader.compile() self._scene_buffer = gu.ShaderStorage(0, gu.Camera().data, True) self._fbo.bind() self._scene_buffer.bind() vao.bind() ibo.bind() shader.use() glEnable(GL_DEPTH_TEST) glClearColor(0.0, 0.0, 0.0, 1.0)