def bind_depth_texture(self, size): """Create depth texture for shadow map.""" width, height = size texture_type = GL_TEXTURE_2D self.__depth_map_fbo = glGenFramebuffers(1) depth_map = self.__textures_ids[len(self.__textures)] glBindTexture(texture_type, depth_map) self.__textures.append(2) glTexImage2D(texture_type, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, None) glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS) glBindFramebuffer(GL_FRAMEBUFFER, self.__depth_map_fbo) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_map, 0) glDrawBuffer(GL_NONE) glReadBuffer(GL_NONE) glBindFramebuffer(GL_FRAMEBUFFER, 0)
def bind_frame_buffer(self): glBindFramebuffer(GL_FRAMEBUFFER, self.__frame_buffer) glClearColor(*self.__clear_color) for setting in self.__enable: glEnable(setting) if self.__blend_settings: glEnable(GL_BLEND) glBlendFunc(*self.__blend_settings)
def bind_to_frame_depth_buffer(self, frame_buffer, depth_buffer=None): glBindFramebuffer(GL_FRAMEBUFFER, frame_buffer) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0) if depth_buffer is not None: glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer) glBindFramebuffer(GL_FRAMEBUFFER, 0)
def paintGL(self): render_start = time.perf_counter() w, h = self.width() * self.devicePixelRatio(), self.height( ) * self.devicePixelRatio() self.ctx.enable(ModernGL.DEPTH_TEST) self.ctx.viewport = (0, 0, w, h) self.ctx.clear(0.9, 0.9, 0.9) # render Entry/Exit-Point Texture into FBO if self.color_texture is None or self.depth_texture is None: # needs to be reset on window resize self.color_texture = self.ctx.texture((w, h), 4, None) self.depth_texture = self.ctx.depth_texture((w, h), None) self.fbo = self.ctx.framebuffer(self.color_texture, self.depth_texture) # set up and draw to the offscreen buffer for the exit points self.fbo.clear() self.fbo.use() self.setup_camera(self.prog_eep) model_mat = Matrix44.from_scale(Vector3([1, 1, 1])) self.prog_eep.uniforms['ModelMat'].write( model_mat.astype('float32').tobytes()) self.draw_box_eep() if hasattr(ModernGL, "default_framebuffer"): ModernGL.default_framebuffer.use() else: # stop using the fbo (@Todo: ModernGL needs a way to handle this) glBindFramebuffer(GL_FRAMEBUFFER, 0) # set up to bind the color texture to the third texture location self.color_texture.use(2) # clear the buffer and set up the uniforms self.ctx.clear(0.9, 0.9, 0.9) self.unf_screensize.value = (w, h) self.unf_stepsize.value = self.sampling_rate / max( self.volume_size) # step size is relative to the number of voxels self.unf_volumetex.value = 0 # the volume texture is bound to the first location self.unf_transferfunc.value = 1 # the transfer function texture is bound to the second location self.unf_exitpoints.value = 2 # the exit points texture is bound to the third location self.setup_camera(self.prog_rc) # do a front face pass to perform the actual raycasting self.draw_box_rc() self.ctx.finish() self.update()
def __generate_shadows(self): """Generate shadow matrix for rotated model.""" glDisable(GL_CULL_FACE) glEnable(GL_POLYGON_OFFSET_FILL) glPolygonOffset(3, 0) self.__sh.change_shader(vertex=1, fragment=1) self.__prepare_shaders(self.__model_matrix, self.__light_matrix, True) self.__sh.bind_fbo() glClear(GL_DEPTH_BUFFER_BIT) glDrawElements(GL_TRIANGLES, View.__triangles.size, GL_UNSIGNED_SHORT, View.__triangles) glFinish() glBindFramebuffer(GL_FRAMEBUFFER, 0) self.__sh.clear()
def export(molecule, file_name, width=500, height=500, show_bonds=True, bonds_method='radii', bonds_param=None, camera=None): """ Draw the given molecule into a given file. The file type is determined by the file extension, e.g. '.png' or '.html'. By default, bonds are drawn, if this is undesired the show_bonds parameter can be set to False. For information on the bond calculation, see Molecule.calculate_bonds. If you pass a tuple of camera position, center of view and an up vector to the camera parameter, the camera will be set accordingly. Otherwise the molecule will be viewed in the direction of the z axis, with the y axis pointing upward. """ global _camera molecule.positions -= np.mean(molecule.positions, axis=0) max_atom_distance = np.max(la.norm(molecule.positions, axis=1)) if show_bonds: molecule.calculate_bonds(bonds_method, bonds_param) if camera is None: if _camera is None: camera_distance = -max_atom_distance * 2.5 camera = ((0, 0, camera_distance), (0, 0, 0), (0, 1, 0)) else: camera = _camera camera = np.array(camera) _camera = camera # Create the GR3 scene gr3.setbackgroundcolor(255, 255, 255, 0) _set_gr3_camera() _create_gr3_scene(molecule, show_bonds) glEnable(GL_DEPTH_TEST) gr3.setquality(gr3.GR3_Quality.GR3_QUALITY_OPENGL_16X_SSAA) gr3.export(file_name, width, height) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_DEPTH_TEST)
def draw(molecule, xmin=0, xmax=1, ymin=0, ymax=1, width=2000, height=2000, show_bonds=True, bonds_method='radii', bonds_param=None, camera=None): """ Draw the given molecule with the GR framework. By default, bonds are drawn, if this is undesired the show_bonds parameter can be set to False. For information on the bond calculation, see Molecule.calculate_bonds. If you pass a tuple of camera position, center of view and an up vector to the camera parameter, the camera will be set accordingly. Otherwise the molecule will be viewed in the direction of the z axis, with the y axis pointing upward. """ global _camera molecule.positions -= np.mean(molecule.positions, axis=0) max_atom_distance = np.max(la.norm(molecule.positions, axis=1)) if show_bonds: molecule.calculate_bonds(bonds_method, bonds_param) if camera is None: camera_distance = -max_atom_distance * 2.5 camera = ((0, 0, camera_distance), (0, 0, 0), (0, 1, 0)) camera = np.array(camera) _camera = camera # Create the GR3 scene gr3.setbackgroundcolor(255, 255, 255, 0) _set_gr3_camera() _create_gr3_scene(molecule, show_bonds) glEnable(GL_DEPTH_TEST) gr3.setquality(gr3.GR3_Quality.GR3_QUALITY_OPENGL_16X_SSAA) gr3.drawimage(xmin, xmax, ymin, ymax, width, height, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_DEPTH_TEST)
def prerenderTiles(self, tiles): if not tiles: return minX = self.pos[0] minY = self.pos[1] maxX = self.pos[0] + self.micronSize maxY = self.pos[1] + self.micronSize viewBox = ((minX, minY), (maxX, maxY)) newTiles = [] for tile in tiles: if tile.intersectsBox(viewBox): newTiles.append(tile) if newTiles: # Allocate memory for our texture, if needed. if not self.haveAllocatedMemory: self.bindTexture() self.refresh() self.haveAllocatedMemory = True self.numRenderedTiles += len(newTiles) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, megaTileFramebuffer) glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, self.texture, 0) glPushMatrix() glLoadIdentity() glViewport(0, 0, self.pixelSize, self.pixelSize) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, self.micronSize, self.micronSize, 0, 1, 0) glTranslatef(-self.pos[0], -self.pos[1], 0) glMatrixMode(GL_MODELVIEW) glEnable(GL_TEXTURE_2D) for tile in newTiles: tile.render(viewBox) glPopMatrix() glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
def export(molecule, file_name, width=500, height=500, show_bonds=True, bonds_method='radii', bonds_param=None, camera=None): """ Draw the given molecule into a given file. The file type is determined by the file extension, e.g. '.png' or '.html'. By default, bonds are drawn, if this is undesired the show_bonds parameter can be set to False. For information on the bond calculation, see Molecule.calculate_bonds. If you pass a tuple of camera position, center of view and an up vector to the camera parameter, the camera will be set accordingly. Otherwise the molecule will be viewed in the direction of the z axis, with the y axis pointing upward. """ global _camera molecule.positions -= np.mean(molecule.positions, axis=0) max_atom_distance = np.max(la.norm(molecule.positions, axis=1)) if show_bonds: molecule.calculate_bonds(bonds_method, bonds_param) if camera is None: if _camera is None: camera_distance = -max_atom_distance*2.5 camera = ((0, 0, camera_distance), (0, 0, 0), (0, 1, 0)) else: camera = _camera camera = np.array(camera) _camera = camera # Create the GR3 scene gr3.setbackgroundcolor(255, 255, 255, 0) _set_gr3_camera() _create_gr3_scene(molecule, show_bonds) glEnable(GL_DEPTH_TEST) gr3.export(file_name, width, height) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_DEPTH_TEST)
def draw(molecule, xmin=0, xmax=1, ymin=0, ymax=1, width=500, height=500, show_bonds=True, bonds_method='radii', bonds_param=None, camera=None): """ Draw the given molecule with the GR framework. By default, bonds are drawn, if this is undesired the show_bonds parameter can be set to False. For information on the bond calculation, see Molecule.calculate_bonds. If you pass a tuple of camera position, center of view and an up vector to the camera parameter, the camera will be set accordingly. Otherwise the molecule will be viewed in the direction of the z axis, with the y axis pointing upward. """ global _camera molecule.positions -= np.mean(molecule.positions, axis=0) max_atom_distance = np.max(la.norm(molecule.positions, axis=1)) if show_bonds: molecule.calculate_bonds(bonds_method, bonds_param) if camera is None: camera_distance = -max_atom_distance*2.5 camera = ((0, 0, camera_distance), (0, 0, 0), (0, 1, 0)) camera = np.array(camera) _camera = camera # Create the GR3 scene gr3.setbackgroundcolor(255, 255, 255, 0) _set_gr3_camera() _create_gr3_scene(molecule, show_bonds) glEnable(GL_DEPTH_TEST) gr3.setquality(gr3.GR3_Quality.GR3_QUALITY_OPENGL_2X_SSAA) gr3.drawimage(xmin, xmax, ymin, ymax, width, height, gr3.GR3_Drawable.GR3_DRAWABLE_GKS) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_DEPTH_TEST)
def bind_fbo(self): """Rebind depth map FBO for current program and shaders.""" glBindFramebuffer(GL_FRAMEBUFFER, self.__depth_map_fbo)
def unbind(self): glBindFramebuffer(GL_FRAMEBUFFER, 0)
def bind(self): glBindFramebuffer(GL_FRAMEBUFFER, self.FBO)
def unbind(self): glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)
def bind(self): #glBindTexture(GL_TEXTURE_2D, 0) glBindFramebuffer(GL_DRAW_FRAMEBUFFER, self._gl_id)
def __exit__(self, exc_type, exc_value, traceback): glBindFramebuffer(GL_FRAMEBUFFER, 0)
def __enter__(self): if self.gl_identifier is None: self.gl_identifier = glGenFramebuffers(1) glBindFramebuffer(GL_FRAMEBUFFER, self.gl_identifier) return self