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 init_back_texture(self, width, height): if self.fbo is None: self.fbo = glGenFramebuffers(1) glActiveTexture(GL_TEXTURE0 + 1) if self.bfTex is not None: glDeleteTextures([self.bfTex]) self.bfTex = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, self.bfTex) glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) w = int(width) h = int(height) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, w, h, 0, GL_RGBA, GL_FLOAT, None) glBindFramebuffer(GL_FRAMEBUFFER, self.fbo) glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, self.bfTex, 0) glBindFramebuffer(GL_FRAMEBUFFER, 0) glBindTexture(GL_TEXTURE_2D, 0)
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 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)