def __init__(self, file_path, make_opengl_textures=True): image_reader = QImageReader(file_path) self.is_animated = image_reader.supportsAnimation() if self.is_animated: self.num_frames = image_reader.imageCount() # -1 means loop infinitely, 0 means no loop, > 0 is finite # of loops self.loop_count = image_reader.loopCount() self.loops_remaining = 0 self.frames = [] self.delays = [] while image_reader.currentImageNumber() < image_reader.imageCount() - 1: self.frames.append(image_reader.read()) self.delays.append(image_reader.nextImageDelay()) if make_opengl_textures: self.open_gl_textures = [QOpenGLTexture(this_frame.mirrored()) for this_frame in self.frames] self.made_opengl_textures = True self.frames_and_delays = zip(self.frames, self.delays) self.current_frame = 0 self.animating = False else: self.image = image_reader.read() assert isinstance(self.image, QImage) if make_opengl_textures: self.open_gl_texture = QOpenGLTexture(self.image.mirrored()) self.made_opengl_textures = True
def __init__(self): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = OpenGL.getInstance().getBindingsObject() self._file_name = None self._image = None
def __init__(self, open_gl_binding_object): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = open_gl_binding_object self._file_name = None self._image = None
def __init__(self): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._qt_texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._gl = OpenGL.getInstance().getBindingsObject()
class QtTexture(Texture): def __init__(self): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = OpenGL.getInstance().getBindingsObject() self._file_name = None def getTextureId(self): return self._qt_texture.textureId() def bind(self, unit): if not self._qt_texture.isCreated(): if self._file_name != None: image = QImage(self._file_name).mirrored() else: image = QImage(1, 1, QImage.Format_ARGB32) self._qt_texture.setData(image) self._qt_texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._qt_texture.bind(unit) def release(self, unit): self._qt_texture.release(unit) def load(self, file_name): self._file_name = file_name
def __init__(self, open_gl_binding_object: QAbstractOpenGLFunctions) -> None: super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = open_gl_binding_object self._file_name = None self._image = None
class Texture: """A class describing the interface to be used for texture objects. This interface should be implemented by OpenGL implementations to handle texture objects. """ def __init__(self, open_gl_binding_object: QAbstractOpenGLFunctions) -> None: super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = open_gl_binding_object self._file_name = None self._image = None def getTextureId(self) -> int: """Get the OpenGL ID of the texture.""" return self._qt_texture.textureId() def bind(self, texture_unit): """Bind the texture to a certain texture unit. :param texture_unit: The texture unit to bind to. """ if not self._qt_texture.isCreated(): if self._file_name != None: self._image = QImage(self._file_name).mirrored() elif self._image is None: # No filename or image set. self._image = QImage(1, 1, QImage.Format_ARGB32) self._image.fill(0) self._qt_texture.setData(self._image) self._qt_texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._qt_texture.bind(texture_unit) def release(self, texture_unit): """Release the texture from a certain texture unit. :param texture_unit: The texture unit to release from. """ self._qt_texture.release(texture_unit) def load(self, file_name): """Load an image and upload it to the texture. :param file_name: The file name of the image to load. """ self._file_name = file_name # Actually loading the texture is postponed until the next bind() call. # This makes sure we are on the right thread and have a current context when trying to upload. def setImage(self, image): self._image = image
def load_texture(material): # Note that the QImage is mirrored vertically to account for the fact that OpenGL and QImage use opposite directions for the y axis. # (https://doc.qt.io/qt-5/qopengltexture.html#details) texture = QOpenGLTexture( QImage(get_resources_path(material['diffuse_map'])).mirrored()) texture.setMinificationFilter(QOpenGLTexture.Nearest) texture.setMagnificationFilter(QOpenGLTexture.Nearest) texture.setWrapMode(QOpenGLTexture.ClampToEdge) return texture
class Texture: def __init__(self, open_gl_binding_object: QAbstractOpenGLFunctions) -> None: super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = open_gl_binding_object self._file_name = None self._image = None ## Get the OpenGL ID of the texture. def getTextureId(self) -> int: return self._qt_texture.textureId() ## Bind the texture to a certain texture unit. # # \param texture_unit The texture unit to bind to. def bind(self, texture_unit): if not self._qt_texture.isCreated(): if self._file_name != None: self._image = QImage(self._file_name).mirrored() elif self._image is None: # No filename or image set. self._image = QImage(1, 1, QImage.Format_ARGB32) self._image.fill(0) self._qt_texture.setData(self._image) self._qt_texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._qt_texture.bind(texture_unit) ## Release the texture from a certain texture unit. # # \param texture_unit The texture unit to release from. def release(self, texture_unit): self._qt_texture.release(texture_unit) ## Load an image and upload it to the texture. # # \param file_name The file name of the image to load. def load(self, file_name): self._file_name = file_name #Actually loading the texture is postponed until the next bind() call. #This makes sure we are on the right thread and have a current context when trying to upload. def setImage(self, image): self._image = image
def makeObject(self): self.texCoords = [(True, True), (False, True), (False, False), (True, False)] self.vertices = [(0.5, -0.5, -0.5), (-0.5, -0.5, -0.5), (-0.5, 0.5, -0.5), (0.5, 0.5, -0.5)] my_movie = QImage('/Users/reno/Dropbox/media/cloudy.png') self.texture = QOpenGLTexture(my_movie.mirrored())
def load_texture(material): # Note that the QImage is mirrored vertically to account for the fact that OpenGL and QImage use opposite directions for the y axis. # (https://doc.qt.io/qt-5/qopengltexture.html#details) texture = QOpenGLTexture(QImage(get_resources_path(material['diffuse_map'])).mirrored()) texture.setMinificationFilter(QOpenGLTexture.Nearest) texture.setMagnificationFilter(QOpenGLTexture.Nearest) texture.setWrapMode(QOpenGLTexture.ClampToEdge) return texture
class Texture(object): def __init__(self, open_gl_binding_object): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = open_gl_binding_object self._file_name = None self._image = None ## Get the OpenGL ID of the texture. def getTextureId(self): return self._qt_texture.textureId() ## Bind the texture to a certain texture unit. # # \param texture_unit The texture unit to bind to. def bind(self, texture_unit): if not self._qt_texture.isCreated(): if self._file_name != None: self._image = QImage(self._file_name).mirrored() elif self._image is None: # No filename or image set. self._image = QImage(1, 1, QImage.Format_ARGB32) self._image.fill(0) self._qt_texture.setData(self._image) self._qt_texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._qt_texture.bind(texture_unit) ## Release the texture from a certain texture unit. # # \param texture_unit The texture unit to release from. def release(self, texture_unit): self._qt_texture.release(texture_unit) ## Load an image and upload it to the texture. # # \param file_name The file name of the image to load. def load(self, file_name): self._file_name = file_name #Actually loading the texture is postponed until the next bind() call. #This makes sure we are on the right thread and have a current context when trying to upload. def setImage(self, image): self._image = image
class QtTexture(Texture): def __init__(self): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._qt_texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._gl = OpenGL.getInstance().getBindingsObject() def getTextureId(self): return self._qt_texture.textureId() def bind(self, unit): self._qt_texture.bind(unit) def release(self, unit): self._qt_texture.release(unit) def load(self, file_name): image = QImage(file_name).mirrored() self._qt_texture.setData(image)
def makeObject(self): self.textures = [] self.texCoords = [] self.vertices = [] root = QFileInfo(__file__).absolutePath() for i in range(6): self.textures.append( QOpenGLTexture( QImage(root + ('/images/side%d.png' % (i + 1))).mirrored())) for j in range(4): self.texCoords.append(((j == 0 or j == 3), (j == 0 or j == 1))) x, y, z = self.coords[i][j] self.vertices.append((0.2 * x, 0.2 * y, 0.2 * z))
def attach_texture_object(self, array): if not hasattr(array, "___tex___"): target = self.to_texture_target(array) if target == None: raise RuntimeError( 'Unable to determine texture binding target') array.___tex___ = QOpenGLTexture(target) array.___tex___.setMinificationFilter(QOpenGLTexture.Linear) array.___tex___.setMagnificationFilter(QOpenGLTexture.Linear) array.___tex___.setWrapMode(QOpenGLTexture.DirectionS, QOpenGLTexture.Repeat) if target in [QOpenGLTexture.Target2D, QOpenGLTexture.Target3D]: array.___tex___.setWrapMode(QOpenGLTexture.DirectionT, QOpenGLTexture.Repeat) if target == QOpenGLTexture.Target3D: array.___tex___.setWrapMode(QOpenGLTexture.DirectionR, QOpenGLTexture.Repeat) array.___tex___.dirty = True array.productDirty.connect( lambda array=array: setattr(array.___tex___, "dirty", True))
def setUniformTexture(self, name, file): if not self._shader_program or self._disable_textures: return if name not in self._uniform_indices: self._uniform_indices[name] = self._shader_program.uniformLocation( name) index = self._uniform_indices[name] texture = QOpenGLTexture(QImage(file).mirrored()) texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._textures[index] = texture self._uniform_values[index] = 1 if self._bound: texture = self._textures[index] texture.bind() self._setUniformValueDirect(index, texture.textureId())
def setUniformTexture(self, name, file): if not self._shader_program: return if name not in self._uniform_indices: self._uniform_indices[name] = self._shader_program.uniformLocation(name) index = self._uniform_indices[name] texture = QOpenGLTexture(QImage(file).mirrored()) texture.setMinMagFilters(QOpenGLTexture.Linear, QOpenGLTexture.Linear) self._textures[index] = texture self._uniform_values[index] = 1 if self._bound: texture = self._textures[index] texture.bind() self._setUniformValueDirect(index, texture.textureId())
def createTexture(self, gl, width=None, height=None, wrapMode=QOpenGLTexture.ClampToEdge, format=QOpenGLTexture.RGBA8U, filter=QOpenGLTexture.Linear, filename=None, allocate=True, **kwparams): ''' Creates texture object with required parameters ''' assert width is not None or filename is not None if height is None: height = width if filename: texture = QOpenGLTexture(QImage('{}/{}'.format(Resources.directory, filename)).mirrored(), QOpenGLTexture.DontGenerateMipMaps) else: texture = QOpenGLTexture(QOpenGLTexture.Target2D) texture.setFormat(format) texture.setSize(width, height) if allocate: texture.allocateStorage() texture.setMinificationFilter(filter) texture.setMagnificationFilter(filter) texture.setWrapMode(wrapMode) assert texture.create() texture.bind() for k, v in kwparams.items(): gl.glTexParameteri(gl.GL_TEXTURE_2D, getattr(gl, k), v) texture.release() return texture
def asQOpenGLTexture(self, gl, context): if not self.data: return if self.glFormat.requirements: minVersion, extensions = self.glFormat.requirements glVersion = (gl.glGetIntegerv(gl.GL_MAJOR_VERSION), gl.glGetIntegerv(gl.GL_MINOR_VERSION)) if glVersion < minVersion or minVersion < (1, 0): compatible = False for extension in extensions: if context.hasExtension(extension): compatible = True break if not compatible: qCritical( self.__tr( "OpenGL driver incompatible with texture format.")) return None if self.header.dwCaps2 & DDSDefinitions.DDS_HEADER.Caps2.DDSCAPS2_CUBEMAP: texture = QOpenGLTexture(QOpenGLTexture.TargetCubeMap) if self.header.dwWidth != self.header.dwHeight: qCritical(self.__tr("Cubemap faces must be square")) return None else: # Assume GL_TEXTURE_2D for now texture = QOpenGLTexture(QOpenGLTexture.Target2D) # Assume single layer for now # self.texture.setLayers(1) mipCount = self.mipLevels() texture.setAutoMipMapGenerationEnabled(False) texture.setMipLevels(mipCount) texture.setMipLevelRange(0, mipCount - 1) texture.setSize(self.header.dwWidth, self.header.dwHeight) texture.setFormat(self.glFormat.internalFormat) texture.allocateStorage() if self.header.dwCaps2 & DDSDefinitions.DDS_HEADER.Caps2.DDSCAPS2_CUBEMAP: # Lisa hasn't whipped David Wang into shape yet. At least there are fewer bugs than under Raja. # The specific bug has been reported and AMD "will try to reproduce it soon" noDSA = "Radeon" in gl.glGetString( gl.GL_RENDERER) and self.glFormat.compressed if noDSA: texture.bind() faceIndex = 0 for face in ddsCubemapFaces: if self.header.dwCaps2 & face: for i in range(mipCount): if self.glFormat.compressed: if not noDSA: texture.setCompressedData( i, 0, ddsCubemapFaces[face], len(self.data[faceIndex * mipCount + i]), self.data[faceIndex * mipCount + i]) else: gl.glCompressedTexSubImage2D( ddsCubemapFaces[face], i, 0, 0, max(self.header.dwWidth // 2**i, 1), max(self.header.dwHeight // 2**i, 1), self.glFormat.internalFormat, len(self.data[faceIndex * mipCount + i]), self.data[faceIndex * mipCount + i]) else: texture.setData( i, 0, ddsCubemapFaces[face], self.glFormat.format, self.glFormat.type, self.glFormat.converter( self.data[faceIndex * mipCount + i])) faceIndex += 1 if noDSA: texture.release() else: for i in range(mipCount): if self.glFormat.compressed: texture.setCompressedData(i, 0, len(self.data[i]), self.data[i]) else: texture.setData(i, 0, self.glFormat.format, self.glFormat.type, self.glFormat.converter(self.data[i])) texture.setWrapMode(QOpenGLTexture.ClampToEdge) if self.glFormat.samplerType != "F": # integer textures can't be filtered texture.setMinMagFilters(QOpenGLTexture.NearestMipMapNearest, QOpenGLTexture.Nearest) return texture
def addTexture(self, imgfile): self._images.append(QImage(imgfile).mirrored()) self._textures.append(QOpenGLTexture(QOpenGLTexture.Target2D))
def __init__(self): super().__init__() self._qt_texture = QOpenGLTexture(QOpenGLTexture.Target2D) self._gl = OpenGL.getInstance().getBindingsObject() self._file_name = None