qcustom使用OpenGL加速

qcustom使用OpenGL加速
qcustomplot开启opengl加速渲染,提示QOpenGLFramebufferObject::bind() called from incompatible context。
在QCPPaintBufferGlFbo::draw()前面加if(QOpenGLContext::currentContext() != mGlContext.data()) {
mGlContext.data()->makeCurrent(mGlContext.data()->surface());
}
使用setOpengl 出现这个情况:
void __cdecl QCustomPlot::setOpenGl(bool,int) QCustomPlot can’t use OpenGL because QCUSTOMPLOT_USE_OPENGL was not defined during compilation (add ‘DEFINES += QCUSTOMPLOT_USE_OPENGL’ to your qmake .pro file)
在pro文件加:Qt +=opengl
添加依赖项:
DEFINES += QCUSTOMPLOT_USE_OPENGL
如果报错不能使用glu的库,这个情况是缺少库文件:
加载qt5.x自带的OpenGL库文件:LIBS+=-lopengl32 -lglu32。
可以看到OpenGL已经进行显卡绘图了

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
lib绘图? 使用PyQt的QChart模块可以很容易地将matplotlib图形嵌入到Qt应用程序中。QChart模块为做出出色的数据可视化提供了无限制的可能性,并且可以使用硬件加速技术进行加速,例如OpenGL。 下面是如何使用带有硬件加速OpenGL的Matplotlib和PyQt的示例程序: ```python import sys import numpy as np import matplotlib matplotlib.use('Qt5Agg') from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtCore import Qt from PyQt5.QtGui import QOpenGLShader, QOpenGLShaderProgram, QOpenGLContext, QSurfaceFormat from PyQt5.QtOpenGL import QOpenGLWidget from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure class GLWidget(QOpenGLWidget): def __init__(self, parent=None): super().__init__(parent) self.shader_prog = None self.vertices = None self.vao = None self.create_vertices() self.setup_opengl() def create_vertices(self): # Create N random heights. N = 100 self.vertices = np.zeros((N, 3)) self.vertices[:,0] = np.linspace(-1, 1, N) self.vertices[:,1] = np.random.rand(N) self.vertices[:,2] = np.linspace(-1, 1, N) def setup_opengl(self): self.initializeOpenGLFunctions() # Create shader program. vs = QOpenGLShader(QOpenGLShader.Vertex, self) vs.compileSourceCode(""" attribute vec3 vertex_position; varying vec4 color; uniform mat4 mvp; void main() { color = vec4(1.0, 0.0, 0.0, 1.0); gl_Position = mvp * vec4(vertex_position, 1.0); } """) fs = QOpenGLShader(QOpenGLShader.Fragment, self) fs.compileSourceCode(""" varying vec4 color; void main() { gl_FragColor = color; } """) self.shader_prog = QOpenGLShaderProgram(self) self.shader_prog.addShader(vs) self.shader_prog.addShader(fs) self.shader_prog.link() # Create VBO and VAO. self.vao = self.createVertexArrayObject() vbo = self.createBuffer(self.vertices.nbytes) vbo.bind() vbo.write(self.vertices.tobytes()) self.shader_prog.enableAttributeArray(0) self.shader_prog.setAttributeBuffer(0, self.gl.GL_FLOAT, 0, 3) vbo.release() def createVertexArrayObject(self): vao = self.gl.glGenVertexArrays(1) self.gl.glBindVertexArray(vao) return vao def createBuffer(self, size): vbo = self.gl.glGenBuffers(1) vbo.bind() self.gl.glBufferData(self.gl.GL_ARRAY_BUFFER, size, None, self.gl.GL_STATIC_DRAW) vbo.release() return vbo def paintGL(self): self.gl.glClearColor(0.0, 0.0, 0.0, 0.0) self.gl.glClear(self.gl.GL_COLOR_BUFFER_BIT | self.gl.GL_DEPTH_BUFFER_BIT) self.shader_prog.bind() mvp = self.projectionMatrix() * self.viewMatrix() * self.modelMatrix() self.shader_prog.setUniformValue("mvp", mvp) self.gl.glBindVertexArray(self.vao) self.gl.glDrawArrays(self.gl.GL_LINE_STRIP, 0, self.vertices.shape[0]) def viewMatrix(self): return np.array([ [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, -1.0], [0.0, 0.0, 1.0, 3.0], [0.0, 0.0, 0.0, 1.0], ], dtype=np.float32) def projectionMatrix(self): W, H = self.width(), self.height() return np.array([ [2.0/W, 0.0, 0.0, 0.0], [0.0, -2.0/H, 0.0, 0.0], [0.0, 0.0, -1.0, 0.0], [-1.0, 1.0, 0.0, 1.0], ], dtype=np.float32) def modelMatrix(self): return np.identity(4, dtype=np.float32) class MainWindow(QMainWindow): def __init__(self, parent=None): super().__init__(parent) # Create figure and canvas. self.figure = Figure(figsize=(4, 4), dpi=100) self.canvas = FigureCanvas(self.figure) # Create GL widget. self.gl_widget = GLWidget() # Set central widget. self.setCentralWidget(self.gl_widget) # Add random heights to the figure. ax = self.figure.add_subplot(111) ax.plot(self.gl_widget.vertices[:, 1]) if __name__ == '__main__': app = QApplication(sys.argv) # Create OpenGL context. format = QSurfaceFormat() format.setVersion(3, 3) format.setProfile(QSurfaceFormat.CoreProfile) format.setDepthBufferSize(24) format.setSamples(4) QSurfaceFormat.setDefaultFormat(format) # Create and show main window. window = MainWindow() window.show() # Enter application event loop. sys.exit(app.exec_()) ``` 这个程序创建了一个带有OpenGL硬件加速的PyQt应用程序,绘制了一个具有随机高度的线条。这个线条是用OpenGL绘制的,并使用matplotlib库在QMainWindow中绘制相同的线条。注意,可以按住鼠标左键在3D视图中旋转场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值