Qt之使用OpenGL硬件加速

Qt之使用OpenGL硬件加速

参考的用例 2D Painting Example

文档说的很清楚,The quality and speed of rendering in the GLWidget
depends on the level of support for multisampling and hardware acceleration
that your system’s OpenGL driver provides.

测试fps函数

#include <chrono>
using namespace std;
using namespace std::chrono;
static double fps(){
    static double fps = 0.0;
    static int frameCount = 0;
    static auto lastTime = system_clock::now();
    static auto curTime = system_clock::now();

    curTime = system_clock::now();

    auto duration = duration_cast<microseconds>(curTime - lastTime);
    double duration_s = double(duration.count()) * microseconds::period::num / microseconds::period::den;

    if (duration_s > 2)//2秒之后开始统计FPS
    {
        fps = frameCount / duration_s;
        frameCount = 0;
        lastTime = curTime;
    }

    ++frameCount;

    return fps;
}

之前帧率一直测的是60fps,原因:

  1. 电脑垂直同步没关

  2. 设置QSurfaceFormat::setSwapInterval(0)

     QSurfaceFormat fmt;
     fmt.setSamples(4);
     fmt.setSwapInterval(0);
     QSurfaceFormat::setDefaultFormat(fmt);
    

判断系统中是否支持Opengl

QGLFormat::hasOpenGL()
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值