├── .gitignore ├── CMakeLists.txt ├── Copyright.txt ├── LICENSE ├── Makefile ├── README.md ├── main.cpp ├── obj ├── teapot-low.obj ├── teapot-lowR90.obj ├── teapot.obj └── teapotR90.obj ├── off ├── cube.off ├── monkey.off └── square.off ├── opengl ├── camera.cpp ├── camera.h ├── framebuffer.cpp ├── framebuffer.h ├── light.cpp ├── light.h ├── material.cpp ├── material.h ├── mesh.cpp ├── mesh.h ├── object.cpp ├── object.h ├── openglheaders.h ├── scene.cpp ├── scene.h ├── texture.cpp └── texture.h ├── qt ├── EditWindow.cpp ├── EditWindow.h ├── Editor.cpp ├── Editor.h ├── GLSLCodeEditor.cpp ├── GLSLCodeEditor.h ├── GLSLEditorWidget.cpp ├── GLSLEditorWidget.h ├── GLSLEditorWidget.ui ├── GLSLEditorWindow.cpp ├── GLSLEditorWindow.h ├── GLSLEditorWindow.ui ├── MatricesWidget.cpp ├── MatricesWidget.h ├── MatricesWidget.ui ├── Matrix4x4Widget.cpp ├── Matrix4x4Widget.h ├── Matrix4x4Widget.ui ├── SyntaxHighlighterGLSL.cpp ├── Vector4Widget.cpp ├── Vector4Widget.h ├── Vector4Widget.ui ├── codeeditor.cpp ├── codeeditor.h ├── gldisplay.cpp ├── gldisplay.h ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui ├── materialEditorWidget.cpp ├── materialEditorWidget.h ├── materialEditorWidget.ui ├── rcs │ ├── mono-key-enter.png │ └── mono-key-enter.xcf ├── ressources.qrc ├── texturewidget.cpp ├── texturewidget.h ├── uniformEditorWidget.cpp ├── uniformEditorWidget.h └── uniformEditorWidget.ui └── textures ├── normalMap.jpg └── texture.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | ShaderLabFramework 2 | build 3 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(ShaderLabFramework) 2 | 3 | cmake_minimum_required(VERSION 2.8) 4 | cmake_policy(SET CMP0020 NEW) 5 | if( POLICY CMP0043) 6 | cmake_policy(SET CMP0043 NEW) 7 | endif() 8 | 9 | if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR}) 10 | message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the source code and call cmake from there. Thank you.") 11 | endif() 12 | 13 | set(CMAKE_INCLUDE_CURRENT_DIR ON) 14 | set(CMAKE_AUTOMOC ON) 15 | set(CMAKE_AUTORCC ON) 16 | set(CMAKE_AUTOUIC ON) 17 | 18 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 19 | 20 | find_package(Qt5Core REQUIRED) 21 | find_package(Qt5Gui REQUIRED) 22 | find_package(Qt5Xml REQUIRED) 23 | find_package(Qt5OpenGL REQUIRED) 24 | find_package(OpenGL REQUIRED) 25 | 26 | 27 | set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_RELEASE} -fprofile-arcs -ftest-coverage") 28 | # set up a mapping so that the Release configuration for the Qt imported target is 29 | # used in the COVERAGE CMake configuration. 30 | set_target_properties(Qt5::Core PROPERTIES MAP_IMPORTED_CONFIG_COVERAGE "RELEASE") 31 | 32 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 33 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 34 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/ShaderLabFramework) 35 | 36 | include_directories(${CMAKE_CURRENT_SOURCE_DIR} 37 | ${CMAKE_CURRENT_SOURCE_DIR}/qt 38 | ${CMAKE_CURRENT_SOURCE_DIR}/opengl 39 | ${CMAKE_CURRENT_BINARY_DIR} 40 | ${CMAKE_BINARY_DIR} ) 41 | 42 | set(SRCS main.cpp 43 | opengl/camera.cpp 44 | opengl/framebuffer.cpp 45 | opengl/light.cpp 46 | opengl/material.cpp 47 | opengl/mesh.cpp 48 | opengl/object.cpp 49 | opengl/scene.cpp 50 | opengl/texture.cpp 51 | qt/gldisplay.cpp 52 | qt/mainwindow.cpp 53 | qt/MatricesWidget.cpp 54 | qt/Matrix4x4Widget.cpp 55 | qt/GLSLCodeEditor.cpp 56 | qt/SyntaxHighlighterGLSL.cpp 57 | qt/codeeditor.cpp 58 | qt/GLSLEditorWindow.cpp 59 | qt/GLSLEditorWidget.cpp 60 | qt/uniformEditorWidget.cpp 61 | qt/Vector4Widget.cpp 62 | qt/texturewidget.cpp 63 | qt/materialEditorWidget.cpp 64 | ) 65 | 66 | set(HDRS opengl/camera.h 67 | opengl/framebuffer.h 68 | opengl/light.h 69 | opengl/material.h 70 | opengl/mesh.h 71 | opengl/object.h 72 | opengl/openglheaders.h 73 | opengl/scene.h 74 | opengl/texture.h 75 | qt/gldisplay.h 76 | qt/mainwindow.h 77 | qt/MatricesWidget.h 78 | qt/Matrix4x4Widget.h 79 | qt/GLSLCodeEditor.h 80 | qt/codeeditor.h 81 | #qt/EditWindow.h 82 | #qt/Editor.h 83 | qt/GLSLEditorWindow.h 84 | qt/GLSLEditorWidget.h 85 | qt/uniformEditorWidget.h 86 | qt/Vector4Widget.h 87 | qt/texturewidget.h 88 | qt/materialEditorWidget.h 89 | ) 90 | 91 | set(FORMS 92 | qt/mainwindow.ui 93 | qt/MatricesWidget.ui 94 | qt/Matrix4x4Widget.ui 95 | qt/GLSLEditorWindow.ui 96 | qt/GLSLEditorWidget.ui 97 | qt/uniformEditorWidget.ui 98 | qt/Vector4Widget.ui 99 | qt/materialEditorWidget.ui 100 | qt/ressources.qrc) 101 | 102 | set(RCS src/images/images.qrc ) 103 | 104 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) 105 | 106 | add_executable(ShaderLabFramework ${SRCS} ${HDRS} ${FORMS}) 107 | qt5_use_modules(ShaderLabFramework Core Gui OpenGL Xml) 108 | target_link_libraries(ShaderLabFramework ${QT_LIBRARIES} ${OPENGL_LIBRARIES}) 109 | 110 | #### package section TODO 111 | #requires to install https://download.qt.io/official_releases/qt-installer-framework/3.0.6/ (or other version) 112 | #set environment variable QTIFWDIR 113 | 114 | set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE) 115 | include(InstallRequiredSystemLibraries) 116 | 117 | install( 118 | TARGETS ShaderLabFramework 119 | RUNTIME DESTINATION ShaderLabFramework 120 | COMPONENT ShaderLabFramework_installer 121 | BUNDLE DESTINATION ShaderLabFramework 122 | COMPONENT ShaderLabFramework_installer 123 | ) 124 | 125 | install(DIRECTORY "${PROJECT_SOURCE_DIR}/obj/" DESTINATION ShaderLabFramework/obj) 126 | install(DIRECTORY "${PROJECT_SOURCE_DIR}/off/" DESTINATION ShaderLabFramework/off) 127 | 128 | function(CPACKIFW_COMMON) 129 | SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Imperial College London ShaderLabFramework") 130 | SET(CPACK_PACKAGE_VENDOR "Department of Computing") 131 | SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md") 132 | SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/Copyright.txt") 133 | SET(CPACK_PACKAGE_VERSION_MAJOR "1") 134 | SET(CPACK_PACKAGE_VERSION_MINOR "0") 135 | SET(CPACK_PACKAGE_VERSION_PATCH "0") 136 | SET(CPACK_PACKAGE_EXECUTABLES "ShaderLabFramework" "Imperial College London ShaderLabFramework") 137 | 138 | set(CPACK_PACKAGE_NAME ShaderLabFramework) 139 | set(CPACK_PACKAGE_FILE_NAME ShaderLabFramework_installer) 140 | set(CPACK_PACKAGE_VERSION "1.0.0") # Version of installer 141 | set(CPACK_COMPONENTS_ALL ShaderLabFramework_installer) 142 | set(CPACK_IFW_PACKAGE_START_MENU_DIRECTORY ShaderLabFramework) 143 | set(CPACK_GENERATOR IFW) 144 | set(CPACK_IFW_VERBOSE ON) 145 | 146 | include(CPack REQUIRED) 147 | include(CPackIFW REQUIRED) 148 | 149 | cpack_add_component( 150 | ShaderLabFramework_installer 151 | DISPLAY_NAME "ShaderLabFramework" 152 | DESCRIPTION "Install me" 153 | REQUIRED 154 | ) 155 | 156 | cpack_ifw_configure_component( 157 | ShaderLabFramework_installer 158 | FORCED_INSTALLATION 159 | NAME qt.ShaderLabFramework.installer 160 | VERSION ${PROJECT_VERSION} # Version of component 161 | LICENSES License ${PROJECT_SOURCE_DIR}/LICENSE 162 | DEFAULT TRUE 163 | ) 164 | endfunction() 165 | 166 | if(CPACK_IFW_ROOT OR DEFINED ENV{QTIFWDIR}) 167 | if(DEFINED ENV{QTDIR}) 168 | if(APPLE) 169 | if(EXISTS $ENV{QTDIR}/bin/macdeployqt) 170 | message("*") 171 | message("* Note") 172 | message("*") 173 | message("* Because of a bug in CPackIFW, it doesn't manage correctly the package created on OSX.") 174 | message("* Unfortunately CPackIFW doesn't forward the .dmg extension to the binarycreator (see IFW documentation for more details).") 175 | message("* Therefore it creates a .app directory on OSX, that isn't properly a file and cannot be treated the same way .run and .exe files are.") 176 | message("* As a result, make package generates an empty directory within the build directory and leaves the bundle somewhere within the _CPack_Packages directory.") 177 | message("*") 178 | message("* I strongly suggest to run binarycreator through an external script and use the .dmg extension directly. At least until the bug in CPackIFW isn't fixed.") 179 | message("* From within the build directory run:") 180 | message("*") 181 | message("* binarycreator -f -c _CPack_Packages/Darwin/IFW/installer/config/config.xml -p _CPack_Packages/Darwin/IFW/installer/packages/ package/installer.dmg") 182 | message("*") 183 | message("* The bundle and the dmg file will be created within the package directory as:") 184 | message("*") 185 | message("* package/installer.app [bundle]") 186 | message("* package/installer.dmg [dmg]") 187 | message("*") 188 | 189 | add_custom_command( 190 | TARGET ShaderLabFramework POST_BUILD 191 | COMMAND $ENV{QTDIR}/bin/macdeployqt ${PROJECT_NAME}.app 192 | ) 193 | 194 | CPACKIFW_COMMON() 195 | else() 196 | message("Unable to find executable QTDIR/bin/macdeployqt.") 197 | endif() 198 | elseif(WIN32) 199 | if(EXISTS $ENV{QTDIR}/bin/windeployqt.exe) 200 | message("starting with deployment") 201 | message($ENV{QTDIR}/bin/windeployqt.exe) 202 | add_custom_command( 203 | TARGET ShaderLabFramework POST_BUILD 204 | COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/windeployqt_stuff 205 | COMMAND $ENV{QTDIR}/bin/windeployqt.exe --compiler-runtime --dir ${CMAKE_BINARY_DIR}/windeployqt_stuff $ 206 | ) 207 | 208 | install( 209 | DIRECTORY ${CMAKE_BINARY_DIR}/windeployqt_stuff/ 210 | DESTINATION ${PROJECT_NAME} 211 | COMPONENT ShaderLabFramework_installer 212 | ) 213 | install(DIRECTORY "${PROJECT_SOURCE_DIR}/obj/" DESTINATION ${PROJECT_NAME}/obj COMPONENT ShaderLabFramework_installer) 214 | install(DIRECTORY "${PROJECT_SOURCE_DIR}/off/" DESTINATION ${PROJECT_NAME}/off COMPONENT ShaderLabFramework_installer) 215 | 216 | CPACKIFW_COMMON() 217 | else() 218 | message("Unable to find executable QTDIR/bin/windeployqt.") 219 | endif() 220 | elseif(UNIX) 221 | CPACKIFW_COMMON() 222 | endif() 223 | else() 224 | message("Set properly environment variable QTDIR to be able to create a package.") 225 | endif() 226 | else() 227 | message("If you want to enable target package you can:") 228 | message("\t* Either pass -DCPACK_IFW_ROOT= to cmake") 229 | message("\t* Or set the environment variable QTIFWDIR") 230 | message("To specify the location of the QtIFW tool suite.") 231 | message("The specified path should not contain bin at the end (for example: D:\\DevTools\\QtIFW2.0.5).") 232 | endif() -------------------------------------------------------------------------------- /Copyright.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | This tool was inspired by 'Shader Maker', a Computer Graphics teaching tool developed by the team of Prof.Dr.Gabriel Zachmann, University of Bremen. The Copyright holders of 'Shader Maker' granted permission to use their code. The GLSL shader editor syntax highlighter classes are based on the implementation provided by 'Shader Maker'. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | This tool was inspired by 'Shader Maker', a Computer Graphics teaching tool developed by the team of Prof.Dr.Gabriel Zachmann, University of Bremen. The Copyright holders of 'Shader Maker' granted permission to use their code. The GLSL shader editor syntax highlighter classes are based on the implementation provided by 'Shader Maker'. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: shaderlabframework 2 | 3 | run: shaderlabframework 4 | ./ShaderLabFramework 5 | 6 | shaderlabframework: clean 7 | mkdir -p build 8 | cd build && cmake .. 9 | cd build && make -j 8 10 | mv build/ShaderLabFramework ./ 11 | 12 | clean: 13 | $(RM) -r build ShaderLabFramework 14 | 15 | .PHONY: all clean 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This is the Computer Graphics Shader Lab Framework. 2 | 3 | The Shader Lab Framework is a teaching tool to solidify the fundamentals of Computer Graphics. The ShaderLab framework is based on Qt5, CMake, OpenGL 4.0, and GLSL and allows the student to modify GLSL shaders in an IDE-like environment. The framework is able to render shaded polyhedral geometry (.off/.obj), supports image-based post-processing, and allows to implement simple ray-tracing algorithms. This tool has been intensively tested by 140 [Imperial College London CO317 Computer Graphics](http://wp.doc.ic.ac.uk/bkainz/teaching/co317-computer-graphics/) students in Spring 2017 and will continue to serve as basis framework for our coursework. 4 | 5 | A comprehensive overview over out Computer Graphics course using ShaderLabFramework has been accepted for the EG 2017 educational track: 6 | [Antoine Toisoul, Daniel Rueckert, Bernhard Kainz, Accessible GLSL Shader Programming](https://diglib.eg.org/handle/10.2312/eged20171024), in Proc. EuroGraphics 2017, 2017 7 | 8 | 9 | If you use the ShaderLab Framework for your research please cite : 10 | 11 |

12 |

@inproceedings{Toisoul2017ShaderLabFramework,
 13 |   title={Accessible GLSL shader programming},
 14 |   author={Toisoul, Antoine and Rueckert, Daniel and Kainz, Bernhard},
 15 |   booktitle={Proceedings of the European Association for Computer Graphics: Education papers},
 16 |   pages={35--42},
 17 |   year={2017},
 18 |   organization={Eurographics Association}
 19 | }
20 |

21 | 22 | YouTube Video: 23 | 24 |

25 | EG educational 2017 fast forward 26 |
27 |
28 |

