PyQt5 und PYOPENGL
Verfasst: Samstag 3. Dezember 2022, 17:13
Hallo ich probiere ein 3D modell (ein Würfel) in einer GUI darzustellen. Das klappt aufch. Jtzt möchte ich dem würfel eine Textur geben das klappt aber nicht kann mir jemand helfen?
Herzlichen Dank
Herzlichen Dank
Code: Alles auswählen
import math
import sys
import numpy as np
from OpenGL.GL import *
from OpenGL.raw.GLU import gluPerspective
from PyQt5.QtGui import QOpenGLVersionProfile, QColor
from PyQt5.QtWidgets import QOpenGLWidget, QApplication
from PyQt5.QtCore import pyqtSignal, QPoint, Qt
from PIL import Image, ImageQt
class PyQtOpenGL(QOpenGLWidget):
x_rotation_changed = pyqtSignal(int)
y_rotation_changed = pyqtSignal(int)
z_rotation_changed = pyqtSignal(int)
def __init__(self, parent=None):
super().__init__(parent)
self.paint_0 = True
self.paint_1 = True
self.paint_2 = True
self.resize_lines = True
self.resize_lines = False
self.img = Image.open("erde.png")
self.mapWidth, self.mapHeight = self.img.size
pgImData = np.asarray(self.img)
self.inputMapFile = np.flipud(pgImData)
self.clear = QColor.fromCmykF(0.0, 0.0, 0.0, 0.0)
self.paint_rotation = True
self.paint_rotation = False
self.x_rotation = 0
self.y_rotation = 0
self.z_rotation = 0
self.last_pos = QPoint()
def normalize_angle(self, angle):
while angle < 0:
angle += 360 * 16
while angle > 360 * 16:
angle -= 360 * 16
return angle
def set_x_rotatio(self, angle):
angle = self.normalize_angle(angle)
if angle != self.x_rotation:
self.x_rotation = angle
self.x_rotation_changed.emit(angle)
self.update()
def set_y_rotatio(self, angle):
angle = self.normalize_angle(angle)
if angle != self.y_rotation:
self.y_rotation = angle
self.y_rotation_changed.emit(angle)
self.update()
def set_z_rotatio(self, angle):
angle = self.normalize_angle(angle)
if angle != self.z_rotation:
self.z_rotation = angle
self.z_rotation_changed.emit(angle)
self.update()
def initializeGL(self):
version_profile = QOpenGLVersionProfile()
version_profile.setVersion(2, 0)
self.gl = self.context().versionFunctions(version_profile)
self.gl.initializeOpenGLFunctions()
self.setClearColor(self.clear.darker())
self.object = self.makeObject()
self.gl.glShadeModel(self.gl.GL_SMOOTH)
self.gl.glEnable(self.gl.GL_DEPTH_TEST)
self.gl.glEnable(self.gl.GL_CULL_FACE)
self.gl.glEnable(self.gl.GL_LIGHTING)
self.gl.glLightModelfv(self.gl.GL_LIGHT_MODEL_AMBIENT, [0.9, 0.9, 0.9, 1.0])
self.gl.glEnable(self.gl.GL_COLOR_MATERIAL)
self.gl.glColorMaterial(self.gl.GL_FRONT, self.gl.GL_AMBIENT_AND_DIFFUSE)
self.gl.glActiveTexture(self.gl.GL_TEXTURE0)
self.text_obj = self.gl.glGenTextures(1)
self.gl.glBindTexture(self.gl.GL_TEXTURE_2D, self.text_obj)
self.gl.glPixelStorei(self.gl.GL_UNPACK_ALIGNMENT, 1)
self.gl.glTexImage2D(self.gl.GL_TEXTURE_2D, 0, self.gl.GL_RGB, self.mapWidth, self.mapHeight, 0, self.gl.GL_RGB, self.gl.GL_UNSIGNED_BYTE, self.inputMapFile.tobytes())
self.gl.glPixelStorei(self.gl.GL_UNPACK_ALIGNMENT, 4)
self.gl.glTexParameterf(self.gl.GL_TEXTURE_2D, self.gl.GL_TEXTURE_MAG_FILTER, self.gl.GL_LINEAR)
self.gl.glTexParameterf(self.gl.GL_TEXTURE_2D, self.gl.GL_TEXTURE_MIN_FILTER, self.gl.GL_LINEAR)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0.0, 0.0, -10.0)
glRotatef(self.x_rotation/ 16.0, 1.0, 0.0, 0.0)
glRotatef(self.y_rotation/ 16.0, 0.0, 1.0, 0.0)
glRotatef(self.z_rotation/ 16.0, 0.0, 0.0, 1.0)
self.gl.glEnable(self.gl.GL_TEXTURE_2D)
self.gl.glBindTexture(self.gl.GL_TEXTURE_2D, self.text_obj)
self.gl.glColor3f(1, 1, 1)
self.gl.glCallList(self.object)
#self.draw()
def makeObject(self):
genList = self.gl.glGenLists(1)
self.gl.glNewList(genList, self.gl.GL_COMPILE)
self.gl.glBegin(self.gl.GL_QUADS)
#mistake
UResolution = 32
VResolution = 32
r = 0.3
startU = 0
startV = 0
endU = math.pi * 2
endV = math.pi
stepU = (endU-startU)/UResolution # step size between U-points on the grid
stepV = (endV-startV)/VResolution # step size between V-points on the grid
for i in range(UResolution): # U-points
for j in range(VResolution): # V-points
t0 = [i/UResolution, 1-j/VResolution]
t1 = [i/UResolution, 1-(j+1)/VResolution]
t2 = [(i+1)/UResolution, 1-j/VResolution]
t3 = [(i+1)/UResolution, 1-(j+1)/VResolution]
#code of the quader
self.gl.glTexCoord2f(*t0)
self.gl.glVertex3f(1.0, 1.0,-1.0)
self.gl.glTexCoord2f(*t1)
self.gl.glVertex3f(-1.0, 1.0,-1.0)
self.gl.glTexCoord2f(*t2)
self.gl.glVertex3f(-1.0, 1.0, 1.0)
self.gl.glTexCoord2f(*t3)
self.gl.glVertex3f( 1.0, 1.0, 1.0)
self.gl.glTexCoord2f(*t0)
self.gl.glVertex3f( 1.0,-1.0, 1.0)
self.gl.glTexCoord2f(*t1)
self.gl.glVertex3f(-1.0,-1.0, 1.0)
self.gl.glTexCoord2f(*t2)
self.gl.glVertex3f(-1.0,-1.0,-1.0)
self.gl.glTexCoord2f(*t3)
self.gl.glVertex3f( 1.0,-1.0,-1.0)
self.gl.glTexCoord2f( *t0)
self.gl.glVertex3f( 1.0, 1.0, 1.0)
self.gl.glTexCoord2f(*t1)
self.gl.glVertex3f(-1.0, 1.0, 1.0)
self.gl.glTexCoord2f(*t2)
self.gl.glVertex3f(-1.0,-1.0, 1.0)
self.gl.glTexCoord2f(*t3)
self.gl.glVertex3f( 1.0,-1.0, 1.0)
self.gl.glTexCoord2f(*t0)
self.gl.glVertex3f( 1.0,-1.0,-1.0)
self.gl.glTexCoord2f(*t1)
self.gl.glVertex3f(-1.0,-1.0,-1.0)
self.gl.glTexCoord2f(*t2)
self.gl.glVertex3f(-1.0, 1.0,-1.0)
self.gl.glTexCoord2f(*t3)
self.gl.glVertex3f( 1.0, 1.0,-1.0)
self.gl.glTexCoord2f(*t0)
self.gl.glTexCoord2f(10.0, 20.0)
self.gl.glTexCoord2f(*t1)
self.gl.glVertex3f(-1.0, 1.0, 1.0)
self.gl.glTexCoord2f(*t2)
self.gl.glVertex3f(-1.0, 1.0,-1.0)
self.gl.glTexCoord2f(*t3)
self.gl.glVertex3f(-1.0,-1.0,-1.0)
self.gl.glTexCoord2f(*t0)
self.gl.glVertex3f(-1.0,-1.0, 1.0)
self.gl.glTexCoord2f(*t0)
self.gl.glTexCoord2f(10.0, 20.0)
self.gl.glTexCoord2f(*t1)
self.gl.glVertex3f( 1.0, 1.0,-1.0)
self.gl.glTexCoord2f(*t2)
self.gl.glVertex3f( 1.0, 1.0, 1.0)
self.gl.glTexCoord2f(*t3)
self.gl.glVertex3f( 1.0,-1.0, 1.0)
self.gl.glTexCoord2f(*t0)
self.gl.glVertex3f( 1.0,-1.0,-1.0)
self.gl.glEnd()
self.gl.glEndList()
return genList
def draw(self):
if self.paint_rotation:
glBegin(GL_QUADS)
glColor3f(0.0,1.0,0.0)
glVertex3f( 1.0, 1.0,-1.0)
glVertex3f(-1.0, 1.0,-1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f( 1.0, 1.0, 1.0)
glColor3f(1.0,0.0,0.0)
glVertex3f( 1.0,-1.0, 1.0)
glVertex3f(-1.0,-1.0, 1.0)
glVertex3f(-1.0,-1.0,-1.0)
glVertex3f( 1.0,-1.0,-1.0)
glColor3f(0.0,1.0,0.0)
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0,-1.0, 1.0)
glVertex3f( 1.0,-1.0, 1.0)
glColor3f(1.0,1.0,0.0)
glVertex3f( 1.0,-1.0,-1.0)
glVertex3f(-1.0,-1.0,-1.0)
glVertex3f(-1.0, 1.0,-1.0)
glVertex3f( 1.0, 1.0,-1.0)
glColor3f(0.0,0.0,1.0)
glVertex3f(-1.0, 1.0, 1.0)
glVertex3f(-1.0, 1.0,-1.0)
glVertex3f(-1.0,-1.0,-1.0)
glVertex3f(-1.0,-1.0, 1.0)
glColor3f(1.0,0.0,1.0)
glVertex3f( 1.0, 1.0,-1.0)
glVertex3f( 1.0, 1.0, 1.0)
glVertex3f( 1.0,-1.0, 1.0)
glVertex3f( 1.0,-1.0,-1.0)
glEnd()
if self.paint_0:
glColor3f(1.0, 0.0, 0.0)
glRectf(-0, -0, 0, 0)
if self.paint_1:
glColor3f(0.0, 1.0, 0.0)
x=10
y=10
self.draw_loop(x, y)
if self.paint_2:
glColor3f(0.0, 0.0, 0.0)
x=5
y=5
self.draw_loop(x, y)
def resizeGL(self, width, height):
side = min(width, height)
if side < 0:
return
glViewport((width -side) //2, (height - side) // 2, side, side)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if self.resize_lines:
glOrtho(-50, 50, -50, 50, -50.0, 50.0)
else:
glOrtho(-2, +2, -2, +2, 1.0, 15.0)
glMatrixMode(GL_MODELVIEW)
def draw_loop(self, x, y, incr=10):
for _ in range(5):
self.draw_square_lines(x, y)
x += incr
y += incr
def draw_square_lines(self, x=10, y=10, z=10):
glBegin(GL_LINES)
glVertex3f(x, y, z)
glVertex3f(x, -y, z)
glVertex3f(x, -y, z)
glVertex3f(-x, -y, z)
glVertex3f(-x, -y, z)
glVertex3f(-x, y, z)
glVertex3f(-x, y, z)
glVertex3f(x, y, z)
glEnd()
def mousePressEvent(self, event):
self.last_pos = event.pos()
def mouseMoveEvent(self, event):
move_x = event.x() - self.last_pos.x()
move_y = event.y() - self.last_pos.y()
if event.buttons() & Qt.LeftButton:
self.set_x_rotatio(self.x_rotation + 8 * move_y)
self.set_y_rotatio(self.y_rotation + 8 * move_x)
elif event.buttons() & Qt.RightButton:
self.set_x_rotatio(self.x_rotation + 8 * move_y)
self.set_z_rotatio(self.z_rotation + 8 * move_x)
self.last_pos = event.pos()
def setClearColor(self, c):
self.gl.glClearColor(c.redF(), c.greenF(), c.blueF(), c.alphaF())
if __name__ == '__main__':
app = QApplication(sys.argv)
widget = PyQtOpenGL()
widget.show()
app.exec_()