#ifndef MYRENDERER_H
#define MYRENDERER_H #include <QOpenGLContext>
#include <QOpenGLFunctions>
#include <QSurface>
#include <QSurfaceFormat>
#include <QScreen>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QColor>
#include <QSize>
class MyRenderer : public QObject
{
Q_OBJECT
public:
MyRenderer(QSurfaceFormat & format, MyRenderer * share = 0, QScreen * screen = 0);
QSurfaceFormat format() const {return m_format;}
public slots:
void Render(QSurface * surface, const QColor & color, const QSize & size);
private:
void Initialize();
void Init_QUAD_VBO();
void Init_QUAD_Shader();
void Draw_QUAD();
private:
bool m_bInitialized;
QSurfaceFormat m_format;
QOpenGLContext * m_context; QOpenGLFunctions * m_fun;
QOpenGLShader * m_QUAD_vShader;
QOpenGLShader * m_QUAD_fShader;
QOpenGLShaderProgram * m_QUAD_program; QVector<QVector3D > m_quad_vertices;
GLuint m_quad_vbo; GLuint m_MODEL_MATRIX_LOC;
GLuint m_VIEW_MATRIX_LOC;
GLuint m_PROJECTION_MATRIX_LOC; QMatrix4x4 m_model_matrix;
QMatrix4x4 m_view_matrix;
QMatrix4x4 m_projection_matrix;
QSize m_viewSize;
}; #endif // MYRENDERER_H
#include "myrenderer.h"

MyRenderer::MyRenderer(QSurfaceFormat &format, MyRenderer *share, QScreen *screen)
:m_bInitialized(false)
,m_format(format)
,m_context()
,m_fun()
,m_QUAD_vShader()
,m_QUAD_fShader()
,m_QUAD_program()
{
m_context = new QOpenGLContext(this);
if (screen)
{
m_context->setScreen(screen);
}
m_context->setFormat(format);
if (share)
{
m_context->setShareContext(share->m_context);
}
m_context->create();
} void MyRenderer::Render(QSurface * surface, const QColor & color, const QSize & viewSize)
{
m_viewSize = surface->size();
if (m_context == NULL)
{
return;
}
if (m_context->makeCurrent(surface) != true)
{
return;
}
if (m_bInitialized == false)
{
m_bInitialized = true;
Initialize();
}
m_fun->glViewport(, , surface->size().width(), surface->size().height());
m_fun->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
m_fun->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Draw_QUAD();
m_context->swapBuffers(surface);
} void MyRenderer::Initialize()
{
m_fun = m_context->functions();
m_fun->glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Init_QUAD_VBO();
Init_QUAD_Shader();
} void MyRenderer::Init_QUAD_VBO()
{
m_quad_vertices << QVector3D(-1.0f, -1.0f, 0.0f);
m_quad_vertices << QVector3D(1.0f, -1.0f, 0.0f);
m_quad_vertices << QVector3D(1.0f, 1.0f, 0.0f);
m_quad_vertices << QVector3D(-1.0f, 1.0f, 0.0f); m_fun->glGenBuffers(, &m_quad_vbo);
m_fun->glBindBuffer(GL_ARRAY_BUFFER, m_quad_vbo);
m_fun->glBufferData(GL_ARRAY_BUFFER, sizeof(float) * * m_quad_vertices.size(), &m_quad_vertices.at(), GL_STATIC_COPY);
m_fun->glBindBuffer(GL_ARRAY_BUFFER, );
} void MyRenderer::Init_QUAD_Shader()
{
m_QUAD_vShader = new QOpenGLShader(QOpenGLShader::Vertex, this);
m_QUAD_fShader = new QOpenGLShader(QOpenGLShader::Fragment, this);
m_QUAD_program = new QOpenGLShaderProgram(this);
m_QUAD_vShader->compileSourceFile(":/quad.vert");
m_QUAD_fShader->compileSourceFile(":/quad.frag");
m_QUAD_program->addShader(m_QUAD_vShader);
m_QUAD_program->addShader(m_QUAD_fShader);
m_QUAD_program->link(); m_MODEL_MATRIX_LOC = m_QUAD_program->uniformLocation("MODEL_MATRIX");
m_VIEW_MATRIX_LOC = m_QUAD_program->uniformLocation("VIEW_MATRIX");
m_PROJECTION_MATRIX_LOC = m_QUAD_program->uniformLocation("PROJECTION_MATRIX");
} void MyRenderer::Draw_QUAD()
{ m_model_matrix.setToIdentity();
m_view_matrix.setToIdentity();
m_projection_matrix.setToIdentity(); m_view_matrix.lookAt(QVector3D(0.0f, 0.0f, 10.0f), QVector3D(0.0f, 0.0f, 0.0f), QVector3D(0.0, 1.0f, 0.0f));
m_projection_matrix.perspective(45.0f, m_viewSize.width()/ m_viewSize.height(), 1.0f, 100.0f); m_QUAD_program->bind();
m_QUAD_program->setUniformValue(m_MODEL_MATRIX_LOC, m_model_matrix);
m_QUAD_program->setUniformValue(m_VIEW_MATRIX_LOC, m_view_matrix);
m_QUAD_program->setUniformValue(m_PROJECTION_MATRIX_LOC, m_projection_matrix); m_fun->glBindBuffer(GL_ARRAY_BUFFER, m_quad_vbo);
m_fun->glEnableVertexAttribArray();
m_fun->glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, , );
m_fun->glDrawArrays(GL_QUADS, , );
m_fun->glDisableVertexAttribArray();
m_fun->glBindBuffer(GL_ARRAY_BUFFER, );
m_QUAD_program->release();
}
#ifndef MYFRAME_H
#define MYFRAME_H #include "myrenderer.h"
#include <QWindow>
class MyFrame : public QWindow
{
Q_OBJECT
public:
MyFrame(const QSharedPointer<MyRenderer> & renderer);
signals:
void NeedRender(QSurface * surface , const QColor & color, const QSize & viewSize);
private:
void Render();
protected:
void exposeEvent(QExposeEvent *);
void timerEvent(QTimerEvent *);
private:
QSharedPointer<MyRenderer> m_myRenderer;
}; #endif // MYFRAME_H
#include "myframe.h"