29 | 30 | ## ShaderLabFramework overview 31 | 32 | ![HSaderLabFramework overview](http://wp.doc.ic.ac.uk/bkainz/wp-content/uploads/sites/97/2016/11/ShaderLab-768x800.png "ShaderLab framework") 33 | 34 | 35 | ## License 36 | 37 | Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 38 | (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 39 | 40 | Permission is hereby granted, free of charge, to any person obtaining a copy 41 | of this software and associated documentation files (the "Software"), to deal 42 | in the Software without restriction, including without limitation the rights 43 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 44 | copies of the Software, and to permit persons to whom the Software is 45 | furnished to do so, subject to the following conditions: 46 | 47 | The above copyright notice and this permission notice shall be included in 48 | all copies or substantial portions of the Software. 49 | 50 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 51 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 52 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 53 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 54 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 55 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 56 | IN THE SOFTWARE. 57 | 58 | This tool was inspired by ['Shader Maker'](http://cgvr.cs.uni-bremen.de/teaching/shader_maker/), a Computer Graphics teaching tool developed by the team of Prof.Dr.Gabriel Zachmann, University of Bremen. The Copyright holders of 'Shader Maker' granted permission to use their code. The GLSL shader editor syntax highlighter classes are based on the implementation provided by 'Shader Maker'. 59 | 60 | ## Dependencies: 61 | - [Qt5 v5.9](https://www.qt.io/download/) 62 | - [CMake](https://cmake.org/download/) 63 | 64 | ## Compile and run: 65 | 66 | git clone https://github.com/bkainz/ShaderLabFramework.git 67 | 68 | cd ShaderLabFramework 69 | mkdir build 70 | 71 | cd build 72 | 73 | cmake .. 74 | 75 | make 76 | 77 | ./ShaderLabFramework 78 | 79 | ## Features: 80 | - render window 81 | - mouse-based interaction 82 | - shader compilation log output 83 | - Shader editor with syntax highlighter 84 | - render-to-texture wrapped shader pipeline 85 | - Object selection 86 | - wireframe mode 87 | - display of origin 88 | - backface culling 89 | - screenshot tool 90 | - Camera parameter adjustments 91 | - direct manipulation widget for Model, View and Projection Matrices 92 | - automatic evaluation of user uniforms in uniform editor; 93 | default material and matrix uniforms are ignored 94 | - simple numeric material editor 95 | - GPU info output 96 | - saving and loading of whole shader pipelines or individual shaders 97 | - full OpenGL 4.x support 98 | 99 | ## TODO: 100 | - search function in code editor 101 | - conversion of the coordinate frame for OpenGL4 (will not work on Mac etc. at the moment) 102 | 103 | ## Examples implemented using the ShaderLabFramework 104 | 105 | ### Gouraud shading 106 | Gouraud shading 107 | 108 | ### Phong shading 109 | Phong shading 110 | 111 | ### Toon shading 112 | Toon shading 113 | 114 | ### Blinn-Phong shading 115 | Blinn-Phong shading 116 | 117 | ### Geometry subdivision 118 | Geometry subdivision level 0 119 | Geometry subdivision level 2 120 | 121 | ### Texture 122 | Texture 123 | Texture 124 | 125 | ### Bump mapping 126 | Bump mapping 127 | 128 | ### Render-to-texture, screen aligned image plane shading (simple blur example) 129 | image-based blur 130 | 131 | ### Simple ray tracing 132 | Simple ray tracing 133 | Simple monte-carlo ray tracing 134 | 135 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include 27 | 28 | #include "qt/mainwindow.h" 29 | 30 | int main(int argc, char *argv[]) 31 | { 32 | //By default sets OpenGL to OpenGL 4 with Core Profile 33 | QSurfaceFormat glFormat; 34 | glFormat.setMajorVersion(4); 35 | glFormat.setMinorVersion(1); 36 | 37 | #ifdef __APPLE__ 38 | glFormat.setProfile(QSurfaceFormat::CoreProfile); 39 | #else 40 | glFormat.setProfile(QSurfaceFormat::CompatibilityProfile); 41 | #endif 42 | 43 | //glFormat.setOption(QSurfaceFormat::DebugContext); 44 | 45 | QSurfaceFormat::setDefaultFormat(glFormat); 46 | 47 | QApplication a(argc, argv); 48 | a.setAttribute(Qt::AA_UseDesktopOpenGL); 49 | MainWindow w; 50 | w.show(); 51 | 52 | return a.exec(); 53 | } 54 | 55 | -------------------------------------------------------------------------------- /off/cube.off: -------------------------------------------------------------------------------- 1 | OFF 2 | 8 12 0 3 | -0.5 -0.5 0.5 4 | 0.5 -0.5 0.5 5 | -0.5 0.5 0.5 6 | 0.5 0.5 0.5 7 | -0.5 0.5 -0.5 8 | 0.5 0.5 -0.5 9 | -0.5 -0.5 -0.5 10 | 0.5 -0.5 -0.5 11 | 3 0 1 3 12 | 3 0 3 2 13 | 3 2 3 5 14 | 3 2 5 4 15 | 3 4 5 7 16 | 3 4 7 6 17 | 3 6 7 1 18 | 3 6 1 0 19 | 3 1 7 5 20 | 3 1 5 3 21 | 3 6 0 2 22 | 3 6 2 4 23 | -------------------------------------------------------------------------------- /off/square.off: -------------------------------------------------------------------------------- 1 | OFF 2 | 4 2 0 3 | 0.5 0.5 0 4 | -0.5 0.5 0 5 | -0.5 -0.5 0 6 | 0.5 -0.5 0 7 | 3 2 3 0 8 | 3 0 1 2 9 | -------------------------------------------------------------------------------- /opengl/camera.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/camera.h" 27 | 28 | using namespace std; 29 | 30 | Camera::Camera() : 31 | m_fieldOfView(0.0), 32 | m_perspectiveCamera(false), m_aspectRatio(1.0), m_viewMatrix(QMatrix4x4()), m_projectionMatrix(QMatrix4x4()) 33 | { 34 | m_viewMatrix.setToIdentity(); 35 | m_projectionMatrix.setToIdentity(); 36 | } 37 | 38 | Camera::Camera(QVector4D position, QVector4D upVector, QVector4D center, 39 | bool perspectiveCamera, float aspectRatio, float fieldOfView) : 40 | m_fieldOfView(fieldOfView), 41 | m_perspectiveCamera(perspectiveCamera), m_aspectRatio(aspectRatio), m_viewMatrix(QMatrix4x4()), m_projectionMatrix(QMatrix4x4()) 42 | { 43 | //Initialise matrices 44 | m_viewMatrix.setToIdentity(); 45 | m_projectionMatrix.setToIdentity(); 46 | 47 | m_viewMatrix.lookAt(position.toVector3D(), center.toVector3D(), upVector.toVector3D()); 48 | 49 | if (m_perspectiveCamera)//If it is a perspective camera 50 | m_projectionMatrix.perspective(fieldOfView, aspectRatio, (float)0.001, (float)10000.0); 51 | else 52 | m_projectionMatrix.ortho(-1.0, 1.0, -1.0, 1.0, 0.001, 10000.0); 53 | } 54 | 55 | void Camera::setViewMatrix(QVector4D position, QVector4D upVector, QVector4D center) 56 | { 57 | m_viewMatrix.setToIdentity(); 58 | m_viewMatrix.lookAt(position.toVector3D(), center.toVector3D(), upVector.toVector3D()); 59 | } 60 | 61 | void Camera::setViewMatrix(QMatrix4x4 &viewMatrix) 62 | { 63 | m_viewMatrix = viewMatrix; 64 | } 65 | 66 | 67 | void Camera::setProjectionMatrix(float aspectRatio, float fieldOfView) 68 | { 69 | m_fieldOfView = fieldOfView; 70 | m_aspectRatio = aspectRatio; 71 | 72 | //Perspective function multiplies by a matrix : need to reinitialize the matrix to identity first 73 | m_projectionMatrix.setToIdentity(); 74 | 75 | if (m_perspectiveCamera)//If it is a perspective camera 76 | m_projectionMatrix.perspective(fieldOfView, aspectRatio, (float)0.001, (float)10000.0); 77 | else 78 | m_projectionMatrix.ortho(-1.0, 1.0, -1.0, 1.0, 0.001, 10000.0); 79 | 80 | } 81 | 82 | void Camera::setProjectionMatrix(QMatrix4x4 &projectionMatrix) 83 | { 84 | m_projectionMatrix = projectionMatrix; 85 | } 86 | 87 | void Camera::changeCameraType(QString cameraType) 88 | { 89 | //Resets the projection matrix 90 | m_projectionMatrix.setToIdentity(); 91 | 92 | if (cameraType == "Perspective") 93 | { 94 | m_projectionMatrix.perspective(m_fieldOfView, m_aspectRatio, (float)0.001, (float)10000.0); 95 | m_perspectiveCamera = true; 96 | } 97 | else 98 | { 99 | m_projectionMatrix.ortho(-1.0, 1.0, -1.0, 1.0, 0.001, 10000.0); 100 | m_perspectiveCamera = false; 101 | } 102 | } 103 | 104 | void Camera::rotateX(float xRotation) 105 | { 106 | m_viewMatrix.rotate(xRotation, 1.0, 0.0, 0.0); 107 | } 108 | 109 | void Camera::rotateY(float yRotation) 110 | { 111 | m_viewMatrix.rotate(yRotation, 0.0, 1.0, 0.0); 112 | } 113 | 114 | void Camera::rotateXY(float xRotation, float yRotation) 115 | { 116 | this->rotateX(xRotation); 117 | this->rotateY(yRotation); 118 | } 119 | 120 | void Camera::translateAlongViewAxis(float translation) 121 | { 122 | m_viewMatrix(2, 3) += translation; 123 | } 124 | 125 | 126 | void Camera::translateX(float translation) 127 | { 128 | //Translate in camera space 129 | m_viewMatrix(0, 3) += translation; 130 | } 131 | 132 | void Camera::translateY(float translation) 133 | { 134 | //Translate in camera space 135 | m_viewMatrix(1, 3) += translation; 136 | } 137 | 138 | void Camera::translateZ(float translation) 139 | { 140 | m_viewMatrix.translate(0.0, 0.0, translation); 141 | } 142 | 143 | void Camera::resetCamera() 144 | { 145 | m_viewMatrix.setToIdentity(); 146 | } 147 | 148 | QMatrix4x4 Camera::getViewMatrix() 149 | { 150 | return m_viewMatrix; 151 | } 152 | 153 | QMatrix4x4 Camera::getProjectionMatrix() 154 | { 155 | return m_projectionMatrix; 156 | } 157 | 158 | bool Camera::isPerspective() 159 | { 160 | return m_perspectiveCamera; 161 | } 162 | -------------------------------------------------------------------------------- /opengl/camera.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef CAMERA_H 27 | #define CAMERA_H 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | class Camera 37 | { 38 | public: 39 | Camera(); 40 | Camera(QVector4D position, QVector4D upVector, QVector4D center, bool perspectiveCamera = true, float aspectRatio = 16.0 / 9.0, float fieldOfView = 30.0); 41 | 42 | /** 43 | * Sets the view matrix so that the camera is at position, looks at the center with a upVector. 44 | * @brief setViewMatrix 45 | * @param position 46 | * @param upVector 47 | * @param center 48 | */ 49 | void setViewMatrix(QVector4D position, QVector4D upVector, QVector4D center); 50 | 51 | 52 | /** 53 | * Sets the view matrix to a given value. 54 | * @brief setViewMatrix 55 | * @param viewMatrix 56 | */ 57 | void setViewMatrix(QMatrix4x4 &viewMatrix); 58 | 59 | /** 60 | * Sets the projection matrix given an aspect ratio and a field of view (if perspective camera, orthographic camera otherwise). 61 | * @brief setProjectionMatrix 62 | * @param aspectRatio 63 | * @param fieldOfView 64 | */ 65 | void setProjectionMatrix(float aspectRatio, float fieldOfView); 66 | 67 | /** 68 | * Sets the projection matrix to a given value. 69 | * @brief setProjectionMatrix 70 | * @param aspectRatio 71 | * @param fieldOfView 72 | */ 73 | void setProjectionMatrix(QMatrix4x4 &viewMatrix); 74 | 75 | /** 76 | * Changes the camera type between perspective and orthographic. 77 | * @brief changeCameraType 78 | * @param cameraType 79 | */ 80 | void changeCameraType(QString cameraType); 81 | 82 | /** 83 | * Rotates the camera along the X axis 84 | * @brief rotateX 85 | * @param xRotation 86 | */ 87 | void rotateX(float xRotation); 88 | 89 | /** 90 | * Rotates the camera along the Y axis 91 | * @brief rotateY 92 | * @param yRotation 93 | */ 94 | void rotateY(float yRotation); 95 | 96 | /** 97 | * Rotates the camera along the X and Y axis 98 | * @brief rotateXY 99 | * @param xRotation 100 | * @param yRotation 101 | */ 102 | void rotateXY(float xRotation, float yRotation); 103 | 104 | /** 105 | * Translation along the axis cameraPosition-ViewCenter 106 | * @brief translateAlongViewAxis 107 | * @param zTranslation 108 | */ 109 | void translateAlongViewAxis(float zTranslation); 110 | 111 | /** 112 | * Translate the camera along the X axis. 113 | * @brief translateX 114 | * @param translation 115 | */ 116 | void translateX(float translation); 117 | 118 | /** 119 | * Translate the camera along the Y axis. 120 | * @brief translateY 121 | * @param translation 122 | */ 123 | void translateY(float translation); 124 | 125 | /** 126 | * Translate the camera along the Z axis. 127 | * @brief translateZ 128 | * @param translation 129 | */ 130 | void translateZ(float translation); 131 | 132 | /** 133 | * Resets the camera position. 134 | * @brief resetCamera 135 | */ 136 | void resetCamera(); 137 | 138 | QMatrix4x4 getViewMatrix(); 139 | QMatrix4x4 getProjectionMatrix(); 140 | 141 | /** 142 | * Returns true if the camera is a perspective camera. 143 | * @brief isPerspective 144 | * @return 145 | */ 146 | bool isPerspective(); 147 | 148 | private: 149 | float m_fieldOfView; 150 | 151 | bool m_perspectiveCamera; 152 | float m_aspectRatio; 153 | 154 | QMatrix4x4 m_viewMatrix; 155 | QMatrix4x4 m_projectionMatrix; 156 | 157 | }; 158 | 159 | #endif // CAMERA_H 160 | -------------------------------------------------------------------------------- /opengl/framebuffer.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/framebuffer.h" 27 | 28 | using namespace std; 29 | 30 | FrameBuffer::FrameBuffer() : m_framebufferId(0), m_width(0), m_height(0), 31 | m_colourBuffers(vector()), m_depthBufferId(0) 32 | { 33 | f = QOpenGLContext::currentContext()->functions(); 34 | } 35 | 36 | FrameBuffer::FrameBuffer(int width, int height) : m_framebufferId(0), m_width(width), m_height(height), 37 | m_colourBuffers(vector()), m_depthBufferId(0) 38 | { 39 | f = QOpenGLContext::currentContext()->functions(); 40 | } 41 | 42 | FrameBuffer::~FrameBuffer() 43 | { 44 | f->glDeleteFramebuffers(1, &m_framebufferId); 45 | f->glDeleteRenderbuffers(1, &m_depthBufferId); 46 | m_colourBuffers.clear(); 47 | 48 | } 49 | 50 | 51 | void FrameBuffer::createRenderBuffer(GLuint &id, GLenum format) 52 | { 53 | //Delete any previous buffer 54 | if (f->glIsRenderbuffer(id) == GL_TRUE) 55 | { 56 | //1 is the number of id to delete 57 | f->glDeleteRenderbuffers(1, &id); 58 | } 59 | 60 | //Generate a new id for the renderbuffer of the Framebuffer 61 | f->glGenRenderbuffers(1, &id); 62 | 63 | //Start working with the renderbuffer : bing 64 | f->glBindRenderbuffer(GL_RENDERBUFFER, id); 65 | 66 | //Create renderbuffer with same width and height 67 | f->glRenderbufferStorage(GL_RENDERBUFFER, format, m_width, m_height); 68 | 69 | //Stop working with it 70 | f->glBindRenderbuffer(GL_RENDERBUFFER, 0); 71 | 72 | } 73 | 74 | bool FrameBuffer::load_8UC3() 75 | { 76 | 77 | if (f->glIsFramebuffer(m_framebufferId) == GL_TRUE) 78 | { 79 | f->glDeleteFramebuffers(1, &m_framebufferId); 80 | m_colourBuffers.clear(); //Empty colour buffers 81 | } 82 | 83 | //Generate ID 84 | f->glGenFramebuffers(1, &m_framebufferId); 85 | 86 | //Create renderbuffer with same width and height 87 | f->glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferId); 88 | 89 | //Colorbuffer 90 | Texture colorBuffer = Texture(m_width, m_height, 3); 91 | colorBuffer.loadEmptyTexture_8UC3(); 92 | 93 | m_colourBuffers.push_back(colorBuffer); 94 | 95 | //Renderbuffer 96 | this->createRenderBuffer(m_depthBufferId, GL_DEPTH24_STENCIL8); 97 | 98 | //Attach colour buffer and render buffer to framebuffer 99 | f->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colourBuffers[0].getTextureId(), 0); 100 | f->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferId); 101 | 102 | if (f->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 103 | { 104 | cout << "Error in framebuffer creation. Clear memory." << endl; 105 | 106 | //Clear the memory associated with the framebuffer, renderbuffer and color buffer 107 | f->glDeleteFramebuffers(1, &m_framebufferId); 108 | f->glDeleteRenderbuffers(1, &m_depthBufferId); 109 | m_colourBuffers.clear(); 110 | 111 | return false; 112 | } 113 | 114 | //Stop working with it 115 | f->glBindFramebuffer(GL_FRAMEBUFFER, 0); 116 | 117 | return true; 118 | 119 | } 120 | 121 | bool FrameBuffer::load_32FC3() 122 | { 123 | 124 | if (f->glIsFramebuffer(m_framebufferId) == GL_TRUE) 125 | { 126 | f->glDeleteFramebuffers(1, &m_framebufferId); 127 | m_colourBuffers.clear(); //Empty colour buffers 128 | } 129 | 130 | //Generate ID 131 | f->glGenFramebuffers(1, &m_framebufferId); 132 | 133 | //Create renderbuffer with same width and height 134 | f->glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferId); 135 | 136 | //Colorbuffer 137 | Texture colorBuffer = Texture(m_width, m_height, 3); 138 | 139 | 140 | colorBuffer.loadEmptyTexture_32FC3(); 141 | 142 | 143 | m_colourBuffers.push_back(colorBuffer); 144 | 145 | //Renderbuffer 146 | this->createRenderBuffer(m_depthBufferId, GL_DEPTH24_STENCIL8); 147 | 148 | //Attach colour buffer and render buffer to framebuffer 149 | f->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colourBuffers[0].getTextureId(), 0); 150 | 151 | f->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_depthBufferId); 152 | 153 | if (f->glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 154 | { 155 | cout << "Error in framebuffer creation. Clear memory." << endl; 156 | 157 | //Clear the memory associated with the framebuffer, renderbuffer and color buffer 158 | f->glDeleteFramebuffers(1, &m_framebufferId); 159 | f->glDeleteRenderbuffers(1, &m_depthBufferId); 160 | m_colourBuffers.clear(); 161 | 162 | return false; 163 | } 164 | 165 | //Stop working with it 166 | f->glBindFramebuffer(GL_FRAMEBUFFER, 0); 167 | 168 | return true; 169 | 170 | } 171 | 172 | GLuint FrameBuffer::getFramebufferID() const 173 | { 174 | return m_framebufferId; 175 | } 176 | 177 | GLuint FrameBuffer::getColorBufferID(unsigned int index) const 178 | { 179 | return m_colourBuffers[index].getTextureId(); 180 | } 181 | 182 | int FrameBuffer::getWidth() const 183 | { 184 | return m_width; 185 | } 186 | 187 | int FrameBuffer::getHeight() const 188 | { 189 | return m_height; 190 | } 191 | -------------------------------------------------------------------------------- /opengl/framebuffer.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef FRAMEBUFFER_H 27 | #define FRAMEBUFFER_H 28 | 29 | #include "opengl/openglheaders.h" 30 | #include "opengl/texture.h" 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | class FrameBuffer 39 | { 40 | public: 41 | FrameBuffer(); 42 | FrameBuffer(int width, int height); 43 | ~FrameBuffer(); 44 | 45 | void createRenderBuffer(GLuint &id, GLenum format); 46 | 47 | /** 48 | * Load a framebuffer with a color buffer of 8 bits. 49 | * @brief load_8UC3 50 | * @return 51 | */ 52 | bool load_8UC3(); 53 | 54 | /** 55 | * Load a framebuffer with a color buffer of 32 bits floats. 56 | * @brief load_32FC3 57 | * @return 58 | */ 59 | bool load_32FC3(); 60 | 61 | GLuint getFramebufferID() const; 62 | GLuint getColorBufferID(unsigned int index) const; 63 | 64 | int getWidth() const; 65 | int getHeight() const; 66 | 67 | signals: 68 | 69 | public slots : 70 | 71 | private: 72 | GLuint m_framebufferId; 73 | int m_width; 74 | int m_height; 75 | 76 | //A framebuffer contains a color buffer, a depth buffer and a stencil buffer 77 | std::vector m_colourBuffers; 78 | GLuint m_depthBufferId; 79 | 80 | QOpenGLFunctions *f; 81 | }; 82 | 83 | #endif // FRAMEBUFFER_H 84 | -------------------------------------------------------------------------------- /opengl/light.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/light.h" 27 | 28 | using namespace std; 29 | 30 | Light::Light() : 31 | m_lightPosition(QVector4D()), m_lightColor(QVector3D()), m_modelMatrix(QMatrix4x4()), m_lightIntensity() 32 | { 33 | 34 | } 35 | 36 | Light::Light(QVector4D lightPosition, QVector3D lightColor, float lightIntensity) : 37 | m_lightPosition(QVector4D(lightPosition)), m_lightColor(QVector3D(lightColor)), m_modelMatrix(QMatrix4x4()), 38 | m_lightIntensity(lightIntensity) 39 | { 40 | 41 | m_modelMatrix.setToIdentity(); 42 | 43 | } 44 | 45 | void Light::setPosition(float x, float y, float z) 46 | { 47 | m_lightPosition = QVector4D(x, y, z, 1.0); 48 | m_modelMatrix.setToIdentity(); 49 | m_modelMatrix.translate(x, y, z); 50 | } 51 | 52 | void Light::translateX(float translationX) 53 | { 54 | m_modelMatrix.translate(translationX, 0.0, 0.0); 55 | 56 | float currentPosition = m_lightPosition.x(); 57 | m_lightPosition.setX(currentPosition + translationX); 58 | } 59 | 60 | void Light::translateY(float translationY) 61 | { 62 | 63 | m_modelMatrix.translate(0.0, translationY, 0.0); 64 | float currentPosition = m_lightPosition.y(); 65 | m_lightPosition.setY(currentPosition + translationY); 66 | } 67 | 68 | void Light::translateZ(float translationZ) 69 | { 70 | m_modelMatrix.translate(0.0, 0.0, translationZ); 71 | float currentPosition = m_lightPosition.z(); 72 | m_lightPosition.setZ(currentPosition + translationZ); 73 | } 74 | 75 | QVector4D Light::getLightPosition() const 76 | { 77 | return m_lightPosition; 78 | } 79 | 80 | QMatrix4x4 Light::getModelMatrix() const 81 | { 82 | return m_modelMatrix; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /opengl/light.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef LIGHT_H 27 | #define LIGHT_H 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | class Light 36 | { 37 | public: 38 | Light(); 39 | Light(QVector4D lightPosition, QVector3D lightColor, float lightIntensity); 40 | void setPosition(float x, float y, float z); 41 | void translateX(float translationX); 42 | void translateY(float translationY); 43 | void translateZ(float translationZ); 44 | 45 | void computeSpectrumRGB(); 46 | 47 | QVector4D getLightPosition() const; 48 | QMatrix4x4 getModelMatrix() const; 49 | 50 | 51 | private: 52 | QVector4D m_lightPosition; 53 | QVector3D m_lightColor; 54 | QMatrix4x4 m_modelMatrix; 55 | float m_lightIntensity; 56 | }; 57 | 58 | #endif // LIGHT_H 59 | -------------------------------------------------------------------------------- /opengl/material.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/material.h" 27 | 28 | using namespace std; 29 | 30 | Material::Material() : m_ambientColor(QColor()), m_diffuseColor(QColor()), m_specularColor(QColor()), m_ambientCoefficient(0), m_diffuseCoefficient(0), m_specularCoefficient(0), m_shininess(0) 31 | { 32 | 33 | } 34 | 35 | Material::Material(QColor ambient, QColor diffuseColor, QColor specularColor, float ambientCoefficient, float diffuseCoefficient, float specularCoefficient, float shininess) : 36 | m_ambientColor(QColor(ambient)), m_diffuseColor(QColor(diffuseColor)), m_specularColor(QColor(specularColor)), 37 | m_ambientCoefficient(ambientCoefficient), m_diffuseCoefficient(diffuseCoefficient), m_specularCoefficient(specularCoefficient), m_shininess(shininess) 38 | { 39 | 40 | } 41 | 42 | QColor Material::getAmbientColor() 43 | { 44 | return m_ambientColor; 45 | } 46 | 47 | QColor Material::getDiffuseColor() 48 | { 49 | return m_diffuseColor; 50 | } 51 | 52 | QColor Material::getSpecularColor() 53 | { 54 | return m_specularColor; 55 | } 56 | 57 | float Material::getAmbientCoefficient() 58 | { 59 | return m_ambientCoefficient; 60 | } 61 | 62 | float Material::getDiffuseCoefficient() 63 | { 64 | return m_diffuseCoefficient; 65 | } 66 | 67 | float Material::getSpecularCoefficient() 68 | { 69 | return m_specularCoefficient; 70 | } 71 | 72 | float Material::getShininess() 73 | { 74 | return m_shininess; 75 | } 76 | 77 | 78 | void Material::setAmbientColor(QColor color) 79 | { 80 | m_ambientColor = color; 81 | } 82 | 83 | void Material::setDiffuseColor(QColor color) 84 | { 85 | m_diffuseColor = color; 86 | } 87 | 88 | void Material::setSpecularColor(QColor color) 89 | { 90 | m_specularColor = color; 91 | } 92 | 93 | void Material::setAmbientCoefficient(float val) 94 | { 95 | m_ambientCoefficient = val; 96 | } 97 | 98 | void Material::setDiffuseCoefficient(float val) 99 | { 100 | m_diffuseCoefficient = val; 101 | } 102 | 103 | void Material::setSpecularCoefficient(float val) 104 | { 105 | m_specularCoefficient = val; 106 | } 107 | 108 | void Material::setShininess(float val) 109 | { 110 | m_shininess = val; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /opengl/material.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef MATERIAL_H 27 | #define MATERIAL_H 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | class Material 34 | { 35 | public: 36 | Material(); 37 | Material(QColor ambientColor, QColor diffuseColor, QColor specularColor, float ambientCoefficient, float diffuseCoefficient, float specularCoefficient, float shininess); 38 | 39 | QColor getAmbientColor(); 40 | QColor getDiffuseColor(); 41 | QColor getSpecularColor(); 42 | float getAmbientCoefficient(); 43 | float getDiffuseCoefficient(); 44 | float getSpecularCoefficient(); 45 | float getShininess(); 46 | 47 | void setAmbientColor(QColor color); 48 | void setDiffuseColor(QColor color); 49 | void setSpecularColor(QColor color); 50 | void setAmbientCoefficient(float val); 51 | void setDiffuseCoefficient(float val); 52 | void setSpecularCoefficient(float val); 53 | void setShininess(float val); 54 | 55 | private: 56 | QColor m_ambientColor; 57 | QColor m_diffuseColor; 58 | QColor m_specularColor; 59 | float m_ambientCoefficient; 60 | float m_diffuseCoefficient; 61 | float m_specularCoefficient; 62 | float m_shininess; 63 | 64 | }; 65 | 66 | #endif // MATERIAL_H 67 | -------------------------------------------------------------------------------- /opengl/mesh.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef MESH_H 27 | #define MESH_H 28 | 29 | #include "opengl/openglheaders.h" 30 | 31 | #include 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | class Mesh 43 | { 44 | public: 45 | Mesh(); 46 | Mesh(const std::string &fileName); 47 | ~Mesh(); 48 | 49 | /** 50 | * Reads off file. 51 | * @brief offReader 52 | */ 53 | void offReader(); 54 | 55 | /** 56 | * Reads obj file. 57 | * @brief objReader 58 | * @param fileName 59 | */ 60 | void objReader(); 61 | 62 | 63 | /** 64 | * Sets the UV texture coordinates. 65 | * @brief setTextureCoordinates 66 | * @param textureCoordinates 67 | */ 68 | void setTextureCoordinates(); 69 | 70 | /** 71 | * Sets the UV texture coordinates of the mesh to the ones given as a parameter. 72 | * @brief setTextureCoordinates 73 | * @param textureCoordinates 74 | */ 75 | void setTextureCoordinates(QVector &textureCoordinates); 76 | 77 | /** 78 | * Centers the mesh so that its center of mass is at the origin of the world coordinate system. 79 | * @brief centerMesh 80 | */ 81 | void centerMesh(); 82 | 83 | /** 84 | * Reads an obj file, rotate the object and normal by 90 degrees around the x axis and saves it. 85 | * @brief obj_rotate90X 86 | */ 87 | void obj_rotate90X(); 88 | 89 | QVector getVertices() const; 90 | QVector getIndices() const; 91 | QVector getIndicesArray() const; 92 | QVector getVertexNormals() const; 93 | QVector getTextureCoordinates() const; 94 | 95 | private: 96 | /** 97 | * Parse a string in the form a/b/c/d/... where a,b,c,d are integers 98 | * Store the values of a,b,c,d in values[]. This is necessary for the obj reader. 99 | * @brief parseString 100 | * @param input 101 | * @param delimiter 102 | * @param values 103 | * @param numberOfValues 104 | */ 105 | void parseString(std::string input, char delimiter, int values[], int numberOfValues); 106 | 107 | std::string m_fileName; 108 | QVector m_vertices; 109 | 110 | /** 111 | * Contains the list of indices for each triangle. QVector3D contains the 3 indices for a given triangle 112 | * @brief m_indices 113 | */ 114 | QVector m_indices; 115 | 116 | /** 117 | * Contains the list of indices for all the triangles. Each consecutive triplet of integers correspond to the three indices of the vertices that make a triangle. 118 | * This is the QVector that openGL needs for rendering (in glDrawElements) 119 | * @brief m_indices 120 | */ 121 | QVector m_indicesArray; 122 | 123 | QVector m_triangleNormals; 124 | QVector m_vertexNormals; 125 | QVector m_textureCoordinates; 126 | }; 127 | 128 | #endif // MESH_H 129 | -------------------------------------------------------------------------------- /opengl/object.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/object.h" 27 | #include 28 | 29 | using namespace std; 30 | 31 | Object::Object() : m_objectName(), m_mesh(Mesh()), m_material(Material()), 32 | m_modelMatrix(QMatrix4x4()), m_rotationX(0), m_rotationY(0), m_rotationZ(0) 33 | { 34 | 35 | } 36 | 37 | Object::Object(string objectName) : m_objectName(objectName), m_mesh(Mesh()), m_material(Material()), 38 | m_modelMatrix(QMatrix4x4()), m_rotationX(0), m_rotationY(0), m_rotationZ(0) 39 | { 40 | string objectPath = loadPath(objectName); 41 | m_mesh = Mesh(objectPath); 42 | this->loadMesh(); 43 | 44 | m_modelMatrix = QMatrix4x4(); 45 | m_modelMatrix.setToIdentity(); 46 | 47 | m_material = Material(QColor(128, 0, 0), QColor(255, 0, 0), QColor(255, 255, 255), 1.0, 1.0, 1.0, 10.0); 48 | 49 | if (m_QtVBO.create()) qDebug() << "Success creating vertex position buffer"; 50 | m_QtVBO.setUsagePattern(QOpenGLBuffer::StaticDraw); 51 | 52 | if (m_QtVBO.bind()) qDebug() << "Success biding vertex position buffer"; 53 | 54 | //qDebug() << m_mesh.getVertices().size(); 55 | //qDebug() << m_mesh.getVertexNormals().size(); 56 | //qDebug() << m_mesh.getTextureCoordinates().size(); 57 | 58 | int numVertices = m_mesh.getVertices().size(); 59 | 60 | int sizeVertices = numVertices * sizeof(QVector3D);//m_mesh.getVertices().size() * sizeof(QVector3D); 61 | int sizeTextureCoords = m_mesh.getTextureCoordinates().size() * sizeof(QVector2D); 62 | int sizeNormals = numVertices * sizeof(QVector3D);//m_mesh.getVertexNormals().size() * sizeof(QVector3D); 63 | 64 | size_t VBOSize = sizeVertices + sizeTextureCoords + sizeNormals; 65 | 66 | m_vertexOffset = 0; 67 | m_texturesCoordsOffset = sizeVertices; 68 | m_normalsOffset = m_texturesCoordsOffset + sizeTextureCoords; 69 | 70 | m_QtVBO.allocate(VBOSize); 71 | 72 | // send the vertice data to the vbo using allocate 73 | m_QtVBO.write(m_vertexOffset, m_mesh.getVertices().constData(), sizeVertices); 74 | 75 | //Send the texture coordinates data 76 | m_QtVBO.write(m_texturesCoordsOffset, m_mesh.getTextureCoordinates().constData(), sizeTextureCoords); 77 | 78 | //Send the normals data 79 | m_QtVBO.write(m_normalsOffset, m_mesh.getVertexNormals().constData(), sizeNormals); 80 | 81 | m_QtIndexBuffer = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer); 82 | 83 | if (m_QtIndexBuffer.create()) 84 | qDebug() << "Success creating the index buffer"; 85 | m_QtIndexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw); 86 | 87 | if (m_QtIndexBuffer.bind()) 88 | qDebug() << "Success biding the index buffer"; 89 | 90 | //Send the indices data 91 | m_QtIndexBuffer.allocate(m_mesh.getIndicesArray().size() * sizeof(GLuint)); 92 | 93 | //Send the indices data 94 | m_QtIndexBuffer.write(0, m_mesh.getIndicesArray().constData(), m_mesh.getIndicesArray().size() * sizeof(GLuint)); 95 | 96 | qDebug() << "VBO buffer size " << m_QtVBO.size(); 97 | qDebug() << "Index buffer buffer size " << m_QtIndexBuffer.size(); 98 | 99 | } 100 | 101 | Object::~Object() 102 | { 103 | 104 | } 105 | 106 | void Object::resetModelMatrix() 107 | { 108 | m_modelMatrix.setToIdentity(); 109 | m_rotationX = 0.0; 110 | m_rotationY = 0.0; 111 | m_rotationZ = 0.0; 112 | } 113 | 114 | void Object::setAspectRatio(float aspectRatio) 115 | { 116 | m_modelMatrix.scale(1.0, 1.0 / aspectRatio); 117 | } 118 | 119 | void Object::scale(float scalingFactor) 120 | { 121 | m_modelMatrix.scale(scalingFactor, scalingFactor); 122 | } 123 | 124 | void Object::rotateX(int angleX) 125 | { 126 | angleX %= 360; 127 | 128 | m_rotationX += angleX; 129 | m_rotationX %= 360; 130 | 131 | m_modelMatrix.rotate(angleX, QVector3D(1.0, 0.0, 0.0)); 132 | } 133 | 134 | void Object::rotateY(int angleY) 135 | { 136 | angleY %= 360; 137 | 138 | m_rotationY += angleY; 139 | m_rotationY %= 360; 140 | 141 | m_modelMatrix.rotate(angleY, QVector3D(0.0, 1.0, 0.0)); 142 | } 143 | 144 | void Object::rotateZ(int angleZ) 145 | { 146 | angleZ %= 360; 147 | 148 | m_rotationZ += angleZ; 149 | m_rotationZ %= 360; 150 | 151 | m_modelMatrix.rotate(angleZ, QVector3D(0.0, 0.0, 1.0)); 152 | } 153 | 154 | string Object::loadPath(string &objectName) 155 | { 156 | string objectPath; 157 | 158 | if (objectName == "square") 159 | { 160 | #ifdef _WIN32 161 | objectPath = string(QDir::currentPath().toStdString() + "\\off\\square.off"); 162 | #endif 163 | 164 | #ifdef __gnu_linux__ 165 | objectPath = QDir::currentPath().toStdString() + string("/off/square.off"); 166 | #endif 167 | 168 | #if defined(__APPLE__) && defined(__MACH__) 169 | objectPath = QDir::currentPath().toStdString() + string("/../../../off/square.off"); 170 | #endif 171 | } 172 | else if (objectName == "monkey") 173 | { 174 | #ifdef _WIN32 175 | objectPath = string(QDir::currentPath().toStdString() + "\\off\\monkey.off"); 176 | #endif 177 | 178 | #ifdef __gnu_linux__ 179 | objectPath = QDir::currentPath().toStdString() + string("/off/monkey.off"); 180 | #endif 181 | 182 | #if defined(__APPLE__) && defined(__MACH__) 183 | objectPath = QDir::currentPath().toStdString() + string("/../../../off/monkey.off"); 184 | #endif 185 | } 186 | else if (objectName == "cube") 187 | { 188 | #ifdef _WIN32 189 | objectPath = string(QDir::currentPath().toStdString() + "\\off\\cube.off"); 190 | #endif 191 | 192 | #ifdef __gnu_linux__ 193 | objectPath = QDir::currentPath().toStdString() + string("/off/cube.off"); 194 | #endif 195 | 196 | #if defined(__APPLE__) && defined(__MACH__) 197 | objectPath = QDir::currentPath().toStdString() + string("/../../../off/cube.off"); 198 | #endif 199 | } 200 | else if (objectName == "teapot") 201 | { 202 | #ifdef _WIN32 203 | objectPath = string(QDir::currentPath().toStdString() + "\\obj\\teapotR90.obj"); 204 | #endif 205 | 206 | #ifdef __gnu_linux__ 207 | objectPath = QDir::currentPath().toStdString() + string("/obj/teapotR90.obj"); 208 | #endif 209 | 210 | #if defined(__APPLE__) && defined(__MACH__) 211 | objectPath = QDir::currentPath().toStdString() + string("/../../../obj/teapotR90.obj"); 212 | #endif 213 | } 214 | else if (objectName == "teapot-low") 215 | { 216 | #ifdef _WIN32 217 | objectPath = string(QDir::currentPath().toStdString() + "\\obj\\teapot-lowR90.obj"); 218 | #endif 219 | 220 | #ifdef __gnu_linux__ 221 | objectPath = QDir::currentPath().toStdString() + string("/obj/teapot-lowR90.obj"); 222 | #endif 223 | 224 | #if defined(__APPLE__) && defined(__MACH__) 225 | objectPath = QDir::currentPath().toStdString() + string("/../../../obj/teapot-lowR90.obj"); 226 | #endif 227 | } 228 | else 229 | { 230 | cerr << objectName << ".off does not exist"; 231 | } 232 | 233 | return objectPath; 234 | } 235 | 236 | void Object::loadMesh() 237 | { 238 | if (m_objectName == "teapot" || m_objectName == "teapot-low") 239 | { 240 | m_mesh.objReader(); 241 | } 242 | else 243 | { 244 | m_mesh.offReader(); 245 | m_mesh.setTextureCoordinates(); 246 | } 247 | 248 | m_mesh.centerMesh(); 249 | } 250 | 251 | void Object::setModelMatrix(QMatrix4x4 modelMatrix) 252 | { 253 | m_modelMatrix = QMatrix4x4(modelMatrix); 254 | } 255 | 256 | Material Object::getMaterial() const 257 | { 258 | return m_material; 259 | } 260 | 261 | void Object::setMaterial(Material material) 262 | { 263 | m_material = material; 264 | } 265 | 266 | Mesh Object::getMesh() const 267 | { 268 | return m_mesh; 269 | } 270 | 271 | 272 | QOpenGLBuffer Object::getQtVBO() const 273 | { 274 | return m_QtVBO; 275 | } 276 | 277 | QOpenGLBuffer Object::getIndexBuffer() const 278 | { 279 | return m_QtIndexBuffer; 280 | } 281 | 282 | int Object::getVertexOffset() const 283 | { 284 | return m_vertexOffset; 285 | } 286 | 287 | int Object::getTextureCoordinatesOffset() const 288 | { 289 | return m_texturesCoordsOffset; 290 | } 291 | 292 | int Object::getNormalsOffset() const 293 | { 294 | return m_normalsOffset; 295 | } 296 | 297 | QMatrix4x4 Object::getModelMatrix() const 298 | { 299 | return m_modelMatrix; 300 | } 301 | 302 | int Object::getRotationX() const 303 | { 304 | return m_rotationX; 305 | } 306 | 307 | int Object::getRotationY() const 308 | { 309 | return m_rotationY; 310 | } 311 | 312 | int Object::getRotationZ() const 313 | { 314 | return m_rotationZ; 315 | } 316 | 317 | 318 | std::string Object::getObjectName() const 319 | { 320 | return m_objectName; 321 | } 322 | -------------------------------------------------------------------------------- /opengl/object.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef OBJECT_H 27 | #define OBJECT_H 28 | 29 | #include "opengl/mesh.h" 30 | #include "opengl/material.h" 31 | #include "opengl/texture.h" 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include 40 | #include 41 | 42 | class Object 43 | { 44 | public: 45 | Object(); 46 | 47 | /** 48 | * Loads an object just from its name. 49 | * @brief Object::Object 50 | * @param objectName 51 | */ 52 | Object(std::string objectName); 53 | 54 | ~Object(); 55 | 56 | void resetModelMatrix(); 57 | 58 | 59 | /** 60 | * Set a given aspect ratio. 61 | * @brief setAspectRatio 62 | * @param aspectRatio 63 | */ 64 | void setAspectRatio(float aspectRatio); 65 | 66 | /** 67 | * Scales the object in every dimension. 68 | * @brief scale 69 | * @param scalingFactor 70 | */ 71 | void scale(float scalingFactor); 72 | 73 | /** 74 | * Function that returns the path of the .off file corresponding to the object. 75 | * Also sets the texture coordinates 76 | * @brief loadPath 77 | * @param objectName 78 | * @return 79 | */ 80 | std::string loadPath(std::string &objectName); 81 | 82 | 83 | /** 84 | * Loads the mesh. 85 | * @brief loadMesh 86 | */ 87 | void loadMesh(); 88 | 89 | void setModelMatrix(QMatrix4x4 modelMatrix); 90 | 91 | void rotateX(int angleX); 92 | void rotateY(int angleY); 93 | void rotateZ(int angleZ); 94 | 95 | Mesh getMesh() const; 96 | Material getMaterial() const; 97 | QOpenGLBuffer getQtVBO() const; 98 | QOpenGLBuffer getIndexBuffer() const; 99 | 100 | void setMaterial(Material material); 101 | 102 | int getVertexOffset() const; 103 | int getIndicesOffset() const; 104 | int getTextureCoordinatesOffset() const; 105 | int getNormalsOffset() const; 106 | 107 | QMatrix4x4 getModelMatrix() const; 108 | int getRotationX() const; 109 | int getRotationY() const; 110 | int getRotationZ() const; 111 | 112 | std::string getObjectName() const; 113 | 114 | private: 115 | std::string m_objectName; 116 | Mesh m_mesh; 117 | Material m_material; 118 | QOpenGLBuffer m_QtVBO; 119 | QOpenGLBuffer m_QtIndexBuffer; 120 | 121 | int m_vertexOffset; 122 | int m_texturesCoordsOffset; 123 | int m_normalsOffset; 124 | 125 | QMatrix4x4 m_modelMatrix; 126 | int m_rotationX; 127 | int m_rotationY; 128 | int m_rotationZ; 129 | 130 | }; 131 | 132 | #endif // OBJECT_H 133 | -------------------------------------------------------------------------------- /opengl/openglheaders.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef OPENGLHEADERS_H 27 | #define OPENGLHEADERS_H 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #endif // OPENGLHEADERS_H 34 | -------------------------------------------------------------------------------- /opengl/scene.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/scene.h" 27 | 28 | using namespace std; 29 | 30 | Scene::Scene() : m_objects(QVector()), m_pointLights(QVector()) 31 | { 32 | 33 | 34 | } 35 | 36 | Scene::Scene(string object) : m_objects(QVector()), m_pointLights(QVector()) 37 | { 38 | buildScene(object); 39 | } 40 | 41 | 42 | Scene::Scene(QVector& listOfObjectNames, const QVector &listOfPointLights) : 43 | m_objects(QVector()), m_pointLights(listOfPointLights) 44 | { 45 | for (int i = 0; i < listOfObjectNames.size(); i++) 46 | { 47 | m_objects.push_back(Object(listOfObjectNames[i])); 48 | } 49 | } 50 | 51 | Scene::~Scene() 52 | { 53 | 54 | } 55 | 56 | 57 | void Scene::updateObjectMaterial(int objectID, Material material) 58 | { 59 | if (!m_objects.empty()) 60 | { 61 | m_objects[objectID].setMaterial(material); 62 | } 63 | 64 | } 65 | 66 | void Scene::translateLightSourceX(int lightNumber, float translationX) 67 | { 68 | if (lightNumber < m_pointLights.size()) 69 | { 70 | m_pointLights[lightNumber].translateX(translationX); 71 | } 72 | } 73 | 74 | void Scene::translateLightSourceY(int lightNumber, float translationY) 75 | { 76 | if (lightNumber < m_pointLights.size()) 77 | { 78 | m_pointLights[lightNumber].translateY(translationY); 79 | } 80 | } 81 | 82 | void Scene::translateLightSourceZ(int lightNumber, float translationZ) 83 | { 84 | if (lightNumber < m_pointLights.size()) 85 | { 86 | m_pointLights[lightNumber].translateZ(translationZ); 87 | } 88 | } 89 | 90 | void Scene::setLightSourcePosition(int lightNumber, float x, float y, float z) 91 | { 92 | if (lightNumber < m_pointLights.size()) 93 | { 94 | m_pointLights[lightNumber].setPosition(x, y, z); 95 | } 96 | } 97 | 98 | void Scene::rotateObjectX(int objectNumber, int rotationX) 99 | { 100 | if (objectNumber < m_objects.size()) 101 | { 102 | m_objects[objectNumber].rotateX(rotationX); 103 | } 104 | } 105 | 106 | void Scene::rotateObjectY(int objectNumber, int rotationY) 107 | { 108 | if (objectNumber < m_objects.size()) 109 | { 110 | m_objects[objectNumber].rotateY(rotationY); 111 | } 112 | } 113 | 114 | void Scene::rotateObjectZ(int objectNumber, int rotationZ) 115 | { 116 | if (objectNumber < m_objects.size()) 117 | { 118 | m_objects[objectNumber].rotateZ(rotationZ); 119 | } 120 | } 121 | 122 | void Scene::setModelMatrix(int objectNumber, QMatrix4x4 &matrix) 123 | { 124 | m_objects[objectNumber].setModelMatrix(matrix); 125 | } 126 | 127 | void Scene::resetTransformationsObjects() 128 | { 129 | for (int k = 0; k < m_objects.size(); k++) 130 | { 131 | //Set the aspect ratio after loading the texture 132 | m_objects[k].resetModelMatrix(); 133 | } 134 | } 135 | 136 | void Scene::setAspectRatiosObjects() 137 | { 138 | 139 | } 140 | 141 | void Scene::buildScene(string object) 142 | { 143 | this->addObject(object); 144 | 145 | //Be careful not to put the light inside the object 146 | m_pointLights.push_back(Light(QVector4D(0.0, 0.0, LIGHT_POSITION_Z, 1.0), QVector3D(1.0, 1.0, 1.0), 1.0)); 147 | } 148 | 149 | 150 | void Scene::removeObjects() 151 | { 152 | m_objects.clear(); 153 | } 154 | 155 | void Scene::addObject(string object) 156 | { 157 | Object newObject = Object(object); 158 | m_objects.push_back(newObject); 159 | } 160 | 161 | void Scene::resetScene() 162 | { 163 | //Reset the lights 164 | //Be careful not to put the light inside the object 165 | for (int k = 0; k < m_objects.size(); k++) 166 | { 167 | m_pointLights[k].setPosition(0.0, 0.0, LIGHT_POSITION_Z); 168 | } 169 | } 170 | 171 | QVector Scene::getObjects() 172 | { 173 | return m_objects; 174 | } 175 | 176 | int Scene::getObjectRotation(int objectNumber, string rotationAxis) 177 | { 178 | int rotation = 0; 179 | 180 | if (rotationAxis == "X") 181 | rotation = m_objects[objectNumber].getRotationX(); 182 | else if (rotationAxis == "Y") 183 | rotation = m_objects[objectNumber].getRotationY(); 184 | else if (rotationAxis == "Z") 185 | rotation = m_objects[objectNumber].getRotationZ(); 186 | else 187 | cerr << "No rotation axis called : " << rotationAxis << endl; 188 | 189 | return rotation; 190 | } 191 | 192 | QVector Scene::getPointLightSources() 193 | { 194 | return m_pointLights; 195 | } 196 | -------------------------------------------------------------------------------- /opengl/scene.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef SCENE_H 27 | #define SCENE_H 28 | 29 | #define LIGHT_POSITION_Z 33.87 30 | 31 | #include "opengl/object.h" 32 | #include "opengl/light.h" 33 | 34 | #include 35 | #include 36 | 37 | class Scene 38 | { 39 | public: 40 | 41 | Scene(); 42 | Scene(std::string object); 43 | Scene(QVector& listOfObjectNames, const QVector &listOfPointLights); 44 | ~Scene(); 45 | 46 | /*---Geometric transformations--*/ 47 | void translateLightSourceX(int lightNumber, float translationX); 48 | void translateLightSourceY(int lightNumber, float translationY); 49 | void translateLightSourceZ(int lightNumber, float translationZ); 50 | void setLightSourcePosition(int lightNumber, float x, float y, float z); 51 | 52 | void rotateObjectX(int objectNumber, int rotationX); 53 | void rotateObjectY(int objectNumber, int rotationY); 54 | void rotateObjectZ(int objectNumber, int rotationZ); 55 | void setModelMatrix(int objectNumber, QMatrix4x4 &matrix); 56 | 57 | /** 58 | * Reset the model matrix. 59 | * @brief resetTransformationsObjects 60 | */ 61 | void resetTransformationsObjects(); 62 | 63 | /** 64 | * Set the aspect ratio of the objects. 65 | * @brief setAspectRatiosObjects 66 | */ 67 | void setAspectRatiosObjects(); 68 | 69 | /** 70 | * Build the scene by loading the geometry and setting the light sources. 71 | * @brief buildScene 72 | */ 73 | void buildScene(std::string object); 74 | 75 | void removeObjects(); 76 | 77 | void addObject(std::string object); 78 | 79 | /** 80 | * Reset the objects and the lights to their original position 81 | * @brief resetScene 82 | */ 83 | void resetScene(); 84 | 85 | void updateObjectMaterial(int objectID, Material material); 86 | 87 | QVector getObjects(); 88 | int getObjectRotation(int objectNumber, std::string rotationAxis); 89 | 90 | QVector getPointLightSources(); 91 | 92 | private: 93 | QVector m_objects; 94 | QVector m_pointLights; 95 | }; 96 | 97 | #endif // SCENE_H 98 | -------------------------------------------------------------------------------- /opengl/texture.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "opengl/texture.h" 27 | #include 28 | 29 | using namespace std; 30 | 31 | Texture::Texture() : m_textureId(0), m_filePath(""), m_width(0), m_height(0), m_numberOfComponents(0), m_isTextureLoaded(false) 32 | { 33 | 34 | } 35 | 36 | Texture::Texture(string filePath) : m_textureId(0), m_filePath(filePath), m_width(0), m_height(0), m_numberOfComponents(0), m_isTextureLoaded(false) 37 | { 38 | 39 | } 40 | 41 | Texture::Texture(int width, int height, int numberOfcomponents) : m_textureId(0), m_filePath(string("")), m_width(width), m_height(height), m_numberOfComponents(numberOfcomponents), m_isTextureLoaded(false) 42 | { 43 | 44 | } 45 | 46 | Texture::~Texture() 47 | { 48 | 49 | } 50 | 51 | void Texture::loadEmptyTexture_8UC3() 52 | { 53 | //remove an eventual previous picture from the memory 54 | if (glIsTexture(m_textureId) == GL_TRUE) 55 | { 56 | glDeleteTextures(1, &m_textureId); 57 | } 58 | 59 | //Generate a new texture ID 60 | glGenTextures(1, &m_textureId); 61 | 62 | //Bind the texture and start working on it 63 | glBindTexture(GL_TEXTURE_2D, m_textureId); 64 | 65 | //Allocate memory for a width*height texture but without data 66 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_FLOAT, NULL); 67 | 68 | //Smooth close textures 69 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 70 | 71 | //Do not smooth textures that are far away (performance optimisation) 72 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 73 | 74 | glBindTexture(GL_TEXTURE_2D, 0); 75 | 76 | m_isTextureLoaded = true; 77 | } 78 | 79 | void Texture::loadEmptyTexture_32FC3() 80 | { 81 | //remove an eventual previous picture from the memory 82 | if (glIsTexture(m_textureId) == GL_TRUE) 83 | { 84 | glDeleteTextures(1, &m_textureId); 85 | } 86 | 87 | //Generate a new texture ID 88 | glGenTextures(1, &m_textureId); 89 | 90 | //Bind the texture and start working on it 91 | glBindTexture(GL_TEXTURE_2D, m_textureId); 92 | 93 | //Allocate memory for a width*height texture but without data 94 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, m_width, m_height, 0, GL_RGB, GL_FLOAT, NULL); 95 | 96 | //Smooth close textures 97 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 98 | 99 | //Do not smooth textures that are far away (performance optimisation) 100 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 101 | 102 | glBindTexture(GL_TEXTURE_2D, 0); 103 | 104 | m_isTextureLoaded = true; 105 | } 106 | 107 | //Untested 108 | /** 109 | * Loads the texture from the given file name (file name sets by the constructor). 110 | * Texture is converted to 8bits 3 channels. 111 | * @brief Texture::load_8UC3 112 | * @return 113 | */ 114 | bool Texture::load_8UC3() 115 | { 116 | //Load the texture in BGR format 117 | QString imagePath = QString("%1").arg(m_filePath.c_str()); 118 | 119 | QImage texture = QImage(imagePath); 120 | 121 | //If the texture cannot be loaded 122 | if (texture.isNull()) 123 | { 124 | cout << "Could not load the texture : " << QDir::currentPath().toStdString() + m_filePath << endl; 125 | return false; 126 | } 127 | else 128 | { 129 | //The texture has to be inverted as the coordinate system for the (u,v) coordinates and the QImage are different 130 | QImage mirroredTexture = texture.mirrored(); 131 | 132 | m_width = mirroredTexture.width(); 133 | m_height = mirroredTexture.height(); 134 | 135 | //Convert image to pixels [in 0;255] 136 | mirroredTexture.convertToFormat(QImage::Format_RGB888); 137 | 138 | //Generate the texture id 139 | glGenTextures(1, &m_textureId); 140 | 141 | //Bind a texture 2D to the texture 142 | glBindTexture(GL_TEXTURE_2D, m_textureId); 143 | 144 | //Send the data to the memory 145 | //The first GL_RGB says that the data used will be in the RGB format 146 | // !!!!!!!!!!!!!!!!!!!!!! GL_RGB clamps the texture between 0 and 1 range. 147 | // To use floats above 1 : use GL_RGB32F 148 | //The second one says that texture.data is stored in BGR format (OpenCV) 149 | QImage textureOpenGL = QGLWidget::convertToGLFormat(mirroredTexture); 150 | m_numberOfComponents = 4; 151 | 152 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureOpenGL.bits()); 153 | 154 | //Smooth close textures 155 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 156 | 157 | //Do not smooth textures that are far away (performance optimisation) 158 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 159 | 160 | //Repeat the texture 161 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 162 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 163 | 164 | //Unbind 165 | glBindTexture(GL_TEXTURE_2D, 0); 166 | } 167 | 168 | //Texture correctly loaded 169 | m_isTextureLoaded = true; 170 | return true; 171 | } 172 | 173 | void Texture::setFilePath(string filePath) 174 | { 175 | this->m_filePath = filePath; 176 | } 177 | 178 | GLuint Texture::getTextureId() const 179 | { 180 | return m_textureId; 181 | } 182 | 183 | float Texture::getAspectRatio() const 184 | { 185 | return (float)m_width / (float)m_height; 186 | } 187 | 188 | int Texture::getWidth() const 189 | { 190 | return m_width; 191 | } 192 | 193 | int Texture::getHeight() const 194 | { 195 | return m_height; 196 | } 197 | 198 | bool Texture::isTextureLoaded() const 199 | { 200 | return m_isTextureLoaded; 201 | } 202 | -------------------------------------------------------------------------------- /opengl/texture.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef TEXTURE_H 27 | #define TEXTURE_H 28 | 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | 35 | #include "opengl/openglheaders.h" 36 | #include 37 | 38 | class Texture 39 | { 40 | public: 41 | 42 | Texture(); 43 | Texture(std::string filePath); 44 | Texture(int width, int height, int numberOfcomponents); 45 | 46 | ~Texture(); 47 | 48 | /** 49 | * Load an ampty texture with a (width,height, numberOfComponents) 50 | * @brief loadEmptyTexture_8UC3 51 | */ 52 | void loadEmptyTexture_8UC3(); 53 | 54 | /** 55 | * Loads an empty texture with a (width,height, numberOfComponents) with 32 bits float for each channel. 56 | * @brief loadEmptyTexture_32FC3 57 | */ 58 | void loadEmptyTexture_32FC3(); 59 | 60 | /** 61 | * Load textures saved as 8 bits images. 62 | * @brief load_8UC3 63 | * @return 64 | */ 65 | bool load_8UC3(); 66 | 67 | 68 | /** 69 | * Set the m_filePath of the texture. 70 | * @brief setFilePath 71 | * @param m_filePath 72 | */ 73 | void setFilePath(std::string filePath); 74 | 75 | /** 76 | * Get the texture id 77 | * @brief getTextureId 78 | * @return 79 | */ 80 | GLuint getTextureId() const; 81 | 82 | /** 83 | * Get the aspect ratio. 84 | * @brief getAspectRatio 85 | * @return 86 | */ 87 | float getAspectRatio() const; 88 | 89 | int getWidth() const; 90 | int getHeight() const; 91 | bool isTextureLoaded() const; 92 | 93 | 94 | private: 95 | GLuint m_textureId; 96 | std::string m_filePath; 97 | 98 | int m_width; 99 | int m_height; 100 | int m_numberOfComponents; 101 | 102 | bool m_isTextureLoaded; 103 | 104 | }; 105 | 106 | #endif // TEXTURE_H 107 | -------------------------------------------------------------------------------- /qt/Editor.cpp: -------------------------------------------------------------------------------- 1 | //============================================================================= 2 | /** @file Editor.cpp 3 | * 4 | * Implements CEditor. 5 | * 6 | Shader Maker - a cross-platform GLSL editor. 7 | Copyright (C) 2007-2008 Markus Kramer 8 | For details, see main.cpp or COPYING. 9 | 10 | =============================================================================*/ 11 | 12 | #include 13 | #include "Editor.h" 14 | #include "GLSLCodeEditor.h" 15 | #include "EditWindow.h" 16 | 17 | #include 18 | 19 | 20 | //============================================================================= 21 | // CEditor implementation 22 | //============================================================================= 23 | 24 | // construction 25 | CEditor::CEditor( /*IShader* shader,*/ QGLShaderProgram* renderToTextureShaderProgram) 26 | { 27 | //m_shader = shader; 28 | m_renderToTextureShaderProgram = renderToTextureShaderProgram; 29 | 30 | // alloc pointer array. 31 | m_editWindows = new GLSLBaseEditWindow* [ MAX_SHADER_TYPES ]; 32 | for( int i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 33 | m_editWindows[ i ] = NULL; 34 | 35 | m_isMDI = true; 36 | } 37 | 38 | CEditor::~CEditor( void ) 39 | { 40 | // free the pointer array. 41 | SAFE_DELETE_ARRAY( m_editWindows ); 42 | } 43 | 44 | 45 | /* 46 | ======================== 47 | init 48 | ======================== 49 | */ 50 | void CEditor::init( const QPoint & position ) 51 | { 52 | // assume not initialized 53 | assert( m_editWindows[0] == NULL ); 54 | 55 | // 56 | // create the editor window(s) 57 | // 58 | #if defined(__APPLE__) // make Mac users happy 59 | createSdiWindow( position, true ); 60 | #else 61 | createMdiWindow( position, true ); 62 | #endif 63 | 64 | // auto-compile the initial shaders. 65 | link(); 66 | } 67 | 68 | 69 | /* 70 | ======================== 71 | shutdown 72 | ======================== 73 | */ 74 | void CEditor::shutdown( void ) 75 | { 76 | destroyEditWindow(); 77 | } 78 | 79 | 80 | /* 81 | ======================== 82 | maybeSave 83 | ======================== 84 | */ 85 | bool CEditor::maybeSave( void ) 86 | { 87 | // ask every editor window 88 | for( int i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 89 | { 90 | if( m_editWindows[ i ] != NULL && 91 | !m_editWindows[ i ]->maybeSave() ) 92 | { 93 | return false; 94 | } 95 | } 96 | 97 | return true; 98 | } 99 | 100 | 101 | /* 102 | ======================== 103 | switchToMDI 104 | ======================== 105 | */ 106 | void CEditor::switchToMDI( void ) 107 | { 108 | int i; 109 | bool hasPosition = false; 110 | QPoint position; 111 | QString fileNames[ MAX_SHADER_TYPES ]; 112 | 113 | // already MDI 114 | if( m_isMDI ) 115 | return; 116 | 117 | // make sure no data is lost 118 | if( !maybeSave() ) 119 | return; 120 | 121 | // 122 | // collect filenames of the shaders 123 | // 124 | for( i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 125 | { 126 | if( m_editWindows[ i ] != NULL ) 127 | { 128 | // remember position of the first window. 129 | if( !hasPosition ) 130 | { 131 | position = m_editWindows[i]->pos(); 132 | hasPosition = true; 133 | } 134 | 135 | GLSLSdiEditWindow* sdi = (GLSLSdiEditWindow*)m_editWindows[ i ]; 136 | fileNames[ i ] = sdi->fileName(); 137 | } 138 | } 139 | 140 | // create the 'other' view 141 | destroyEditWindow(); 142 | createMdiWindow( position, false ); 143 | 144 | // 145 | // load the files that were in use before 146 | // 147 | GLSLMdiEditWindow* mdi = (GLSLMdiEditWindow*)m_editWindows[ 0 ]; 148 | for( i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 149 | { 150 | mdi->loadSourceFile( i, fileNames[i] ); 151 | } 152 | } 153 | 154 | 155 | /* 156 | ======================== 157 | switchToSDI 158 | ======================== 159 | */ 160 | void CEditor::switchToSDI( void ) 161 | { 162 | int i; 163 | QPoint position; 164 | QString fileNames[ MAX_SHADER_TYPES ]; 165 | 166 | // already SDI 167 | if( !m_isMDI ) 168 | return; 169 | 170 | // make sure no data is lost 171 | if( !maybeSave() ) 172 | return; 173 | 174 | // 175 | // collect filenames of the shaders 176 | // 177 | position = m_editWindows[0]->pos(); // remember position 178 | GLSLMdiEditWindow* mdi = (GLSLMdiEditWindow*)m_editWindows[ 0 ]; 179 | for( i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 180 | { 181 | fileNames[ i ] = mdi->fileName( i ); 182 | } 183 | 184 | // create the 'other' view 185 | destroyEditWindow(); 186 | createSdiWindow( position, false ); 187 | 188 | // 189 | // load the files that were in use before 190 | // 191 | for( i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 192 | { 193 | if( m_editWindows[ i ] != NULL ) 194 | { 195 | GLSLSdiEditWindow* sdi = (GLSLSdiEditWindow*)m_editWindows[ i ]; 196 | sdi->loadSourceFile( fileNames[i] ); 197 | } 198 | } 199 | } 200 | 201 | 202 | /* 203 | ======================== 204 | createMdiWindow 205 | ======================== 206 | */ 207 | void CEditor::createMdiWindow( const QPoint & position, bool loadStdSource ) 208 | { 209 | //TODO 210 | #if 0// HOOK 1 211 | m_editWindows[0] = new GLSLMdiEditWindow( /*m_shader*/ ); 212 | setupSignals(); 213 | 214 | if( loadStdSource ) 215 | { 216 | m_editWindows[0]->loadInitialShaderSource(); 217 | } 218 | 219 | m_editWindows[0]->move( position ); 220 | m_editWindows[0]->show(); 221 | 222 | m_isMDI = true; 223 | #endif 224 | } 225 | 226 | 227 | /* 228 | ======================== 229 | createSdiWindow 230 | ======================== 231 | */ 232 | void CEditor::createSdiWindow( const QPoint & position, bool loadStdSource ) 233 | { 234 | int i; 235 | 236 | // create editor windows 237 | for( i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 238 | { 239 | if( /*m_shader->isShaderTypeAvailable( i ) TODO */ true) 240 | { 241 | m_editWindows[ i ] = new GLSLSdiEditWindow( /*m_shader,*/ i ); 242 | } 243 | } 244 | 245 | setupSignals(); 246 | 247 | // initialize editor windows 248 | for( i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 249 | { 250 | if( m_editWindows[ i ] == NULL ) 251 | continue; 252 | 253 | if( loadStdSource ) 254 | { 255 | m_editWindows[i]->loadInitialShaderSource(); 256 | } 257 | 258 | m_editWindows[i]->move( position + QPoint( i * 20, 0 ) ); 259 | m_editWindows[i]->show(); 260 | } 261 | 262 | m_isMDI = false; 263 | } 264 | 265 | 266 | /* 267 | ======================== 268 | destroyEditWindow 269 | ======================== 270 | */ 271 | void CEditor::destroyEditWindow( void ) 272 | { 273 | for( int i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 274 | { 275 | if( m_editWindows[ i ] != NULL ) 276 | { 277 | disconnect( m_editWindows[ i ], 0, this, 0 ); 278 | SAFE_DELETE( m_editWindows[ i ] ); 279 | } 280 | } 281 | } 282 | 283 | 284 | /* 285 | ======================== 286 | setupSignals 287 | ======================== 288 | */ 289 | void CEditor::setupSignals( void ) 290 | { 291 | //TODO 292 | #if 0 293 | for( int i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 294 | { 295 | if(i == IShader::TYPE_VERTEX_FBO || i == IShader::TYPE_FRAGMENT_FBO) 296 | { 297 | if(m_editWindows[ i ] != NULL ) 298 | { 299 | connect( m_editWindows[i], SIGNAL(linkRenderToTextureProgram()), this, SLOT(link()) ); 300 | connect( m_editWindows[i], SIGNAL(deactivateProgram()), this, SLOT(shouldDeactivateProgram()) ); 301 | connect( m_editWindows[i], SIGNAL(aboutToQuit()), this, SLOT(gotQuitSignal()) ); 302 | connect( m_editWindows[i], SIGNAL(requestMdiMode()), this, SLOT(switchToMDI()) ); 303 | connect( m_editWindows[i], SIGNAL(requestSdiMode()), this, SLOT(switchToSDI()) ); 304 | } 305 | } 306 | else 307 | { 308 | if(m_editWindows[ i ] != NULL ) 309 | { 310 | connect( m_editWindows[i], SIGNAL(linkProgram()), this, SLOT(link()) ); 311 | connect( m_editWindows[i], SIGNAL(deactivateProgram()), this, SLOT(shouldDeactivateProgram()) ); 312 | connect( m_editWindows[i], SIGNAL(aboutToQuit()), this, SLOT(gotQuitSignal()) ); 313 | connect( m_editWindows[i], SIGNAL(requestMdiMode()), this, SLOT(switchToMDI()) ); 314 | connect( m_editWindows[i], SIGNAL(requestSdiMode()), this, SLOT(switchToSDI()) ); 315 | } 316 | } 317 | 318 | 319 | } 320 | #endif 321 | } 322 | 323 | 324 | /* 325 | ======================== 326 | link 327 | ======================== 328 | */ 329 | void CEditor::link( void ) 330 | { 331 | m_renderToTextureShaderProgram->removeAllShaders(); 332 | 333 | // upload shader source code. 334 | for( int i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 335 | { 336 | if( m_editWindows[ i ] != NULL ) 337 | { 338 | // m_editWindows[ i ]->uploadShaderSource(m_shader, m_renderToTextureShaderProgram); 339 | } 340 | } 341 | 342 | // request a link operation. 343 | emit linkProgram(); 344 | } 345 | 346 | 347 | /* 348 | ======================== 349 | shouldDeactivateProgram 350 | ======================== 351 | */ 352 | void CEditor::shouldDeactivateProgram( void ) 353 | { 354 | emit deactivateProgram(); 355 | } 356 | 357 | 358 | /* 359 | ======================== 360 | gotQuitSignal 361 | ======================== 362 | */ 363 | void CEditor::gotQuitSignal( void ) 364 | { 365 | // make sure all editors can save their work 366 | for( int i = 0 ; i < MAX_SHADER_TYPES ; i++ ) 367 | { 368 | if( m_editWindows[ i ] != NULL && 369 | !m_editWindows[ i ]->maybeSave() ) 370 | { 371 | return; 372 | } 373 | } 374 | 375 | emit aboutToQuit(); 376 | } 377 | 378 | 379 | -------------------------------------------------------------------------------- /qt/Editor.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __EDITOR_HPP_INCLUDED__ 3 | #define __EDITOR_HPP_INCLUDED__ 4 | 5 | #include "opengl/openglheaders.h" 6 | #include 7 | #include 8 | #include 9 | 10 | // forward declarations 11 | //class IShader; 12 | class GLSLBaseEditWindow; 13 | 14 | 15 | //TODO write own editor 16 | //editor takes QGLShaderProgram and extracts vertex, geom and fragment shader? 17 | 18 | 19 | 20 | //============================================================================= 21 | // CEditor 22 | //============================================================================= 23 | 24 | /** The main source code editing component. 25 | * This class is the top level interface to the GLSL source code 26 | * editor component. It is bound to an IShader object which 27 | * must be already initilized when this constructor is called. 28 | * The created text editing widgets are based on the state 29 | * in that shader object. The source code written with 30 | * this editor will be stored in that shader object, too. 31 | */ 32 | class CEditor : public QObject 33 | { 34 | Q_OBJECT 35 | public: 36 | /** Constructor. 37 | * @param shader IShader object this editor is bound to. 38 | */ 39 | CEditor( /*IShader* shader,*/ QGLShaderProgram* renderToTextureShaderProgram); 40 | 41 | virtual ~CEditor( void ); ///< Destruction. 42 | 43 | /** Initializes and shows the editor. 44 | * @param position Screen coordinates of the top left corner 45 | * of the editor window to appear. 46 | */ 47 | void init( const QPoint & position ); 48 | 49 | /** Cleans up state and prepares for destruction. 50 | */ 51 | void shutdown( void ); 52 | 53 | /** Asks the user wether to save modified documents. 54 | * This call asks the user for each modified shader source code. 55 | * @return Returns true if the calling operation should coninue, otherwise false. 56 | * Simple: True means 'no data will get lost'. 57 | */ 58 | bool maybeSave( void ); 59 | 60 | signals: 61 | /** The user requested to compile link the current shader. 62 | * @pre 63 | * When this signal is emitted, the editor has already stored the required 64 | * source code in the bound IShader object. 65 | */ 66 | void linkProgram( void ); 67 | 68 | void linkRenderToTextureProgram(); 69 | 70 | /** The current shader should be deactivated. 71 | * This happens, when the user opens an existing or creates a new shader. 72 | */ 73 | void deactivateProgram( void ); 74 | 75 | /** Tells the application that the user wants to quit the application. 76 | */ 77 | void aboutToQuit( void ); // the editor is ready to quit the app. 78 | 79 | private slots:; 80 | 81 | // these are forwarded from the editor windows 82 | void link(); 83 | void shouldDeactivateProgram( void ); 84 | void gotQuitSignal( void ); 85 | 86 | // switching between SDI and MDI windows. 87 | void switchToSDI( void ); 88 | void switchToMDI( void ); 89 | 90 | private: 91 | 92 | // changing the editor window 93 | void createMdiWindow( const QPoint & position, bool loadStdSource ); 94 | void createSdiWindow( const QPoint & position, bool loadStdSource ); 95 | void destroyEditWindow( void ); 96 | void setupSignals( void ); 97 | 98 | // the shader object 99 | //IShader* m_shader; 100 | QGLShaderProgram* m_renderToTextureShaderProgram; 101 | 102 | // Wether the editor is in MDI mode. 103 | // The initial state is true. 104 | bool m_isMDI; 105 | 106 | // Ihere is a single window for every shader type. 107 | // If the editor is in single-window mode, 108 | // then only m_editWindows[0] is used, the others are NULL. 109 | GLSLBaseEditWindow** m_editWindows; // [ IShader::MAX_SHADER_TYPES ] 110 | }; 111 | 112 | #endif // __EDITOR_HPP_INCLUDED__ 113 | 114 | -------------------------------------------------------------------------------- /qt/GLSLCodeEditor.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __SOURCEEDITOR_HPP_INCLUDED__ 27 | #define __SOURCEEDITOR_HPP_INCLUDED__ 28 | 29 | //============================================================================= 30 | // GLSLCodeEditor - text editor for GLSL source code. 31 | //============================================================================= 32 | 33 | #include "opengl/openglheaders.h" 34 | #include 35 | #include 36 | #include 37 | #include "codeeditor.h" 38 | 39 | //TODO move to config 40 | //============================================================================= 41 | // misc 42 | //============================================================================= 43 | 44 | #define CONFIG_SHADER_DIRECTORY "shaders/" ///< Where to look for shader source files 45 | #define CONFIG_MODEL_DIRECTORY "models/" ///< Where to look for .OBJ model files 46 | #define CONFIG_TEXTURE_DIRECTORY "textures/" ///> Where to look for texture iamge files 47 | #define CONFIG_EDITOR_FONT_NAME "Consolas" ///< Editor font 48 | #define CONFIG_TAB_SIZE 4 ///< One tab quals that many spaces 49 | #define CONFIG_REFRESH_INTERVAL 10 ///< 100 fps, periodic screen refesh in ms 50 | #define CONFIG_MAX_USED_TMUS 4 ///< number of texture mapping units accessable by CTextureWidget 51 | 52 | /** Commet this out to disable geometry shader support */ 53 | #define CONFIG_ENABLE_GEOMETRY_SHADER 54 | 55 | /** Font size for the editor. 56 | * 10 is hard to read on linux. 57 | */ 58 | #ifdef WIN32 59 | #define CONFIG_EDITOR_FONT_SIZE 10 60 | #else // linux, mac 61 | #define CONFIG_EDITOR_FONT_SIZE 12 62 | #endif 63 | 64 | #define MAX_SHADER_TYPES 5 65 | 66 | #define CONFIG_STRING_APPLICATION_TITLE "Imperial CO317 Computer Graphics" 67 | 68 | //============================================================================= 69 | // useful macros 70 | //============================================================================= 71 | 72 | /** Deletes an object, if not NULL. 73 | * @warning Do not use this for arrays! 74 | */ 75 | #define SAFE_DELETE(p) do{ if( (p) != NULL ) { delete (p); (p)=NULL; } } while(0) 76 | 77 | /** Deletes an array of objects, if not NULL. 78 | * @warning Only use this for arrays! 79 | */ 80 | #define SAFE_DELETE_ARRAY(p) do{ if( (p) != NULL ) { delete [] (p); (p)=NULL; } } while(0) 81 | 82 | ////////////////////////////////////////////////// 83 | 84 | /** This is a GLSL source code editing widget. 85 | * It is simply an improved QTextEdit with some additional functionality 86 | * like open/save/new file operations and syntax highlighting. 87 | * This widget knows about the shader type it is editing. This allows automatic 88 | * selection of filename filters in dialogs. 89 | * The shader type must be set during object construction. 90 | */ 91 | class GLSLCodeEditor : public CodeEditor 92 | { 93 | Q_OBJECT 94 | public: 95 | /** Constructs a GLSLCodeEditor object. 96 | * A shader type must be specified that controls some aspects of the editor, 97 | * like filters in open/save dialogs. 98 | * @param shaderType Shader type from IShader. 99 | */ 100 | GLSLCodeEditor(QGLShader::ShaderType shaderType); 101 | 102 | /** Load the initial source code for the assigned shader type. 103 | * The source code loaded is based on the shaderType argument passed to the constructor. 104 | */ 105 | void loadInitialShaderSource(void); 106 | 107 | /** Returns the shader type assigned to this source editor. 108 | */ 109 | QGLShader::ShaderType shaderType(void) const; 110 | 111 | /** Clears the content to an empty string. 112 | * Pops up a 'save changes' dialog if necessary. 113 | */ 114 | void newFile(void); 115 | 116 | /** Asks the user to open a file. 117 | * Pops up a 'save changes' dialog if necessary. 118 | */ 119 | void open(void); 120 | 121 | /** Saves the currently opened file. 122 | * If no filename is stored, the uses is asked for a file name. 123 | */ 124 | bool save(void); 125 | 126 | /** Asks the user for a file name and saves the content in that file. 127 | */ 128 | bool saveAs(void); 129 | 130 | /** Asks the user to save changes, if necessary. 131 | * @return True if the data is saved and the calling operation can be continued. 132 | * False if the calling operation should be aborded. 133 | */ 134 | bool maybeSave(void); 135 | 136 | /** Returns the filename of the currently opened file. 137 | * An empty string indicates that no file name is assigned to the current content. 138 | */ 139 | QString fileName(void) const; 140 | 141 | /** Tells the widget to directly load a named file. 142 | * @param fileName Name of the file to load. 143 | */ 144 | void loadFile(const QString & fileName); 145 | 146 | signals: 147 | /** Emitted every time the owner of this widget needs to update. 148 | * For example, this happens when the documents modified flag changed. 149 | */ 150 | void updateMainWindow(void); 151 | 152 | /** Indicates that the used opened an existing or created a new shader. 153 | * It is emitted every time a document was successfully loaded from disk. 154 | */ 155 | void shaderChangedCompletely(void); 156 | 157 | private slots: 158 | void documentContentsChanged(void); 159 | 160 | private: 161 | bool saveFile(const QString & fileName); 162 | void setFileName(const QString & fileName); 163 | QString fileTypeFilter(void); 164 | 165 | // auto sizing 166 | QSize minimumSizeHint(void) const { return QSize(200, 100); } 167 | QSize sizeHint(void) const { return QSize(800, 600); } 168 | 169 | 170 | // creates the syntax highlighter for GLSL 171 | // and assigns it to this GLSLCodeEditor. 172 | void createSyntaxHighlighter(void); 173 | QSyntaxHighlighter* m_highlighter; 174 | 175 | QGLShader::ShaderType m_shaderType; 176 | QString m_fileName; // empty string == untitled document 177 | }; 178 | 179 | #endif // __SOURCEEDITOR_HPP_INCLUDED__ 180 | 181 | -------------------------------------------------------------------------------- /qt/GLSLEditorWidget.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | #include "qt/GLSLEditorWidget.h" 26 | #include "qt/Matrix4x4Widget.h" 27 | #include "qt/GLSLCodeEditor.h" 28 | #include 29 | 30 | GLSLEditorWidget::GLSLEditorWidget(QGLShader* shader, QWidget *parent) : QWidget(parent), ui(new Ui::GLSLEditorWidget) 31 | { 32 | m_shader = shader; 33 | ui->setupUi(this); 34 | 35 | sEditor = new GLSLCodeEditor(shader->shaderType()); 36 | 37 | sEditor->setPlainText(removeQtDefines(shader->sourceCode())); 38 | 39 | ui->mainVLayout->insertWidget(0, sEditor); 40 | connect(ui->compileAndLinkButton, SIGNAL(clicked()), this, SLOT(updateShaderSource())); 41 | } 42 | 43 | GLSLEditorWidget::~GLSLEditorWidget() 44 | { 45 | delete ui; 46 | } 47 | 48 | QString GLSLEditorWidget::removeQtDefines(QString sourceCode) 49 | { 50 | QString defines[3] = { QString("#define lowp") , QString("#define mediump") , QString("#define highp") }; 51 | 52 | for (int i = 0; i < 3; ++i) 53 | { 54 | int pos = sourceCode.indexOf(defines[i]); 55 | 56 | if (pos != -1) 57 | { 58 | sourceCode.remove(pos, defines[i].length()); 59 | 60 | pos = sourceCode.indexOf('\n', pos); 61 | 62 | // If the removal of QtDefine leaves empty lines 63 | // remove the \n 64 | if (pos != -1) 65 | { 66 | sourceCode.remove(pos, 1); 67 | } 68 | } 69 | 70 | } 71 | 72 | return sourceCode; 73 | } 74 | 75 | void GLSLEditorWidget::setLinkToProgram(bool val) 76 | { 77 | ui->linkToProgramCheckBox->setChecked(val); 78 | } 79 | 80 | bool GLSLEditorWidget::getLinkToProgram() 81 | { 82 | return ui->linkToProgramCheckBox->isChecked(); 83 | } 84 | 85 | QGLShader* GLSLEditorWidget::getShader() 86 | { 87 | return m_shader; 88 | }; 89 | 90 | void GLSLEditorWidget::updateShaderSource() 91 | { 92 | if (!m_shader->compileSourceCode(sEditor->toPlainText())) 93 | { 94 | QString error = m_shader->log(); 95 | emit updateLog(error); 96 | emit displayLog(); 97 | } 98 | else 99 | { 100 | QString text = QString("Compilation of %1 successfull.").arg(this->objectName()); 101 | emit updateLog(text); 102 | emit displayLog(); 103 | if (ui->linkToProgramCheckBox->isChecked()) 104 | { 105 | emit(compileAndLink()); 106 | } 107 | else 108 | { 109 | QString text_ = QString("________________________________________________\n"); 110 | emit updateLog(text_); 111 | emit displayLog(); 112 | } 113 | } 114 | } 115 | 116 | void GLSLEditorWidget::setShaderCode(QString &text) 117 | { 118 | sEditor->setPlainText(removeQtDefines(text)); 119 | } 120 | 121 | QString GLSLEditorWidget::getShaderCode() 122 | { 123 | return sEditor->toPlainText(); 124 | } 125 | 126 | void GLSLEditorWidget::setCurrentFile(const QString &fileName) 127 | { 128 | currentFileName = fileName; 129 | sEditor->document()->setModified(false); 130 | setWindowModified(false); 131 | 132 | QString shownName = currentFileName; 133 | if (currentFileName.isEmpty()) 134 | shownName = "untitled.txt"; 135 | setWindowFilePath(shownName); 136 | } 137 | 138 | QString GLSLEditorWidget::getCurFileName() 139 | { 140 | return currentFileName; 141 | } 142 | 143 | GLSLCodeEditor* GLSLEditorWidget::getCodeEditor() 144 | { 145 | return sEditor; 146 | } 147 | 148 | void GLSLEditorWidget::keyPressEvent(QKeyEvent *event) 149 | { 150 | if (event->key() == Qt::Key_F5) 151 | { 152 | updateShaderSource(); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /qt/GLSLEditorWidget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __GLSLEditorWidget_HPP_INCLUDED__ 27 | #define __GLSLEditorWidget_HPP_INCLUDED__ 28 | 29 | #include "ui_GLSLEditorWidget.h" 30 | #include 31 | #include 32 | 33 | class QGLShader; 34 | class GLSLCodeEditor; 35 | 36 | class GLSLEditorWidget : public QWidget 37 | { 38 | Q_OBJECT 39 | 40 | public: 41 | GLSLEditorWidget(QGLShader* shader, QWidget *parent); 42 | ~GLSLEditorWidget(); 43 | 44 | /** 45 | * Removes the OpenGLES defines added by Qt. 46 | * @brief removeQtDefines 47 | * @param sourceCode 48 | * @return 49 | */ 50 | QString removeQtDefines(QString sourceCode); 51 | void setLinkToProgram(bool val); 52 | bool getLinkToProgram(); 53 | QGLShader* getShader(); 54 | QString getCurFileName(); 55 | void setShaderCode(QString& text); 56 | QString getShaderCode(); 57 | GLSLCodeEditor* getCodeEditor(); 58 | 59 | signals: 60 | /** 61 | * triggers linking 62 | * @brief updateLog 63 | */ 64 | void compileAndLink(); 65 | /** 66 | * Appends text to the log tab 67 | * @brief updateLog 68 | */ 69 | void updateLog(QString); 70 | 71 | /** 72 | * Displays the log tab. 73 | * @brief displayLog 74 | */ 75 | void displayLog(); 76 | 77 | public slots: 78 | void setCurrentFile(const QString &fileName); 79 | void updateShaderSource(); 80 | 81 | protected: 82 | void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; 83 | 84 | private: 85 | Ui::GLSLEditorWidget* ui; 86 | QGLShader* m_shader; 87 | GLSLCodeEditor* sEditor; 88 | QString currentFileName; 89 | }; 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /qt/GLSLEditorWidget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | GLSLEditorWidget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | link to program 23 | 24 | 25 | 26 | 27 | 28 | 29 | Qt::Horizontal 30 | 31 | 32 | 33 | 40 34 | 20 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | Compile / Link (F5) 43 | 44 | 45 | 46 | 47 | 48 | 49 | Qt::Horizontal 50 | 51 | 52 | 53 | 40 54 | 20 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /qt/GLSLEditorWindow.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __GLSLEditorWindow_HPP_INCLUDED__ 27 | #define __GLSLEditorWindow_HPP_INCLUDED__ 28 | 29 | #include "opengl/openglheaders.h" 30 | #include "ui_GLSLEditorWindow.h" 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | class GLSLCodeEditor; 37 | 38 | class GLSLEditorWindow : public QMainWindow 39 | { 40 | Q_OBJECT 41 | 42 | public: 43 | GLSLEditorWindow(QGLShaderProgram* sProgram, QGLShaderProgram* dsProgram, QWidget *parent); 44 | ~GLSLEditorWindow(); 45 | 46 | //TODO documentation 47 | void loadDefaultShaders(); 48 | void linkShader(); 49 | QGLShaderProgram* getShaderProgram() { return m_shaderProgram; }; 50 | QGLShaderProgram* getShaderProgramDisplay() { return m_shaderProgramDisplay; }; 51 | 52 | // private slots: 53 | 54 | signals: 55 | /** 56 | * Appends text to the log tab 57 | * @brief updateLog 58 | */ 59 | void updateLog(QString); 60 | 61 | /** 62 | * Displays the log tab. 63 | * @brief displayLog 64 | */ 65 | void displayLog(); 66 | 67 | /** 68 | * Update the uniform tab in mainwindow. 69 | * @brief updateUniformWiget 70 | */ 71 | void updateUniformTab(); 72 | 73 | /** 74 | * Link the shaderprogram in mainwindow. 75 | * @brief updateUniformWiget 76 | */ 77 | void updateShaderProgram(); 78 | 79 | public slots: 80 | void compileAndLink(); 81 | bool savePipelineAction(); 82 | bool savePipelineAsAction(); 83 | bool loadPipeline(); 84 | void loadFromFileAction(); 85 | bool saveToFileAction(); 86 | void exitApplicationAction(); 87 | void documentWasModified(); 88 | bool saveAs(); 89 | void about(); 90 | 91 | protected: 92 | void setupTabs(); 93 | bool maybeSave(); 94 | void loadFile(QString &fileName); 95 | bool save(); 96 | bool saveFile(const QString &fileName); 97 | bool savePipeline(QString &fileName); 98 | void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; 99 | 100 | private: 101 | void writeSettings(); 102 | void readSettings(); 103 | 104 | Ui::GLSLEditorWindow* ui; 105 | QGLShaderProgram* m_shaderProgram; 106 | QGLShaderProgram* m_shaderProgramDisplay; 107 | QString pipelineFileName; 108 | }; 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /qt/GLSLEditorWindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | GLSLEditorWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 671 10 | 481 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 21 | -1 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 0 31 | 0 32 | 671 33 | 38 34 | 35 | 36 | 37 | 38 | File 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | Help 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | New 62 | 63 | 64 | 65 | 66 | Open 67 | 68 | 69 | 70 | 71 | Save As 72 | 73 | 74 | 75 | 76 | Exit 77 | 78 | 79 | 80 | 81 | Load Shader (Ctrl-L) 82 | 83 | 84 | &Load (Ctrl-L) 85 | 86 | 87 | Ctrl+L 88 | 89 | 90 | 91 | 92 | Save Shader (Ctrl-U) 93 | 94 | 95 | &Save (Ctrl-U) 96 | 97 | 98 | Ctrl+U 99 | 100 | 101 | 102 | 103 | Save Shader As (Ctrl-Shift-U) 104 | 105 | 106 | Ctrl+Shift+U 107 | 108 | 109 | 110 | 111 | Exit (Ctrl-Q) 112 | 113 | 114 | Ctrl+Q 115 | 116 | 117 | 118 | 119 | About 120 | 121 | 122 | 123 | 124 | Save pipeline (Ctrl-S) 125 | 126 | 127 | Ctrl+S 128 | 129 | 130 | 131 | 132 | Load pipeline (Ctrl-O) 133 | 134 | 135 | Ctrl+O 136 | 137 | 138 | 139 | 140 | Save pipeline As (Ctrl-E) 141 | 142 | 143 | Ctrl+E 144 | 145 | 146 | 147 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /qt/MatricesWidget.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "MatricesWidget.h" 27 | #include "Matrix4x4Widget.h" 28 | 29 | MatricesWidget::MatricesWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MatricesWidget) 30 | { 31 | 32 | ui->setupUi(this); 33 | m_ModelMatrix.setToIdentity(); 34 | m_ViewMatrix.setToIdentity(); 35 | m_ProjectionMatrix.setToIdentity(); 36 | 37 | QHBoxLayout *vbox1 = new QHBoxLayout; 38 | modelMatrixWidget = new Matrix4x4Widget(ui->ModelMatrixBox); 39 | vbox1->addWidget(modelMatrixWidget); 40 | ui->ModelMatrixBox->setLayout(vbox1); 41 | 42 | QHBoxLayout *vbox2 = new QHBoxLayout; 43 | viewMatrixWidget = new Matrix4x4Widget(ui->ViewMatrixBox); 44 | vbox2->addWidget(viewMatrixWidget); 45 | ui->ViewMatrixBox->setLayout(vbox2); 46 | 47 | QHBoxLayout *vbox3 = new QHBoxLayout; 48 | projectionMatrixWidget = new Matrix4x4Widget(ui->ProjectionMatrixBox); 49 | vbox3->addWidget(projectionMatrixWidget); 50 | ui->ProjectionMatrixBox->setLayout(vbox3); 51 | 52 | connect(modelMatrixWidget, SIGNAL(matrixChanged(QMatrix4x4)), this, SIGNAL(modelMatrixChanged(QMatrix4x4))); 53 | connect(viewMatrixWidget, SIGNAL(matrixChanged(QMatrix4x4)), this, SIGNAL(viewMatrixChanged(QMatrix4x4))); 54 | connect(projectionMatrixWidget, SIGNAL(matrixChanged(QMatrix4x4)), this, SIGNAL(projectionMatrixChanged(QMatrix4x4))); 55 | connect(ui->m_resetButton, SIGNAL(clicked()), this, SIGNAL(resetMatrices())); 56 | } 57 | 58 | MatricesWidget::~MatricesWidget() 59 | { 60 | delete ui; 61 | } 62 | 63 | void MatricesWidget::updateModelMatrix(QMatrix4x4 mat) 64 | { 65 | modelMatrixWidget->updateSpinBoxes(mat); 66 | } 67 | 68 | void MatricesWidget::updateViewMatrix(QMatrix4x4 mat) 69 | { 70 | viewMatrixWidget->updateSpinBoxes(mat); 71 | //qDebug() << mat; 72 | } 73 | 74 | void MatricesWidget::updateProjectionMatrix(QMatrix4x4 mat) 75 | { 76 | projectionMatrixWidget->updateSpinBoxes(mat); 77 | } 78 | 79 | void MatricesWidget::updateMatrices() 80 | { 81 | //TODO 82 | QMatrix4x4 m; 83 | //m_scene->getCameraState()->getProjectionMatrix(m); 84 | projectionMatrixWidget->updateSpinBoxes(m); 85 | 86 | /*QMatrix4x4 m; 87 | m_scene->getCameraState()->getModelMatrix(m); 88 | modelMatrixWidget->updateSpinBoxes(m); 89 | 90 | QMatrix4x4 m; 91 | m_scene->getCameraState()->getViewMatrix(m); 92 | viewMatrixWidget->updateSpinBoxes(m);*/ 93 | } 94 | 95 | -------------------------------------------------------------------------------- /qt/MatricesWidget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __MatricesWidget__HPP_INCLUDED__ 27 | #define __MatricesWidget__HPP_INCLUDED__ 28 | 29 | #include "ui_Matrix4x4Widget.h" 30 | #include "ui_MatricesWidget.h" 31 | #include 32 | 33 | class Matrix4x4Widget; 34 | 35 | class MatricesWidget : public QWidget 36 | { 37 | Q_OBJECT 38 | 39 | public: 40 | MatricesWidget(QWidget *parent); 41 | ~MatricesWidget(); 42 | 43 | private slots: 44 | void updateModelMatrix(QMatrix4x4 mat); 45 | void updateViewMatrix(QMatrix4x4 mat); 46 | void updateProjectionMatrix(QMatrix4x4 mat); 47 | void updateMatrices(); 48 | 49 | signals: 50 | void modelMatrixChanged(QMatrix4x4 mat); 51 | void viewMatrixChanged(QMatrix4x4 mat); 52 | void projectionMatrixChanged(QMatrix4x4 mat); 53 | void resetMatrices(); 54 | 55 | private: 56 | Ui::MatricesWidget *ui; 57 | QMatrix4x4 m_ModelMatrix; 58 | QMatrix4x4 m_ViewMatrix; 59 | QMatrix4x4 m_ProjectionMatrix; 60 | Matrix4x4Widget* projectionMatrixWidget; 61 | Matrix4x4Widget* viewMatrixWidget; 62 | Matrix4x4Widget* modelMatrixWidget; 63 | }; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /qt/MatricesWidget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MatricesWidget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 961 10 | 739 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 20 | 21 | 0 22 | 0 23 | 24 | 25 | 26 | QFrame::StyledPanel 27 | 28 | 29 | QFrame::Raised 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 0 39 | 0 40 | 41 | 42 | 43 | 44 | 0 45 | 0 46 | 47 | 48 | 49 | Model Matrix 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 0 58 | 0 59 | 60 | 61 | 62 | 63 | 0 64 | 0 65 | 66 | 67 | 68 | Projection Matrix 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 0 81 | 0 82 | 83 | 84 | 85 | 86 | 0 87 | 0 88 | 89 | 90 | 91 | View Matrix 92 | 93 | 94 | 95 | 96 | 97 | 98 | Reset Matrices 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 8 114 | 75 115 | true 116 | 117 | 118 | 119 | please press Enter 120 | or change focus to apply 121 | manual matrix changes! 122 | 123 | 124 | Qt::PlainText 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 40 133 | 50 134 | 135 | 136 | 137 | 138 | 139 | 140 | :/enter/rcs/mono-key-enter.png 141 | 142 | 143 | true 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /qt/Matrix4x4Widget.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "Matrix4x4Widget.h" 27 | #include 28 | 29 | Matrix4x4Widget::Matrix4x4Widget(QWidget *parent) : QWidget(parent), ui(new Ui::matrix4x4Widget) 30 | { 31 | ui->setupUi(this); 32 | m_matrix.setToIdentity(); 33 | 34 | this->setTabKeyboardOrder(); 35 | 36 | connect(ui->a0_0, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 37 | connect(ui->a0_1, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 38 | connect(ui->a0_2, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 39 | connect(ui->a0_3, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 40 | 41 | connect(ui->a1_0, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 42 | connect(ui->a1_1, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 43 | connect(ui->a1_2, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 44 | connect(ui->a1_3, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 45 | 46 | connect(ui->a2_0, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 47 | connect(ui->a2_1, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 48 | connect(ui->a2_2, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 49 | connect(ui->a2_3, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 50 | 51 | connect(ui->a3_0, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 52 | connect(ui->a3_1, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 53 | connect(ui->a3_2, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 54 | connect(ui->a3_3, SIGNAL(editingFinished()), this, SLOT(updateMatrix())); 55 | } 56 | 57 | Matrix4x4Widget::~Matrix4x4Widget() 58 | { 59 | delete ui; 60 | } 61 | 62 | 63 | void Matrix4x4Widget::set3x3() 64 | { 65 | ui->a0_3->setEnabled(false); 66 | ui->a1_3->setEnabled(false); 67 | ui->a2_3->setEnabled(false); 68 | ui->a3_3->setEnabled(false); 69 | 70 | ui->a3_0->setEnabled(false); 71 | ui->a3_1->setEnabled(false); 72 | ui->a3_2->setEnabled(false); 73 | ui->a3_3->setEnabled(false); 74 | } 75 | 76 | void Matrix4x4Widget::set4x4() 77 | { 78 | ui->a0_3->setEnabled(true); 79 | ui->a1_3->setEnabled(true); 80 | ui->a2_3->setEnabled(true); 81 | ui->a3_3->setEnabled(true); 82 | 83 | ui->a3_0->setEnabled(true); 84 | ui->a3_1->setEnabled(true); 85 | ui->a3_2->setEnabled(true); 86 | ui->a3_3->setEnabled(true); 87 | } 88 | 89 | void Matrix4x4Widget::updateSpinBoxes(QMatrix4x4 matrix) 90 | { 91 | //TODO Macro this 92 | try 93 | { 94 | ui->a0_0->setValue(matrix(0, 0)); 95 | ui->a0_1->setValue(matrix(0, 1)); 96 | ui->a0_2->setValue(matrix(0, 2)); 97 | ui->a0_3->setValue(matrix(0, 3)); 98 | 99 | ui->a1_0->setValue(matrix(1, 0)); 100 | ui->a1_1->setValue(matrix(1, 1)); 101 | ui->a1_2->setValue(matrix(1, 2)); 102 | ui->a1_3->setValue(matrix(1, 3)); 103 | 104 | ui->a2_0->setValue(matrix(2, 0)); 105 | ui->a2_1->setValue(matrix(2, 1)); 106 | ui->a2_2->setValue(matrix(2, 2)); 107 | ui->a2_3->setValue(matrix(2, 3)); 108 | 109 | ui->a3_0->setValue(matrix(3, 0)); 110 | ui->a3_1->setValue(matrix(3, 1)); 111 | ui->a3_2->setValue(matrix(3, 2)); 112 | ui->a3_3->setValue(matrix(3, 3)); 113 | m_matrix = matrix; 114 | } 115 | catch (...) 116 | { 117 | qDebug() << "Handling exception not caught in slot."; 118 | } 119 | 120 | //qDebug() << matrix; 121 | } 122 | 123 | void Matrix4x4Widget::updateMatrix() 124 | { 125 | m_matrix.setToIdentity(); 126 | m_matrix(0, 0) = ui->a0_0->value(); 127 | m_matrix(0, 1) = ui->a0_1->value(); 128 | m_matrix(0, 2) = ui->a0_2->value(); 129 | m_matrix(0, 3) = ui->a0_3->value(); 130 | 131 | m_matrix(1, 0) = ui->a1_0->value(); 132 | m_matrix(1, 1) = ui->a1_1->value(); 133 | m_matrix(1, 2) = ui->a1_2->value(); 134 | m_matrix(1, 3) = ui->a1_3->value(); 135 | 136 | m_matrix(2, 0) = ui->a2_0->value(); 137 | m_matrix(2, 1) = ui->a2_1->value(); 138 | m_matrix(2, 2) = ui->a2_2->value(); 139 | m_matrix(2, 3) = ui->a2_3->value(); 140 | 141 | m_matrix(3, 0) = ui->a3_0->value(); 142 | m_matrix(3, 1) = ui->a3_1->value(); 143 | m_matrix(3, 2) = ui->a3_2->value(); 144 | m_matrix(3, 3) = ui->a3_3->value(); 145 | emit(matrixChanged(m_matrix)); 146 | } 147 | 148 | 149 | /** 150 | * Sets the order in which the spin boxes are selected when pressing tab. 151 | * @brief setTabKeyboardOrder 152 | */ 153 | void Matrix4x4Widget::setTabKeyboardOrder() 154 | { 155 | this->setTabOrder(ui->a0_0, ui->a0_1); 156 | this->setTabOrder(ui->a0_1, ui->a0_2); 157 | this->setTabOrder(ui->a0_2, ui->a0_3); 158 | this->setTabOrder(ui->a0_3, ui->a1_0); 159 | 160 | this->setTabOrder(ui->a1_0, ui->a1_1); 161 | this->setTabOrder(ui->a1_1, ui->a1_2); 162 | this->setTabOrder(ui->a1_2, ui->a1_3); 163 | this->setTabOrder(ui->a1_3, ui->a2_0); 164 | 165 | this->setTabOrder(ui->a2_0, ui->a2_1); 166 | this->setTabOrder(ui->a2_1, ui->a2_2); 167 | this->setTabOrder(ui->a2_2, ui->a2_3); 168 | this->setTabOrder(ui->a2_3, ui->a3_0); 169 | 170 | this->setTabOrder(ui->a3_0, ui->a3_1); 171 | this->setTabOrder(ui->a3_1, ui->a3_2); 172 | this->setTabOrder(ui->a3_2, ui->a3_3); 173 | } 174 | 175 | -------------------------------------------------------------------------------- /qt/Matrix4x4Widget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __Matrix4x4Widget_HPP_INCLUDED__ 27 | #define __Matrix4x4Widget_HPP_INCLUDED__ 28 | 29 | #include "ui_Matrix4x4Widget.h" 30 | #include 31 | 32 | class Matrix4x4Widget : public QWidget 33 | { 34 | Q_OBJECT 35 | 36 | public: 37 | Matrix4x4Widget(QWidget *parent = 0); 38 | ~Matrix4x4Widget(); 39 | 40 | /** 41 | * Sets the order in which the spin boxes are selected when pressing tab. 42 | * @brief setTabKeyboardOrder 43 | */ 44 | void setTabKeyboardOrder(); 45 | 46 | public slots: 47 | void updateSpinBoxes(QMatrix4x4 matrix); 48 | void set3x3(); 49 | void set4x4(); 50 | 51 | private slots: 52 | void updateMatrix(); 53 | 54 | signals: 55 | void matrixChanged(QMatrix4x4 matrix); 56 | 57 | private: 58 | Ui::matrix4x4Widget *ui; 59 | QMatrix4x4 m_matrix; 60 | 61 | }; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /qt/Matrix4x4Widget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | matrix4x4Widget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 370 10 | 204 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 10 20 | 20 21 | 348 22 | 162 23 | 24 | 25 | 26 | 27 | 28 | 29 | 3 30 | 31 | 32 | -99.989999999999995 33 | 34 | 35 | 0.001000000000000 36 | 37 | 38 | 39 | 40 | 41 | 42 | 3 43 | 44 | 45 | -99.989999999999995 46 | 47 | 48 | 0.001000000000000 49 | 50 | 51 | 52 | 53 | 54 | 55 | 3 56 | 57 | 58 | -99.989999999999995 59 | 60 | 61 | 0.001000000000000 62 | 63 | 64 | 65 | 66 | 67 | 68 | 3 69 | 70 | 71 | -99.989999999999995 72 | 73 | 74 | 0.001000000000000 75 | 76 | 77 | 78 | 79 | 80 | 81 | 3 82 | 83 | 84 | -99.989999999999995 85 | 86 | 87 | 0.001000000000000 88 | 89 | 90 | 1.000000000000000 91 | 92 | 93 | 94 | 95 | 96 | 97 | 3 98 | 99 | 100 | -99.989999999999995 101 | 102 | 103 | 0.001000000000000 104 | 105 | 106 | 1.000000000000000 107 | 108 | 109 | 110 | 111 | 112 | 113 | 3 114 | 115 | 116 | -99.989999999999995 117 | 118 | 119 | 0.001000000000000 120 | 121 | 122 | 123 | 124 | 125 | 126 | 3 127 | 128 | 129 | -99.989999999999995 130 | 131 | 132 | 0.001000000000000 133 | 134 | 135 | 136 | 137 | 138 | 139 | 3 140 | 141 | 142 | -99.989999999999995 143 | 144 | 145 | 0.001000000000000 146 | 147 | 148 | 149 | 150 | 151 | 152 | 3 153 | 154 | 155 | -99.989999999999995 156 | 157 | 158 | 0.001000000000000 159 | 160 | 161 | 162 | 163 | 164 | 165 | 3 166 | 167 | 168 | -99.989999999999995 169 | 170 | 171 | 0.001000000000000 172 | 173 | 174 | 175 | 176 | 177 | 178 | 3 179 | 180 | 181 | -99.989999999999995 182 | 183 | 184 | 0.001000000000000 185 | 186 | 187 | 1.000000000000000 188 | 189 | 190 | 191 | 192 | 193 | 194 | 3 195 | 196 | 197 | -99.989999999999995 198 | 199 | 200 | 0.001000000000000 201 | 202 | 203 | 204 | 205 | 206 | 207 | 3 208 | 209 | 210 | -99.989999999999995 211 | 212 | 213 | 0.001000000000000 214 | 215 | 216 | 217 | 218 | 219 | 220 | 3 221 | 222 | 223 | -99.989999999999995 224 | 225 | 226 | 0.001000000000000 227 | 228 | 229 | 230 | 231 | 232 | 233 | 3 234 | 235 | 236 | -99.989999999999995 237 | 238 | 239 | 0.001000000000000 240 | 241 | 242 | 1.000000000000000 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | -------------------------------------------------------------------------------- /qt/Vector4Widget.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "Vector4Widget.h" 27 | #include 28 | #include 29 | 30 | Vector4Widget::Vector4Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Vector4Widget) 31 | { 32 | ui->setupUi(this); 33 | 34 | connect(ui->a0_0, SIGNAL(valueChanged(double)), this, SLOT(updateVector(double))); 35 | connect(ui->a0_1, SIGNAL(valueChanged(double)), this, SLOT(updateVector(double))); 36 | connect(ui->a0_2, SIGNAL(valueChanged(double)), this, SLOT(updateVector(double))); 37 | connect(ui->a0_3, SIGNAL(valueChanged(double)), this, SLOT(updateVector(double))); 38 | 39 | } 40 | 41 | Vector4Widget::~Vector4Widget() 42 | { 43 | delete ui; 44 | } 45 | 46 | void Vector4Widget::set2D() 47 | { 48 | ui->a0_0->setEnabled(true); 49 | ui->a0_1->setEnabled(true); 50 | ui->a0_2->setEnabled(false); 51 | ui->a0_3->setEnabled(false); 52 | } 53 | 54 | void Vector4Widget::set3D() 55 | { 56 | ui->a0_0->setEnabled(true); 57 | ui->a0_1->setEnabled(true); 58 | ui->a0_2->setEnabled(true); 59 | ui->a0_3->setEnabled(false); 60 | } 61 | 62 | void Vector4Widget::set4D() 63 | { 64 | ui->a0_0->setEnabled(true); 65 | ui->a0_1->setEnabled(true); 66 | ui->a0_2->setEnabled(true); 67 | ui->a0_3->setEnabled(true); 68 | } 69 | 70 | void Vector4Widget::updateSpinBoxes(QVector4D vector) 71 | { 72 | try 73 | { 74 | ui->a0_0->setValue(vector.x()); 75 | ui->a0_1->setValue(vector.y()); 76 | ui->a0_2->setValue(vector.z()); 77 | ui->a0_3->setValue(vector.w()); 78 | 79 | m_vector = vector; 80 | } 81 | catch (...) 82 | { 83 | qDebug() << "Handling exception not caught in slot."; 84 | } 85 | 86 | } 87 | 88 | void Vector4Widget::updateVector(double val) 89 | { 90 | m_vector.setX(ui->a0_0->value()); 91 | m_vector.setY(ui->a0_1->value()); 92 | m_vector.setZ(ui->a0_2->value()); 93 | m_vector.setW(ui->a0_3->value()); 94 | 95 | emit(vectorChanged(m_vector)); 96 | } 97 | 98 | 99 | -------------------------------------------------------------------------------- /qt/Vector4Widget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __Vector4Widget_HPP_INCLUDED__ 27 | #define __Vector4Widget_HPP_INCLUDED__ 28 | 29 | #include "ui_Vector4Widget.h" 30 | #include 31 | 32 | class Vector4Widget : public QWidget 33 | { 34 | Q_OBJECT 35 | 36 | public: 37 | Vector4Widget(QWidget *parent = 0); 38 | ~Vector4Widget(); 39 | 40 | public slots: 41 | void updateSpinBoxes(QVector4D vector); 42 | void set2D(); 43 | void set3D(); 44 | void set4D(); 45 | 46 | private slots: 47 | void updateVector(double val); 48 | 49 | signals: 50 | void vectorChanged(QVector4D vector); 51 | 52 | private: 53 | Ui::Vector4Widget *ui; 54 | QVector4D m_vector; 55 | 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /qt/Vector4Widget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | Vector4Widget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 790 10 | 429 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 10 20 | 20 21 | 430 22 | 162 23 | 24 | 25 | 26 | 27 | 28 | 29 | 3 30 | 31 | 32 | -99.989999999999995 33 | 34 | 35 | 0.001000000000000 36 | 37 | 38 | 0.000000000000000 39 | 40 | 41 | 42 | 43 | 44 | 45 | 3 46 | 47 | 48 | -99.989999999999995 49 | 50 | 51 | 0.001000000000000 52 | 53 | 54 | 55 | 56 | 57 | 58 | 3 59 | 60 | 61 | -99.989999999999995 62 | 63 | 64 | 0.001000000000000 65 | 66 | 67 | 68 | 69 | 70 | 71 | 3 72 | 73 | 74 | -99.989999999999995 75 | 76 | 77 | 0.001000000000000 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /qt/codeeditor.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | /**************************************************************************** 27 | ** 28 | ** Copyright (C) 2016 The Qt Company Ltd. 29 | ** Contact: https://www.qt.io/licensing/ 30 | ** 31 | ** This file is part of the examples of the Qt Toolkit. 32 | ** 33 | ** $QT_BEGIN_LICENSE:BSD$ 34 | ** Commercial License Usage 35 | ** Licensees holding valid commercial Qt licenses may use this file in 36 | ** accordance with the commercial license agreement provided with the 37 | ** Software or, alternatively, in accordance with the terms contained in 38 | ** a written agreement between you and The Qt Company. For licensing terms 39 | ** and conditions see https://www.qt.io/terms-conditions. For further 40 | ** information use the contact form at https://www.qt.io/contact-us. 41 | ** 42 | ** BSD License Usage 43 | ** Alternatively, you may use this file under the terms of the BSD license 44 | ** as follows: 45 | ** 46 | ** "Redistribution and use in source and binary forms, with or without 47 | ** modification, are permitted provided that the following conditions are 48 | ** met: 49 | ** * Redistributions of source code must retain the above copyright 50 | ** notice, this list of conditions and the following disclaimer. 51 | ** * Redistributions in binary form must reproduce the above copyright 52 | ** notice, this list of conditions and the following disclaimer in 53 | ** the documentation and/or other materials provided with the 54 | ** distribution. 55 | ** * Neither the name of The Qt Company Ltd nor the names of its 56 | ** contributors may be used to endorse or promote products derived 57 | ** from this software without specific prior written permission. 58 | ** 59 | ** 60 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 61 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 62 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 63 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 64 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 65 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 66 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 67 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 68 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 69 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 70 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 71 | ** 72 | ** $QT_END_LICENSE$ 73 | ** 74 | ****************************************************************************/ 75 | 76 | #include 77 | 78 | #include "codeeditor.h" 79 | 80 | 81 | CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) 82 | { 83 | lineNumberArea = new LineNumberArea(this); 84 | 85 | connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); 86 | connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int))); 87 | connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); 88 | 89 | updateLineNumberAreaWidth(0); 90 | highlightCurrentLine(); 91 | } 92 | 93 | 94 | 95 | int CodeEditor::lineNumberAreaWidth() 96 | { 97 | int digits = 1; 98 | int max = qMax(1, blockCount()); 99 | while (max >= 10) { 100 | max /= 10; 101 | ++digits; 102 | } 103 | 104 | int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; 105 | 106 | return space; 107 | } 108 | 109 | 110 | 111 | void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) 112 | { 113 | setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); 114 | } 115 | 116 | 117 | 118 | void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) 119 | { 120 | if (dy) 121 | lineNumberArea->scroll(0, dy); 122 | else 123 | lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); 124 | 125 | if (rect.contains(viewport()->rect())) 126 | updateLineNumberAreaWidth(0); 127 | } 128 | 129 | 130 | 131 | void CodeEditor::resizeEvent(QResizeEvent *e) 132 | { 133 | QPlainTextEdit::resizeEvent(e); 134 | 135 | QRect cr = contentsRect(); 136 | lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); 137 | } 138 | 139 | 140 | 141 | void CodeEditor::highlightCurrentLine() 142 | { 143 | QList extraSelections; 144 | 145 | if (!isReadOnly()) { 146 | QTextEdit::ExtraSelection selection; 147 | 148 | QColor lineColor = QColor(Qt::yellow).lighter(160); 149 | 150 | selection.format.setBackground(lineColor); 151 | selection.format.setProperty(QTextFormat::FullWidthSelection, true); 152 | selection.cursor = textCursor(); 153 | selection.cursor.clearSelection(); 154 | extraSelections.append(selection); 155 | } 156 | 157 | setExtraSelections(extraSelections); 158 | } 159 | 160 | 161 | 162 | void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) 163 | { 164 | QPainter painter(lineNumberArea); 165 | painter.fillRect(event->rect(), Qt::lightGray); 166 | 167 | 168 | QTextBlock block = firstVisibleBlock(); 169 | int blockNumber = block.blockNumber(); 170 | int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top(); 171 | int bottom = top + (int)blockBoundingRect(block).height(); 172 | 173 | while (block.isValid() && top <= event->rect().bottom()) { 174 | if (block.isVisible() && bottom >= event->rect().top()) { 175 | QString number = QString::number(blockNumber + 1); 176 | painter.setPen(Qt::black); 177 | painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), 178 | Qt::AlignRight, number); 179 | } 180 | 181 | block = block.next(); 182 | top = bottom; 183 | bottom = top + (int)blockBoundingRect(block).height(); 184 | ++blockNumber; 185 | } 186 | } -------------------------------------------------------------------------------- /qt/codeeditor.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | /**************************************************************************** 26 | ** 27 | ** Copyright (C) 2016 The Qt Company Ltd. 28 | ** Contact: https://www.qt.io/licensing/ 29 | ** 30 | ** This file is part of the examples of the Qt Toolkit. 31 | ** 32 | ** $QT_BEGIN_LICENSE:BSD$ 33 | ** Commercial License Usage 34 | ** Licensees holding valid commercial Qt licenses may use this file in 35 | ** accordance with the commercial license agreement provided with the 36 | ** Software or, alternatively, in accordance with the terms contained in 37 | ** a written agreement between you and The Qt Company. For licensing terms 38 | ** and conditions see https://www.qt.io/terms-conditions. For further 39 | ** information use the contact form at https://www.qt.io/contact-us. 40 | ** 41 | ** BSD License Usage 42 | ** Alternatively, you may use this file under the terms of the BSD license 43 | ** as follows: 44 | ** 45 | ** "Redistribution and use in source and binary forms, with or without 46 | ** modification, are permitted provided that the following conditions are 47 | ** met: 48 | ** * Redistributions of source code must retain the above copyright 49 | ** notice, this list of conditions and the following disclaimer. 50 | ** * Redistributions in binary form must reproduce the above copyright 51 | ** notice, this list of conditions and the following disclaimer in 52 | ** the documentation and/or other materials provided with the 53 | ** distribution. 54 | ** * Neither the name of The Qt Company Ltd nor the names of its 55 | ** contributors may be used to endorse or promote products derived 56 | ** from this software without specific prior written permission. 57 | ** 58 | ** 59 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 60 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 61 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 62 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 63 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 64 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 65 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 66 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 67 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 68 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 69 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 70 | ** 71 | ** $QT_END_LICENSE$ 72 | ** 73 | ****************************************************************************/ 74 | 75 | #ifndef CODEEDITOR_H 76 | #define CODEEDITOR_H 77 | 78 | #include 79 | #include 80 | 81 | class QPaintEvent; 82 | class QResizeEvent; 83 | class QSize; 84 | class QWidget; 85 | 86 | class LineNumberArea; 87 | 88 | 89 | class CodeEditor : public QPlainTextEdit 90 | { 91 | Q_OBJECT 92 | 93 | public: 94 | CodeEditor(QWidget *parent = 0); 95 | 96 | void lineNumberAreaPaintEvent(QPaintEvent *event); 97 | int lineNumberAreaWidth(); 98 | 99 | protected: 100 | void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; 101 | 102 | private slots: 103 | void updateLineNumberAreaWidth(int newBlockCount); 104 | void highlightCurrentLine(); 105 | void updateLineNumberArea(const QRect &, int); 106 | 107 | private: 108 | QWidget *lineNumberArea; 109 | }; 110 | 111 | 112 | class LineNumberArea : public QWidget 113 | { 114 | public: 115 | LineNumberArea(CodeEditor *editor) : QWidget(editor) { 116 | codeEditor = editor; 117 | } 118 | 119 | QSize sizeHint() const Q_DECL_OVERRIDE { 120 | return QSize(codeEditor->lineNumberAreaWidth(), 0); 121 | } 122 | 123 | protected: 124 | void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE { 125 | codeEditor->lineNumberAreaPaintEvent(event); 126 | } 127 | 128 | private: 129 | CodeEditor *codeEditor; 130 | }; 131 | 132 | #endif -------------------------------------------------------------------------------- /qt/gldisplay.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef GLDISPLAY_H 27 | #define GLDISPLAY_H 28 | 29 | #define FRAMEBUFFER_WIDTH 1280 30 | #define MAX_FPS 60.0 31 | #define INITIAL_CAMERA_Z_POSITION 40.0 32 | 33 | #include "opengl/material.h" 34 | #include "opengl/object.h" 35 | #include "opengl/light.h" 36 | #include "opengl/scene.h" 37 | #include "opengl/framebuffer.h" 38 | #include "opengl/camera.h" 39 | 40 | #include "opengl/openglheaders.h" 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | #include 55 | #include 56 | 57 | #include 58 | #include 59 | #include 60 | #include 61 | 62 | class GLSLEditorWindow; 63 | 64 | using namespace std; 65 | 66 | class GLDisplay : public QOpenGLWidget 67 | { 68 | Q_OBJECT 69 | 70 | public: 71 | GLDisplay(QWidget *parent); 72 | virtual ~GLDisplay(); 73 | 74 | /** 75 | * Sets the opengl parameters (e.g wireframe rendering ...) 76 | * @brief setOpenGLRenderingState 77 | */ 78 | void setOpenGLRenderingState(); 79 | void setOpenGLWireframeState(bool activateWireframeMode); 80 | 81 | /** 82 | * Render the coordinate frame 83 | * @brief renderCoordinateFrame 84 | */ 85 | void renderCoordinateFrame(); 86 | 87 | /** 88 | * Renders the scene to a FBO. 89 | * @brief renderScene 90 | */ 91 | void renderScene(); 92 | 93 | /** 94 | * Renders the textureID on a quad. 95 | * @brief renderToTexture 96 | * @param textureID 97 | */ 98 | void renderToTexture(const int textureID, bool simplifiedPipeline); 99 | 100 | /** 101 | * Function to load textures and the framebuffers. 102 | * @brief loadTexturesAndFramebuffers 103 | */ 104 | void loadTexturesAndFramebuffers(); 105 | 106 | /** 107 | * Sends object properties to shaders. 108 | * @brief sendObjectDataToShaders 109 | * @param object 110 | */ 111 | void sendObjectDataToShaders(Object &object); 112 | 113 | /** 114 | * Counts and draw the FPS on the screen. 115 | * @brief drawFPS 116 | */ 117 | void drawFPS(); 118 | 119 | //test 120 | QGLShaderProgram* getShaderProgram() { return m_shaderProgram; }; 121 | QGLShaderProgram* getShaderDisplayProgram() { return m_shaderProgramDisplay; }; 122 | Scene* getScene() { return m_scene; }; 123 | 124 | signals: 125 | /** 126 | * Appends text to the log tab 127 | * @brief updateLog 128 | */ 129 | void updateLog(QString); 130 | 131 | /** 132 | * Displays the log tab. 133 | * @brief displayLog 134 | */ 135 | void displayLog(); 136 | 137 | /** 138 | * Update the GLInfo tab. 139 | * @brief updateGLInfo 140 | */ 141 | void updateGLInfo(QString); 142 | 143 | void updateModelMatrix(QMatrix4x4); 144 | void updateViewMatrix(QMatrix4x4); 145 | void updateProjectionMatrix(QMatrix4x4); 146 | void updateTexturePath(QString); 147 | 148 | void updateUniformTab(); 149 | void updateMaterialTab(); 150 | 151 | 152 | public slots: 153 | void updateCameraType(QString cameraType); 154 | void updateCameraFieldOfView(double fieldOfView); 155 | void updateObject(QString object); 156 | void updateWireframeRendering(bool wireframe); 157 | void updateBackfaceCulling(bool backface); 158 | void updateRenderCoordinateFrame(bool renderCoordFrame); 159 | void modelMatrixUpdated(QMatrix4x4 modelMatrix); 160 | void viewMatrixUpdated(QMatrix4x4 viewMatrix); 161 | void projectionMatrixUpdated(QMatrix4x4 projectionMatrix); 162 | void takeScreenshot(); 163 | void resetMatrices(); 164 | void updateMaterial(int objectID, Material material); 165 | void linkShaderProgram(); 166 | 167 | /** 168 | * Slot to select a texture file and use it in a shader. 169 | * @brief setTexture 170 | * @param name 171 | */ 172 | void setTexture(QString name, bool isAShaderProgramUniform); 173 | 174 | /** 175 | * Update the openGL window when the timer reaches the end and restarts the timer. 176 | * @brief updateOpenGL 177 | */ 178 | void updateOpenGL(); 179 | 180 | void glMessageLogged(QOpenGLDebugMessage m); 181 | 182 | protected: 183 | void initializeGL(); 184 | void reinitGL(); 185 | void resizeGL(int width, int height); 186 | void paintGL(); 187 | void wheelEvent(QWheelEvent* event); 188 | void mousePressEvent(QMouseEvent *event); 189 | void mouseMoveEvent(QMouseEvent *event); 190 | void keyPressEvent(QKeyEvent *event); 191 | 192 | 193 | private: 194 | void renderText(double x, double y, const QString &str, const QFont & font = QFont()); 195 | 196 | //Framebuffer for highres rendering 197 | FrameBuffer* m_framebuffer; 198 | FrameBuffer* m_framebufferFinalResult; 199 | 200 | //Camera 201 | Camera m_cameraScene; 202 | Camera m_cameraQuad; 203 | 204 | //Mouse events 205 | QVector2D m_mousePos; 206 | 207 | //Frame per second 208 | QTime m_timeFPS; 209 | int m_lastFPSUpdate; 210 | int m_frameCounter; 211 | int m_FPS; 212 | QTimer m_timer; 213 | 214 | //Shaders 215 | QGLShaderProgram* m_shaderProgram; 216 | QGLShaderProgram* m_shaderProgramDisplay; 217 | 218 | //Scene 219 | Scene* m_scene; 220 | 221 | //Textures 222 | QVector m_texturesShaderProgram; 223 | QVector m_textureNamesShaderProgram; 224 | QVector m_texturesDisplayProgram; 225 | QVector m_textureNamesDisplayProgram; 226 | 227 | //Rendering 228 | bool m_wireframe; 229 | bool m_backFaceCulling; 230 | bool m_renderCoordinateFrame; 231 | 232 | //Editor 233 | GLSLEditorWindow* m_shaderEditor; 234 | 235 | QOpenGLFunctions *f; 236 | bool m_shaderProgramNeedsLink; 237 | 238 | Object m_R2Tsquare; 239 | QOpenGLVertexArrayObject m_renderingVAO; 240 | QOpenGLVertexArrayObject m_R2TVAO; 241 | QOpenGLDebugLogger logger; 242 | string m_objectFileName; 243 | }; 244 | 245 | #endif // GLDISPLAY_H 246 | -------------------------------------------------------------------------------- /qt/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "qt/mainwindow.h" 27 | #include "qt/uniformEditorWidget.h" 28 | #include "qt/materialEditorWidget.h" 29 | #include 30 | #include 31 | #include 32 | #include "Vector4Widget.h" 33 | 34 | using namespace std; 35 | 36 | MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) 37 | { 38 | ui->setupUi(this); 39 | //Enable multisampling 40 | QSurfaceFormat format = ui->m_GLWidget->format(); 41 | format.setSamples(16); 42 | ui->m_GLWidget->setFormat(format); 43 | 44 | this->resize(QDesktopWidget().availableGeometry(this).size().width() * 0.5, 45 | QDesktopWidget().availableGeometry(this).size().height() * 0.97); 46 | 47 | QApplication::restoreOverrideCursor(); 48 | QCoreApplication::organizationName() = QString("Imperial College London, Department of Computing"); 49 | QCoreApplication::applicationName() = QString("Computer Graphics GLSL Shader Lab"); 50 | 51 | connect(ui->matricesWidget, SIGNAL(modelMatrixChanged(QMatrix4x4)), ui->m_GLWidget, SLOT(modelMatrixUpdated(QMatrix4x4))); 52 | connect(ui->matricesWidget, SIGNAL(viewMatrixChanged(QMatrix4x4)), ui->m_GLWidget, SLOT(viewMatrixUpdated(QMatrix4x4))); 53 | connect(ui->matricesWidget, SIGNAL(projectionMatrixChanged(QMatrix4x4)), ui->m_GLWidget, SLOT(projectionMatrixUpdated(QMatrix4x4))); 54 | 55 | //TODO define special signal, if shader is valid 56 | connect(ui->m_GLWidget, SIGNAL(updateUniformTab()), this, SLOT(updateUniformTab())); 57 | connect(ui->m_GLWidget, SIGNAL(updateMaterialTab()), this, SLOT(updateMaterialTab())); 58 | } 59 | 60 | MainWindow::~MainWindow() 61 | { 62 | delete ui; 63 | } 64 | 65 | 66 | void MainWindow::showLogTab() 67 | { 68 | //Display the log tab 69 | ui->m_tabWidget->setCurrentIndex(0); 70 | } 71 | 72 | void MainWindow::updateUniformTab() 73 | { 74 | //TODO: this is triggered twice. why? 75 | ui->m_GLWidget->update(); 76 | if (ui->m_uniformsTab->children().length() == 0) //init layout and editor 77 | { 78 | m_uniformEditor = new UniformEditorWidget( 79 | ui->m_GLWidget->getShaderProgram(), ui->m_GLWidget->getShaderDisplayProgram(), 80 | ui->m_GLWidget->context(), this, ui->m_GLWidget); 81 | 82 | QHBoxLayout* uniformHBoxLayout = new QHBoxLayout(ui->m_uniformsTab); 83 | uniformHBoxLayout->addWidget(m_uniformEditor); 84 | 85 | ui->m_uniformsTab->setLayout(uniformHBoxLayout); 86 | 87 | //Update the texture path 88 | connect(m_uniformEditor, SIGNAL(textureBrowse(QString, bool)), ui->m_GLWidget, SLOT(setTexture(QString, bool))); 89 | 90 | connect(ui->m_GLWidget, SIGNAL(updateTexturePath(QString)), m_uniformEditor, SLOT(updateTexturePath(QString))); 91 | 92 | connect(m_uniformEditor, SIGNAL(updateGL()), ui->m_GLWidget, SLOT(update())); 93 | } 94 | else //update widget 95 | { 96 | ui->m_GLWidget->makeCurrent(); 97 | ui->m_GLWidget->update(); 98 | m_uniformEditor->updateShaderPrograms( 99 | ui->m_GLWidget->getShaderProgram(), ui->m_GLWidget->getShaderDisplayProgram(), 100 | ui->m_GLWidget->context()); 101 | } 102 | } 103 | 104 | void MainWindow::updateMaterialTab() 105 | { 106 | QVector objectList = ui->m_GLWidget->getScene()->getObjects(); 107 | ui->m_GLWidget->update(); 108 | 109 | if (ui->m_materialTab->children().length() == 0) //init layout and editor 110 | { 111 | for (int k = 0; k < objectList.size(); k++) 112 | { 113 | MaterialEditorWidget* m_materialEditor = new MaterialEditorWidget(k, ui->m_GLWidget->getScene(), this); 114 | 115 | QHBoxLayout* HBoxLayout = new QHBoxLayout(ui->m_materialTab); 116 | HBoxLayout->addWidget(m_materialEditor); 117 | 118 | ui->m_materialTab->setLayout(HBoxLayout); 119 | 120 | connect(m_materialEditor, SIGNAL(updateGL()), ui->m_GLWidget, SLOT(update())); 121 | m_materialEditors.push_back(m_materialEditor); 122 | 123 | connect(m_materialEditor, SIGNAL(updateMaterial(int, Material)), ui->m_GLWidget, SLOT(updateMaterial(int, Material))); 124 | } 125 | } 126 | else //update widgets 127 | { 128 | for (int k = 0; k < objectList.size(); k++) 129 | { 130 | ui->m_GLWidget->makeCurrent(); 131 | ui->m_GLWidget->update(); 132 | m_materialEditors.at(k)->updateEditor(k); 133 | } 134 | } 135 | 136 | } 137 | 138 | -------------------------------------------------------------------------------- /qt/mainwindow.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef MAINWINDOW_H 27 | #define MAINWINDOW_H 28 | 29 | #include "opengl/openglheaders.h" 30 | #include "qt/gldisplay.h" 31 | #include "qt/texturewidget.h" 32 | #include "ui_mainwindow.h" 33 | 34 | #include 35 | #include 36 | 37 | class UniformEditorWidget; 38 | class MaterialEditorWidget; 39 | 40 | namespace Ui { 41 | class MainWindow; 42 | } 43 | 44 | class MainWindow : public QMainWindow 45 | { 46 | Q_OBJECT 47 | 48 | public: 49 | explicit MainWindow(QWidget *parent = 0); 50 | ~MainWindow(); 51 | 52 | public slots: 53 | 54 | /** 55 | * Shows the log tab when this slot is executed. 56 | * @brief showLogTab 57 | */ 58 | void showLogTab(); 59 | 60 | /** 61 | * Creates the uniform tab from current shader programme. 62 | * @brief createUniformTab 63 | */ 64 | void updateUniformTab(); 65 | 66 | /** 67 | * Creates the material tab from current object. 68 | * @brief createUniformTab 69 | */ 70 | void updateMaterialTab(); 71 | 72 | 73 | private: 74 | Ui::MainWindow *ui; 75 | UniformEditorWidget* m_uniformEditor; 76 | QVector m_materialEditors; 77 | }; 78 | 79 | #endif // MAINWINDOW_H 80 | -------------------------------------------------------------------------------- /qt/materialEditorWidget.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "qt/materialEditorWidget.h" 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include "Vector4Widget.h" 33 | #include "Matrix4x4Widget.h" 34 | #include 35 | #include 36 | 37 | MaterialEditorWidget::MaterialEditorWidget(int objectID, Scene* scene, QWidget *parent) : QWidget(parent), ui(new Ui::MaterialEditor) 38 | { 39 | ui->setupUi(this); 40 | m_scene = scene; 41 | updateEditor(objectID); 42 | } 43 | 44 | MaterialEditorWidget::~MaterialEditorWidget() 45 | { 46 | delete ui; 47 | } 48 | 49 | void MaterialEditorWidget::updateEditor(int objectID) 50 | { 51 | m_objectID = objectID; 52 | 53 | QVector objectList = m_scene->getObjects(); 54 | 55 | Material material = objectList.at(m_objectID).getMaterial(); 56 | QColor ambient = material.getAmbientColor(); 57 | QColor diffuse = material.getDiffuseColor(); 58 | QColor specular = material.getSpecularColor(); 59 | float shininess = material.getShininess(); 60 | float aceoff = material.getAmbientCoefficient(); 61 | float dceoff = material.getDiffuseCoefficient(); 62 | float sceoff = material.getSpecularCoefficient(); 63 | 64 | ui->a0_0->setValue(ambient.redF()); 65 | ui->a0_1->setValue(ambient.greenF()); 66 | ui->a0_2->setValue(ambient.blueF()); 67 | ui->a0_3->setValue(ambient.alphaF()); 68 | 69 | ui->d0_0->setValue(diffuse.redF()); 70 | ui->d0_1->setValue(diffuse.greenF()); 71 | ui->d0_2->setValue(diffuse.blueF()); 72 | ui->d0_3->setValue(diffuse.alphaF()); 73 | 74 | ui->s0_0->setValue(specular.redF()); 75 | ui->s0_1->setValue(specular.greenF()); 76 | ui->s0_2->setValue(specular.blueF()); 77 | ui->s0_3->setValue(specular.alphaF()); 78 | 79 | ui->shininess->setValue(shininess); 80 | 81 | ui->acoeff->setValue(aceoff); 82 | ui->dcoeff->setValue(dceoff); 83 | ui->scoeff->setValue(sceoff); 84 | } 85 | 86 | void MaterialEditorWidget::updateSceneObject() 87 | { 88 | //qDebug() << "updateSceneObject()"; 89 | Material newMaterial = 90 | Material( 91 | QColor::fromRgbF(ui->a0_0->value(), ui->a0_1->value(), ui->a0_2->value(), ui->a0_3->value()), 92 | QColor::fromRgbF(ui->d0_0->value(), ui->d0_1->value(), ui->d0_2->value(), ui->d0_3->value()), 93 | QColor::fromRgbF(ui->s0_0->value(), ui->s0_1->value(), ui->s0_2->value(), ui->s0_3->value()), 94 | ui->acoeff->value(), ui->dcoeff->value(), ui->scoeff->value(), 95 | ui->shininess->value()); 96 | 97 | emit(updateMaterial(m_objectID, newMaterial)); 98 | 99 | } -------------------------------------------------------------------------------- /qt/materialEditorWidget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __materialEditorWidget_HPP_INCLUDED__ 27 | #define __materialEditorWidget_HPP_INCLUDED__ 28 | 29 | #include "opengl/openglheaders.h" 30 | #include "texturewidget.h" 31 | #include "ui_materialEditorWidget.h" 32 | #include 33 | #include 34 | #include "opengl/scene.h" 35 | 36 | class MaterialEditorWidget : public QWidget 37 | { 38 | Q_OBJECT 39 | 40 | public: 41 | MaterialEditorWidget(int objectID, Scene* scene, QWidget *parent); 42 | ~MaterialEditorWidget(); 43 | 44 | void updateEditor(int objectID); 45 | 46 | public slots: 47 | void updateSceneObject(); 48 | 49 | signals: 50 | void updateGL(); 51 | void updateMaterial(int objectID, Material material); 52 | 53 | private: 54 | Scene* m_scene; 55 | int m_objectID; 56 | Ui::MaterialEditor* ui; 57 | }; 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /qt/rcs/mono-key-enter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bkainz/ShaderLabFramework/cd949e4ff08c38a1ad83e8254c5c36ce1792e310/qt/rcs/mono-key-enter.png -------------------------------------------------------------------------------- /qt/rcs/mono-key-enter.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bkainz/ShaderLabFramework/cd949e4ff08c38a1ad83e8254c5c36ce1792e310/qt/rcs/mono-key-enter.xcf -------------------------------------------------------------------------------- /qt/ressources.qrc: -------------------------------------------------------------------------------- 1 | 2 | 3 | rcs/mono-key-enter.png 4 | 5 | 6 | -------------------------------------------------------------------------------- /qt/texturewidget.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #include "qt/texturewidget.h" 27 | 28 | TextureWidget::TextureWidget(QWidget *parent) : QWidget(parent) 29 | { 30 | 31 | //Create a group box with a line edit and a push button 32 | m_groupBox = new QGroupBox("Texture"); 33 | m_lineEdit = new QLineEdit(); 34 | m_pushButton = new QPushButton("Browse"); 35 | 36 | //Put the line edit and the button in a layout 37 | QGridLayout *gridLayout = new QGridLayout(); 38 | gridLayout->addWidget(m_lineEdit, 0, 0); 39 | gridLayout->addWidget(m_pushButton, 0, 1); 40 | 41 | //Set the group box layout 42 | m_groupBox->setLayout(gridLayout); 43 | 44 | //Add the group box to a vertical layout 45 | //add a stretch after the last widget to get a group box of a correct size. 46 | m_verticalLayout = new QVBoxLayout(this); 47 | m_verticalLayout->addWidget(m_groupBox); 48 | 49 | m_verticalLayout->addStretch(1); 50 | 51 | //Connections 52 | connect(m_pushButton, SIGNAL(clicked()), this, SLOT(buttonClicked2())); 53 | 54 | } 55 | 56 | TextureWidget::TextureWidget(QWidget *parent, QString textureName, bool isAShaderProgramUniform) : 57 | QWidget(parent), m_textureName(textureName), m_isAShaderProgramUniform(isAShaderProgramUniform) 58 | { 59 | //Create a group box with a line edit and a push button 60 | m_groupBox = new QGroupBox("Texture"); 61 | m_lineEdit = new QLineEdit(); 62 | m_pushButton = new QPushButton("Browse"); 63 | 64 | //Put the line edit and the button in a layout 65 | QGridLayout *gridLayout = new QGridLayout(); 66 | gridLayout->addWidget(m_lineEdit, 0, 0); 67 | gridLayout->addWidget(m_pushButton, 0, 1); 68 | 69 | //Set the group box layout 70 | m_groupBox->setLayout(gridLayout); 71 | 72 | //Add the group box to a vertical layout 73 | //add a stretch after the last widget to get a group box of a correct size. 74 | m_verticalLayout = new QVBoxLayout(this); 75 | m_verticalLayout->addWidget(m_groupBox); 76 | 77 | m_verticalLayout->addStretch(1); 78 | 79 | //Connections 80 | connect(m_pushButton, SIGNAL(clicked()), this, SLOT(buttonClicked())); 81 | } 82 | 83 | TextureWidget::~TextureWidget() 84 | { 85 | delete m_groupBox; 86 | } 87 | 88 | void TextureWidget::setTextureIcon(QString& filePath) 89 | { 90 | QIcon icon = QIcon(QPixmap(filePath)); 91 | m_pushButton->setIcon(icon); 92 | m_pushButton->setText(""); 93 | } 94 | 95 | void TextureWidget::updatePath(QString path) 96 | { 97 | m_lineEdit->setText(path); 98 | this->setTextureIcon(path); 99 | } 100 | 101 | void TextureWidget::buttonClicked() 102 | { 103 | emit sendTextureName(m_textureName, m_isAShaderProgramUniform); 104 | } 105 | 106 | -------------------------------------------------------------------------------- /qt/texturewidget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef TEXTUREWIDGET_H 27 | #define TEXTUREWIDGET_H 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "uniformEditorWidget.h" 38 | 39 | class TextureWidget : public QWidget 40 | { 41 | Q_OBJECT 42 | public: 43 | explicit TextureWidget(QWidget *parent = 0); 44 | TextureWidget(QWidget *parent, QString textureName, bool isAShaderProgramUniform); 45 | ~TextureWidget(); 46 | void setTextureIcon(QString &filePath); 47 | 48 | signals: 49 | void textUpdated(QString); 50 | void sendTextureName(QString, bool); 51 | 52 | public slots: 53 | void updatePath(QString path); 54 | void buttonClicked(); 55 | 56 | private: 57 | QVBoxLayout *m_verticalLayout; 58 | QGroupBox *m_groupBox; 59 | QLineEdit *m_lineEdit; 60 | QPushButton *m_pushButton; 61 | QString m_textureName; 62 | bool m_isAShaderProgramUniform; 63 | }; 64 | 65 | #endif // TEXTUREWIDGET_H 66 | -------------------------------------------------------------------------------- /qt/uniformEditorWidget.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * This is the Computer Graphics Shader Lab. 3 | * 4 | * Copyright (c) 2016 Bernhard Kainz, Antoine S Toisoul 5 | * (b.kainz@imperial.ac.uk, antoine.toisoul13@imperial.ac.uk) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 | * IN THE SOFTWARE. 24 | ****************************************************************************/ 25 | 26 | #ifndef __uniformEditorWidget_HPP_INCLUDED__ 27 | #define __uniformEditorWidget_HPP_INCLUDED__ 28 | 29 | #include "opengl/openglheaders.h" 30 | #include "texturewidget.h" 31 | #include "ui_uniformEditorWidget.h" 32 | #include 33 | #include 34 | #include 35 | 36 | class UniformEditorWidget : public QWidget 37 | { 38 | Q_OBJECT 39 | 40 | public: 41 | UniformEditorWidget(QGLShaderProgram* sProgram, QGLShaderProgram* dsProgram, 42 | QOpenGLContext* glContext, QWidget *parent, QOpenGLWidget* glWidget); 43 | ~UniformEditorWidget(); 44 | 45 | //enum uniformType { BOOL, INT, UINT, FLOAT, DOUBLE, VEC2, VEC3, VEC4, MAT3, MAT4, SAMPLER}; 46 | struct mUniform 47 | { 48 | QString type; 49 | QString name; 50 | }; 51 | 52 | void updateShaderPrograms(QGLShaderProgram* sProgram, QGLShaderProgram* dsProgram, QOpenGLContext* glContext); 53 | 54 | //TODO documentation 55 | 56 | // it would have been nice to read the uniforms directly from the program 57 | // but this seems to be broken in Qt 5 58 | // need to parse source code manually 59 | // Qt has some restirctions here, thus this is just a simple Uniform editor 60 | 61 | /** 62 | * Creates the widgets that are necessary in the uniform editor depending on the type of uniform. 63 | * index must be a value relative to the uniform list and NOT the QComboBox of the uniform editor. 64 | * The uniforms are also bound to the correct program shader : the render program shader or the display program (Render to Texture). 65 | * Sets the connection between a uniform widget and the updateUniform SLOT. 66 | * @brief setUniformEditorWidgets 67 | * @param uniformList 68 | * @param index 69 | * @param isAShaderProgramUniform 70 | */ 71 | void setUniformEditorWidgets(QList &uniformList, int index, bool isAShaderProgramUniform); 72 | 73 | public slots: 74 | void uniformComboBoxActivated(int index); 75 | 76 | //This could probably be simplified to take into account the two shader programs 77 | //TODO any way to templetize this (GLtype is used for overloads)? 78 | //Qt does not allow templates for slots 79 | void updateUniform(double value); 80 | void updateUniform(int value); 81 | void updateUniformVector2D(QVector4D value); 82 | void updateUniformVector3D(QVector4D value); 83 | void updateUniformVector4D(QVector4D value); 84 | void updateUniformMatrix3x3(QMatrix4x4 value); 85 | void updateUniformMatrix4x4(QMatrix4x4 value); 86 | 87 | void updateUniformDisplay(double value); 88 | void updateUniformDisplay(int value); 89 | void updateUniformDisplayVector2D(QVector4D value); 90 | void updateUniformDisplayVector3D(QVector4D value); 91 | void updateUniformDisplayVector4D(QVector4D value); 92 | void updateUniformDisplayMatrix3x3(QMatrix4x4 value); 93 | void updateUniformDisplayMatrix4x4(QMatrix4x4 value); 94 | 95 | /** 96 | * Update texture path for a sampler2D. 97 | * @brief updateTexturePath 98 | * @param filePath 99 | */ 100 | void updateTexturePath(QString filePath); 101 | 102 | signals: 103 | void updateGL(); 104 | 105 | /** 106 | * Sends a signal to open a QFileDialog to look for the texture. 107 | * The texture name is the first parameter and the second parameter 108 | * is true if the texture has to be bound to the shader program or false if it has to be bound to the Render To Texture display program. 109 | * @brief textureBrowse 110 | */ 111 | void textureBrowse(QString, bool); 112 | 113 | void updateShaderProgram(); 114 | void updateUniformsSignal(); 115 | 116 | private: 117 | void updateEditorWidget(); 118 | QList parseUniformsFromSource(QString sourceCode); 119 | 120 | Ui::UniformEditorWidget* ui; 121 | QOpenGLWidget* m_glWidget; 122 | QGLShaderProgram* m_shaderProgram; 123 | QGLShaderProgram* m_shaderProgramDisplay; 124 | QList m_shaderProgramUserUniforms; //List of uniforms in the shader program 125 | QList m_displayShaderUserUniforms; //List of uniforms in the display shader program 126 | QOpenGLContext* m_glContext; 127 | }; 128 | 129 | #endif 130 | -------------------------------------------------------------------------------- /qt/uniformEditorWidget.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | UniformEditorWidget 4 | 5 | 6 | 7 | 0 8 | 0 9 | 733 10 | 611 11 | 12 | 13 | 14 | Form 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | QFrame::StyledPanel 24 | 25 | 26 | QFrame::Raised 27 | 28 | 29 | 30 | 31 | 32 | Qt::Vertical 33 | 34 | 35 | 36 | 20 37 | 40 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /textures/normalMap.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bkainz/ShaderLabFramework/cd949e4ff08c38a1ad83e8254c5c36ce1792e310/textures/normalMap.jpg -------------------------------------------------------------------------------- /textures/texture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bkainz/ShaderLabFramework/cd949e4ff08c38a1ad83e8254c5c36ce1792e310/textures/texture.jpg --------------------------------------------------------------------------------