在initializeGL
中QGLWidget
:
我正在加载一个.exr
图像(128mb)4000x2000imageio.imread
from imageio import imread
img_array = imread("c:/sample.exr")
self.textureID = glGenTextures(1)
in paintGL
in QGLWidget
:
我绘制我的单个四边形,glBegin(GL_QUADS)
然后glEnd()
将纹理输入glTexImage2D
并将其绑定到我之前创建的四边形。
glBindTexture(GL_TEXTURE_2D, self.textureID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 1920, 1080, 0, GL_RGBA, GL_FLOAT, img_array)
我加载图像或代码本身没有任何问题,一切正常,但我面临性能问题并且导航场景开始滞后,我认为是因为图像的巨大尺寸。
有什么办法可以减小尺寸吗?还是提高性能呢?
注意:即使是图像,numpy.float32
我也只显示它,GL_RGBA
这意味着有 0.4545 伽马乘法,我不知道这是否会影响性能,但我想指出这一点。
提前致谢。
更新:
这是代码:
import os
from PySide.QtGui import QColor
from PySide.QtOpenGL import QGLWidget
from imageio import imread
from OpenGL.GL import *
from OpenGL.GLU import *
from engine.nodes import (ImageNode, NodeType, CubeNode, Vector3)
class QImageProcessor(QGLWidget):
def __init__(self, parent=None):
super(QImageProcessor, self).__init__(parent)
self.model = list()
self.zoomVal = 1.2
self.local_translate = (0.0, 0.0, 0.0)
self.local_scale = (1.0, 1.0, 1.0)
self.xRot = 0
self.yRot = 0
self.zRot = 0
def initializeGL(self):
self.qglClearColor(QColor.fromCmykF(0.0, 0.1, 0.0, 0.882))
glViewport( 0, 0, self.width(), self.height())
glEnable(GL_TEXTURE_2D)
glEnable(GL_CULL_FACE)
glEnable(GL_MULTISAMPLE)
glEnable(GL_LINE_SMOOTH, GL_LINE_WIDTH, GL_ALIASED_LINE_WIDTH_RANGE)
glShadeModel(GL_FLAT)
glEnable(GL_DEPTH_TEST)
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST)
glDepthRange (0.1, 1.0)
# adding images
image1 = "c:/sample.exr"
new_image1 = ImageNode(image1)
new_image1.TextureID = glGenTextures(1)
new_image1.Data = imread(image1, format='.exr')
self.model.append(new_image1)
image2 = "c:/sample2.jpg"
new_image2 = ImageNode(image2)
new_image2.TextureID = glGenTextures(1)
new_image2.Data = imread(image2, format='.jpg')
self.model.append(new_image2)
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
gluOrtho2D(-self.zoomVal, +self.zoomVal, -self.zoomVal, +self.zoomVal)
glLoadIdentity()
glTranslated(*self.local_translate)
glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
glScalef(*self.local_scale)
genList = glGenLists(1)
glNewList(genList, GL_COMPILE)
for node in self.model:
vertices = node.Vertices # list of vertices
edges = node.Edges # list of edges
face = node.Face # list of faces
texcoords = node.TextureCoordinates # list of texture coordinates
glPushAttrib(GL_ALL_ATTRIB_BITS)
glPolygonMode(GL_FRONT, GL_FILL)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, node.TextureID)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, node.InternalFormat, node.Width, node.Height, 0, node.Format, node.Type, node.Data)
# face and UV
glBegin(GL_QUADS)
self.qglColor(QColor(255,255,255))
for vertex in face:
glVertex3fv(vertices[vertex])
glTexCoord2f(texcoords[vertex][0], texcoords[vertex][1])
glEnd()
glPopAttrib()
glEndList()
glCallList(genList)
我试过这个:
我转移glTexCoord2f
到initializeGL
函数,绑定一次,然后将它绑定到 0:
image2 = "c:/sample2.jpg"
new_image2 = ImageNode(image2)
new_image2.TextureID = glGenTextures(1)
new_image2.Data = imread(image2, format='.jpg')
glTexImage2D(GL_TEXTURE_2D, 0, new_image2.InternalFormat, new_image2.Width, new_image2.Height, 0, new_image2.Format, new_image2.Type, new_image2.Data)
glBindTexture(GL_TEXTURE_2D, new_image2.TextureID)
glBindTexture(GL_TEXTURE_2D, 0)
self.model.append(new_image2)
然后paintGL
再次绑定它,
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
gluOrtho2D(-self.zoomVal, +self.zoomVal, -self.zoomVal, +self.zoomVal)
glLoadIdentity()
glTranslated(*self.local_translate)
glRotated(self.xRot / 16.0, 1.0, 0.0, 0.0)
glRotated(self.yRot / 16.0, 0.0, 1.0, 0.0)
glRotated(self.zRot / 16.0, 0.0, 0.0, 1.0)
glScalef(*self.local_scale)
genList = glGenLists(1)
glNewList(genList, GL_COMPILE)
for node in self.model:
vertices = node.Vertices # list of vertices
edges = node.Edges # list of edges
face = node.Face # list of faces
texcoords = node.TextureCoordinates # list of texture coordinates
glPushAttrib(GL_ALL_ATTRIB_BITS)
glPolygonMode(GL_FRONT, GL_FILL)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, node.TextureID)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# face and UV
glBegin(GL_QUADS)
self.qglColor(QColor(255,255,255))
for vertex in face:
glVertex3fv(vertices[vertex])
glTexCoord2f(texcoords[vertex][0], texcoords[vertex][1])
glEnd()
glPopAttrib()
glEndList()
glCallList(genList)
但它没有显示纹理,不确定我是否遗漏了什么。