├── .gitignore ├── LICENSE ├── README.md ├── SIGGRAPH2007_AlphaTestedMagnification.pdf ├── code ├── .gitignore ├── Example01 │ ├── Camera.h │ ├── DebugApplication.h │ ├── Example01.pro │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Vertex.h │ ├── main.cpp │ ├── resources.qrc │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Example02 │ ├── Camera.h │ ├── DebugApplication.h │ ├── Example02.pro │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Vertex.h │ ├── main.cpp │ ├── resources.qrc │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Example03 │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── Camera.h │ ├── DebugApplication.h │ ├── Example03.pro │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── PickLineObject.cpp │ ├── PickLineObject.h │ ├── PickObject.cpp │ ├── PickObject.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Vertex.h │ ├── main.cpp │ ├── resources.qrc │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Example04 │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── Camera.h │ ├── DebugApplication.h │ ├── Example04.pro │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── PickLineObject.cpp │ ├── PickLineObject.h │ ├── PickObject.cpp │ ├── PickObject.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Vertex.h │ ├── main.cpp │ ├── resources.qrc │ └── shaders │ │ ├── VertexNormalColor.vert │ │ ├── diffuse.frag │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Example05 │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── Camera.h │ ├── DebugApplication.h │ ├── Example05.pro │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── PickLineObject.cpp │ ├── PickLineObject.h │ ├── PickObject.cpp │ ├── PickObject.h │ ├── PlaneMesh.cpp │ ├── PlaneMesh.h │ ├── PlaneObject.cpp │ ├── PlaneObject.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Vertex.h │ ├── main.cpp │ ├── resources.qrc │ └── shaders │ │ ├── VertexColorTransparent.vert │ │ ├── VertexNormalColor.vert │ │ ├── diffuse.frag │ │ ├── diffuseTransparent.frag │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Example06 │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── Camera.h │ ├── DebugApplication.h │ ├── Example06.pro │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── PickLineObject.cpp │ ├── PickLineObject.h │ ├── PickObject.cpp │ ├── PickObject.h │ ├── PlaneMesh.cpp │ ├── PlaneMesh.h │ ├── PlaneObject.cpp │ ├── PlaneObject.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── TextObject.cpp │ ├── TextObject.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Vertex.h │ ├── main.cpp │ ├── resources.qrc │ └── shaders │ │ ├── VertexColorTransparent.vert │ │ ├── VertexFontTexture.vert │ │ ├── VertexNormalColor.vert │ │ ├── diffuse.frag │ │ ├── diffuseTransparent.frag │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ ├── texture.frag │ │ └── withWorldAndCamera.vert ├── Example07 │ ├── Example07.pro │ ├── RectangleWindow.cpp │ ├── RectangleWindow.h │ ├── example.qrc │ ├── main.cpp │ └── shaders │ │ ├── pass_through.vert │ │ └── simple.frag ├── Example08 │ ├── Example08.pro │ ├── RectangleWindow.cpp │ ├── RectangleWindow.h │ ├── example.qrc │ ├── main.cpp │ └── shaders │ │ ├── pass_through.vert │ │ └── simple.frag ├── Tutorial_01 │ ├── .gitignore │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── TriangleWindow.cpp │ ├── TriangleWindow.h │ ├── Tutorial_01.pro │ ├── Tutorial_01.qrc │ ├── main.cpp │ └── shaders │ │ ├── pass_through.vert │ │ └── uniform_color.frag ├── Tutorial_02 │ ├── .gitignore │ ├── RectangleWindow.cpp │ ├── RectangleWindow.h │ ├── Tutorial_02.pro │ ├── Tutorial_02.qrc │ ├── main.cpp │ └── shaders │ │ ├── pass_through.vert │ │ └── simple.frag ├── Tutorial_03 │ ├── .gitignore │ ├── RectangleWindow.cpp │ ├── RectangleWindow.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Tutorial_03.pro │ ├── Tutorial_03.qrc │ ├── main.cpp │ └── shaders │ │ ├── pass_through.vert │ │ └── simple.frag ├── Tutorial_04 │ ├── .gitignore │ ├── RectangleWindow.cpp │ ├── RectangleWindow.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Tutorial_04.pro │ ├── Tutorial_04.qrc │ ├── main.cpp │ └── shaders │ │ ├── pass_through.vert │ │ └── simple.frag ├── Tutorial_05 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_05.pro │ ├── Tutorial_05.qrc │ ├── Vertex.h │ ├── main.cpp │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Tutorial_06 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── DebugApplication.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_06.pro │ ├── Tutorial_06.qrc │ ├── Vertex.h │ ├── build │ │ ├── .gitignore │ │ ├── build.bat │ │ ├── deploy.bat │ │ └── readme.md │ ├── main.cpp │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Tutorial_07 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── DebugApplication.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── PickLineObject.cpp │ ├── PickLineObject.h │ ├── PickObject.cpp │ ├── PickObject.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_07.pro │ ├── Tutorial_07.qrc │ ├── Vertex.h │ ├── main.cpp │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Tutorial_08 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── DebugApplication.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_08.pro │ ├── Tutorial_08.qrc │ ├── Vertex.h │ ├── main.cpp │ ├── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── pass_through.vert │ │ ├── simple.frag │ │ ├── texture.frag │ │ ├── withTexture.vert │ │ └── withWorldAndCamera.vert │ └── textures │ │ ├── brickwall.jpg │ │ ├── plaster.jpg │ │ └── tiles.jpg ├── Tutorial_09 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── DebugApplication.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Texture2ScreenObject.cpp │ ├── Texture2ScreenObject.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_09.pro │ ├── Tutorial_09.qrc │ ├── Vertex.h │ ├── main.cpp │ └── shaders │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── screenfill.frag │ │ ├── screenfill.vert │ │ ├── screenfill_with_kernel.frag │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert ├── Tutorial_10 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── DebugApplication.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Texture2ScreenObject.cpp │ ├── Texture2ScreenObject.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_10.pro │ ├── Tutorial_10.qrc │ ├── Vertex.h │ ├── main.cpp │ └── shaders │ │ ├── depthMap.frag │ │ ├── depthMap.vert │ │ ├── grid.frag │ │ ├── grid.vert │ │ ├── sceneWithShadowMap.frag │ │ ├── sceneWithShadowMap.vert │ │ ├── screenfill.frag │ │ ├── screenfill.vert │ │ ├── simple.frag │ │ └── withWorldAndCamera.vert └── Tutorial_11 │ ├── .gitignore │ ├── BoxMesh.cpp │ ├── BoxMesh.h │ ├── BoxObject.cpp │ ├── BoxObject.h │ ├── CMakeLists.txt │ ├── Camera.h │ ├── DebugApplication.h │ ├── GridObject.cpp │ ├── GridObject.h │ ├── KeyboardMouseHandler.cpp │ ├── KeyboardMouseHandler.h │ ├── OpenGLException.cpp │ ├── OpenGLException.h │ ├── OpenGLWindow.cpp │ ├── OpenGLWindow.h │ ├── SceneView.cpp │ ├── SceneView.h │ ├── ShaderProgram.cpp │ ├── ShaderProgram.h │ ├── TestDialog.cpp │ ├── TestDialog.h │ ├── Texture2ScreenObject.cpp │ ├── Texture2ScreenObject.h │ ├── Transform3D.cpp │ ├── Transform3D.h │ ├── Tutorial_11.pro │ ├── Tutorial_11.qrc │ ├── Vertex.h │ ├── main.cpp │ └── shaders │ ├── depthMap.frag │ ├── depthMap.vert │ ├── grid.frag │ ├── grid.vert │ ├── sceneWithShadowMap.frag │ ├── sceneWithShadowMap.vert │ ├── screenfill.frag │ ├── screenfill.vert │ ├── simple.frag │ └── withWorldAndCamera.vert ├── docs ├── OpenGLQtTutorial.pdf ├── images │ ├── Tutorial_01_mac.png │ ├── Tutorial_02_linux.png │ ├── Tutorial_03_win10.png │ ├── Tutorial_04_linux.png │ ├── Tutorial_05_BoxVertexNumbering.png │ ├── Tutorial_05_BoxVertexNumbering_print.jpg │ ├── Tutorial_05_PlaneVertexNumbering.png │ ├── Tutorial_05_PlaneVertexNumbering_print.jpg │ ├── Tutorial_05_grid_fragshaderfade.png │ ├── Tutorial_05_grid_vertexshaderfade.png │ ├── Tutorial_05_gridplain.png │ ├── Tutorial_05_linux.png │ ├── Tutorial_05_vertexbuffer.png │ ├── Tutorial_05_vertexbuffer_print.jpg │ ├── Tutorial_07_RayTracking.png │ ├── Tutorial_08_Textures.png │ ├── Tutorial_09_FramebufferImageKernel.png │ ├── Tutorial_11_QuartierMitSchattenUndBelichtung.png │ ├── stem-0b5528d63c2e2952792f6e00782503fb.png │ ├── stem-17841fc9750fa9c1e660c1d1a17e8f73.png │ ├── stem-291a8857bef2c45250b10de3b8c6e935.png │ ├── stem-2abbd6a9f6c07d8f9219e623d7b3c2fe.png │ ├── stem-3e50c583aae250db742c6e234a07c6a3.png │ ├── stem-596af39d496be51612f3cf769a782bc1.png │ ├── stem-6f808b9a2616f307356d425ea39fb35c.png │ ├── stem-9828cfb947c30b5db90541b612b3da45.png │ ├── stem-a2c89dea229e10d4f067329c51463ee8.png │ ├── stem-ad8bc4a48ba75573404d7250014d49c0.png │ └── stem-e5b93a5483b23cf5d2a5e032e8df8026.png └── index.html ├── learnopengl_book.pdf └── text ├── .gitignore ├── Introduction.adoc ├── OpenGLQtTutorial.adoc ├── Tutorial_01.adoc ├── Tutorial_02.adoc ├── Tutorial_03.adoc ├── Tutorial_04.adoc ├── Tutorial_05.adoc ├── Tutorial_06.adoc ├── Tutorial_07.adoc ├── Tutorial_08.adoc ├── Tutorial_09.adoc ├── Tutorial_0x.adoc ├── build.sh ├── css └── roboto_ubuntu.css ├── fonts ├── robotocondensed-bold.ttf ├── robotocondensed-bold_italic.ttf ├── robotocondensed-italic.ttf └── robotocondensed-normal.ttf ├── images ├── .gitignore ├── Tutorial_01_mac.png ├── Tutorial_02_linux.png ├── Tutorial_03_win10.png ├── Tutorial_04_linux.png ├── Tutorial_05_BoxVertexNumbering.png ├── Tutorial_05_BoxVertexNumbering_print.jpg ├── Tutorial_05_PlaneVertexNumbering.png ├── Tutorial_05_PlaneVertexNumbering_print.jpg ├── Tutorial_05_grid_fragshaderfade.png ├── Tutorial_05_grid_vertexshaderfade.png ├── Tutorial_05_gridplain.png ├── Tutorial_05_linux.png ├── Tutorial_05_vertexbuffer.png ├── Tutorial_05_vertexbuffer_print.jpg ├── Tutorial_07_RayTracking.png ├── Tutorial_08_Textures.png ├── Tutorial_09_FramebufferImageKernel.png └── Tutorial_11_QuartierMitSchattenUndBelichtung.png ├── pdf-theme.yml ├── readme.md └── rouge_theme.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | 3 | *.pro.user 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, Andreas Nicolai 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenGL + Qt Tutorial 2 | A tutorial to use OpenGL within Qt Widgets applications. A comprehensive discussion of many Qt Wrapper and convenience classes and functions to assist with OpenGL development. Includes discussion of best-practice approaches for many topics, including input handling and battery-efficient render-on-demand. 3 | 4 | The generated documentation can be found on: 5 | 6 | - https://ghorwin.github.io/OpenGLWithQt-Tutorial 7 | 8 | The offline-PDF version can be downloaded from the repo: 9 | 10 | https://github.com/ghorwin/OpenGLWithQt-Tutorial/raw/master/docs/OpenGLQtTutorial.pdf 11 | 12 | 13 | ## Contents 14 | 15 | Tutorial discusses modern OpenGL approach with shader programs and vertex buffers/arrays. The following Qt classes are discussed: 16 | 17 | - `QMatrix4x4` 18 | - `QVector3D` and `QVector4D` 19 | - `QOpenGLWindow` and `QOpenGLWidget` 20 | - `QOpenGLContext`, `QSurfaceFormat` and `QOpenGLFunctions` 21 | - `QOpenGLShaderProgram` 22 | - `QOpenGLTexture` 23 | - `QOpenGLFramebufferObject` 24 | - `QOpenGLVertexArrayObject` and `QOpenGLBuffer` 25 | - `QOpenGLDebugLogger` 26 | - `QOpenGLTimerQuery`, `QOpenGLTimeMonitor`, and `QElapsedTimer` 27 | 28 | 29 | ## Writing and generating the docs 30 | 31 | Uses Asciidoctor. Go to directory `text` and run `build.sh`. 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /SIGGRAPH2007_AlphaTestedMagnification.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/SIGGRAPH2007_AlphaTestedMagnification.pdf -------------------------------------------------------------------------------- /code/.gitignore: -------------------------------------------------------------------------------- 1 | /build-*/ 2 | /build_Tutorial_05/ 3 | -------------------------------------------------------------------------------- /code/Example01/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Example01/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Example01/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Example01/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Example01/Vertex.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef VERTEX_H 13 | #define VERTEX_H 14 | 15 | #include 16 | #include 17 | 18 | /*! A container class to store data (coordinates, normals, textures, colors) of a vertex, used for interleaved 19 | storage. Expand this class as needed. 20 | 21 | Memory layout (each char is a byte): xxxxyyyyzzzzrrrrggggbbbb = 6*4 = 24 Bytes 22 | 23 | You can define a vector and use this directly as input to the vertex buffer. 24 | 25 | Mind implicit padding by compiler! Hence, for allocation use: 26 | - sizeof(Vertex) as stride 27 | - offsetof(Vertex, r) as start offset for the color 28 | 29 | This will only become important, if mixed data types are used in the struct. 30 | Read http://www.catb.org/esr/structure-packing/ for an in-depth explanation. 31 | */ 32 | struct Vertex { 33 | Vertex() {} 34 | Vertex(const QVector3D & coords, const QColor & col) : 35 | x(float(coords.x())), 36 | y(float(coords.y())), 37 | z(float(coords.z())), 38 | r(float(col.redF())), 39 | g(float(col.greenF())), 40 | b(float(col.blueF())) 41 | { 42 | } 43 | 44 | float x,y,z; 45 | float r,g,b; 46 | }; 47 | 48 | #endif // VERTEX_H 49 | -------------------------------------------------------------------------------- /code/Example01/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Example01/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | 8 | 9 | -------------------------------------------------------------------------------- /code/Example01/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | 7 | void main() { 8 | finalColor = vec4( gridColor, 1.0 ); 9 | } 10 | -------------------------------------------------------------------------------- /code/Example01/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Example01/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Example01/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Example02/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Example02/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Example02/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Example02/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Example02/Vertex.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef VERTEX_H 13 | #define VERTEX_H 14 | 15 | #include 16 | #include 17 | 18 | /*! A container class to store data (coordinates, normals, textures, colors) of a vertex, used for interleaved 19 | storage. Expand this class as needed. 20 | 21 | Memory layout (each char is a byte): xxxxyyyyzzzzrrrrggggbbbb = 6*4 = 24 Bytes 22 | 23 | You can define a vector and use this directly as input to the vertex buffer. 24 | 25 | Mind implicit padding by compiler! Hence, for allocation use: 26 | - sizeof(Vertex) as stride 27 | - offsetof(Vertex, r) as start offset for the color 28 | 29 | This will only become important, if mixed data types are used in the struct. 30 | Read http://www.catb.org/esr/structure-packing/ for an in-depth explanation. 31 | */ 32 | struct Vertex { 33 | Vertex() {} 34 | Vertex(const QVector3D & coords, const QColor & col) : 35 | x(float(coords.x())), 36 | y(float(coords.y())), 37 | z(float(coords.z())), 38 | r(float(col.redF())), 39 | g(float(col.greenF())), 40 | b(float(col.blueF())) 41 | { 42 | } 43 | 44 | float x,y,z; 45 | float r,g,b; 46 | }; 47 | 48 | #endif // VERTEX_H 49 | -------------------------------------------------------------------------------- /code/Example02/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Example02/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | 8 | 9 | -------------------------------------------------------------------------------- /code/Example02/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 1000; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Example02/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Example02/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Example02/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Example03/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Example03/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Example03/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Example03/PickLineObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PICKLINEOBJECT_H 13 | #define PICKLINEOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | 23 | #include "Vertex.h" 24 | 25 | /*! For drawing a simple line. */ 26 | class PickLineObject { 27 | public: 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | void render(); 31 | 32 | void setPoints(const QVector3D & a, const QVector3D & b); 33 | 34 | bool m_visible = false; 35 | std::vector m_vertexBufferData; 36 | QOpenGLVertexArrayObject m_vao; 37 | QOpenGLBuffer m_vbo; 38 | }; 39 | 40 | #endif // PICKLINEOBJECT_H 41 | -------------------------------------------------------------------------------- /code/Example03/PickObject.h: -------------------------------------------------------------------------------- 1 | #ifndef PICKOBJECT_H 2 | #define PICKOBJECT_H 3 | 4 | #include 5 | 6 | /*! An Object to hold information on the clicked-on object. */ 7 | struct PickObject { 8 | PickObject(float dist, unsigned int id) : 9 | m_dist(dist), m_objectId(id), m_faceId(0) 10 | { 11 | } 12 | PickObject(float dist, unsigned int id, unsigned int faceId) : 13 | m_dist(dist), m_objectId(id), m_faceId(faceId) 14 | { 15 | } 16 | 17 | float m_dist; // the normalized distance of the intersection point from starting point of pick line 18 | unsigned int m_objectId; // the object clicked on 19 | unsigned int m_faceId; // the actual triangle/plane clicked on 20 | }; 21 | 22 | 23 | /*! Tests if a line (with equation p = p1 + t * d) hits a plane, defined by 24 | p = x * a + y * b. Returns true if intersection is found, and returns 25 | the normalized distance (t) between intersection point and point p1. 26 | */ 27 | bool intersectsRect(const QVector3D & a, 28 | const QVector3D & b, 29 | const QVector3D & normal, 30 | const QVector3D & offset, 31 | const QVector3D & p1, 32 | const QVector3D & d, 33 | float & dist); 34 | 35 | 36 | #endif // PICKOBJECT_H 37 | -------------------------------------------------------------------------------- /code/Example03/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Example03/Vertex.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef VERTEX_H 13 | #define VERTEX_H 14 | 15 | #include 16 | #include 17 | 18 | /*! A container class to store data (coordinates, normals, textures, colors) of a vertex, used for interleaved 19 | storage. Expand this class as needed. 20 | 21 | Memory layout (each char is a byte): xxxxyyyyzzzzrrrrggggbbbb = 6*4 = 24 Bytes 22 | 23 | You can define a vector and use this directly as input to the vertex buffer. 24 | 25 | Mind implicit padding by compiler! Hence, for allocation use: 26 | - sizeof(Vertex) as stride 27 | - offsetof(Vertex, r) as start offset for the color 28 | 29 | This will only become important, if mixed data types are used in the struct. 30 | Read http://www.catb.org/esr/structure-packing/ for an in-depth explanation. 31 | */ 32 | struct Vertex { 33 | Vertex() {} 34 | Vertex(const QVector3D & coords, const QColor & col) : 35 | x(float(coords.x())), 36 | y(float(coords.y())), 37 | z(float(coords.z())), 38 | r(float(col.redF())), 39 | g(float(col.greenF())), 40 | b(float(col.blueF())) 41 | { 42 | } 43 | 44 | float x,y,z; 45 | float r,g,b; 46 | }; 47 | 48 | #endif // VERTEX_H 49 | -------------------------------------------------------------------------------- /code/Example03/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Example03/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | 8 | 9 | -------------------------------------------------------------------------------- /code/Example03/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 1000; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Example03/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Example03/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Example03/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Example04/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Example04/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Example04/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Example04/PickLineObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PICKLINEOBJECT_H 13 | #define PICKLINEOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | 23 | #include "Vertex.h" 24 | 25 | /*! For drawing a simple line. */ 26 | class PickLineObject { 27 | public: 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | void render(); 31 | 32 | void setPoints(const QVector3D & a, const QVector3D & b); 33 | 34 | bool m_visible = false; 35 | std::vector m_vertexBufferData; 36 | QOpenGLVertexArrayObject m_vao; 37 | QOpenGLBuffer m_vbo; 38 | }; 39 | 40 | #endif // PICKLINEOBJECT_H 41 | -------------------------------------------------------------------------------- /code/Example04/PickObject.h: -------------------------------------------------------------------------------- 1 | #ifndef PICKOBJECT_H 2 | #define PICKOBJECT_H 3 | 4 | #include 5 | 6 | /*! An Object to hold information on the clicked-on object. */ 7 | struct PickObject { 8 | PickObject(float dist, unsigned int id) : 9 | m_dist(dist), m_objectId(id), m_faceId(0) 10 | { 11 | } 12 | PickObject(float dist, unsigned int id, unsigned int faceId) : 13 | m_dist(dist), m_objectId(id), m_faceId(faceId) 14 | { 15 | } 16 | 17 | float m_dist; // the normalized distance of the intersection point from starting point of pick line 18 | unsigned int m_objectId; // the object clicked on 19 | unsigned int m_faceId; // the actual triangle/plane clicked on 20 | }; 21 | 22 | 23 | /*! Tests if a line (with equation p = p1 + t * d) hits a plane, defined by 24 | p = x * a + y * b. Returns true if intersection is found, and returns 25 | the normalized distance (t) between intersection point and point p1. 26 | */ 27 | bool intersectsRect(const QVector3D & a, 28 | const QVector3D & b, 29 | const QVector3D & normal, 30 | const QVector3D & offset, 31 | const QVector3D & p1, 32 | const QVector3D & d, 33 | float & dist); 34 | 35 | 36 | #endif // PICKOBJECT_H 37 | -------------------------------------------------------------------------------- /code/Example04/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Example04/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Example04/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/diffuse.frag 8 | shaders/VertexNormalColor.vert 9 | 10 | 11 | -------------------------------------------------------------------------------- /code/Example04/shaders/VertexNormalColor.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 normal; // input: attribute with index '1' with 3 elements per vertex 8 | layout(location = 2) in vec3 color; // input: attribute with index '2' with 3 elements (=rgb) per vertex 9 | out vec3 fragColor; // output: fragment color 10 | out vec3 fragNormal; // output: fragment normal vector 11 | out vec3 fragPos; // output: fragment position in world coords 12 | 13 | uniform mat4 worldToView; // parameter: the camera matrix 14 | 15 | void main() { 16 | // Mind multiplication order for matrixes 17 | gl_Position = worldToView * vec4(position, 1.0); 18 | fragPos = position; 19 | fragColor = color; 20 | fragNormal = normal; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /code/Example04/shaders/diffuse.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader with diffuse lighting 4 | 5 | in vec3 fragColor; // input: interpolated color as rgba-value 6 | in vec3 fragNormal; // input: normal vector of fragment 7 | in vec3 fragPos; // input: fragment position (world coords) 8 | 9 | out vec4 finalColor; // output: final color value as rgba-value 10 | 11 | uniform vec3 lightPos; // parameter: light position as vec3 (world coords) 12 | uniform vec3 lightColor; // parameter: light color as rgb 13 | 14 | void main() { 15 | // ambient 16 | float ambientStrength = 0.1; 17 | vec3 ambient = ambientStrength * lightColor; 18 | 19 | // diffuse 20 | vec3 norm = normalize(fragNormal); 21 | vec3 lightDir = normalize(lightPos - fragPos); 22 | float diff = max(dot(norm, lightDir), 0.0); 23 | vec3 diffuse = diff * lightColor; 24 | 25 | vec3 result = (ambient + diffuse) * fragColor; 26 | 27 | // to test correct passing of lightPos 28 | // finalColor = vec4(normalize(lightPos), 1.0); 29 | 30 | // to test correct passing of lightColor 31 | // finalColor = vec4(lightColor, 1.0); 32 | 33 | // to test fragment position component DISCUSS 34 | // finalColor = vec4(normalize(fragPos), 1.0); 35 | 36 | // to test fragment color 37 | // finalColor = vec4(fragColor, 1.0); 38 | 39 | // to test normal component 40 | // finalColor = vec4(norm, 1.0); 41 | 42 | finalColor = vec4(result, 1.0); 43 | } 44 | -------------------------------------------------------------------------------- /code/Example04/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 1000; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Example04/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Example04/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Example04/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Example05/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Example05/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Example05/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Example05/PickLineObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PICKLINEOBJECT_H 13 | #define PICKLINEOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | 23 | #include "Vertex.h" 24 | 25 | /*! For drawing a simple line. */ 26 | class PickLineObject { 27 | public: 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | void render(); 31 | 32 | void setPoints(const QVector3D & a, const QVector3D & b); 33 | 34 | bool m_visible = false; 35 | std::vector m_vertexBufferData; 36 | QOpenGLVertexArrayObject m_vao; 37 | QOpenGLBuffer m_vbo; 38 | }; 39 | 40 | #endif // PICKLINEOBJECT_H 41 | -------------------------------------------------------------------------------- /code/Example05/PickObject.h: -------------------------------------------------------------------------------- 1 | #ifndef PICKOBJECT_H 2 | #define PICKOBJECT_H 3 | 4 | #include 5 | 6 | /*! An Object to hold information on the clicked-on object. */ 7 | struct PickObject { 8 | PickObject(float dist, unsigned int id) : 9 | m_dist(dist), m_objectId(id), m_faceId(0) 10 | { 11 | } 12 | PickObject(float dist, unsigned int id, unsigned int faceId) : 13 | m_dist(dist), m_objectId(id), m_faceId(faceId) 14 | { 15 | } 16 | 17 | float m_dist; // the normalized distance of the intersection point from starting point of pick line 18 | unsigned int m_objectId; // the object clicked on 19 | unsigned int m_faceId; // the actual triangle/plane clicked on 20 | }; 21 | 22 | 23 | /*! Tests if a line (with equation p = p1 + t * d) hits a plane, defined by 24 | p = x * a + y * b. Returns true if intersection is found, and returns 25 | the normalized distance (t) between intersection point and point p1. 26 | */ 27 | bool intersectsRect(const QVector3D & a, 28 | const QVector3D & b, 29 | const QVector3D & normal, 30 | const QVector3D & offset, 31 | const QVector3D & p1, 32 | const QVector3D & d, 33 | float & dist); 34 | 35 | 36 | #endif // PICKOBJECT_H 37 | -------------------------------------------------------------------------------- /code/Example05/PlaneObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PlaneObjectH 13 | #define PlaneObjectH 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "PlaneMesh.h" 23 | 24 | /*! A container for transparent planes. 25 | */ 26 | class PlaneObject { 27 | public: 28 | PlaneObject(); 29 | 30 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 31 | void create(QOpenGLShaderProgram * shaderProgramm); 32 | void destroy(); 33 | 34 | void render(); 35 | 36 | std::vector m_planes; 37 | 38 | std::vector m_vertexBufferData; 39 | std::vector m_elementBufferData; 40 | 41 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 42 | QOpenGLVertexArrayObject m_vao; 43 | 44 | /*! Holds position and colors in a single buffer. */ 45 | QOpenGLBuffer m_vbo; 46 | /*! Holds elements. */ 47 | QOpenGLBuffer m_ebo; 48 | }; 49 | 50 | #endif // PlaneObjectH 51 | -------------------------------------------------------------------------------- /code/Example05/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Example05/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Example05/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/diffuse.frag 8 | shaders/VertexNormalColor.vert 9 | shaders/VertexColorTransparent.vert 10 | shaders/diffuseTransparent.frag 11 | 12 | 13 | -------------------------------------------------------------------------------- /code/Example05/shaders/VertexColorTransparent.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec4 position; // input: attribute with index '0' with 4 elements per vertex 7 | layout(location = 1) in vec4 color; // input: attribute with index '1' with 4 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position.xyz, 1.0); 15 | fragColor = color; 16 | } 17 | -------------------------------------------------------------------------------- /code/Example05/shaders/VertexNormalColor.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 normal; // input: attribute with index '1' with 3 elements per vertex 8 | layout(location = 2) in vec3 color; // input: attribute with index '2' with 3 elements (=rgb) per vertex 9 | out vec3 fragColor; // output: fragment color 10 | out vec3 fragNormal; // output: fragment normal vector 11 | out vec3 fragPos; // output: fragment position in world coords 12 | 13 | uniform mat4 worldToView; // parameter: the camera matrix 14 | 15 | void main() { 16 | // Mind multiplication order for matrixes 17 | gl_Position = worldToView * vec4(position, 1.0); 18 | fragPos = position; 19 | fragColor = color; 20 | fragNormal = normal; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /code/Example05/shaders/diffuse.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader with diffuse lighting 4 | 5 | in vec3 fragColor; // input: interpolated color as rgba-value 6 | in vec3 fragNormal; // input: normal vector of fragment 7 | in vec3 fragPos; // input: fragment position (world coords) 8 | 9 | out vec4 finalColor; // output: final color value as rgba-value 10 | 11 | uniform vec3 lightPos; // parameter: light position as vec3 (world coords) 12 | uniform vec3 lightColor; // parameter: light color as rgb 13 | 14 | void main() { 15 | // ambient 16 | float ambientStrength = 0.2; 17 | vec3 ambient = ambientStrength * lightColor; 18 | 19 | // diffuse 20 | vec3 norm = normalize(fragNormal); 21 | vec3 lightDir = normalize(lightPos - fragPos); 22 | float diff = max(dot(norm, lightDir), 0.0); 23 | vec3 diffuse = diff * lightColor; 24 | 25 | vec3 result = (ambient + diffuse) * fragColor; 26 | 27 | // to test correct passing of lightPos 28 | // finalColor = vec4(normalize(lightPos), 1.0); 29 | 30 | // to test correct passing of lightColor 31 | // finalColor = vec4(lightColor, 1.0); 32 | 33 | // to test fragment position component DISCUSS 34 | // finalColor = vec4(normalize(fragPos), 1.0); 35 | 36 | // to test fragment color 37 | // finalColor = vec4(fragColor, 1.0); 38 | 39 | // to test normal component 40 | // finalColor = vec4(norm, 1.0); 41 | 42 | finalColor = vec4(result, 1.0); 43 | } 44 | -------------------------------------------------------------------------------- /code/Example05/shaders/diffuseTransparent.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader with diffuse lighting 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | in vec3 fragNormal; // input: normal vector of fragment 7 | in vec3 fragPos; // input: fragment position (world coords) 8 | 9 | out vec4 finalColor; // output: final color value as rgba-value 10 | 11 | uniform vec3 lightPos; // parameter: light position as vec3 (world coords) 12 | uniform vec3 lightColor; // parameter: light color as rgb 13 | 14 | void main() { 15 | // ambient 16 | float ambientStrength = 0.2; 17 | vec3 ambient = ambientStrength * lightColor; 18 | 19 | // diffuse 20 | vec3 norm = normalize(fragNormal); 21 | vec3 lightDir = normalize(lightPos - fragPos); 22 | float diff = max(dot(norm, lightDir), 0.0); 23 | vec3 diffuse = diff * lightColor; 24 | 25 | vec4 result = vec4(ambient + diffuse, 1.0) * fragColor; 26 | 27 | // to test correct passing of lightPos 28 | // finalColor = vec4(normalize(lightPos), 1.0); 29 | 30 | // to test correct passing of lightColor 31 | // finalColor = vec4(lightColor, 1.0); 32 | 33 | // to test fragment position component DISCUSS 34 | // finalColor = vec4(normalize(fragPos), 1.0); 35 | 36 | // to test fragment color 37 | // finalColor = vec4(fragColor, 1.0); 38 | 39 | // to test normal component 40 | // finalColor = vec4(norm, 1.0); 41 | 42 | finalColor = result; 43 | } 44 | -------------------------------------------------------------------------------- /code/Example05/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 1000; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Example05/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Example05/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Example05/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Example06/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Example06/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Example06/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Example06/PickLineObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PICKLINEOBJECT_H 13 | #define PICKLINEOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | 23 | #include "Vertex.h" 24 | 25 | /*! For drawing a simple line. */ 26 | class PickLineObject { 27 | public: 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | void render(); 31 | 32 | void setPoints(const QVector3D & a, const QVector3D & b); 33 | 34 | bool m_visible = false; 35 | std::vector m_vertexBufferData; 36 | QOpenGLVertexArrayObject m_vao; 37 | QOpenGLBuffer m_vbo; 38 | }; 39 | 40 | #endif // PICKLINEOBJECT_H 41 | -------------------------------------------------------------------------------- /code/Example06/PickObject.h: -------------------------------------------------------------------------------- 1 | #ifndef PICKOBJECT_H 2 | #define PICKOBJECT_H 3 | 4 | #include 5 | 6 | /*! An Object to hold information on the clicked-on object. */ 7 | struct PickObject { 8 | PickObject(float dist, unsigned int id) : 9 | m_dist(dist), m_objectId(id), m_faceId(0) 10 | { 11 | } 12 | PickObject(float dist, unsigned int id, unsigned int faceId) : 13 | m_dist(dist), m_objectId(id), m_faceId(faceId) 14 | { 15 | } 16 | 17 | float m_dist; // the normalized distance of the intersection point from starting point of pick line 18 | unsigned int m_objectId; // the object clicked on 19 | unsigned int m_faceId; // the actual triangle/plane clicked on 20 | }; 21 | 22 | 23 | /*! Tests if a line (with equation p = p1 + t * d) hits a plane, defined by 24 | p = x * a + y * b. Returns true if intersection is found, and returns 25 | the normalized distance (t) between intersection point and point p1. 26 | */ 27 | bool intersectsRect(const QVector3D & a, 28 | const QVector3D & b, 29 | const QVector3D & normal, 30 | const QVector3D & offset, 31 | const QVector3D & p1, 32 | const QVector3D & d, 33 | float & dist); 34 | 35 | 36 | #endif // PICKOBJECT_H 37 | -------------------------------------------------------------------------------- /code/Example06/PlaneObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PlaneObjectH 13 | #define PlaneObjectH 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "PlaneMesh.h" 23 | 24 | /*! A container for transparent planes. 25 | */ 26 | class PlaneObject { 27 | public: 28 | PlaneObject(); 29 | 30 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 31 | void create(QOpenGLShaderProgram * shaderProgramm); 32 | void destroy(); 33 | 34 | void render(); 35 | 36 | std::vector m_planes; 37 | 38 | std::vector m_vertexBufferData; 39 | std::vector m_elementBufferData; 40 | 41 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 42 | QOpenGLVertexArrayObject m_vao; 43 | 44 | /*! Holds position and colors in a single buffer. */ 45 | QOpenGLBuffer m_vbo; 46 | /*! Holds elements. */ 47 | QOpenGLBuffer m_ebo; 48 | }; 49 | 50 | #endif // PlaneObjectH 51 | -------------------------------------------------------------------------------- /code/Example06/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Example06/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Example06/resources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/diffuse.frag 8 | shaders/VertexNormalColor.vert 9 | shaders/VertexColorTransparent.vert 10 | shaders/diffuseTransparent.frag 11 | shaders/texture.frag 12 | shaders/VertexFontTexture.vert 13 | 14 | 15 | -------------------------------------------------------------------------------- /code/Example06/shaders/VertexColorTransparent.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec4 position; // input: attribute with index '0' with 4 elements per vertex 7 | layout(location = 1) in vec4 color; // input: attribute with index '1' with 4 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position.xyz, 1.0); 15 | fragColor = color; 16 | } 17 | -------------------------------------------------------------------------------- /code/Example06/shaders/VertexFontTexture.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec2 texinfo; // input: attribute with index '1' with 2 elements per vertex (texi, texj) 8 | 9 | out vec2 texCoord; // output: computed texture coordinates 10 | //flat out float texID; // output: texture ID - mind the 'flat' attribute (disables interpolation)! 11 | 12 | uniform mat4 worldToView; // parameter: the camera matrix 13 | 14 | void main() { 15 | gl_Position = worldToView * vec4(position, 1.0); 16 | texCoord = texinfo; 17 | // texID = texinfo.z; 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /code/Example06/shaders/VertexNormalColor.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 normal; // input: attribute with index '1' with 3 elements per vertex 8 | layout(location = 2) in vec3 color; // input: attribute with index '2' with 3 elements (=rgb) per vertex 9 | out vec3 fragColor; // output: fragment color 10 | out vec3 fragNormal; // output: fragment normal vector 11 | out vec3 fragPos; // output: fragment position in world coords 12 | 13 | uniform mat4 worldToView; // parameter: the camera matrix 14 | 15 | void main() { 16 | // Mind multiplication order for matrixes 17 | gl_Position = worldToView * vec4(position, 1.0); 18 | fragPos = position; 19 | fragColor = color; 20 | fragNormal = normal; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /code/Example06/shaders/diffuse.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader with diffuse lighting 4 | 5 | in vec3 fragColor; // input: interpolated color as rgba-value 6 | in vec3 fragNormal; // input: normal vector of fragment 7 | in vec3 fragPos; // input: fragment position (world coords) 8 | 9 | out vec4 finalColor; // output: final color value as rgba-value 10 | 11 | uniform vec3 lightPos; // parameter: light position as vec3 (world coords) 12 | uniform vec3 lightColor; // parameter: light color as rgb 13 | 14 | void main() { 15 | // ambient 16 | float ambientStrength = 0.2; 17 | vec3 ambient = ambientStrength * lightColor; 18 | 19 | // diffuse 20 | vec3 norm = normalize(fragNormal); 21 | vec3 lightDir = normalize(lightPos - fragPos); 22 | float diff = max(dot(norm, lightDir), 0.0); 23 | vec3 diffuse = diff * lightColor; 24 | 25 | vec3 result = (ambient + diffuse) * fragColor; 26 | 27 | // to test correct passing of lightPos 28 | // finalColor = vec4(normalize(lightPos), 1.0); 29 | 30 | // to test correct passing of lightColor 31 | // finalColor = vec4(lightColor, 1.0); 32 | 33 | // to test fragment position component DISCUSS 34 | // finalColor = vec4(normalize(fragPos), 1.0); 35 | 36 | // to test fragment color 37 | // finalColor = vec4(fragColor, 1.0); 38 | 39 | // to test normal component 40 | // finalColor = vec4(norm, 1.0); 41 | 42 | finalColor = vec4(result, 1.0); 43 | } 44 | -------------------------------------------------------------------------------- /code/Example06/shaders/diffuseTransparent.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader with diffuse lighting 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | in vec3 fragNormal; // input: normal vector of fragment 7 | in vec3 fragPos; // input: fragment position (world coords) 8 | 9 | out vec4 finalColor; // output: final color value as rgba-value 10 | 11 | uniform vec3 lightPos; // parameter: light position as vec3 (world coords) 12 | uniform vec3 lightColor; // parameter: light color as rgb 13 | 14 | void main() { 15 | // ambient 16 | float ambientStrength = 0.2; 17 | vec3 ambient = ambientStrength * lightColor; 18 | 19 | // diffuse 20 | vec3 norm = normalize(fragNormal); 21 | vec3 lightDir = normalize(lightPos - fragPos); 22 | float diff = max(dot(norm, lightDir), 0.0); 23 | vec3 diffuse = diff * lightColor; 24 | 25 | vec4 result = vec4(ambient + diffuse, 1.0) * fragColor; 26 | 27 | // to test correct passing of lightPos 28 | // finalColor = vec4(normalize(lightPos), 1.0); 29 | 30 | // to test correct passing of lightColor 31 | // finalColor = vec4(lightColor, 1.0); 32 | 33 | // to test fragment position component DISCUSS 34 | // finalColor = vec4(normalize(fragPos), 1.0); 35 | 36 | // to test fragment color 37 | // finalColor = vec4(fragColor, 1.0); 38 | 39 | // to test normal component 40 | // finalColor = vec4(norm, 1.0); 41 | 42 | finalColor = result; 43 | } 44 | -------------------------------------------------------------------------------- /code/Example06/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 1000; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Example06/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Example06/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Example06/shaders/texture.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec2 texCoord; // input: texture coordinate (xy-coordinates within the texture) 6 | //flat in float texID; // input: textureID 7 | out vec4 finalColor; // output: final color value as rgba-value 8 | 9 | uniform sampler2D text01; // the font texture 10 | 11 | void main() { 12 | // todo switch texture based on texID 13 | finalColor = texture(text01, texCoord); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /code/Example06/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Example07/Example07.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------ 2 | # 3 | # Testproject to check for OpenGL functionality in a Qt application 4 | # 5 | #------------------------------------------------------------------ 6 | 7 | QT += core gui widgets 8 | 9 | TARGET = Tutorial_02 10 | TEMPLATE = app 11 | 12 | # The following define makes your compiler emit warnings if you use 13 | # any feature of Qt which has been marked as deprecated (the exact warnings 14 | # depend on your compiler). Please consult the documentation of the 15 | # deprecated API in order to know how to port your code away from it. 16 | DEFINES += QT_DEPRECATED_WARNINGS 17 | 18 | # You can also make your code fail to compile if you use deprecated APIs. 19 | # In order to do so, uncomment the following line. 20 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 21 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 22 | 23 | CONFIG += c++11 24 | 25 | SOURCES += \ 26 | RectangleWindow.cpp \ 27 | main.cpp 28 | 29 | HEADERS += \ 30 | RectangleWindow.h 31 | 32 | win32 { 33 | LIBS += -lopengl32 34 | } 35 | 36 | # Default rules for deployment. 37 | qnx: target.path = /tmp/$${TARGET}/bin 38 | else: unix:!android: target.path = /opt/$${TARGET}/bin 39 | !isEmpty(target.path): INSTALLS += target 40 | 41 | RESOURCES += \ 42 | example.qrc 43 | -------------------------------------------------------------------------------- /code/Example07/example.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/pass_through.vert 4 | shaders/simple.frag 5 | 6 | 7 | -------------------------------------------------------------------------------- /code/Example07/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "RectangleWindow.h" 13 | 14 | #include 15 | #include 16 | 17 | int main(int argc, char **argv) { 18 | QGuiApplication app(argc, argv); 19 | 20 | // Set OpenGL Version information 21 | QSurfaceFormat format; 22 | format.setRenderableType(QSurfaceFormat::OpenGL); 23 | format.setProfile(QSurfaceFormat::CoreProfile); 24 | format.setVersion(3,3); 25 | 26 | RectangleWindow window; 27 | // Note: The format must be set before show() is called. 28 | window.setFormat(format); 29 | window.resize(640, 480); 30 | window.show(); 31 | 32 | return app.exec(); 33 | } 34 | -------------------------------------------------------------------------------- /code/Example07/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | layout (location = 1) in vec3 color; 8 | 9 | // test: 'flat' in front of fragColor disables color interpolation 10 | out vec4 fragColor; 11 | 12 | void main() { 13 | gl_Position = vec4(position, 1.0); 14 | fragColor = vec4(color, 1.0); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /code/Example07/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | // test: 'flat' in front of fragColor disables color interpolation 6 | in vec4 fragColor; // input: interpolated color as rgba-value 7 | out vec4 finalColor; // output: final color value as rgba-value 8 | 9 | void main() { 10 | finalColor = fragColor; 11 | } 12 | -------------------------------------------------------------------------------- /code/Example08/Example08.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------ 2 | # 3 | # Testproject to check for OpenGL functionality in a Qt application 4 | # 5 | #------------------------------------------------------------------ 6 | 7 | QT += core gui widgets 8 | 9 | TARGET = Example_08 10 | TEMPLATE = app 11 | 12 | # The following define makes your compiler emit warnings if you use 13 | # any feature of Qt which has been marked as deprecated (the exact warnings 14 | # depend on your compiler). Please consult the documentation of the 15 | # deprecated API in order to know how to port your code away from it. 16 | DEFINES += QT_DEPRECATED_WARNINGS 17 | 18 | # You can also make your code fail to compile if you use deprecated APIs. 19 | # In order to do so, uncomment the following line. 20 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 21 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 22 | 23 | CONFIG += c++11 24 | 25 | SOURCES += \ 26 | RectangleWindow.cpp \ 27 | main.cpp 28 | 29 | HEADERS += \ 30 | RectangleWindow.h 31 | 32 | win32 { 33 | LIBS += -lopengl32 34 | } 35 | 36 | # Default rules for deployment. 37 | qnx: target.path = /tmp/$${TARGET}/bin 38 | else: unix:!android: target.path = /opt/$${TARGET}/bin 39 | !isEmpty(target.path): INSTALLS += target 40 | 41 | RESOURCES += \ 42 | example.qrc 43 | -------------------------------------------------------------------------------- /code/Example08/example.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/pass_through.vert 4 | shaders/simple.frag 5 | 6 | 7 | -------------------------------------------------------------------------------- /code/Example08/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "RectangleWindow.h" 13 | 14 | #include 15 | #include 16 | 17 | int main(int argc, char **argv) { 18 | QGuiApplication app(argc, argv); 19 | 20 | // Set OpenGL Version information 21 | QSurfaceFormat format; 22 | format.setRenderableType(QSurfaceFormat::OpenGL); 23 | format.setProfile(QSurfaceFormat::CoreProfile); 24 | format.setVersion(3,3); 25 | 26 | RectangleWindow window; 27 | // Note: The format must be set before show() is called. 28 | window.setFormat(format); 29 | window.resize(640, 480); 30 | window.show(); 31 | 32 | return app.exec(); 33 | } 34 | -------------------------------------------------------------------------------- /code/Example08/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | layout (location = 1) in vec4 color; 8 | 9 | // test: 'flat' in front of fragColor disables color interpolation 10 | out vec4 fragColor; 11 | 12 | void main() { 13 | gl_Position = vec4(position, 1.0); 14 | fragColor = color; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /code/Example08/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | // test: 'flat' in front of fragColor disables color interpolation 6 | in vec4 fragColor; // input: interpolated color as rgba-value 7 | out vec4 finalColor; // output: final color value as rgba-value 8 | 9 | void main() { 10 | finalColor = fragColor; 11 | } 12 | -------------------------------------------------------------------------------- /code/Tutorial_01/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | -------------------------------------------------------------------------------- /code/Tutorial_01/OpenGLWindow.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | Source code is based on Qt Example OpenGLWindow, but has been simplified a lot. 11 | 12 | ************************************************************************************/ 13 | 14 | #ifndef OpenGLWindow_H 15 | #define OpenGLWindow_H 16 | 17 | #include 18 | #include 19 | 20 | QT_BEGIN_NAMESPACE 21 | class QOpenGLContext; 22 | QT_END_NAMESPACE 23 | 24 | class OpenGLWindow : public QWindow, protected QOpenGLFunctions { 25 | Q_OBJECT 26 | public: 27 | explicit OpenGLWindow(QWindow *parent = nullptr); 28 | 29 | virtual void render() = 0; 30 | virtual void initialize() = 0; 31 | 32 | public slots: 33 | void renderLater(); 34 | void renderNow(); 35 | 36 | protected: 37 | bool event(QEvent *event) Q_DECL_OVERRIDE; 38 | void exposeEvent(QExposeEvent *event) Q_DECL_OVERRIDE; 39 | 40 | QOpenGLContext *m_context; 41 | }; 42 | 43 | #endif // OpenGLWindow_H 44 | -------------------------------------------------------------------------------- /code/Tutorial_01/TriangleWindow.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TriangleWindow_H 13 | #define TriangleWindow_H 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "OpenGLWindow.h" 20 | 21 | /* This is the window that shows the triangle. 22 | We derive from our OpenGLWindow base class and implement the 23 | virtual initialize() and render() functions. 24 | */ 25 | class TriangleWindow : public OpenGLWindow { 26 | public: 27 | TriangleWindow(); 28 | ~TriangleWindow() Q_DECL_OVERRIDE; 29 | 30 | void initialize() Q_DECL_OVERRIDE; 31 | void render() Q_DECL_OVERRIDE; 32 | 33 | private: 34 | // Wraps an OpenGL VertexArrayObject (VAO) 35 | QOpenGLVertexArrayObject m_vao; 36 | // Vertex buffer (only positions now). 37 | QOpenGLBuffer m_vertexBufferObject; 38 | 39 | // Holds the compiled shader programs. 40 | QOpenGLShaderProgram *m_program; 41 | }; 42 | 43 | #endif // TriangleWindow_H 44 | -------------------------------------------------------------------------------- /code/Tutorial_01/Tutorial_01.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------ 2 | # 3 | # Testproject to check for OpenGL functionality in a Qt application 4 | # 5 | #------------------------------------------------------------------ 6 | 7 | QT += core gui widgets 8 | 9 | TARGET = Tutorial_01 10 | TEMPLATE = app 11 | 12 | # The following define makes your compiler emit warnings if you use 13 | # any feature of Qt which has been marked as deprecated (the exact warnings 14 | # depend on your compiler). Please consult the documentation of the 15 | # deprecated API in order to know how to port your code away from it. 16 | DEFINES += QT_DEPRECATED_WARNINGS 17 | 18 | # You can also make your code fail to compile if you use deprecated APIs. 19 | # In order to do so, uncomment the following line. 20 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 21 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 22 | 23 | CONFIG += c++11 24 | 25 | win32 { 26 | LIBS += -lopengl32 27 | } 28 | 29 | SOURCES += \ 30 | main.cpp \ 31 | OpenGLWindow.cpp \ 32 | TriangleWindow.cpp 33 | 34 | HEADERS += \ 35 | OpenGLWindow.h \ 36 | TriangleWindow.h 37 | 38 | # Default rules for deployment. 39 | qnx: target.path = /tmp/$${TARGET}/bin 40 | else: unix:!android: target.path = /opt/$${TARGET}/bin 41 | !isEmpty(target.path): INSTALLS += target 42 | 43 | RESOURCES += \ 44 | Tutorial_01.qrc 45 | -------------------------------------------------------------------------------- /code/Tutorial_01/Tutorial_01.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/pass_through.vert 4 | shaders/uniform_color.frag 5 | 6 | 7 | -------------------------------------------------------------------------------- /code/Tutorial_01/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TriangleWindow.h" 13 | 14 | #include 15 | #include 16 | 17 | int main(int argc, char **argv) { 18 | QGuiApplication app(argc, argv); 19 | 20 | // Set OpenGL Version information 21 | QSurfaceFormat format; 22 | format.setRenderableType(QSurfaceFormat::OpenGL); 23 | format.setProfile(QSurfaceFormat::CoreProfile); 24 | format.setVersion(3,3); 25 | 26 | TriangleWindow window; 27 | // Note: The format must be set before show() is called. 28 | window.setFormat(format); 29 | window.resize(640, 480); 30 | window.show(); 31 | 32 | return app.exec(); 33 | } 34 | -------------------------------------------------------------------------------- /code/Tutorial_01/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | 8 | void main() { 9 | gl_Position = vec4(position, 1.0); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /code/Tutorial_01/shaders/uniform_color.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | out vec4 FragColor; // output: final color value as rgba-value 6 | 7 | void main() { 8 | FragColor = vec4(0.8, 0.2, 0.1, 1); 9 | } 10 | -------------------------------------------------------------------------------- /code/Tutorial_02/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | -------------------------------------------------------------------------------- /code/Tutorial_02/RectangleWindow.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef RectangleWindow_H 13 | #define RectangleWindow_H 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | /* This is the window that shows the two triangles to form a rectangle. 21 | We derive from our QOpenGLWindow base class and implement the 22 | virtual initializeGL() and paintGL() functions. 23 | */ 24 | class RectangleWindow : public QOpenGLWindow { 25 | public: 26 | RectangleWindow(); 27 | virtual ~RectangleWindow() Q_DECL_OVERRIDE; 28 | 29 | void initializeGL() Q_DECL_OVERRIDE; 30 | void paintGL() Q_DECL_OVERRIDE; 31 | 32 | private: 33 | // Wraps an OpenGL VertexArrayObject (VAO) 34 | QOpenGLVertexArrayObject m_vao; 35 | // Vertex buffer (positions and colors, interleaved storage mode). 36 | QOpenGLBuffer m_vertexBufferObject; 37 | // Index buffer to draw two rectangles 38 | QOpenGLBuffer m_indexBufferObject; 39 | 40 | // Holds the compiled shader programs. 41 | QOpenGLShaderProgram *m_program; 42 | }; 43 | 44 | #endif // RectangleWindow_H 45 | -------------------------------------------------------------------------------- /code/Tutorial_02/Tutorial_02.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------ 2 | # 3 | # Testproject to check for OpenGL functionality in a Qt application 4 | # 5 | #------------------------------------------------------------------ 6 | 7 | QT += core gui widgets 8 | 9 | TARGET = Tutorial_02 10 | TEMPLATE = app 11 | 12 | # The following define makes your compiler emit warnings if you use 13 | # any feature of Qt which has been marked as deprecated (the exact warnings 14 | # depend on your compiler). Please consult the documentation of the 15 | # deprecated API in order to know how to port your code away from it. 16 | DEFINES += QT_DEPRECATED_WARNINGS 17 | 18 | # You can also make your code fail to compile if you use deprecated APIs. 19 | # In order to do so, uncomment the following line. 20 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 21 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 22 | 23 | CONFIG += c++11 24 | 25 | SOURCES += \ 26 | RectangleWindow.cpp \ 27 | main.cpp 28 | 29 | HEADERS += \ 30 | RectangleWindow.h 31 | 32 | win32 { 33 | LIBS += -lopengl32 34 | } 35 | 36 | # Default rules for deployment. 37 | qnx: target.path = /tmp/$${TARGET}/bin 38 | else: unix:!android: target.path = /opt/$${TARGET}/bin 39 | !isEmpty(target.path): INSTALLS += target 40 | 41 | RESOURCES += \ 42 | Tutorial_02.qrc 43 | -------------------------------------------------------------------------------- /code/Tutorial_02/Tutorial_02.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/pass_through.vert 4 | shaders/simple.frag 5 | 6 | 7 | -------------------------------------------------------------------------------- /code/Tutorial_02/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "RectangleWindow.h" 13 | 14 | #include 15 | #include 16 | 17 | int main(int argc, char **argv) { 18 | QGuiApplication app(argc, argv); 19 | 20 | // Set OpenGL Version information 21 | QSurfaceFormat format; 22 | format.setRenderableType(QSurfaceFormat::OpenGL); 23 | format.setProfile(QSurfaceFormat::CoreProfile); 24 | format.setVersion(3,3); 25 | 26 | RectangleWindow window; 27 | // Note: The format must be set before show() is called. 28 | window.setFormat(format); 29 | window.resize(640, 480); 30 | window.show(); 31 | 32 | return app.exec(); 33 | } 34 | -------------------------------------------------------------------------------- /code/Tutorial_02/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | layout (location = 1) in vec3 color; 8 | 9 | out vec4 fragColor; 10 | 11 | void main() { 12 | gl_Position = vec4(position, 1.0); 13 | fragColor = vec4(color, 1.0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /code/Tutorial_02/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_03/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_03/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class RectangleWindow; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private slots: 26 | void onChangeColors(); 27 | void onAnimateColors(); 28 | 29 | private: 30 | RectangleWindow * m_rectangleWindow; 31 | }; 32 | 33 | #endif // TESTDIALOG_H 34 | -------------------------------------------------------------------------------- /code/Tutorial_03/Tutorial_03.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------ 2 | # 3 | # Testproject to check for OpenGL functionality in a Qt application 4 | # 5 | #------------------------------------------------------------------ 6 | 7 | QT += core gui widgets 8 | 9 | TARGET = Tutorial_03 10 | TEMPLATE = app 11 | 12 | # The following define makes your compiler emit warnings if you use 13 | # any feature of Qt which has been marked as deprecated (the exact warnings 14 | # depend on your compiler). Please consult the documentation of the 15 | # deprecated API in order to know how to port your code away from it. 16 | DEFINES += QT_DEPRECATED_WARNINGS 17 | 18 | # You can also make your code fail to compile if you use deprecated APIs. 19 | # In order to do so, uncomment the following line. 20 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 21 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 22 | 23 | CONFIG += c++11 24 | 25 | win32 { 26 | LIBS += -lopengl32 27 | } 28 | 29 | SOURCES += \ 30 | RectangleWindow.cpp \ 31 | TestDialog.cpp \ 32 | main.cpp 33 | 34 | HEADERS += \ 35 | RectangleWindow.h \ 36 | TestDialog.h 37 | 38 | # Default rules for deployment. 39 | qnx: target.path = /tmp/$${TARGET}/bin 40 | else: unix:!android: target.path = /opt/$${TARGET}/bin 41 | !isEmpty(target.path): INSTALLS += target 42 | 43 | RESOURCES += \ 44 | Tutorial_03.qrc 45 | -------------------------------------------------------------------------------- /code/Tutorial_03/Tutorial_03.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/pass_through.vert 4 | shaders/simple.frag 5 | 6 | 7 | -------------------------------------------------------------------------------- /code/Tutorial_03/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | 16 | int main(int argc, char **argv) { 17 | QApplication app(argc, argv); 18 | 19 | TestDialog dlg; 20 | dlg.show(); 21 | 22 | return app.exec(); 23 | } 24 | -------------------------------------------------------------------------------- /code/Tutorial_03/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | layout (location = 1) in vec3 color; 8 | 9 | out vec4 fragColor; 10 | 11 | void main() { 12 | gl_Position = vec4(position, 1.0); 13 | fragColor = vec4(color, 1.0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /code/Tutorial_03/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_04/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_04/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class RectangleWindow; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private slots: 26 | void onChangeColors(); 27 | void onAnimateColors(); 28 | 29 | private: 30 | RectangleWindow * m_rectangleWindow; 31 | }; 32 | 33 | #endif // TESTDIALOG_H 34 | -------------------------------------------------------------------------------- /code/Tutorial_04/Tutorial_04.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------ 2 | # 3 | # Testproject to check for OpenGL functionality in a Qt application 4 | # 5 | #------------------------------------------------------------------ 6 | 7 | QT += core gui widgets 8 | 9 | TARGET = Tutorial_04 10 | TEMPLATE = app 11 | 12 | # The following define makes your compiler emit warnings if you use 13 | # any feature of Qt which has been marked as deprecated (the exact warnings 14 | # depend on your compiler). Please consult the documentation of the 15 | # deprecated API in order to know how to port your code away from it. 16 | DEFINES += QT_DEPRECATED_WARNINGS 17 | 18 | # You can also make your code fail to compile if you use deprecated APIs. 19 | # In order to do so, uncomment the following line. 20 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 21 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 22 | 23 | CONFIG += c++11 24 | 25 | win32 { 26 | LIBS += -lopengl32 27 | } 28 | 29 | SOURCES += \ 30 | RectangleWindow.cpp \ 31 | TestDialog.cpp \ 32 | main.cpp 33 | 34 | HEADERS += \ 35 | RectangleWindow.h \ 36 | TestDialog.h 37 | 38 | # Default rules for deployment. 39 | qnx: target.path = /tmp/$${TARGET}/bin 40 | else: unix:!android: target.path = /opt/$${TARGET}/bin 41 | !isEmpty(target.path): INSTALLS += target 42 | 43 | RESOURCES += \ 44 | Tutorial_04.qrc 45 | -------------------------------------------------------------------------------- /code/Tutorial_04/Tutorial_04.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/pass_through.vert 4 | shaders/simple.frag 5 | 6 | 7 | -------------------------------------------------------------------------------- /code/Tutorial_04/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | 16 | int main(int argc, char **argv) { 17 | QApplication app(argc, argv); 18 | 19 | TestDialog dlg; 20 | dlg.show(); 21 | 22 | return app.exec(); 23 | } 24 | -------------------------------------------------------------------------------- /code/Tutorial_04/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | layout (location = 1) in vec3 color; 8 | 9 | out vec4 fragColor; 10 | 11 | void main() { 12 | gl_Position = vec4(position, 1.0); 13 | fragColor = vec4(color, 1.0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /code/Tutorial_04/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_05/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_05/BoxObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef BOXOBJECT_H 13 | #define BOXOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "BoxMesh.h" 23 | 24 | /*! A container for all the boxes. 25 | Basically creates the geometry of the individual boxes and populates the buffers. 26 | */ 27 | class BoxObject { 28 | public: 29 | BoxObject(); 30 | 31 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 32 | void create(QOpenGLShaderProgram * shaderProgramm); 33 | void destroy(); 34 | 35 | void render(); 36 | 37 | std::vector m_boxes; 38 | 39 | std::vector m_vertexBufferData; 40 | std::vector m_elementBufferData; 41 | 42 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 43 | QOpenGLVertexArrayObject m_vao; 44 | 45 | /*! Holds position and colors in a single buffer. */ 46 | QOpenGLBuffer m_vbo; 47 | /*! Holds elements. */ 48 | QOpenGLBuffer m_ebo; 49 | }; 50 | 51 | #endif // BOXOBJECT_H 52 | -------------------------------------------------------------------------------- /code/Tutorial_05/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_05/Tutorial_05.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | 8 | 9 | -------------------------------------------------------------------------------- /code/Tutorial_05/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | int main(int argc, char **argv) { 18 | QApplication app(argc, argv); 19 | 20 | qsrand(time(nullptr)); 21 | 22 | TestDialog dlg; 23 | dlg.show(); 24 | 25 | return app.exec(); 26 | } 27 | -------------------------------------------------------------------------------- /code/Tutorial_05/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_05/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_05/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_05/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Tutorial_06/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_06/BoxObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef BOXOBJECT_H 13 | #define BOXOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "BoxMesh.h" 23 | 24 | /*! A container for all the boxes. 25 | Basically creates the geometry of the individual boxes and populates the buffers. 26 | */ 27 | class BoxObject { 28 | public: 29 | BoxObject(); 30 | 31 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 32 | void create(QOpenGLShaderProgram * shaderProgramm); 33 | void destroy(); 34 | 35 | void render(); 36 | 37 | std::vector m_boxes; 38 | 39 | std::vector m_vertexBufferData; 40 | std::vector m_elementBufferData; 41 | 42 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 43 | QOpenGLVertexArrayObject m_vao; 44 | 45 | /*! Holds position and colors in a single buffer. */ 46 | QOpenGLBuffer m_vbo; 47 | /*! Holds elements. */ 48 | QOpenGLBuffer m_ebo; 49 | }; 50 | 51 | #endif // BOXOBJECT_H 52 | -------------------------------------------------------------------------------- /code/Tutorial_06/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_06/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | 5 | OpenGLException::OpenGLException(const QString & msg) { 6 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 7 | } 8 | 9 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 10 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 11 | } 12 | 13 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 14 | m_msgStack(previous.m_msgStack) 15 | { 16 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 17 | } 18 | 19 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 20 | m_msgStack(previous.m_msgStack) 21 | { 22 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 23 | } 24 | 25 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 26 | for (std::list >::const_iterator it = m_msgStack.begin(); 27 | it != m_msgStack.end(); ++it) 28 | { 29 | QStringList lines = it->first.split("\n"); 30 | QString indx("[%1] "); 31 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 32 | for (const QString & l : lines) { 33 | if (it->second.isEmpty()) 34 | strm << (indx + l + "\n").toStdString(); 35 | else 36 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 37 | } 38 | strm.flush(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /code/Tutorial_06/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Tutorial_06/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_06/Tutorial_06.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | 8 | 9 | -------------------------------------------------------------------------------- /code/Tutorial_06/build/.gitignore: -------------------------------------------------------------------------------- 1 | /vc_x64_deploy/ 2 | /vc_x64_release/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_06/build/build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: cl compiler paths 4 | call "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x64 5 | 6 | :: path to Qt 7 | set CMAKE_PREFIX_PATH=c:\Qt\5.11.3\msvc2015_64 8 | 9 | :: we build in subdir 10 | mkdir vc_x64_release 11 | cd vc_x64_release 12 | 13 | :: configure in release mode, to build with JOM 14 | cmake -G "NMake Makefiles JOM" -DCMAKE_BUILD_TYPE:String="Release" ..\.. 15 | 16 | :: build 17 | jom 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /code/Tutorial_06/build/deploy.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :: path to Qt 4 | set CMAKE_PREFIX_PATH=c:\Qt\5.11.3\msvc2015_64 5 | 6 | :: deployment 7 | mkdir vc_x64_deploy 8 | copy vc_x64_release\Tutorial_06.exe vc_x64_deploy 9 | 10 | :: copy dependencies 11 | %CMAKE_PREFIX_PATH%\bin\windeployqt vc_x64_deploy\Tutorial_06.exe 12 | 13 | 14 | -------------------------------------------------------------------------------- /code/Tutorial_06/build/readme.md: -------------------------------------------------------------------------------- 1 | This directory contains example build scripts as demonstration for CMakeLists.txt usage. 2 | -------------------------------------------------------------------------------- /code/Tutorial_06/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Tutorial_06/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_06/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_06/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_06/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Tutorial_07/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_07/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_07/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Tutorial_07/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Tutorial_07/PickLineObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef PICKLINEOBJECT_H 13 | #define PICKLINEOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | 23 | #include "Vertex.h" 24 | 25 | /*! For drawing a simple line. */ 26 | class PickLineObject { 27 | public: 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | void render(); 31 | 32 | void setPoints(const QVector3D & a, const QVector3D & b); 33 | 34 | bool m_visible = false; 35 | std::vector m_vertexBufferData; 36 | QOpenGLVertexArrayObject m_vao; 37 | QOpenGLBuffer m_vbo; 38 | }; 39 | 40 | #endif // PICKLINEOBJECT_H 41 | -------------------------------------------------------------------------------- /code/Tutorial_07/PickObject.h: -------------------------------------------------------------------------------- 1 | #ifndef PICKOBJECT_H 2 | #define PICKOBJECT_H 3 | 4 | #include 5 | 6 | /*! An Object to hold information on the clicked-on object. */ 7 | struct PickObject { 8 | PickObject(float dist, unsigned int id) : 9 | m_dist(dist), m_objectId(id), m_faceId(0) 10 | { 11 | } 12 | PickObject(float dist, unsigned int id, unsigned int faceId) : 13 | m_dist(dist), m_objectId(id), m_faceId(faceId) 14 | { 15 | } 16 | 17 | float m_dist; // the normalized distance of the intersection point from starting point of pick line 18 | unsigned int m_objectId; // the object clicked on 19 | unsigned int m_faceId; // the actual triangle/plane clicked on 20 | }; 21 | 22 | 23 | /*! Tests if a line (with equation p = p1 + t * d) hits a plane, defined by 24 | p = x * a + y * b. Returns true if intersection is found, and returns 25 | the normalized distance (t) between intersection point and point p1. 26 | */ 27 | bool intersectsRect(const QVector3D & a, 28 | const QVector3D & b, 29 | const QVector3D & normal, 30 | const QVector3D & offset, 31 | const QVector3D & p1, 32 | const QVector3D & d, 33 | float & dist); 34 | 35 | 36 | #endif // PICKOBJECT_H 37 | -------------------------------------------------------------------------------- /code/Tutorial_07/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_07/Tutorial_07.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | 8 | 9 | -------------------------------------------------------------------------------- /code/Tutorial_07/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #include "TestDialog.h" 13 | 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | 20 | #include "OpenGLException.h" 21 | #include "DebugApplication.h" 22 | 23 | void qDebugMsgHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { 24 | (void) context; 25 | QString msgPrefix = "[" + QDateTime::currentDateTime().toString() + "] "; 26 | switch (type) { 27 | case QtDebugMsg : msgPrefix += "Debug: "; break; 28 | case QtWarningMsg : msgPrefix += "Warning: "; break; 29 | case QtCriticalMsg : msgPrefix += "Critical: "; break; 30 | case QtFatalMsg : msgPrefix += "Fatal: "; break; 31 | case QtInfoMsg : msgPrefix += "Info: "; break; 32 | } 33 | QStringList lines = msg.split("\n"); 34 | for (const QString & l : lines) 35 | std::cout << (msgPrefix + l).toStdString() << std::endl; 36 | } 37 | 38 | 39 | int main(int argc, char **argv) { 40 | qInstallMessageHandler(qDebugMsgHandler); 41 | 42 | DebugApplication app(argc, argv); 43 | 44 | qsrand(time(nullptr)); 45 | 46 | TestDialog dlg; 47 | dlg.show(); 48 | return app.exec(); 49 | } 50 | -------------------------------------------------------------------------------- /code/Tutorial_07/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_07/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_07/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_07/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Tutorial_08/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_08/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_08/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | 5 | OpenGLException::OpenGLException(const QString & msg) { 6 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 7 | } 8 | 9 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 10 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 11 | } 12 | 13 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 14 | m_msgStack(previous.m_msgStack) 15 | { 16 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 17 | } 18 | 19 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 20 | m_msgStack(previous.m_msgStack) 21 | { 22 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 23 | } 24 | 25 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 26 | for (std::list >::const_iterator it = m_msgStack.begin(); 27 | it != m_msgStack.end(); ++it) 28 | { 29 | QStringList lines = it->first.split("\n"); 30 | QString indx("[%1] "); 31 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 32 | for (const QString & l : lines) { 33 | if (it->second.isEmpty()) 34 | strm << (indx + l + "\n").toStdString(); 35 | else 36 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 37 | } 38 | strm.flush(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /code/Tutorial_08/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Tutorial_08/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_08/Tutorial_08.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/texture.frag 8 | shaders/withTexture.vert 9 | textures/brickwall.jpg 10 | textures/tiles.jpg 11 | textures/plaster.jpg 12 | 13 | 14 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/pass_through.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // vertex shader 4 | 5 | // input: attribute named 'position' with 3 floats per vertex 6 | layout (location = 0) in vec3 position; 7 | layout (location = 1) in vec3 color; 8 | 9 | out vec4 fragColor; 10 | 11 | void main() { 12 | gl_Position = vec4(position, 1.0); 13 | fragColor = vec4(color, 1.0); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/texture.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | in vec2 texCoord; // input: texture coordinate (xy-coordinates) 7 | flat in float texID; // input: textureID 8 | out vec4 finalColor; // output: final color value as rgba-value 9 | 10 | uniform sampler2D brickTexture; // the wall texture 11 | uniform sampler2D plasterTexture; // the plaster texture 12 | uniform sampler2D rooftiles; // the roof texture 13 | 14 | void main() { 15 | // finalColor = vec4(texCoord,0,0); 16 | if (texID == 0) 17 | finalColor = texture(brickTexture, texCoord); 18 | else if (texID == 2) 19 | finalColor = texture(rooftiles, texCoord); 20 | else if (texID == 1) 21 | finalColor = texture(plasterTexture, texCoord); 22 | else 23 | finalColor = vec4(texCoord,0,0);//texture(rooftiles, texCoord); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/withTexture.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | layout(location = 2) in vec2 texcoords; // input: attribute with index '2' with 2 elements per vertex 9 | layout(location = 3) in float texnr; // input: attribute with index '3' - uint per vertex 10 | out vec4 fragColor; // output: computed fragmentation color 11 | out vec2 texCoord; // output: computed texture coordinates 12 | flat out float texID; // output: texture ID - mind the 'flat' attribute! 13 | uniform mat4 worldToView; // parameter: the camera matrix 14 | 15 | void main() { 16 | // Mind multiplication order for matrixes 17 | gl_Position = worldToView * vec4(position, 1.0); 18 | fragColor = vec4(color, 1.0); 19 | texCoord = texcoords; 20 | texID = texnr; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /code/Tutorial_08/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Tutorial_08/textures/brickwall.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/code/Tutorial_08/textures/brickwall.jpg -------------------------------------------------------------------------------- /code/Tutorial_08/textures/plaster.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/code/Tutorial_08/textures/plaster.jpg -------------------------------------------------------------------------------- /code/Tutorial_08/textures/tiles.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/code/Tutorial_08/textures/tiles.jpg -------------------------------------------------------------------------------- /code/Tutorial_09/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_09/BoxObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef BOXOBJECT_H 13 | #define BOXOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "BoxMesh.h" 23 | 24 | /*! A container for all the boxes. 25 | Basically creates the geometry of the individual boxes and populates the buffers. 26 | */ 27 | class BoxObject { 28 | public: 29 | BoxObject(); 30 | 31 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 32 | void create(QOpenGLShaderProgram * shaderProgramm); 33 | void destroy(); 34 | 35 | void render(); 36 | 37 | std::vector m_boxes; 38 | 39 | std::vector m_vertexBufferData; 40 | std::vector m_elementBufferData; 41 | 42 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 43 | QOpenGLVertexArrayObject m_vao; 44 | 45 | /*! Holds position and colors in a single buffer. */ 46 | QOpenGLBuffer m_vbo; 47 | /*! Holds elements. */ 48 | QOpenGLBuffer m_ebo; 49 | }; 50 | 51 | #endif // BOXOBJECT_H 52 | -------------------------------------------------------------------------------- /code/Tutorial_09/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_09/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | 5 | OpenGLException::OpenGLException(const QString & msg) { 6 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 7 | } 8 | 9 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 10 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 11 | } 12 | 13 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 14 | m_msgStack(previous.m_msgStack) 15 | { 16 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 17 | } 18 | 19 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 20 | m_msgStack(previous.m_msgStack) 21 | { 22 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 23 | } 24 | 25 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 26 | for (std::list >::const_iterator it = m_msgStack.begin(); 27 | it != m_msgStack.end(); ++it) 28 | { 29 | QStringList lines = it->first.split("\n"); 30 | QString indx("[%1] "); 31 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 32 | for (const QString & l : lines) { 33 | if (it->second.isEmpty()) 34 | strm << (indx + l + "\n").toStdString(); 35 | else 36 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 37 | } 38 | strm.flush(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /code/Tutorial_09/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Tutorial_09/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_09/Texture2ScreenObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef Texture2ScreenObject_H 13 | #define Texture2ScreenObject_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | /*! Paints a texture to the screen - no transformation, just dumping the texture to the screen. 23 | Optionally, image kernels are used in fragment shader. 24 | */ 25 | class Texture2ScreenObject { 26 | public: 27 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | 31 | /*! Binds the buffer and paints. Expects the texture to be bound already. */ 32 | void render(); 33 | 34 | QOpenGLVertexArrayObject m_vao; 35 | QOpenGLBuffer m_vbo; 36 | }; 37 | 38 | #endif // Texture2ScreenObject_H 39 | -------------------------------------------------------------------------------- /code/Tutorial_09/Tutorial_09.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/screenfill.frag 8 | shaders/screenfill.vert 9 | shaders/screenfill_with_kernel.frag 10 | 11 | 12 | -------------------------------------------------------------------------------- /code/Tutorial_09/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_09/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_09/shaders/screenfill.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | out vec4 FragColor; 3 | 4 | in vec2 TexCoords; 5 | 6 | uniform sampler2D screenTexture; 7 | 8 | void main() 9 | { 10 | FragColor = texture(screenTexture, TexCoords); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /code/Tutorial_09/shaders/screenfill.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | layout (location = 0) in vec2 aPos; 3 | layout (location = 1) in vec2 aTexCoords; 4 | 5 | out vec2 TexCoords; 6 | 7 | void main() { 8 | TexCoords = aTexCoords; 9 | gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /code/Tutorial_09/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_09/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Tutorial_10/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_10/BoxObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef BOXOBJECT_H 13 | #define BOXOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "BoxMesh.h" 23 | 24 | /*! A container for all the boxes. 25 | Basically creates the geometry of the individual boxes and populates the buffers. 26 | */ 27 | class BoxObject { 28 | public: 29 | BoxObject(); 30 | 31 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 32 | void create(QOpenGLShaderProgram * shaderProgramm); 33 | void destroy(); 34 | 35 | void render(); 36 | 37 | std::vector m_boxes; 38 | 39 | std::vector m_vertexBufferData; 40 | std::vector m_elementBufferData; 41 | 42 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 43 | QOpenGLVertexArrayObject m_vao; 44 | 45 | /*! Holds position and colors in a single buffer. */ 46 | QOpenGLBuffer m_vbo; 47 | /*! Holds elements. */ 48 | QOpenGLBuffer m_ebo; 49 | }; 50 | 51 | #endif // BOXOBJECT_H 52 | -------------------------------------------------------------------------------- /code/Tutorial_10/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_10/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | 5 | OpenGLException::OpenGLException(const QString & msg) { 6 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 7 | } 8 | 9 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 10 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 11 | } 12 | 13 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 14 | m_msgStack(previous.m_msgStack) 15 | { 16 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 17 | } 18 | 19 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 20 | m_msgStack(previous.m_msgStack) 21 | { 22 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 23 | } 24 | 25 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 26 | for (std::list >::const_iterator it = m_msgStack.begin(); 27 | it != m_msgStack.end(); ++it) 28 | { 29 | QStringList lines = it->first.split("\n"); 30 | QString indx("[%1] "); 31 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 32 | for (const QString & l : lines) { 33 | if (it->second.isEmpty()) 34 | strm << (indx + l + "\n").toStdString(); 35 | else 36 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 37 | } 38 | strm.flush(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /code/Tutorial_10/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Tutorial_10/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_10/Texture2ScreenObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef Texture2ScreenObject_H 13 | #define Texture2ScreenObject_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | /*! Paints a texture to the screen - no transformation, just dumping the texture to the screen. 23 | Optionally, image kernels are used in fragment shader. 24 | */ 25 | class Texture2ScreenObject { 26 | public: 27 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | 31 | /*! Binds the buffer and paints. Expects the texture to be bound already. */ 32 | void render(); 33 | 34 | QOpenGLVertexArrayObject m_vao; 35 | QOpenGLBuffer m_vbo; 36 | }; 37 | 38 | #endif // Texture2ScreenObject_H 39 | -------------------------------------------------------------------------------- /code/Tutorial_10/Tutorial_10.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/screenfill.frag 8 | shaders/screenfill.vert 9 | shaders/depthMap.vert 10 | shaders/depthMap.frag 11 | shaders/sceneWithShadowMap.vert 12 | shaders/sceneWithShadowMap.frag 13 | 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/depthMap.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | void main() 4 | { 5 | // nothing needed for depth map 6 | // gl_FragDepth = gl_FragCoord.z; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/depthMap.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | 8 | uniform mat4 worldToView; // parameter: the camera matrix 9 | 10 | void main() { 11 | gl_Position = worldToView * vec4(position, 1.0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/sceneWithShadowMap.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | out vec4 FragColor; 3 | 4 | in VS_OUT { 5 | vec3 FragPos; 6 | vec4 FragPosLightSpace; 7 | vec3 FragColor; 8 | } fs_in; 9 | 10 | uniform sampler2D shadowMap; 11 | 12 | uniform vec3 lightPos; 13 | uniform vec3 viewPos; 14 | 15 | float ShadowCalculation(vec4 fragPosLightSpace) 16 | { 17 | // perform perspective divide 18 | vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; 19 | // transform to [0,1] range 20 | projCoords = projCoords * 0.5 + 0.5; 21 | // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) 22 | float closestDepth = texture(shadowMap, projCoords.xy).r; 23 | // get depth of current fragment from light's perspective 24 | float currentDepth = projCoords.z; 25 | // check whether current frag pos is in shadow 26 | // float shadow = currentDepth > closestDepth ? 1.0 : 0.0; 27 | float bias = 0.0007; 28 | float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0; 29 | 30 | return shadow; 31 | } 32 | 33 | void main() 34 | { 35 | vec3 color = fs_in.FragColor; 36 | // ambient 37 | vec3 ambient = 0.35 * color; 38 | // diffuse 39 | // calculate shadow 40 | float shadow = ShadowCalculation(fs_in.FragPosLightSpace); 41 | vec3 lighting = (ambient + (1.0 - shadow) ) * color; 42 | FragColor = vec4(lighting, 1.0); 43 | } 44 | 45 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/sceneWithShadowMap.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 3 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 4 | 5 | out VS_OUT { 6 | vec3 FragPos; 7 | vec4 FragPosLightSpace; 8 | vec3 FragColor; 9 | } vs_out; 10 | 11 | uniform mat4 worldToView; // parameter: the camera matrix 12 | uniform mat4 lightSpaceMatrix; // parameter: the light space matrix 13 | 14 | void main() 15 | { 16 | vs_out.FragPos = position; 17 | vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); 18 | vs_out.FragColor = color; 19 | gl_Position = worldToView * vec4(vs_out.FragPos, 1.0); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/screenfill.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | out vec4 FragColor; 3 | 4 | in vec2 TexCoords; 5 | 6 | uniform sampler2D depthMap; 7 | 8 | void main() 9 | { 10 | float depthValue = texture(depthMap, TexCoords).r; 11 | FragColor = vec4(vec3(depthValue), 1.0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/screenfill.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | layout (location = 0) in vec2 aPos; 3 | layout (location = 1) in vec2 aTexCoords; 4 | 5 | out vec2 TexCoords; 6 | 7 | void main() { 8 | TexCoords = aTexCoords; 9 | gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_10/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /code/Tutorial_11/.gitignore: -------------------------------------------------------------------------------- 1 | *.pro.user 2 | /bb/ 3 | -------------------------------------------------------------------------------- /code/Tutorial_11/BoxObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef BOXOBJECT_H 13 | #define BOXOBJECT_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | #include "BoxMesh.h" 23 | 24 | /*! A container for all the boxes. 25 | Basically creates the geometry of the individual boxes and populates the buffers. 26 | */ 27 | class BoxObject { 28 | public: 29 | BoxObject(); 30 | 31 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 32 | void create(QOpenGLShaderProgram * shaderProgramm); 33 | void destroy(); 34 | 35 | void render(); 36 | 37 | std::vector m_boxes; 38 | 39 | std::vector m_vertexBufferData; 40 | std::vector m_elementBufferData; 41 | 42 | /*! Wraps an OpenGL VertexArrayObject, that references the vertex coordinates and color buffers. */ 43 | QOpenGLVertexArrayObject m_vao; 44 | 45 | /*! Holds position and colors in a single buffer. */ 46 | QOpenGLBuffer m_vbo; 47 | /*! Holds elements. */ 48 | QOpenGLBuffer m_ebo; 49 | }; 50 | 51 | #endif // BOXOBJECT_H 52 | -------------------------------------------------------------------------------- /code/Tutorial_11/DebugApplication.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUGAPPLICATION_H 2 | #define DEBUGAPPLICATION_H 3 | 4 | #include 5 | #include 6 | 7 | #include "OpenGLException.h" 8 | 9 | class DebugApplication : public QApplication { 10 | public: 11 | DebugApplication( int & argc, char ** argv) : QApplication(argc, argv), m_aboutToTerminate(false) 12 | {} 13 | 14 | virtual bool notify(QObject *recv, QEvent *e) override { 15 | try { 16 | return QApplication::notify( recv, e ); 17 | } 18 | catch (OpenGLException &ex) { 19 | ex.writeMsgStackToStream(std::cerr); 20 | m_aboutToTerminate = true; 21 | QApplication::exit(1); 22 | } 23 | return false; 24 | } 25 | 26 | // Flag to check for program abort 27 | bool m_aboutToTerminate; 28 | }; 29 | 30 | #endif // DEBUGAPPLICATION_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_11/OpenGLException.cpp: -------------------------------------------------------------------------------- 1 | #include "OpenGLException.h" 2 | 3 | #include 4 | #include 5 | 6 | OpenGLException::OpenGLException(const QString & msg) { 7 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 8 | } 9 | 10 | OpenGLException::OpenGLException(const QString & msg, const QString & where) { 11 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 12 | } 13 | 14 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg) : 15 | m_msgStack(previous.m_msgStack) 16 | { 17 | m_msgStack.push_back( std::make_pair(QString(msg), QString())); 18 | } 19 | 20 | OpenGLException::OpenGLException(OpenGLException & previous, const QString & msg, const QString & where) : 21 | m_msgStack(previous.m_msgStack) 22 | { 23 | m_msgStack.push_back( std::make_pair(QString(msg), where)); 24 | } 25 | 26 | void OpenGLException::writeMsgStackToStream(std::ostream & strm) const { 27 | for (std::list >::const_iterator it = m_msgStack.begin(); 28 | it != m_msgStack.end(); ++it) 29 | { 30 | QStringList lines = it->first.split("\n"); 31 | QString indx("[%1] "); 32 | indx = indx.arg(std::distance(m_msgStack.begin(),it)); 33 | for (const QString & l : lines) { 34 | if (it->second.isEmpty()) 35 | strm << (indx + l + "\n").toStdString(); 36 | else 37 | strm << (indx + it->second + " : " + l + "\n").toStdString(); 38 | } 39 | strm.flush(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /code/Tutorial_11/OpenGLException.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENGLEXCEPTION_H 2 | #define OPENGLEXCEPTION_H 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | class OpenGLException : public std::exception { 10 | public: 11 | OpenGLException(const QString & msg); 12 | OpenGLException(const QString & msg, const QString & where); 13 | OpenGLException(OpenGLException & previous, const QString & msg); 14 | OpenGLException(OpenGLException & previous, const QString & msg, const QString & where); 15 | void writeMsgStackToStream(std::ostream & strm) const; 16 | 17 | private: 18 | std::list > m_msgStack; 19 | }; 20 | 21 | #define FUNCID(x) const char * const FUNC_ID = "[" #x "]" 22 | 23 | #endif // OPENGLEXCEPTION_H 24 | -------------------------------------------------------------------------------- /code/Tutorial_11/TestDialog.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef TESTDIALOG_H 13 | #define TESTDIALOG_H 14 | 15 | #include 16 | 17 | class SceneView; 18 | 19 | /*! A simple test dialog for an embedded OpenGL window. */ 20 | class TestDialog : public QDialog { 21 | Q_OBJECT 22 | public: 23 | TestDialog(); 24 | 25 | private: 26 | // The scene view, that shows our world and allows navigation 27 | SceneView * m_sceneView; 28 | }; 29 | 30 | #endif // TESTDIALOG_H 31 | -------------------------------------------------------------------------------- /code/Tutorial_11/Texture2ScreenObject.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************ 2 | 3 | OpenGL with Qt - Tutorial 4 | ------------------------- 5 | Autor : Andreas Nicolai 6 | Repository : https://github.com/ghorwin/OpenGLWithQt-Tutorial 7 | License : BSD License, 8 | see https://github.com/ghorwin/OpenGLWithQt-Tutorial/blob/master/LICENSE 9 | 10 | ************************************************************************************/ 11 | 12 | #ifndef Texture2ScreenObject_H 13 | #define Texture2ScreenObject_H 14 | 15 | #include 16 | #include 17 | 18 | QT_BEGIN_NAMESPACE 19 | class QOpenGLShaderProgram; 20 | QT_END_NAMESPACE 21 | 22 | /*! Paints a texture to the screen - no transformation, just dumping the texture to the screen. 23 | Optionally, image kernels are used in fragment shader. 24 | */ 25 | class Texture2ScreenObject { 26 | public: 27 | /*! The function is called during OpenGL initialization, where the OpenGL context is current. */ 28 | void create(QOpenGLShaderProgram * shaderProgramm); 29 | void destroy(); 30 | 31 | /*! Binds the buffer and paints. Expects the texture to be bound already. */ 32 | void render(); 33 | 34 | QOpenGLVertexArrayObject m_vao; 35 | QOpenGLBuffer m_vbo; 36 | }; 37 | 38 | #endif // Texture2ScreenObject_H 39 | -------------------------------------------------------------------------------- /code/Tutorial_11/Tutorial_11.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | shaders/grid.vert 4 | shaders/simple.frag 5 | shaders/withWorldAndCamera.vert 6 | shaders/grid.frag 7 | shaders/screenfill.frag 8 | shaders/screenfill.vert 9 | shaders/depthMap.vert 10 | shaders/depthMap.frag 11 | shaders/sceneWithShadowMap.vert 12 | shaders/sceneWithShadowMap.frag 13 | 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/depthMap.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | void main() 4 | { 5 | // nothing needed for depth map 6 | // gl_FragDepth = gl_FragCoord.z; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/depthMap.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | 8 | uniform mat4 worldToView; // parameter: the camera matrix 9 | 10 | void main() { 11 | gl_Position = worldToView * vec4(position, 1.0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/grid.frag: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | out vec4 finalColor; // output: final color value as rgba-value 4 | 5 | uniform vec3 gridColor; // parameter: grid color as rgb triple 6 | uniform vec3 backColor; // parameter: background color as rgb triple 7 | const float FARPLANE = 500; // threshold 8 | 9 | void main() { 10 | float distanceFromCamera = (gl_FragCoord.z / gl_FragCoord.w) / FARPLANE; 11 | distanceFromCamera = max(0, min(1, distanceFromCamera)); // clip to valid value range 12 | finalColor = vec4( mix(gridColor, backColor, distanceFromCamera), 1.0 ); 13 | } 14 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/grid.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec2 position; // input: attribute with index '0' 7 | // with 2 floats (x, z coords) per vertex 8 | 9 | uniform mat4 worldToView; // parameter: world to view transformation matrix 10 | 11 | void main() { 12 | gl_Position = worldToView * vec4(position.x, 0.0, position.y, 1.0); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/sceneWithShadowMap.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 3 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 4 | layout(location = 2) in vec3 normal; // input: attribute with index '2' with 3 elements per vertex 5 | 6 | out VS_OUT { 7 | vec3 FragPos; // position of fragment in world coordinates 8 | vec4 FragPosLightSpace; // position of fragment in light-space coordinates 9 | vec3 FragNormal; // normal vector of fragment 10 | vec3 FragColor; // color of fragment 11 | } vs_out; 12 | 13 | uniform mat4 worldToView; // parameter: the camera matrix 14 | uniform mat4 lightSpaceMatrix; // parameter: the light space matrix 15 | 16 | void main() 17 | { 18 | vs_out.FragPos = position; 19 | vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); 20 | vs_out.FragNormal = normal; 21 | vs_out.FragColor = color; 22 | gl_Position = worldToView * vec4(vs_out.FragPos, 1.0); 23 | } 24 | 25 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/screenfill.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | out vec4 FragColor; 3 | 4 | in vec2 TexCoords; 5 | 6 | uniform sampler2D depthMap; 7 | 8 | void main() 9 | { 10 | float depthValue = texture(depthMap, TexCoords).r; 11 | FragColor = vec4(vec3(depthValue), 1.0); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/screenfill.vert: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | layout (location = 0) in vec2 aPos; 3 | layout (location = 1) in vec2 aTexCoords; 4 | 5 | out vec2 TexCoords; 6 | 7 | void main() { 8 | TexCoords = aTexCoords; 9 | gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/simple.frag: -------------------------------------------------------------------------------- 1 | #version 330 core 2 | 3 | // fragment shader 4 | 5 | in vec4 fragColor; // input: interpolated color as rgba-value 6 | out vec4 finalColor; // output: final color value as rgba-value 7 | 8 | void main() { 9 | finalColor = fragColor; 10 | } 11 | -------------------------------------------------------------------------------- /code/Tutorial_11/shaders/withWorldAndCamera.vert: -------------------------------------------------------------------------------- 1 | #version 330 2 | 3 | // GLSL version 3.3 4 | // vertex shader 5 | 6 | layout(location = 0) in vec3 position; // input: attribute with index '0' with 3 elements per vertex 7 | layout(location = 1) in vec3 color; // input: attribute with index '1' with 3 elements (=rgb) per vertex 8 | out vec4 fragColor; // output: computed fragmentation color 9 | 10 | uniform mat4 worldToView; // parameter: the camera matrix 11 | 12 | void main() { 13 | // Mind multiplication order for matrixes 14 | gl_Position = worldToView * vec4(position, 1.0); 15 | fragColor = vec4(color, 1.0); 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /docs/OpenGLQtTutorial.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/OpenGLQtTutorial.pdf -------------------------------------------------------------------------------- /docs/images/Tutorial_01_mac.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_01_mac.png -------------------------------------------------------------------------------- /docs/images/Tutorial_02_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_02_linux.png -------------------------------------------------------------------------------- /docs/images/Tutorial_03_win10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_03_win10.png -------------------------------------------------------------------------------- /docs/images/Tutorial_04_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_04_linux.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_BoxVertexNumbering.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_BoxVertexNumbering.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_BoxVertexNumbering_print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_BoxVertexNumbering_print.jpg -------------------------------------------------------------------------------- /docs/images/Tutorial_05_PlaneVertexNumbering.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_PlaneVertexNumbering.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_PlaneVertexNumbering_print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_PlaneVertexNumbering_print.jpg -------------------------------------------------------------------------------- /docs/images/Tutorial_05_grid_fragshaderfade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_grid_fragshaderfade.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_grid_vertexshaderfade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_grid_vertexshaderfade.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_gridplain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_gridplain.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_linux.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_vertexbuffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_vertexbuffer.png -------------------------------------------------------------------------------- /docs/images/Tutorial_05_vertexbuffer_print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_05_vertexbuffer_print.jpg -------------------------------------------------------------------------------- /docs/images/Tutorial_07_RayTracking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_07_RayTracking.png -------------------------------------------------------------------------------- /docs/images/Tutorial_08_Textures.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_08_Textures.png -------------------------------------------------------------------------------- /docs/images/Tutorial_09_FramebufferImageKernel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_09_FramebufferImageKernel.png -------------------------------------------------------------------------------- /docs/images/Tutorial_11_QuartierMitSchattenUndBelichtung.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/Tutorial_11_QuartierMitSchattenUndBelichtung.png -------------------------------------------------------------------------------- /docs/images/stem-0b5528d63c2e2952792f6e00782503fb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-0b5528d63c2e2952792f6e00782503fb.png -------------------------------------------------------------------------------- /docs/images/stem-17841fc9750fa9c1e660c1d1a17e8f73.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-17841fc9750fa9c1e660c1d1a17e8f73.png -------------------------------------------------------------------------------- /docs/images/stem-291a8857bef2c45250b10de3b8c6e935.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-291a8857bef2c45250b10de3b8c6e935.png -------------------------------------------------------------------------------- /docs/images/stem-2abbd6a9f6c07d8f9219e623d7b3c2fe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-2abbd6a9f6c07d8f9219e623d7b3c2fe.png -------------------------------------------------------------------------------- /docs/images/stem-3e50c583aae250db742c6e234a07c6a3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-3e50c583aae250db742c6e234a07c6a3.png -------------------------------------------------------------------------------- /docs/images/stem-596af39d496be51612f3cf769a782bc1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-596af39d496be51612f3cf769a782bc1.png -------------------------------------------------------------------------------- /docs/images/stem-6f808b9a2616f307356d425ea39fb35c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-6f808b9a2616f307356d425ea39fb35c.png -------------------------------------------------------------------------------- /docs/images/stem-9828cfb947c30b5db90541b612b3da45.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-9828cfb947c30b5db90541b612b3da45.png -------------------------------------------------------------------------------- /docs/images/stem-a2c89dea229e10d4f067329c51463ee8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-a2c89dea229e10d4f067329c51463ee8.png -------------------------------------------------------------------------------- /docs/images/stem-ad8bc4a48ba75573404d7250014d49c0.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-ad8bc4a48ba75573404d7250014d49c0.png -------------------------------------------------------------------------------- /docs/images/stem-e5b93a5483b23cf5d2a5e032e8df8026.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/docs/images/stem-e5b93a5483b23cf5d2a5e032e8df8026.png -------------------------------------------------------------------------------- /learnopengl_book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/learnopengl_book.pdf -------------------------------------------------------------------------------- /text/.gitignore: -------------------------------------------------------------------------------- 1 | /OpenGLQtTutorial.html 2 | /OpenGLQtTutorial.pdf 3 | -------------------------------------------------------------------------------- /text/OpenGLQtTutorial.adoc: -------------------------------------------------------------------------------- 1 | OpenGL + Qt Tutorial 2 | ==================== 3 | Andreas Nicolai 4 | v1.0.0, April 2020 5 | :Author Initials: AN 6 | :toc: left 7 | :toclevels: 3 8 | :toc-title: Inhaltsverzeichnis 9 | :icons: font 10 | :imagesdir: ./images 11 | :numbered: 12 | :website: https://ghorwin.github.io/OpenGLWithQt-Tutorial 13 | :source-highlighter: rouge 14 | :rouge-style: custom 15 | :title-page: 16 | :stylesdir: ./css 17 | :stylesheet: roboto_ubuntu.css 18 | 19 | :numbered!: 20 | include::Introduction.adoc[leveloffset=+1] 21 | :numbered: 22 | include::Tutorial_01.adoc[leveloffset=+1] 23 | include::Tutorial_02.adoc[leveloffset=+1] 24 | include::Tutorial_03.adoc[leveloffset=+1] 25 | include::Tutorial_04.adoc[leveloffset=+1] 26 | include::Tutorial_05.adoc[leveloffset=+1] 27 | include::Tutorial_06.adoc[leveloffset=+1] 28 | include::Tutorial_07.adoc[leveloffset=+1] 29 | include::Tutorial_08.adoc[leveloffset=+1] 30 | include::Tutorial_09.adoc[leveloffset=+1] 31 | 32 | include::Tutorial_0x.adoc[leveloffset=+1] 33 | -------------------------------------------------------------------------------- /text/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo '*** Generating html ***' 4 | asciidoctor OpenGLQtTutorial.adoc 5 | 6 | echo '*** Generating pdf ***' 7 | asciidoctor-pdf -a pdf-theme=pdf-theme.yml -r ./rouge_theme.rb -r asciidoctor-mathematical -a pdf-fontsdir="./fonts;GEM_FONTS_DIR" OpenGLQtTutorial.adoc 8 | 9 | echo '*** Copying files to ../docs directory' 10 | 11 | mv OpenGLQtTutorial.html ../docs/index.html 12 | mv OpenGLQtTutorial.pdf ../docs 13 | 14 | cp images/* ../docs/images 15 | 16 | -------------------------------------------------------------------------------- /text/fonts/robotocondensed-bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/fonts/robotocondensed-bold.ttf -------------------------------------------------------------------------------- /text/fonts/robotocondensed-bold_italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/fonts/robotocondensed-bold_italic.ttf -------------------------------------------------------------------------------- /text/fonts/robotocondensed-italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/fonts/robotocondensed-italic.ttf -------------------------------------------------------------------------------- /text/fonts/robotocondensed-normal.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/fonts/robotocondensed-normal.ttf -------------------------------------------------------------------------------- /text/images/.gitignore: -------------------------------------------------------------------------------- 1 | stem-*.png 2 | -------------------------------------------------------------------------------- /text/images/Tutorial_01_mac.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_01_mac.png -------------------------------------------------------------------------------- /text/images/Tutorial_02_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_02_linux.png -------------------------------------------------------------------------------- /text/images/Tutorial_03_win10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_03_win10.png -------------------------------------------------------------------------------- /text/images/Tutorial_04_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_04_linux.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_BoxVertexNumbering.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_BoxVertexNumbering.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_BoxVertexNumbering_print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_BoxVertexNumbering_print.jpg -------------------------------------------------------------------------------- /text/images/Tutorial_05_PlaneVertexNumbering.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_PlaneVertexNumbering.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_PlaneVertexNumbering_print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_PlaneVertexNumbering_print.jpg -------------------------------------------------------------------------------- /text/images/Tutorial_05_grid_fragshaderfade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_grid_fragshaderfade.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_grid_vertexshaderfade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_grid_vertexshaderfade.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_gridplain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_gridplain.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_linux.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_linux.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_vertexbuffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_vertexbuffer.png -------------------------------------------------------------------------------- /text/images/Tutorial_05_vertexbuffer_print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_05_vertexbuffer_print.jpg -------------------------------------------------------------------------------- /text/images/Tutorial_07_RayTracking.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_07_RayTracking.png -------------------------------------------------------------------------------- /text/images/Tutorial_08_Textures.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_08_Textures.png -------------------------------------------------------------------------------- /text/images/Tutorial_09_FramebufferImageKernel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_09_FramebufferImageKernel.png -------------------------------------------------------------------------------- /text/images/Tutorial_11_QuartierMitSchattenUndBelichtung.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghorwin/OpenGLWithQt-Tutorial/dce4cc18a509ab84470bab8ba7ba61ab863d96f5/text/images/Tutorial_11_QuartierMitSchattenUndBelichtung.png -------------------------------------------------------------------------------- /text/pdf-theme.yml: -------------------------------------------------------------------------------- 1 | extends: default 2 | 3 | #font: 4 | # catalog: 5 | # RobotoCondensed: 6 | # normal: robotocondensed-normal.ttf 7 | # italic: robotocondensed-italic.ttf 8 | # bold: robotocondensed-bold.ttf 9 | # bold_italic: robotocondensed-bold_italic.ttf 10 | 11 | base: 12 | font-size: 9 13 | # font-family: RobotoCondensed 14 | 15 | code: 16 | font-size: 8 17 | 18 | toc: 19 | font-size: 10 20 | 21 | toc-h2: 22 | font-style: bold 23 | 24 | heading: 25 | h1-font-size: $base-font-size * 2.2 26 | h2-font-size: $base-font-size * 1.6 27 | h3-font-size: $base-font-size * 1.4 28 | h4-font-size: $base-font-size * 1.1 29 | 30 | title-page: 31 | authors: 32 | content: 33 | name_only: "{author}" 34 | with_email: "{author}\n{email}" 35 | 36 | page: 37 | margin: [0.5in, 0.67in, 1.4in, 0.67in] 38 | 39 | image: 40 | align: center 41 | 42 | footer: 43 | height: 0.75in 44 | line-height: 1 45 | recto: 46 | left: 47 | content: '_OpenGL+Qt Tutorial {revnumber}, {revdate}_' 48 | right: 49 | content: '{section-or-chapter-title} | *{page-number}*' 50 | verso: 51 | left: 52 | content: '*{page-number}* | {section-or-chapter-title}' 53 | 54 | thematic-break: 55 | margin-bottom: 50 56 | 57 | -------------------------------------------------------------------------------- /text/rouge_theme.rb: -------------------------------------------------------------------------------- 1 | require 'rouge' unless defined? ::Rouge.version 2 | 3 | module Rouge; module Themes 4 | class Custom < CSSTheme 5 | name 'custom' 6 | 7 | style Comment, fg: '#3a8800', italic: true 8 | style Error, fg: '#a61717', bg: '#e3d2d2' 9 | style Str, fg: '#000080' 10 | style Str::Char, fg: '#800080' 11 | style Num, fg: '#0000ff' 12 | style Keyword, fg: '#bec000', bold: true 13 | style Operator::Word, bold: true 14 | style Name::Tag, fg: '#046bb4', bold: true 15 | style Name::Attribute, fg: '#800000' 16 | style Generic::Deleted, fg: '#000000', bg: '#ffdddd', inline_block: true, extend: true 17 | style Generic::Inserted, fg: '#000000', bg: '#ddffdd', inline_block: true, extend: true 18 | style Text, {} 19 | end 20 | end; end --------------------------------------------------------------------------------