QT5之opengl零基础学习(一)

标签:
itqt5opengl |
分类: QT4/5 |
闲暇无事,周末玩玩QT平台下opengl渲染开发,网上找了一些博客以及帖子,不是编译错误就是缺少这缺少那的,在阅读一些资料以后,最后还是把目标投在了QT自带的实例当中,不得不说QT这个IDE还是不错的,想学什么东西,看看里面的例子,确实收获颇多,下面是根据QT自带实例拷贝自己的版本(不喜勿喷),还是一个opengl小菜,废话不多说直接上效果贴图,方便初学者学习:
构建平台:QT5.9 其他的也可以 编译器
mingw32
http://s2/bmiddle/003vzqvzzy7mX3F3Hz301&690
main.cpp \
openglwindow.cpp \
trianglewindow.cpp
openglwindow.h \
trianglewindow.h
QGuiApplication
app(argc, argv);
QSurfaceFormat
format;//曲面格式对象
format.setSamples(16);
TriangleWindow
window;
window.setFormat(format);
window.resize(640,
480);
window.show();
window.setAnimating(true);
return app.exec();
Q_OBJECT
explicit
OpenGLWindow(QWindow *parent = 0);
~OpenGLWindow();
virtual void
render(QPainter *painter);
virtual void
render();
virtual void
initialize();
void setAnimating(bool
animating);
void
renderLater();
void renderNow();
bool event(QEvent
*event) override;
void
exposeEvent(QExposeEvent *event) override;//重写曝光事件
bool m_animating;
QOpenGLContext
*m_context;
QOpenGLPaintDevice
*m_device;
: QWindow(parent)
,
m_animating(false)
, m_context(0)
, m_device(0)
setSurfaceType(QWindow::OpenGLSurface);
delete m_device;
Q_UNUSED(painter);
if (!m_device)
m_device = new QOpenGLPaintDevice;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
m_device->setSize(size());
QPainter
painter(m_device);
render(&painter);
requestUpdate();
switch
(event->type()) {
e
QEvent::UpdateRequest:
renderNow();
return true;
default:
return QWindow::event(event);
}
Q_UNUSED(event);
if (isExposed())
renderNow();
if (!isExposed())
return;
bool needsInitialize =
false;
if (!m_context) {
m_context = new QOpenGLContext(this);
m_context->setFormat(requestedFormat());
m_context->create();
needsInitialize = true;
}
m_context->makeCurrent(this);
if (needsInitialize)
{
initializeOpenGLFunctions();
initialize();
}
render();
m_context->swapBuffers(this);
if (m_animating)
renderLater();
m_animating =
animating;
if (animating)
renderLater();
TriangleWindow();
~TriangleWindow();
void initialize()
override;//重写
void render()
override;
GLuint m_posAttr;
GLuint m_colAttr;
GLuint
m_matrixUniform;
QOpenGLShaderProgram
*m_program;
int m_frame;
: m_program(0)
, m_frame(0)
static const char
*vertexShaderSource =
"attribute
highp vec4 posAttr;\n"
"attribute
lowp vec4 colAttr;\n"
"varying
lowp vec4 col;\n"
"uniform
highp mat4 matrix;\n"
"void
main() {\n"
"
col = colAttr;\n"
"
gl_Position = matrix * posAttr;\n"
"}\n";
static const char
*fragmentShaderSource =
"varying
lowp vec4 col;\n"
"void
main() {\n"
"
gl_FragColor = col;\n"
"}\n";
m_program = new
QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
fragmentShaderSource);
m_program->link();
m_posAttr =
m_program->attributeLocation("posAttr");
m_colAttr =
m_program->attributeLocation("colAttr");
m_matrixUniform =
m_program->uniformLocation("matrix");
const qreal reticale =
devicePixelRatio();
glViewport(0, 0, width()
* reticale, height() * reticale);
glClear(GL_COLOR_BUFFER_BIT);
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
matrix.translate(0, 0,
-2);
matrix.rotate(100.0f *
m_frame / screen()->refreshRate(), 0, 1, 0);
m_program->setUniformValue(m_matrixUniform, matrix);
//制高点
GLfloat vertices[] =
{
0.0f, 0.707f,
-0.5f, -0.5f,
0.5f, -0.5f
};
GLfloat colors[] =
{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0,
vertices);
glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0,
colors);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
m_program->rele();
++m_frame;
pro:
#
# Project created by QtCreator 2018-08-19T14:21:03
#
# author : daozhong wan
#-------------------------------------------------
QT +=
core gui opengl
TARGET = openglfirst
TEMPLATE = app
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += \
HEADERS += \
main.cpp:
#include "openglwindow.h"
#include "trianglewindow.h"
#include QGuiApplication头文件
#include QSurfaceFormat 头文件
int main(int argc, char **argv)
{
}
openglwindow.h
#ifndef OPENGLWINDOW_H
#define OPENGLWINDOW_H
#include QtGui/QWindow
#include QtGui/QOpenGLFunctions
QT_BEGIN_NAMESPACE
cs QPainter;
cs QOpenGLContext;
cs QOpenGLPaintDevice;
QT_END_NAMESPACE
cs OpenGLWindow : public QWindow, protected
QOpenGLFunctions
{
public:
public slots:
protected:
private:
};
#endif // OPENGLWINDOW_H
openglwindow.cpp
#include openglwindow.h头文件
#include QtCore/QCoreApplication
#include QtGui/QOpenGLContext
#include QtGui/QOpenGLPaintDevice
#include QtGui/QPainter
OpenGLWindow::OpenGLWindow(QWindow *parent)
{
}
OpenGLWindow::~OpenGLWindow()
{
}
void OpenGLWindow::render(QPainter *painter)
{
}
void OpenGLWindow::initialize()
{
}
void OpenGLWindow::render()
{
}
void OpenGLWindow::renderLater()//渲染后更新
{
}
bool OpenGLWindow::event(QEvent *event)
{
}
void OpenGLWindow::exposeEvent(QExposeEvent *event)
{
}
void OpenGLWindow::renderNow()
{
}
void OpenGLWindow::setAnimating(bool animating)
{
}
trianglewindow.h :
#ifndef TRIANGLEWINDOW_H
#define TRIANGLEWINDOW_H
#include openglwindow.h
#include QtGui/QGuiApplication
#include QtGui/QMatrix4x4
#include QtGui/QOpenGLShaderProgram
#include QtGui/QScreen
#include QtCore/qmath.h
#include QWidget
cs TriangleWindow : public OpenGLWindow
{
public:
private:
};
#endif // TRIANGLEWINDOW_H
trianglewindow.cpp:
#include trianglewindow.h
TriangleWindow::TriangleWindow()
{
}
TriangleWindow::~TriangleWindow()
{
}
void TriangleWindow::initialize()
{
}
void TriangleWindow::render()//渲染
{
}
源码下载地址:https://download.csdn.net/download/wandaozhong/10613670
前一篇:进程间通信(IPC)介绍(整理)