先记录下源码
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QOpenGLWindow>
#include <QScreen>
#include <QPainter>
#include <QGuiApplication>
#include <QMatrix4x4>
#include <QStaticText>
#include <QKeyEvent>
#include <QDebug>
#include <QOpenGLExtraFunctions>
#include <QOpenGLFunctions>
#include "background_renderer.h"
#include <QOpenGLShaderProgram>
#include <QOpenGLShader>
#include <QTimer>
#include <QObject>
class QOpenGLContext;
class QOpenGLBuffer;
class QOpenGLVertexArrayObject;
class QOpenGLExtraFunctions;
//"FragColor = vec4(0.0f, 0.5f, 0.0f, 1.0f);\n"
static const char *fragmentShaderSource =
"#version 330\n"
"uniform vec4 timeColor;\n"
"out highp vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor = timeColor;\n"
"}\n";
static const char *vertexShaderSource =
"#version 330\n"
"layout (location = 0) in vec3 aPos;\n"
"out highp vec4 colorForfragment;\n"
"void main() {\n"
"gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f);\n"
"colorForfragment = vec4(0.0f, 1.0f, 0.0f, 1.0f);\n"
"}\n";
const GLuint indicesArray[] = { // 注意索引从0开始!
0, 1, 3, // 第一个三角形
1, 2, 3 // 第二个三角形
};
class OpenGLWindow : public QOpenGLWindow{
Q_OBJECT
public:
OpenGLWindow();
~OpenGLWindow();
public slots:
void Colorupdate();
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int w, int h) override;
void keyPressEvent(QKeyEvent *e) override;
private:
QOpenGLBuffer *m_vbo;
QOpenGLVertexArrayObject *m_vao;
QOpenGLBuffer *m_ebo;
QOpenGLShader *m_shader;
QOpenGLShaderProgram *m_shaderProgram;
QOpenGLShaderProgram *m_shaderVertex;
QTimer m_timer;
};
OpenGLWindow::OpenGLWindow()
: QOpenGLWindow(QOpenGLWindow::NoPartialUpdate),
m_vbo(nullptr),
m_vao(nullptr),
m_shaderProgram(nullptr),
m_shaderVertex(nullptr),
m_ebo(nullptr)
{
qDebug() << "这里是OpenGLWindow的构造函数";
}
OpenGLWindow::~OpenGLWindow()
{
if(m_vbo)
{
m_vbo->release();
delete m_vbo;
m_vbo = nullptr;
}
if(m_vao)
{
m_vao->release();
delete m_vao;
m_vao = nullptr;
}
if(m_ebo)
{
m_vbo->release();
delete m_ebo;
m_ebo = nullptr;
}
}
void OpenGLWindow::Colorupdate()
{
update();
qDebug() << "this is colorup date";
}
void OpenGLWindow::initializeGL()
{
//initializeOpenGLFunctions();
GLfloat TraArray[9] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
GLfloat RectArray[] = {
0.5f, 0.5f, 0.0f, // 右上角
0.5f, -0.5f, 0.0f, // 右下角
-0.5f, -0.5f, 0.0f, // 左下角
-0.5f, 0.5f, 0.0f // 左上角
};
if(m_vao)
{
delete m_vao;
m_vao = nullptr;
}
m_vao = new QOpenGLVertexArrayObject;
m_vao->create();
m_vao->bind();
if(m_vbo)
{
delete m_vbo;
m_vbo = nullptr;
}
m_vbo = new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
m_vbo->create();
m_vbo->bind();
m_vbo->allocate(RectArray, sizeof(RectArray));
if(m_shaderProgram)
{
delete m_shaderProgram;
m_shaderProgram = nullptr;
}
if(m_ebo)
{
delete m_ebo;
m_ebo = nullptr;
}
m_ebo = new QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
m_ebo->create();
m_ebo->bind();
m_ebo->allocate(indicesArray, sizeof(indicesArray));
if(m_shaderProgram)
{
delete m_shaderProgram;
m_shaderProgram = nullptr;
}
m_shaderProgram = new QOpenGLShaderProgram;
qDebug()<<"添加着色器源码结果"<< m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
qDebug() << "添加定点着色器源码" << m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_shaderProgram->link();
int attr = m_shaderProgram->attributeLocation("aPos");
m_shaderProgram->setAttributeBuffer(attr, GL_FLOAT, 0, 3, sizeof (GLfloat)*3);
m_shaderProgram->enableAttributeArray(attr);
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(Colorupdate()));
m_timer.start(1000);
}
/*知识点 模型视图投影矩阵 = 投影矩阵 * 视图矩阵 * 模型矩阵 */
void OpenGLWindow::paintGL()
{
QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions();
static uint8_t flag = 0;
qDebug() << "start draw 0";
f->glClearColor(0, 0, 0, 1);
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_shaderProgram->bind();
int timeColorAtrId = m_shaderProgram->uniformLocation("timeColor");
if(timeColorAtrId == -1)
{
qDebug() << "error uniform name";
}
else {
if(flag == 0)
{
flag = 1;
m_shaderProgram->setUniformValue(timeColorAtrId, 1.0f, 0.0f, 0.0f, 1.0f);
} else {
flag = 0;
m_shaderProgram->setUniformValue(timeColorAtrId, 0.0f, 1.0f, 0.0f, 1.0f);
}
}
qDebug() << "start draw 1";
f->glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr, sizeof(indicesArray));
}
void OpenGLWindow::resizeGL(int w, int h)
{
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
qDebug() << "This is resizeGl";
}
void OpenGLWindow::keyPressEvent(QKeyEvent *e)
{
//update();
}
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
OpenGLWindow window;
QSurfaceFormat fmt; //! 图像渲染句柄
fmt.setDepthBufferSize(24); //! 设置深度缓存大小
fmt.setStencilBufferSize(8); //! 设置模板缓存大小
if(QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL)
{
fmt.setVersion(3, 3); //! 设置所需的OPENGL 版本号
fmt.setProfile(QSurfaceFormat::CompatibilityProfile); //! 设置所需的OpenGL上下文配置文件
}
else {
fmt.setVersion(3, 0);
}
window.setFormat(fmt); //! 设置图像渲染
window.setTitle("Test OpenGL"); //! 设置标题
window.showMaximized();
return app.exec();
}
#include "main.moc"