0
我需要在2D中顯示大量的四邊形,使用Python和OpenGL進行設計的最佳方式是什麼?我們的目標是而不是在調用paintGL時將所有數據發送到GPU,因此需要很長時間...我猜想必須存在將頂點數據存儲在GPU上的可能性,以及如何使用Python ?這裏是我的問題的工作示例:Python,OpenGL:如何在GPU上存儲頂點數據
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from OpenGL.GL import *
from PyQt4 import QtGui, QtCore, QtOpenGL
def get_squares(squareCount, edgeSize):
"""
Returns vertex list for a matrix of squares
squareCount - number of squares on one edge
edgeSize - size of the matrix edge
"""
edgeLength = edgeSize/squareCount/2
vertexList = []
x = edgeLength/2
y = edgeLength/2
z = 0
for i in range(squareCount):
for j in range(squareCount):
vertexList.append([x,y,z])
y += edgeLength
vertexList.append([x,y,z])
x += edgeLength
vertexList.append([x,y,z])
y -= edgeLength
vertexList.append([x,y,z])
x += edgeLength
x = edgeLength/2
y += 2*edgeLength
return vertexList
class OpenGLWidget(QtOpenGL.QGLWidget):
def __init__(self, squareCount = 20, parent = None):
QtOpenGL.QGLWidget.__init__(self, parent)
self.squares = get_squares(squareCount, 50)
def paintGL(self):
glBegin(GL_QUADS)
for point in self.squares:
glVertex3f(*point)
glEnd()
def resizeGL(self, w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, 50, 0, 50, 0, 1)
glViewport(0, 0, w, h)
def initializeGL(self):
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GL_COLOR_BUFFER_BIT)
if __name__ == '__main__':
import sys
app = QtGui.QApplication([])
w = OpenGLWidget(squareCount = 20) # try increasing that number and resizing the window...
# for me at squareCount = 200 (40000 squares) it's really slow
w.show()
sys.exit(app.exec_())