75 lines
2.1 KiB
Python
75 lines
2.1 KiB
Python
from PyQt5.QtWidgets import QOpenGLWidget
|
|
from OpenGL.GL import *
|
|
from OpenGL.GLU import gluOrtho2D
|
|
|
|
|
|
class TransparentGLWidget(QOpenGLWidget):
|
|
def __init__(self, img):
|
|
super().__init__()
|
|
self.img = img
|
|
self.texture = None
|
|
self.image_width = 1
|
|
self.image_height = 1
|
|
|
|
def initializeGL(self):
|
|
glEnable(GL_BLEND)
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
|
|
glEnable(GL_DEPTH_TEST)
|
|
glClearColor(0.0, 0.0, 0.0, 0.0)
|
|
|
|
self.texture, self.image_width, self.image_height = self.load_texture()
|
|
|
|
def resizeGL(self, w, h):
|
|
glViewport(0, 0, w, h)
|
|
glMatrixMode(GL_PROJECTION)
|
|
glLoadIdentity()
|
|
gluOrtho2D(0, w, 0, h) # Orthografische Projektion für 2D
|
|
glMatrixMode(GL_MODELVIEW)
|
|
|
|
def paintGL(self):
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
|
|
glLoadIdentity()
|
|
glPushMatrix() # Matrix speichern
|
|
self.draw_textured_quad()
|
|
glPopMatrix() # Matrix wiederherstellen
|
|
|
|
def load_texture(self):
|
|
img_data = self.img.tobytes()
|
|
width, height = self.img.size
|
|
|
|
# Create OpenGL texture
|
|
texture_id = glGenTextures(1)
|
|
glBindTexture(GL_TEXTURE_2D, texture_id)
|
|
|
|
# Set texture parameters
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
|
|
|
|
# Load texture
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)
|
|
return texture_id, width, height
|
|
|
|
def draw_textured_quad(self):
|
|
try:
|
|
glEnable(GL_TEXTURE_2D)
|
|
glBindTexture(GL_TEXTURE_2D, self.texture)
|
|
|
|
width, height = self.width(), self.height()
|
|
|
|
glBegin(GL_QUADS)
|
|
glTexCoord2f(0.0, 0.0)
|
|
glVertex2f(0, 0) # Down left corner
|
|
glTexCoord2f(1.0, 0.0)
|
|
glVertex2f(width, 0) # Down right corner
|
|
glTexCoord2f(1.0, 1.0)
|
|
glVertex2f(width, height) # Up right corner
|
|
glTexCoord2f(0.0, 1.0)
|
|
glVertex2f(0, height) # Up left corner
|
|
glEnd()
|
|
|
|
glDisable(GL_TEXTURE_2D)
|
|
except:
|
|
pass
|