def addTexture(self, imgPath): dir, file = path.split(imgPath) if dir not in resource.path: resource.path.append(dir) resource.reindex() texture = resource.texture(file) self.textures.append(texture) gl.glBindTexture(texture.target, texture.id) gl.glGenerateMipmap(gl.GL_TEXTURE_2D) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST_MIPMAP_LINEAR)
def create_cubemap_texture(imgfiles): """Create a cubemap texture from image files. :param imgfiles: a list of six strings specifying the path to the cubemap images. In the order [pos+X, pos-X, pos+Y, ..., pos-Z] """ if len(imgfiles) != 6: raise ValueError( "exactly six images are required for a cubemap texture") # generate a new texture cubemap = gl.GLuint() gl.glGenTextures(1, ct.pointer(cubemap)) # bind it to `GL_TEXTURE_CUBE_MAP` and set the filter and wrap mode gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, cubemap) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_R, gl.GL_CLAMP_TO_EDGE) # it seems we need to flip the images to make the cubemap look correct faces = [ImageOps.flip(Image.open(img)) for img in imgfiles] # set the faces of the cubemap texture for i, face in enumerate(faces): width, height = face.size try: data = face.tobytes("raw", "RGBX", 0, -1) except TypeError: data = face.tobytes("raw", "RGBA", 0, -1) gl.glTexImage2D( gl.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, data, ) gl.glGenerateMipmap(gl.GL_TEXTURE_CUBE_MAP) gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, 0) return cubemap
def create_cubemap_texture(imgfiles): """Create a cubemap texture from image files. """ if len(imgfiles) != 6: raise ValueError( "exactly six images are required for a cubemap texture") # generate a new texture cubemap = gl.GLuint() gl.glGenTextures(1, ct.pointer(cubemap)) # bind it to `GL_TEXTURE_CUBE_MAP` and set the filter and wrap mode gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, cubemap) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP_TO_EDGE) gl.glTexParameteri(gl.GL_TEXTURE_CUBE_MAP, gl.GL_TEXTURE_WRAP_R, gl.GL_CLAMP_TO_EDGE) faces = [Image.open(img) for img in imgfiles] # set the faces of the cubemap texture for i, face in enumerate(faces): width, height = face.size try: data = face.tobytes("raw", "RGBX", 0, -1) except TypeError: data = face.tobytes("raw", "RGBA", 0, -1) gl.glTexImage2D( gl.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.GL_RGBA, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, data, ) gl.glGenerateMipmap(gl.GL_TEXTURE_CUBE_MAP) gl.glBindTexture(gl.GL_TEXTURE_CUBE_MAP, 0) return cubemap
def build_mipmaps(self, base: int = 0, max_level: int = 1000) -> None: """Generate mipmaps for this texture. Leaveing the default arguments will usually does the job. Building mipmaps will create several smaller versions of the texture (256 x 256, 128 x 128, 64 x 64, 32 x 32 etc) helping OpenGL in rendering a nicer version of texture when it's rendered to the screen in smaller version. Note that mipmaps will only be used if the texture filter is configured with a mipmap-type minification:: # Set up linear interpolating minification filter texture.filter = ctx.LINEAR_MIPMAP_LINEAR, ctx.LINEAR :param int base: Level the mipmaps start at (usually 0) :param int max_level: The maximum levels to generate Also see: https://www.khronos.org/opengl/wiki/Texture#Mip_maps """ self.use() gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_BASE_LEVEL, base) gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAX_LEVEL, max_level) gl.glGenerateMipmap(gl.GL_TEXTURE_2D)
def generate_mipmap(self): if self.mipmap: gl.glGenerateMipmap(self.target)
def generate_mipmaps(self): gl.glGenerateMipmap(gl.GL_TEXTURE_2D_ARRAY)
def generate_mipmaps(self): logging.debug(f"Generating Mipmaps, using mipmap type {options.MIPMAP_TYPE}") gl.glGenerateMipmap(gl.GL_TEXTURE_2D_ARRAY)
def generate_mipmaps(self): gl.glBindTexture(gl.GL_TEXTURE_2D_ARRAY, self.texture_array) # make sure our texture is bound gl.glGenerateMipmap(gl.GL_TEXTURE_2D_ARRAY) # generate mipmaps for our texture