diff options
Diffstat (limited to 'examples/opengl/openglwindow/main.cpp')
-rw-r--r-- | examples/opengl/openglwindow/main.cpp | 132 |
1 files changed, 40 insertions, 92 deletions
diff --git a/examples/opengl/openglwindow/main.cpp b/examples/opengl/openglwindow/main.cpp index 03a6ece06f..89933fa379 100644 --- a/examples/opengl/openglwindow/main.cpp +++ b/examples/opengl/openglwindow/main.cpp @@ -1,62 +1,15 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "openglwindow.h" #include <QGuiApplication> #include <QMatrix4x4> #include <QOpenGLShaderProgram> +#include <QOpenGLBuffer> #include <QScreen> #include <QtMath> - //! [1] class TriangleWindow : public OpenGLWindow { @@ -67,10 +20,8 @@ public: void render() override; private: - GLint m_posAttr = 0; - GLint m_colAttr = 0; GLint m_matrixUniform = 0; - + QOpenGLBuffer m_vbo; QOpenGLShaderProgram *m_program = nullptr; int m_frame = 0; }; @@ -95,36 +46,48 @@ int main(int argc, char **argv) } //! [2] - //! [3] -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"; +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"; //! [3] //! [4] void TriangleWindow::initialize() { + static const GLfloat vertices_colors[] = { +0.0f, +0.707f, 1.0f, 0.0f, 0.0f, + -0.5f, -0.500f, 0.0f, 1.0f, 0.0f, + +0.5f, -0.500f, 0.0f, 0.0f, 1.0f }; + + m_vbo.create(); + m_vbo.bind(); + m_vbo.allocate(vertices_colors, sizeof(vertices_colors)); + + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), nullptr); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), + reinterpret_cast<void *>(2 * sizeof(GLfloat))); + m_program = new QOpenGLShaderProgram(this); m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource); + m_program->bindAttributeLocation("posAttr", 0); + m_program->bindAttributeLocation("colAttr", 1); m_program->link(); - m_posAttr = m_program->attributeLocation("posAttr"); - Q_ASSERT(m_posAttr != -1); - m_colAttr = m_program->attributeLocation("colAttr"); - Q_ASSERT(m_colAttr != -1); + m_program->bind(); + m_matrixUniform = m_program->uniformLocation("matrix"); Q_ASSERT(m_matrixUniform != -1); } @@ -147,28 +110,13 @@ void TriangleWindow::render() m_program->setUniformValue(m_matrixUniform, matrix); - static const GLfloat vertices[] = { - 0.0f, 0.707f, - -0.5f, -0.5f, - 0.5f, -0.5f - }; - - static const 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(m_posAttr); - glEnableVertexAttribArray(m_colAttr); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); glDrawArrays(GL_TRIANGLES, 0, 3); - glDisableVertexAttribArray(m_colAttr); - glDisableVertexAttribArray(m_posAttr); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); m_program->release(); |