MyFrame::MyFrame(const QSharedPointer<MyRenderer> &renderer)
:m_myRenderer(renderer)
{
setSurfaceType(QWindow::OpenGLSurface);
setFormat(renderer->format());
setGeometry(, , , );
connect(this, SIGNAL(NeedRender(QSurface*,QColor,QSize)),
renderer.data(), SLOT(Render(QSurface*,QColor,QSize)));
startTimer();
} void MyFrame::Render()
{
if (isExposed())
emit NeedRender(this, QColor(0.0f, 0.0f, 0.0f, 1.0f), size());
} void MyFrame::exposeEvent(QExposeEvent *)
{
Render();
} void MyFrame::timerEvent(QTimerEvent *)
{
Render();
}
#version
layout(location = ) in vec4 VERTEX;
uniform mat4 MODEL_MATRIX;
uniform mat4 VIEW_MATRIX;
uniform mat4 PROJECTION_MATRIX;
out vec4 VERTEX_COLOR;
void main()
{
VERTEX_COLOR = VERTEX;
gl_Position = PROJECTION_MATRIX * VIEW_MATRIX * MODEL_MATRIX * VERTEX;
}
#version
out vec4 FragColor;
in vec4 VERTEX_COLOR;
void main()
{
FragColor = VERTEX_COLOR;
}
#include "myframe.h"
#include <QGuiApplication> int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QScreen * screen = QGuiApplication::primaryScreen();
QSurfaceFormat format;
format.setDepthBufferSize();
format.setSamples();
QSharedPointer<MyRenderer>renderer(new MyRenderer(format, , screen));
MyFrame w(renderer);
w.setVisible(true);
w.show();
return a.exec();
}

