<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>glwidget.cpp Example File | Qt OpenGL</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.9</td><td ><a href="qtopengl-index.html">Qt OpenGL</a></td><td ><a href="qtopengl-hellogl2-example.html">Hello GL2 Example</a></td><td >glwidget.cpp Example File</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">glwidget.cpp Example File</h1> <span class="subtitle">hellogl2/glwidget.cpp</span> <!-- $$$hellogl2/glwidget.cpp-description --> <div class="descr"> <a name="details"></a> <pre class="cpp"> <span class="comment">/**************************************************************************** ** ** 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$ ** ****************************************************************************/</span> <span class="preprocessor">#include "glwidget.h"</span> <span class="preprocessor">#include <QMouseEvent></span> <span class="preprocessor">#include <QOpenGLShaderProgram></span> <span class="preprocessor">#include <QCoreApplication></span> <span class="preprocessor">#include <math.h></span> bool GLWidget<span class="operator">::</span>m_transparent <span class="operator">=</span> <span class="keyword">false</span>; GLWidget<span class="operator">::</span>GLWidget(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) : <span class="type"><a href="../qtwidgets/qopenglwidget.html">QOpenGLWidget</a></span>(parent)<span class="operator">,</span> m_xRot(<span class="number">0</span>)<span class="operator">,</span> m_yRot(<span class="number">0</span>)<span class="operator">,</span> m_zRot(<span class="number">0</span>)<span class="operator">,</span> m_program(<span class="number">0</span>) { m_core <span class="operator">=</span> <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span><span class="operator">::</span>defaultFormat()<span class="operator">.</span>profile() <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span><span class="operator">::</span>CoreProfile; <span class="comment">// --transparent causes the clear color to be transparent. Therefore, on systems that</span> <span class="comment">// support it, the widget will become transparent apart from the logo.</span> <span class="keyword">if</span> (m_transparent) { <span class="type"><a href="../qtgui/qsurfaceformat.html">QSurfaceFormat</a></span> fmt <span class="operator">=</span> format(); fmt<span class="operator">.</span>setAlphaBufferSize(<span class="number">8</span>); setFormat(fmt); } } GLWidget<span class="operator">::</span><span class="operator">~</span>GLWidget() { cleanup(); } <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> GLWidget<span class="operator">::</span>minimumSizeHint() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(<span class="number">50</span><span class="operator">,</span> <span class="number">50</span>); } <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> GLWidget<span class="operator">::</span>sizeHint() <span class="keyword">const</span> { <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(<span class="number">400</span><span class="operator">,</span> <span class="number">400</span>); } <span class="keyword">static</span> <span class="type">void</span> qNormalizeAngle(<span class="type">int</span> <span class="operator">&</span>angle) { <span class="keyword">while</span> (angle <span class="operator"><</span> <span class="number">0</span>) angle <span class="operator">+</span><span class="operator">=</span> <span class="number">360</span> <span class="operator">*</span> <span class="number">16</span>; <span class="keyword">while</span> (angle <span class="operator">></span> <span class="number">360</span> <span class="operator">*</span> <span class="number">16</span>) angle <span class="operator">-</span><span class="operator">=</span> <span class="number">360</span> <span class="operator">*</span> <span class="number">16</span>; } <span class="type">void</span> GLWidget<span class="operator">::</span>setXRotation(<span class="type">int</span> angle) { qNormalizeAngle(angle); <span class="keyword">if</span> (angle <span class="operator">!</span><span class="operator">=</span> m_xRot) { m_xRot <span class="operator">=</span> angle; <span class="keyword">emit</span> xRotationChanged(angle); update(); } } <span class="type">void</span> GLWidget<span class="operator">::</span>setYRotation(<span class="type">int</span> angle) { qNormalizeAngle(angle); <span class="keyword">if</span> (angle <span class="operator">!</span><span class="operator">=</span> m_yRot) { m_yRot <span class="operator">=</span> angle; <span class="keyword">emit</span> yRotationChanged(angle); update(); } } <span class="type">void</span> GLWidget<span class="operator">::</span>setZRotation(<span class="type">int</span> angle) { qNormalizeAngle(angle); <span class="keyword">if</span> (angle <span class="operator">!</span><span class="operator">=</span> m_zRot) { m_zRot <span class="operator">=</span> angle; <span class="keyword">emit</span> zRotationChanged(angle); update(); } } <span class="type">void</span> GLWidget<span class="operator">::</span>cleanup() { <span class="keyword">if</span> (m_program <span class="operator">=</span><span class="operator">=</span> nullptr) <span class="keyword">return</span>; makeCurrent(); m_logoVbo<span class="operator">.</span>destroy(); <span class="keyword">delete</span> m_program; m_program <span class="operator">=</span> <span class="number">0</span>; doneCurrent(); } <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>vertexShaderSourceCore <span class="operator">=</span> <span class="string">"#version 150\n"</span> <span class="string">"in vec4 vertex;\n"</span> <span class="string">"in vec3 normal;\n"</span> <span class="string">"out vec3 vert;\n"</span> <span class="string">"out vec3 vertNormal;\n"</span> <span class="string">"uniform mat4 projMatrix;\n"</span> <span class="string">"uniform mat4 mvMatrix;\n"</span> <span class="string">"uniform mat3 normalMatrix;\n"</span> <span class="string">"void main() {\n"</span> <span class="string">" vert = vertex.xyz;\n"</span> <span class="string">" vertNormal = normalMatrix * normal;\n"</span> <span class="string">" gl_Position = projMatrix * mvMatrix * vertex;\n"</span> <span class="string">"}\n"</span>; <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>fragmentShaderSourceCore <span class="operator">=</span> <span class="string">"#version 150\n"</span> <span class="string">"in highp vec3 vert;\n"</span> <span class="string">"in highp vec3 vertNormal;\n"</span> <span class="string">"out highp vec4 fragColor;\n"</span> <span class="string">"uniform highp vec3 lightPos;\n"</span> <span class="string">"void main() {\n"</span> <span class="string">" highp vec3 L = normalize(lightPos - vert);\n"</span> <span class="string">" highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"</span> <span class="string">" highp vec3 color = vec3(0.39, 1.0, 0.0);\n"</span> <span class="string">" highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"</span> <span class="string">" fragColor = vec4(col, 1.0);\n"</span> <span class="string">"}\n"</span>; <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>vertexShaderSource <span class="operator">=</span> <span class="string">"attribute vec4 vertex;\n"</span> <span class="string">"attribute vec3 normal;\n"</span> <span class="string">"varying vec3 vert;\n"</span> <span class="string">"varying vec3 vertNormal;\n"</span> <span class="string">"uniform mat4 projMatrix;\n"</span> <span class="string">"uniform mat4 mvMatrix;\n"</span> <span class="string">"uniform mat3 normalMatrix;\n"</span> <span class="string">"void main() {\n"</span> <span class="string">" vert = vertex.xyz;\n"</span> <span class="string">" vertNormal = normalMatrix * normal;\n"</span> <span class="string">" gl_Position = projMatrix * mvMatrix * vertex;\n"</span> <span class="string">"}\n"</span>; <span class="keyword">static</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>fragmentShaderSource <span class="operator">=</span> <span class="string">"varying highp vec3 vert;\n"</span> <span class="string">"varying highp vec3 vertNormal;\n"</span> <span class="string">"uniform highp vec3 lightPos;\n"</span> <span class="string">"void main() {\n"</span> <span class="string">" highp vec3 L = normalize(lightPos - vert);\n"</span> <span class="string">" highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"</span> <span class="string">" highp vec3 color = vec3(0.39, 1.0, 0.0);\n"</span> <span class="string">" highp vec3 col = clamp(color * 0.2 + color * 0.8 * NL, 0.0, 1.0);\n"</span> <span class="string">" gl_FragColor = vec4(col, 1.0);\n"</span> <span class="string">"}\n"</span>; <span class="type">void</span> GLWidget<span class="operator">::</span>initializeGL() { <span class="comment">// In this example the widget's corresponding top-level window can change</span> <span class="comment">// several times during the widget's lifetime. Whenever this happens, the</span> <span class="comment">// QOpenGLWidget's associated context is destroyed and a new one is created.</span> <span class="comment">// Therefore we have to be prepared to clean up the resources on the</span> <span class="comment">// aboutToBeDestroyed() signal, instead of the destructor. The emission of</span> <span class="comment">// the signal will be followed by an invocation of initializeGL() where we</span> <span class="comment">// can recreate all resources.</span> connect(context()<span class="operator">,</span> <span class="operator">&</span><span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span><span class="operator">::</span>aboutToBeDestroyed<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> <span class="operator">&</span>GLWidget<span class="operator">::</span>cleanup); initializeOpenGLFunctions(); glClearColor(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> m_transparent <span class="operator">?</span> <span class="number">0</span> : <span class="number">1</span>); m_program <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qopenglshaderprogram.html">QOpenGLShaderProgram</a></span>; m_program<span class="operator">-</span><span class="operator">></span>addShaderFromSourceCode(<span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span><span class="operator">::</span>Vertex<span class="operator">,</span> m_core <span class="operator">?</span> vertexShaderSourceCore : vertexShaderSource); m_program<span class="operator">-</span><span class="operator">></span>addShaderFromSourceCode(<span class="type"><a href="../qtgui/qopenglshader.html">QOpenGLShader</a></span><span class="operator">::</span>Fragment<span class="operator">,</span> m_core <span class="operator">?</span> fragmentShaderSourceCore : fragmentShaderSource); m_program<span class="operator">-</span><span class="operator">></span>bindAttributeLocation(<span class="string">"vertex"</span><span class="operator">,</span> <span class="number">0</span>); m_program<span class="operator">-</span><span class="operator">></span>bindAttributeLocation(<span class="string">"normal"</span><span class="operator">,</span> <span class="number">1</span>); m_program<span class="operator">-</span><span class="operator">></span>link(); m_program<span class="operator">-</span><span class="operator">></span>bind(); m_projMatrixLoc <span class="operator">=</span> m_program<span class="operator">-</span><span class="operator">></span>uniformLocation(<span class="string">"projMatrix"</span>); m_mvMatrixLoc <span class="operator">=</span> m_program<span class="operator">-</span><span class="operator">></span>uniformLocation(<span class="string">"mvMatrix"</span>); m_normalMatrixLoc <span class="operator">=</span> m_program<span class="operator">-</span><span class="operator">></span>uniformLocation(<span class="string">"normalMatrix"</span>); m_lightPosLoc <span class="operator">=</span> m_program<span class="operator">-</span><span class="operator">></span>uniformLocation(<span class="string">"lightPos"</span>); <span class="comment">// Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x</span> <span class="comment">// implementations this is optional and support may not be present</span> <span class="comment">// at all. Nonetheless the below code works in all cases and makes</span> <span class="comment">// sure there is a VAO when one is needed.</span> m_vao<span class="operator">.</span>create(); <span class="type"><a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a></span><span class="operator">::</span>Binder vaoBinder(<span class="operator">&</span>m_vao); <span class="comment">// Setup our vertex buffer object.</span> m_logoVbo<span class="operator">.</span>create(); m_logoVbo<span class="operator">.</span>bind(); m_logoVbo<span class="operator">.</span>allocate(m_logo<span class="operator">.</span>constData()<span class="operator">,</span> m_logo<span class="operator">.</span>count() <span class="operator">*</span> <span class="keyword">sizeof</span>(GLfloat)); <span class="comment">// Store the vertex attribute bindings for the program.</span> setupVertexAttribs(); <span class="comment">// Our camera never changes in this example.</span> m_camera<span class="operator">.</span>setToIdentity(); m_camera<span class="operator">.</span>translate(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="operator">-</span><span class="number">1</span>); <span class="comment">// Light position is fixed.</span> m_program<span class="operator">-</span><span class="operator">></span>setUniformValue(m_lightPosLoc<span class="operator">,</span> QVector3D(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">70</span>)); m_program<span class="operator">-</span><span class="operator">></span>release(); } <span class="type">void</span> GLWidget<span class="operator">::</span>setupVertexAttribs() { m_logoVbo<span class="operator">.</span>bind(); <span class="type"><a href="../qtgui/qopenglfunctions.html">QOpenGLFunctions</a></span> <span class="operator">*</span>f <span class="operator">=</span> <span class="type"><a href="../qtgui/qopenglcontext.html">QOpenGLContext</a></span><span class="operator">::</span>currentContext()<span class="operator">-</span><span class="operator">></span>functions(); f<span class="operator">-</span><span class="operator">></span>glEnableVertexAttribArray(<span class="number">0</span>); f<span class="operator">-</span><span class="operator">></span>glEnableVertexAttribArray(<span class="number">1</span>); f<span class="operator">-</span><span class="operator">></span>glVertexAttribPointer(<span class="number">0</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> GL_FLOAT<span class="operator">,</span> GL_FALSE<span class="operator">,</span> <span class="number">6</span> <span class="operator">*</span> <span class="keyword">sizeof</span>(GLfloat)<span class="operator">,</span> <span class="number">0</span>); f<span class="operator">-</span><span class="operator">></span>glVertexAttribPointer(<span class="number">1</span><span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> GL_FLOAT<span class="operator">,</span> GL_FALSE<span class="operator">,</span> <span class="number">6</span> <span class="operator">*</span> <span class="keyword">sizeof</span>(GLfloat)<span class="operator">,</span> <span class="keyword">reinterpret_cast</span><span class="operator"><</span><span class="type">void</span> <span class="operator">*</span><span class="operator">></span>(<span class="number">3</span> <span class="operator">*</span> <span class="keyword">sizeof</span>(GLfloat))); m_logoVbo<span class="operator">.</span>release(); } <span class="type">void</span> GLWidget<span class="operator">::</span>paintGL() { glClear(GL_COLOR_BUFFER_BIT <span class="operator">|</span> GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); m_world<span class="operator">.</span>setToIdentity(); m_world<span class="operator">.</span>rotate(<span class="number">180.0f</span> <span class="operator">-</span> (m_xRot <span class="operator">/</span> <span class="number">16.0f</span>)<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>); m_world<span class="operator">.</span>rotate(m_yRot <span class="operator">/</span> <span class="number">16.0f</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>); m_world<span class="operator">.</span>rotate(m_zRot <span class="operator">/</span> <span class="number">16.0f</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span>); <span class="type"><a href="../qtgui/qopenglvertexarrayobject.html">QOpenGLVertexArrayObject</a></span><span class="operator">::</span>Binder vaoBinder(<span class="operator">&</span>m_vao); m_program<span class="operator">-</span><span class="operator">></span>bind(); m_program<span class="operator">-</span><span class="operator">></span>setUniformValue(m_projMatrixLoc<span class="operator">,</span> m_proj); m_program<span class="operator">-</span><span class="operator">></span>setUniformValue(m_mvMatrixLoc<span class="operator">,</span> m_camera <span class="operator">*</span> m_world); QMatrix3x3 normalMatrix <span class="operator">=</span> m_world<span class="operator">.</span>normalMatrix(); m_program<span class="operator">-</span><span class="operator">></span>setUniformValue(m_normalMatrixLoc<span class="operator">,</span> normalMatrix); glDrawArrays(GL_TRIANGLES<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> m_logo<span class="operator">.</span>vertexCount()); m_program<span class="operator">-</span><span class="operator">></span>release(); } <span class="type">void</span> GLWidget<span class="operator">::</span>resizeGL(<span class="type">int</span> w<span class="operator">,</span> <span class="type">int</span> h) { m_proj<span class="operator">.</span>setToIdentity(); m_proj<span class="operator">.</span>perspective(<span class="number">45.0f</span><span class="operator">,</span> GLfloat(w) <span class="operator">/</span> h<span class="operator">,</span> <span class="number">0.01f</span><span class="operator">,</span> <span class="number">100.0f</span>); } <span class="type">void</span> GLWidget<span class="operator">::</span>mousePressEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event) { m_lastPos <span class="operator">=</span> event<span class="operator">-</span><span class="operator">></span>pos(); } <span class="type">void</span> GLWidget<span class="operator">::</span>mouseMoveEvent(<span class="type"><a href="../qtgui/qmouseevent.html">QMouseEvent</a></span> <span class="operator">*</span>event) { <span class="type">int</span> dx <span class="operator">=</span> event<span class="operator">-</span><span class="operator">></span>x() <span class="operator">-</span> m_lastPos<span class="operator">.</span>x(); <span class="type">int</span> dy <span class="operator">=</span> event<span class="operator">-</span><span class="operator">></span>y() <span class="operator">-</span> m_lastPos<span class="operator">.</span>y(); <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">></span>buttons() <span class="operator">&</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>LeftButton) { setXRotation(m_xRot <span class="operator">+</span> <span class="number">8</span> <span class="operator">*</span> dy); setYRotation(m_yRot <span class="operator">+</span> <span class="number">8</span> <span class="operator">*</span> dx); } <span class="keyword">else</span> <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">></span>buttons() <span class="operator">&</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>RightButton) { setXRotation(m_xRot <span class="operator">+</span> <span class="number">8</span> <span class="operator">*</span> dy); setZRotation(m_zRot <span class="operator">+</span> <span class="number">8</span> <span class="operator">*</span> dx); } m_lastPos <span class="operator">=</span> event<span class="operator">-</span><span class="operator">></span>pos(); } </pre> </div> <!-- @@@hellogl2/glwidget.cpp --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>