Qt5 OpenGL框架的更多相关文章

  1. OpenGL框架+QT版

    原地址:http://blog.chinaunix.net/uid-25799257-id-3498005.html 之前一直做地图的算法,没什么时间学习opengl,之前看nehe_OpenGL.c ...

  2. VC基于单文档OpenGL框架

    本文是在VC6.0的环境下,运用MFC实现的OpenGL最基本框架,需要简单了解MFC编程(会在VC6.0里创建MFC单文档应用程序就行),甚至不必了解OpenGL的知识.以下是具体的步骤. 1.创建 ...

  3. QT5 OpenGL (六, 键盘事件, 开关灯,放大缩小综合运用)

    概要 实例效果图 立体图放大图 立体图缩小图 不加矢量开灯图 不加矢量关灯图 加矢量关灯图1 加矢量关灯图2 部分代码展示 主要内容解析 QT键盘事件 立体图形的放大和缩小 上下左右键以及A键D争键控 ...

  4. OpenGL编程-OpenGL框架-win32项目

    在win32项目中开发的程序 小知识: 控制台应用程序运行就是dos的界面 项目一般采用了可视化开发 开发出来的东西就像QQ之类的 是有窗口界面的 程序运行结果是这样的 源代码:对第45行进行覆盖 # ...

  5. 基于对话框的Opengl框架

    转自:http://blog.csdn.net/longxiaoshi/article/details/8238933 12-11-29 14:55 1198人阅读 评论(6) 收藏 举报  分类: ...

  6. iOS 底层框架的浅析

    1.简介 IOS是由苹果公司为iPhone.iPod touch和iPad等设备开发的操作系统. 2.知识点 iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设 ...

  7. ios框架

    iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设备的操作系统.        1,Core OS: 是用FreeBSD和Mach所改写的Darwin, 是开源 ...

  8. (1)opengl-nehe 4种框架

    http://www.yakergong.net/nehe/ 这个网站还是opengl方面比较权威的,作者叫nehe 这东西估计是要先装个ndk,然后才能运行代码 先睡觉! 以下内容参考自http:/ ...

  9. MFC中添加OpenGL

    WINDOWS下展示OpenGL有多种形式: MFC 或 win32,该如何向MFC中添加OpenGL?下面是介绍最简单OpenGL框架. 1.首先通过VS建立MFC应用程序-MyOpenGL,选择单 ...

随机推荐

  1. Vue.js教程

    https://aotu.io/notes/2016/10/13/vue2/?o2src=juejin&o2layout=compat

  2. 定位(position)

    position :属性规定元素的定位类型 语法: position : static | absolute | fixed | relative JavaScript语法:object.style. ...

  3. 报错找不到org.apache,http...的解决办法

    在build.gradle中加入 android { useLibrary 'org.apache.http.legacy' }

  4. 认识css

    (一)认识css: CSS全称为"层叠样式表 (Cascading Style Sheets)",它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. ...

  5. C#隐藏tabcontrol

    //tabControl1.SizeMode = TabSizeMode.Fixed; //tabControl1.ItemSize = new Size(0, 1);

  6. bzDemo

    <Public> <property name="Types"> <get/> </property> <method nam ...

  7. swift 经典代码收集 和 赏析

    代码1:protocol PickableEnum { var displayName: String { get } var permanentID: String { get } static v ...

  8. informix数据迁移工具使用介绍

    一.dbschema  USAGE:     dbschema [-q] [-t tabname] [-s user] [-p user] [-r rolename] [-f procname]    ...

  9. eclipse背景主题

    长时间对着电脑敲代码,眼睛受不了啊,果然程序员就没人权吗?之前咱给codeblocks背景换了颜色,去官网上粘粘代码然后替换一下就行了,这里不多说, 这里说的是给ecllipse换主题,按理说ecli ...

  10. lumen框架

    1,获取配置 app(); 说明:app()返回的是全局唯一的application对象,该对象的原型是Laravel\Lumen\Application 加载配置文件(config/app.php) ...