Compare commits

...

7 Commits

Author SHA1 Message Date
Shaun Reed 0f372f71f4 Add CI for assimp target build flag
+ Update README
2022-08-21 18:07:56 -04:00
Shaun Reed ad4d4636a4 Fix integration with Qt Creator
+ CMake build flag `QTK_UPDATE_SUBMODULES` to include Assimp as a git submodule
2022-08-21 18:03:04 -04:00
Shaun Reed 86b3f78368 Use configure_file to set QTK_RESOURCES 2022-08-21 18:03:04 -04:00
Shaun Reed 44d9092306 Use git submodule for assimp 2022-08-21 18:03:04 -04:00
Shaun Reed f3406ee749 Add assimp as external dependency 2022-08-21 18:03:04 -04:00
Shaun Reed 249a2b4446 Work on separating widget from main application
+ Thanks @OgreTransporter for help in #4!
+ Closes #4
2022-08-21 18:02:53 -04:00
Shaun Reed 432bf9919c Work on Windows CI 2022-08-06 15:18:55 -04:00
42 changed files with 1179 additions and 823 deletions

View File

@ -10,12 +10,14 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
CMAKE_PARAMS: -DCMAKE_PREFIX_PATH=/home/runner/work/qtk/Qt/6.3.1/gcc_64/
cmake: -DCMAKE_PREFIX_PATH="/home/runner/work/qtk/Qt/6.3.1/gcc_64/"
- os: windows-latest
cmake: -DCMAKE_PREFIX_PATH="D:/a/qtk/qtk/Qt/6.3.1/mingw81_64/"
- os: macos-latest
CMAKE_PARAMS: -DCMAKE_PREFIX_PATH=/home/runner/work/qtk/Qt/6.3.1/gcc_64/ -DASSIMP_NEW_INTERFACE=on
cmake: -DCMAKE_PREFIX_PATH="/home/runner/work/qtk/Qt/6.3.1/gcc_64/"
runs-on: ${{ matrix.os }}
steps:
@ -26,11 +28,37 @@ jobs:
with:
version: '6.3.1'
- name: Install Assimp Linux
if: matrix.os == 'ubuntu-latest'
- name: Chocolatey Action
if: matrix.os == 'windows-latest'
uses: crazy-max/ghaction-chocolatey@v2.0.0
with:
args: install pkgconfiglite
- name: Build Qtk
shell: bash
run: |
sudo apt install libassimp-dev -y
cmake -S . -B build/ ${{ matrix.cmake }} && cmake --build build/ \
--target qtk-main
Build-Qtk-Assimp-Targets:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
include:
- os: ubuntu-latest
cmake: -DCMAKE_PREFIX_PATH="/home/runner/work/qtk/Qt/6.3.1/gcc_64/" -DQTK_UPDATE_SUBMODULES=OFF
- os: macos-latest
cmake: -DCMAKE_PREFIX_PATH="/home/runner/work/qtk/Qt/6.3.1/gcc_64/" -DASSIMP_NEW_INTERFACE=ON -DQTK_UPDATE_SUBMODULES=OFF
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Install Qt
uses: jurplel/install-qt-action@v2
with:
version: '6.3.1'
- name: Install Assimp MacOS
if: matrix.os == 'macos-latest'
@ -38,8 +66,14 @@ jobs:
run: |
brew install assimp
- name: Install Assimp Ubuntu
if: matrix.os == 'ubuntu-latest'
shell: bash
run: |
sudo apt install libassimp-dev
- name: Build Qtk
shell: bash
run: |
mkdir build && cd build
cmake .. ${{ matrix.CMAKE_PARAMS }} && cmake --build .
cmake -S . -B build/ ${{ matrix.cmake }} && cmake --build build/ \
--target qtk-main

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "extern/assimp/assimp"]
path = extern/assimp/assimp
url = https://github.com/assimp/assimp.git

View File

@ -3,7 +3,7 @@
## ##
## Project for working with OpenGL and Qt6 widgets ##
################################################################################
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.2)
project(
#[[NAME]] Qtk
@ -16,72 +16,212 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# For CLion builds, point CMAKE_PREFIX_PATH to Qt6 install directory
list(APPEND CMAKE_PREFIX_PATH $ENV{HOME}/Code/Clones/Qt/6.3.1/gcc_64/)
find_package(Qt6 COMPONENTS OpenGLWidgets)
if (NOT Qt6_FOUND)
message(SEND_ERROR "Unable to find Qt6 at CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
message(FATAL_ERROR "Specify path to Qt6 with `cmake -DCMAKE_PREFIX_PATH=/path/to/Qt/6.x.x/gcc_64 -S /path/to/qtk -B /path/to/qtk/build && cmake --build /path/to/qtk/build -j $(nprocs)`")
if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
add_compile_options(/wd4131 /wd4127)
endif()
message(STATUS "[Qtk] Compiling with ${CMAKE_CXX_COMPILER_ID}")
# Qtk build options
option(QTK_DEBUG "Enable debugger" ON)
message(STATUS "[Qtk] Compiling with QTK_DEBUG=${QTK_DEBUG}")
option(QTK_UPDATE_SUBMODULES "Update external project (assimp) submodule" ON)
message(
STATUS
"[Qtk] Compiling with QTK_UPDATE_SUBMODULES=${QTK_UPDATE_SUBMODULES}"
)
# Qt options
set(QT_DIR "$ENV{HOME}/Code/Clones/Qt/6.3.1/gcc_64/" CACHE PATH "Path to Qt6")
# Options for bringing your own assimp installation; Otherwise not needed
# + If assimp is available system-wide we can just set QTK_UPDATE_SUBMODULES OFF
option(ASSIMP_NEW_INTERFACE "Use the assimp::assimp interface (WIN / OSX)" OFF)
message(
STATUS
"[Qtk] Compiling with ASSIMP_NEW_INTERFACE=${ASSIMP_NEW_INTERFACE}"
)
################################################################################
# External Libraries
################################################################################
# https://github.com/assimp/assimp/commit/6ac8279977c3a54118551e549d77329497116f66
# For CLion builds, point CMAKE_PREFIX_PATH to Qt6 install directory
# + QtCreator will handle this for you if that is used instead
list(APPEND CMAKE_PREFIX_PATH "${QT_DIR}")
# Find Qt
find_package(Qt6 COMPONENTS OpenGLWidgets)
if (NOT Qt6_FOUND)
message(
SEND_ERROR "[Qtk] Error: Unable to find Qt6 at CMAKE_PREFIX_PATH: "
"${CMAKE_PREFIX_PATH}"
)
message(
FATAL_ERROR
"[Qtk] Error: Specify path to Qt6 with `cmake "
"-DCMAKE_PREFIX_PATH=/path/to/Qt/6.x.x/gcc_64 -S /path/to/qtk -B "
"/path/to/qtk/build && cmake --build /path/to/qtk/build -j $(nprocs)`"
)
endif()
if (QTK_UPDATE_SUBMODULES)
message(STATUS "[Qtk] Updating submodules...")
include("${CMAKE_SOURCE_DIR}/cmake/include/git_submodule.cmake")
submodule_update("${CMAKE_CURRENT_SOURCE_DIR}/extern/assimp/assimp/")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/extern/assimp/assimp/")
else()
find_package(assimp REQUIRED)
option(ASSIMP_NEW_INTERFACE "Use assimp::assimp as target instead of assimp" OFF)
endif()
################################################################################
# Qtk
################################################################################
set(
PUBLIC_HEADERS
src/qtkwidget.h
src/abstractscene.h
src/camera3d.h
src/mesh.h
src/meshrenderer.h
src/model.h
src/object.h
src/skybox.h
src/texture.h
src/transform3D.h
)
set(
SOURCE_FILES
src/qtkwidget.cpp
src/abstractscene.cpp
src/camera3d.cpp
src/input.cpp
src/input.h
src/mesh.cpp
src/meshrenderer.cpp
src/model.cpp
src/object.cpp
src/qtkapi.h
src/skybox.cpp
src/texture.cpp
src/transform3D.cpp
)
include(GenerateExportHeader)
add_library(qtk-widget STATIC ${PUBLIC_HEADERS} ${SOURCE_FILES})
target_include_directories(qtk-widget PRIVATE src/ app/)
set_target_properties(qtk-widget PROPERTIES
PUBLIC_HEADER "${PUBLIC_HEADERS}"
VERSION ${PROJECT_VERSION}
)
target_link_libraries(qtk-widget PUBLIC Qt6::OpenGLWidgets)
target_link_libraries(qtk-widget PUBLIC Qt6::Widgets)
if (QTK_UPDATE_SUBMODULES OR NOT ASSIMP_NEW_INTERFACE)
target_link_libraries(qtk-widget PUBLIC assimp)
elseif(ASSIMP_NEW_INTERFACE)
target_link_libraries(qtk-widget PUBLIC assimp::assimp)
endif()
if(QTK_DEBUG)
message(STATUS "[Qtk] Building with QTK_DEBUG=${QTK_DEBUG}")
target_compile_definitions(qtk-widget PUBLIC QTK_DEBUG)
endif()
if(WIN32)
find_package(OpenGL REQUIRED)
target_link_libraries(qtk-widget PUBLIC OpenGL::GL)
endif()
# Install files
install(TARGETS qtk-widget
# Associate qtk-widget target with qtk-export
EXPORT qtk-export
# <prefix>/bin on DLL systems and <prefix>/lib on non-dll systems
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include
)
# Install export
# qtkTargets.cmake will only be installed when one of the CONFIGURATIONS is installed
# + The generated import will only reference that qtk configuration
install(EXPORT qtk-export
FILE qtkTargets.cmake
CONFIGURATIONS Debug|Release
DESTINATION ${CMAKE_INSTALL_PREFIX}/cmake
)
################################################################################
# Final Application
################################################################################
# Add our Qt resources.qrc file to our application
set(SOURCES app/main.cpp)
qt6_add_big_resources(SOURCES resources.qrc)
qt_add_executable(qtk ${SOURCES})
set(SOURCES
src/mainwidget.cpp src/mainwidget.h
src/mainwindow.cpp src/mainwindow.h src/mainwindow.ui
src/input.cpp src/input.h
src/mesh.cpp src/mesh.h
src/texture.cpp src/texture.h
src/object.cpp src/object.h
src/meshrenderer.cpp src/meshrenderer.h
src/camera3d.cpp src/camera3d.h
src/skybox.cpp src/skybox.h
src/transform3D.cpp src/transform3D.h
src/model.cpp src/model.h
src/scene.cpp src/scene.h
src/resourcemanager.cpp src/resourcemanager.h
set(QTK_RESOURCES "${CMAKE_SOURCE_DIR}/resources")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/src/qtkresources.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/src/qtkresources.h"
@ONLY
)
qt_add_library(main-widget STATIC ${SOURCES})
target_include_directories(main-widget PUBLIC src/)
if(ASSIMP_NEW_INTERFACE)
target_link_libraries(main-widget PRIVATE assimp::assimp)
else()
target_link_libraries(main-widget PRIVATE assimp)
endif()
target_link_libraries(main-widget PUBLIC Qt6::OpenGLWidgets)
if(WIN32)
find_package(OpenGL REQUIRED)
target_link_libraries(main-widget PUBLIC OpenGL::GL)
endif()
# Add our Qt resources.qrc file to our application
set(QTK_APP_SOURCES app/main.cpp
app/examplescene.cpp app/examplescene.h
app/mainwindow.cpp app/mainwindow.h app/mainwindow.ui
app/resourcemanager.h
src/qtkresources.h.in
)
qt6_add_big_resources(QTK_APP_SOURCES resources.qrc)
target_link_libraries(qtk PUBLIC main-widget)
qt_add_executable(qtk-main ${QTK_APP_SOURCES})
target_include_directories(qtk-main PRIVATE src/ app/)
# Link qtk executable to main main-widget library
set_target_properties(qtk PROPERTIES
# Link qtk-main executable to main qtk-widget library
target_link_libraries(qtk-main PUBLIC qtk-widget)
set_target_properties(qtk-main PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
)
install(TARGETS qtk-main
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
generate_export_header(main-widget)
generate_export_header(qtk-widget)
if(WIN32)
get_target_property(_qt6_qmake_location Qt6::qmake IMPORTED_LOCATION)
execute_process(COMMAND "${_qt6_qmake_location}" -query QT_INSTALL_PREFIX RESULT_VARIABLE return_code OUTPUT_VARIABLE qt6_install_prefix OUTPUT_STRIP_TRAILING_WHITESPACE)
file(TO_NATIVE_PATH "${qt6_install_prefix}/bin" qt6_install_prefix)
if(TARGET Qt6::windeployqt)
add_custom_command(TARGET qtk-main
POST_BUILD
COMMAND set PATH=%PATH%$<SEMICOLON>${qt6_install_prefix}
COMMAND Qt6::windeployqt --dir "${CMAKE_BINARY_DIR}/windeployqt" "$<TARGET_FILE_DIR:qtk-main>/$<TARGET_FILE_NAME:qtk-main>"
)
install(DIRECTORY "${CMAKE_BINARY_DIR}/windeployqt/" DESTINATION bin)
endif()
if(MSVC AND TARGET Qt6::qmake)
set(VSUSER_FILE ${CMAKE_CURRENT_BINARY_DIR}/qtk-main.vcxproj.user)
file(TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/extern/assimp/assimp/bin" assimp_bin)
file(WRITE ${VSUSER_FILE} "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file(APPEND ${VSUSER_FILE} "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n")
file(APPEND ${VSUSER_FILE} " <PropertyGroup>\n")
file(APPEND ${VSUSER_FILE} " <LocalDebuggerEnvironment>Path=$(SolutionDir)\\lib\\$(Configuration);${qt6_install_prefix};${assimp_bin};$(Path)\n")
file(APPEND ${VSUSER_FILE} "$(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>\n")
file(APPEND ${VSUSER_FILE} " <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>\n")
file(APPEND ${VSUSER_FILE} " </PropertyGroup>\n")
file(APPEND ${VSUSER_FILE} "</Project>\n")
endif()
endif()

View File

@ -3,18 +3,18 @@
Practice project for learning about using OpenGL in Qt widget applications.
Model loader using [Assimp](https://assimp.org/) within a Qt widget application.
You can import your own models within `mainwdget.cpp`, inside the
`MainWidget::initObjects()` function. I've commented throughout the code there
to explain which model or example I'm modifying. Rotations and translations
happen in `MainWidget::update()`, to get textures loading on models look into
[material files](http://www.paulbourke.net/dataformats/mtl/) and see some
examples in the `resources/models/` directory. For more in-depth examples, see
`scene.h` and `scene.cpp`
You can import your own models within `app/examplescene.cpp`, inside the
`ExampleScene::init()` function. Rotations and translations
happen in `ExampleScene::update()`.
Can be built with cmake manually or using
[Qt Creator](https://github.com/qt-creator/qt-creator).
For the build to be successful, I've found through testing on VMs that the system requires around 6GB of RAM.
This is mostly due to the large .obj files that are built into the project using [Qt Resource System](https://doc.qt.io/qt-6/resources.html)
To get textures loading on models look into [material files](http://www.paulbourke.net/dataformats/mtl/)
and see some examples in the `resources/models/` directory.
### Source Builds
Builds are configured for CLion or [Qt Creator](https://github.com/qt-creator/qt-creator).
Simply open the root `CMakeLists.txt` with either of these editors and configurations will be loaded.
This project has been ported to Qt6, which is not yet available in Ubuntu apt repositories.
To run this project, you will *need* to install [Qt6 Open Source Binaries](https://www.qt.io/download-qt-installer) for your system.
@ -24,10 +24,32 @@ Once Qt6 is installed, to build and run `qtk` on Ubuntu -
```bash
sudo apt update -y && sudo apt install freeglut3-dev libassimp-dev cmake build-essential git
git clone https://gitlab.com/shaunrd0/qtk
cmake -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64 -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nprocs)
./qtk/build/qtk
cmake -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64 -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
./qtk/build/qtk-main
```
By default, the build will initialize Assimp as a git submodule and build from source.
We can turn this off by setting the `-DQTK_UPDATE_SUBMODULES=OFF` flag when running CMake.
This will greatly increase build speed, but we will need to make sure Assimp is available either system-wide or using a custom `CMAKE_PREFIX_PATH`.
Using `-DQTK_UPDATE_SUBMODULES=ON` supports providing assimp on cross-platform builds (Windows / Mac / Linux) and may be easier to configure.
```bash
sudo apt update -y && sudo apt install freeglut3-dev libassimp-dev cmake build-essential git
git clone https://gitlab.com/shaunrd0/qtk
cmake -DQTK_UPDATE_SUBMODULES=OFF -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64 -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
# We can also provide a path to assimp -
#cmake -DQTK_UPDATE_SUBMODULES=OFF -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64;/path/to/assimp/ -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
./qtk/build/qtk-main
```
If you are building on **Windows / Mac** and bringing your own installation of Assimp, consider setting the `-DASSIMP_NEW_INTERFACE` build flag.
```bash
cmake -DASSIMP_NEW_INTERFACE=ON -DQTK_UPDATE_SUBMODULES=OFF -DCMAKE_PREFIX_PATH=$HOME/Qt/6.3.1/gcc_64;/path/to/assimp/ -S qtk/ -B qtk/build/ && cmake --build qtk/build/ -j $(nproc --ignore=2) --target qtk-main
```
### Controls
You can fly around the scene if you hold the right mouse button and use WASD.
If you see a small triangle floating by a model it represents the light source
that is being used for the shader rendering the model. These appear on models
@ -43,8 +65,30 @@ Spartan with normals -
![](resources/spartan-normals.png)
## Model Artists
### QtkWidget in Qt Creator
The `QtkWidget` class is exported as a shared library for use in Qt Creator's design mode.
We can add more QtkWidgets to view and render the scene from multiple perspectives.
There is still some work to be done here, so there isn't a builtin way to add an additional view within the application.
![](resources/qtk-views.png)
After building Qtk, we can drag and drop an `OpenGL Widget` onto the `mainwindow.ui`.
Then right-click the new OpenGLWidget and `Promote To->QtkWidget` to add a second view.
![](resources/qtk-views-setup.png)
If we demote or delete all widgets in `mainwindow.ui` and rebuild the project, Qt Creator will drop `QtkWidget` from the list of possible promoted widgets.
Add an `OpenGL Widget` to the UI, right-click it and navigate to `Promote Widget...` and enter the information below.
![](resources/qtk-reference.png)
After you fill out the `New Promoted Class` form, click `Add` *and*`Promote`, then rebuild.
After following these steps Qt Creator will list `QtkWidget` as an option to promote `OpenGL Widgets` again.
## Model Artists
"Alien Hominid" (https://skfb.ly/onStx) by Nwilly_art is licensed under Creative Commons Attribution (http://creativecommons.org/licenses/by/4.0/).
@ -59,4 +103,3 @@ Modified (learnopengl.com) material assignment (Joey de Vries) for easier load i
"Terror-bird (NHMW-Geo 2012/0007/0001)" (https://skfb.ly/onAWy) by Natural History Museum Vienna is licensed under Creative Commons Attribution-NonCommercial (http://creativecommons.org/licenses/by-nc/4.0/).
"Golden Lion Sitting OBJ Low Poly FREE" (https://skfb.ly/onZAH) by LordSamueliSolo is licensed under Creative Commons Attribution (http://creativecommons.org/licenses/by/4.0/).

View File

@ -6,32 +6,27 @@
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#include <abstractscene.h>
#include <camera3d.h>
#include <texture.h>
#include <meshrenderer.h>
#include <model.h>
#include <resourcemanager.h>
#include <examplescene.h>
#include <texture.h>
#include <scene.h>
Camera3D Scene::mCamera;
QMatrix4x4 Scene::mProjection;
using namespace Qtk;
/*******************************************************************************
* Constructors, Destructors
******************************************************************************/
Scene::Scene()
ExampleScene::ExampleScene()
{
mCamera.transform().setTranslation(0.0f, 0.0f, 20.0f);
mCamera.transform().setRotation(-5.0f, 0.0f, 1.0f, 0.0f);
init();
Camera().transform().setTranslation(0.0f, 0.0f, 20.0f);
Camera().transform().setRotation(-5.0f, 0.0f, 1.0f, 0.0f);
}
Scene::~Scene()
ExampleScene::~ExampleScene()
{
delete mTestPhong;
delete mTestSpecular;
@ -39,6 +34,7 @@ Scene::~Scene()
delete mTestAmbient;
for (auto & mesh : mMeshes) delete mesh;
for (auto & model : mModels) delete model;
delete mSkybox;
}
@ -46,10 +42,13 @@ Scene::~Scene()
* Public Member Functions
******************************************************************************/
void Scene::init()
void ExampleScene::init()
{
Qtk::Skybox * sb = new Qtk::Skybox("Skybox");
setSkybox(sb);
// Initialize Phong example cube
mTestPhong = new MeshRenderer("phong", Cube());
mTestPhong = new Qtk::MeshRenderer("phong", Qtk::Cube());
mTestPhong->mTransform.setTranslation(3.0f, 0.0f, -2.0f);
mTestPhong->setShaders(":/solid-phong.vert", ":/solid-phong.frag");
mTestPhong->init();
@ -76,7 +75,7 @@ void Scene::init()
// Initialize Ambient example cube
mTestAmbient = new MeshRenderer("ambient", Cube());
mTestAmbient = new Qtk::MeshRenderer("ambient", Cube());
mTestAmbient->mTransform.setTranslation(7.0f, 0.0f, -2.0f);
mTestAmbient->setShaders(":/solid-ambient.vert", ":/solid-ambient.frag");
mTestAmbient->init();
@ -100,7 +99,7 @@ void Scene::init()
mTestAmbient->mProgram.release();
// Initialize Diffuse example cube
mTestDiffuse = new MeshRenderer("diffuse", Cube());
mTestDiffuse = new Qtk::MeshRenderer("diffuse", Cube());
mTestDiffuse->mTransform.setTranslation(9.0f, 0.0f, -2.0f);
mTestDiffuse->setShaders(":/solid-diffuse.vert", ":/solid-diffuse.frag");
mTestDiffuse->init();
@ -124,7 +123,7 @@ void Scene::init()
mTestDiffuse->mProgram.release();
// Initialize Specular example cube
mTestSpecular = new MeshRenderer("specular", Cube());
mTestSpecular = new Qtk::MeshRenderer("specular", Cube());
mTestSpecular->mTransform.setTranslation(11.0f, 0.0f, -2.0f);
mTestSpecular->setShaders(":/solid-specular.vert", ":/solid-specular.frag");
mTestSpecular->init();
@ -152,31 +151,31 @@ void Scene::init()
//
// Model loading
mModels.push_back(new Model("backpack", ":/models/backpack/backpack.obj"));
mModels.push_back(new Qtk::Model("backpack", ":/models/backpack/backpack.obj"));
// Sometimes model textures need flipped in certain directions
mModels.back()->flipTexture("diffuse.jpg", false, true);
mModels.back()->mTransform.setTranslation(0.0f, 0.0f, -10.0f);
mModels.push_back(new Model("bird", ":/models/bird/bird.obj"));
mModels.push_back(new Qtk::Model("bird", ":/models/bird/bird.obj"));
mModels.back()->mTransform.setTranslation(2.0f, 2.0f, -10.0f);
// Sometimes the models are very large
mModels.back()->mTransform.scale(0.0025f);
mModels.back()->mTransform.rotate(-110.0f, 0.0f, 1.0f, 0.0f);
mModels.push_back(new Model("lion", ":/models/lion/lion.obj"));
mModels.push_back(new Qtk::Model("lion", ":/models/lion/lion.obj"));
mModels.back()->mTransform.setTranslation(-3.0f, -1.0f, -10.0f);
mModels.back()->mTransform.scale(0.15f);
mModels.push_back(new Model("alien", ":/models/alien-hominid/alien.obj"));
mModels.push_back(new Qtk::Model("alien", ":/models/alien-hominid/alien.obj"));
mModels.back()->mTransform.setTranslation(2.0f, -1.0f, -5.0f);
mModels.back()->mTransform.scale(0.15f);
mModels.push_back(new Model("scythe", ":/models/scythe/scythe.obj"));
mModels.push_back(new Qtk::Model("scythe", ":/models/scythe/scythe.obj"));
mModels.back()->mTransform.setTranslation(-6.0f, 0.0f, -10.0f);
mModels.back()->mTransform.rotate(-90.0f, 1.0f, 0.0f, 0.0f);
mModels.back()->mTransform.rotate(90.0f, 0.0f, 1.0f, 0.0f);
mModels.push_back(new Model("masterChief", ":/models/spartan/spartan.obj"));
mModels.push_back(new Qtk::Model("masterChief", ":/models/spartan/spartan.obj"));
mModels.back()->mTransform.setTranslation(-1.5f, 0.5f, -2.0f);
@ -186,7 +185,7 @@ void Scene::init()
// Render an alien with specular
// Test alien Model with phong lighting and specular mapping
mMeshes.push_back(
new MeshRenderer("alienTestLight", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("alienTestLight", Triangle(Qtk::QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(4.0f, 1.5f, 10.0f);
mMeshes.back()->mTransform.scale(0.25f);
// This function changes values we have allocated in a buffer, so init() after
@ -194,7 +193,7 @@ void Scene::init()
mMeshes.back()->init();
mModels.push_back(
new Model("alienTest", ":/models/alien-hominid/alien.obj",
new Qtk::Model("alienTest", ":/models/alien-hominid/alien.obj",
":/model-specular.vert", ":/model-specular.frag")
);
mModels.back()->mTransform.setTranslation(3.0f, -1.0f, 10.0f);
@ -214,7 +213,7 @@ void Scene::init()
// Test spartan Model with phong lighting, specular and normal mapping
mMeshes.push_back(
new MeshRenderer("spartanTestLight", Triangle(QTK_DRAW_ELEMENTS))
new Qtk::MeshRenderer("spartanTestLight", Triangle(QTK_DRAW_ELEMENTS))
);
mMeshes.back()->mTransform.setTranslation(1.0f, 1.5f, 10.0f);
mMeshes.back()->mTransform.scale(0.25f);
@ -223,7 +222,7 @@ void Scene::init()
mMeshes.back()->init();
mModels.push_back(
new Model("spartanTest", ":/models/spartan/spartan.obj",
new Qtk::Model("spartanTest", ":/models/spartan/spartan.obj",
":/model-normals.vert", ":/model-normals.frag")
);
mModels.back()->mTransform.setTranslation(0.0f, -1.0f, 10.0f);
@ -242,7 +241,7 @@ void Scene::init()
// Test basic cube with phong.vert and phong.frag shaders
mMeshes.push_back(
new MeshRenderer("testLight", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("testLight", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(5.0f, 1.25f, 10.0f);
mMeshes.back()->mTransform.scale(0.25f);
mMeshes.back()->setDrawType(GL_LINE_LOOP);
@ -251,7 +250,7 @@ void Scene::init()
mMeshes.back()->init();
mMeshes.push_back(
new MeshRenderer("testPhong", Cube(QTK_DRAW_ARRAYS)));
new Qtk::MeshRenderer("testPhong", Cube(QTK_DRAW_ARRAYS)));
mMeshes.back()->mTransform.setTranslation(5.0f, 0.0f, 10.0f);
mMeshes.back()->setShaders(":/phong.vert", ":/phong.frag");
mMeshes.back()->setColor(QVector3D(0.0f, 0.25f, 0.0f));
@ -291,25 +290,25 @@ void Scene::init()
// Create simple shapes using MeshRenderer class and data in mesh.h
mMeshes.push_back(
new MeshRenderer("rightTriangle", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("rightTriangle", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(-5.0f, 0.0f, -2.0f);
mMeshes.push_back(
new MeshRenderer("centerCube", Cube(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("centerCube", Cube(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(-7.0f, 0.0f, -2.0f);
mMeshes.push_back(
new MeshRenderer("leftTriangle", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("leftTriangle", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(-9.0f, 0.0f, -2.0f);
mMeshes.back()->setDrawType(GL_LINE_LOOP);
mMeshes.push_back(
new MeshRenderer("topTriangle", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("topTriangle", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(-7.0f, 2.0f, -2.0f);
mMeshes.back()->mTransform.scale(0.25f);
mMeshes.push_back(
new MeshRenderer("bottomTriangle", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("bottomTriangle", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(-7.0f, -2.0f, -2.0f);
mMeshes.back()->mTransform.scale(0.25f);
mMeshes.back()->setDrawType(GL_LINE_LOOP);
@ -322,7 +321,7 @@ void Scene::init()
// RGB Normals cube to show normals are correct with QTK_DRAW_ARRAYS
mMeshes.push_back(
new MeshRenderer("rgbNormalsCubeArraysTest", Cube(QTK_DRAW_ARRAYS)));
new Qtk::MeshRenderer("rgbNormalsCubeArraysTest", Cube(QTK_DRAW_ARRAYS)));
mMeshes.back()->mTransform.setTranslation(5.0f, 0.0f, 4.0f);
mMeshes.back()->setShaders(":/rgb-normals.vert", ":/rgb-normals.frag");
mMeshes.back()->init();
@ -344,7 +343,7 @@ void Scene::init()
// RGB Normals cube to show normals are correct with QTK_DRAW_ELEMENTS_NORMALS
mMeshes.push_back(
new MeshRenderer("rgbNormalsCubeElementsTest", Cube(QTK_DRAW_ELEMENTS_NORMALS)));
new Qtk::MeshRenderer("rgbNormalsCubeElementsTest", Cube(QTK_DRAW_ELEMENTS_NORMALS)));
mMeshes.back()->mTransform.setTranslation(5.0f, 0.0f, 2.0f);
mMeshes.back()->setShaders(":/rgb-normals.vert", ":/rgb-normals.frag");
mMeshes.back()->init();
@ -369,7 +368,7 @@ void Scene::init()
// + UVs required duplicating element position data from QTK_DRAW_ELEMENTS
// + This is because the same position must use different UV coordinates
mMeshes.push_back(
new MeshRenderer("uvCubeArraysTest", Cube(QTK_DRAW_ARRAYS)));
new Qtk::MeshRenderer("uvCubeArraysTest", Cube(QTK_DRAW_ARRAYS)));
mMeshes.back()->mTransform.setTranslation(-3.0f, 0.0f, -2.0f);
mMeshes.back()->setShaders(":/texture2d.vert", ":/texture2d.frag");
mMeshes.back()->init();
@ -397,7 +396,7 @@ void Scene::init()
// Test drawing a cube with texture coordinates using glDrawElements
mMeshes.push_back(
new MeshRenderer("uvCubeElementsTest", Cube(QTK_DRAW_ELEMENTS_NORMALS)));
new Qtk::MeshRenderer("uvCubeElementsTest", Cube(QTK_DRAW_ELEMENTS_NORMALS)));
mMeshes.back()->mTransform.setTranslation(-1.7f, 0.0f, -2.0f);
mMeshes.back()->setShaders(":/texture2d.vert", ":/texture2d.frag");
mMeshes.back()->init();
@ -423,7 +422,7 @@ void Scene::init()
// Texturing a cube using a cube map
// + Cube map texturing works with both QTK_DRAW_ARRAYS and QTK_DRAW_ELEMENTS
mMeshes.push_back(
new MeshRenderer("testCubeMap", Cube(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("testCubeMap", Cube(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(-3.0f, 1.0f, -2.0f);
mMeshes.back()->mTransform.setRotation(45.0f, 0.0f, 1.0f, 0.0f);
mMeshes.back()->setShaders(":/texture-cubemap.vert", ":/texture-cubemap.frag");
@ -450,7 +449,7 @@ void Scene::init()
// Create a cube with custom shaders
// + Apply RGB normals shader and spin the cube for a neat effect
mMeshes.push_back(
new MeshRenderer("rgbNormalsCube", Cube(QTK_DRAW_ARRAYS)));
new Qtk::MeshRenderer("rgbNormalsCube", Cube(QTK_DRAW_ARRAYS)));
mMeshes.back()->mTransform.setTranslation(5.0f, 2.0f, -2.0f);
mMeshes.back()->setShaders(":/rgb-normals.vert", ":/rgb-normals.frag");
mMeshes.back()->init();
@ -472,7 +471,7 @@ void Scene::init()
// RGB Normals triangle to show normals are correct with QTK_DRAW_ARRAYS
mMeshes.push_back(
new MeshRenderer("rgbTriangleArraysTest", Triangle(QTK_DRAW_ARRAYS)));
new Qtk::MeshRenderer("rgbTriangleArraysTest", Triangle(QTK_DRAW_ARRAYS)));
mMeshes.back()->mTransform.setTranslation(7.0f, 0.0f, 2.0f);
mMeshes.back()->setShaders(":/rgb-normals.vert", ":/rgb-normals.frag");
mMeshes.back()->init();
@ -493,7 +492,7 @@ void Scene::init()
// RGB Normals triangle to show normals are correct with QTK_DRAW_ELEMENTS
mMeshes.push_back(
new MeshRenderer("rgbTriangleElementsTest",
new Qtk::MeshRenderer("rgbTriangleElementsTest",
Triangle(QTK_DRAW_ELEMENTS_NORMALS)));
mMeshes.back()->mTransform.setTranslation(7.0f, 0.0f, 4.0f);
mMeshes.back()->setShaders(":/rgb-normals.vert", ":/rgb-normals.frag");
@ -515,7 +514,7 @@ void Scene::init()
// Test drawing triangle with glDrawArrays with texture coordinates
mMeshes.push_back(
new MeshRenderer("testTriangleArraysUV", Triangle(QTK_DRAW_ARRAYS)));
new Qtk::MeshRenderer("testTriangleArraysUV", Triangle(QTK_DRAW_ARRAYS)));
mMeshes.back()->mTransform.setTranslation(-3.0f, 2.0f, -2.0f);
mMeshes.back()->setShaders(":/texture2d.vert", ":/texture2d.frag");
mMeshes.back()->init();
@ -543,7 +542,7 @@ void Scene::init()
// Test drawing triangle with glDrawElements with texture coordinates
mMeshes.push_back(
new MeshRenderer("testTriangleElementsUV",
new Qtk::MeshRenderer("testTriangleElementsUV",
Triangle(QTK_DRAW_ELEMENTS_NORMALS)));
mMeshes.back()->mTransform.setTranslation(-2.5f, 0.0f, -1.0f);
mMeshes.back()->setShaders(":/texture2d.vert", ":/texture2d.frag");
@ -575,7 +574,7 @@ void Scene::init()
// Example of a cube with no lighting applied
mMeshes.push_back(
new MeshRenderer("noLight", Cube(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("noLight", Cube(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(5.0f, 0.0f, -2.0f);
mMeshes.back()->setShaders(":/solid-perspective.vert",
":/solid-perspective.frag");
@ -586,28 +585,24 @@ void Scene::init()
// Create objects that represent light sources for lighting examples
mMeshes.push_back(
new MeshRenderer("phongLight", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("phongLight", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(3.0f, 2.0f, -2.0f);
mMeshes.back()->mTransform.scale(0.25f);
mMeshes.push_back(
new MeshRenderer("diffuseLight", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("diffuseLight", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(9.0f, 2.0f, -2.0f);
mMeshes.back()->mTransform.scale(0.25f);
mMeshes.push_back(
new MeshRenderer("specularLight", Triangle(QTK_DRAW_ELEMENTS)));
new Qtk::MeshRenderer("specularLight", Triangle(QTK_DRAW_ELEMENTS)));
mMeshes.back()->mTransform.setTranslation(11.0f, 2.0f, -2.0f);
mMeshes.back()->mTransform.scale(0.25f);
}
void Scene::draw()
void ExampleScene::draw()
{
mSkybox.draw();
for (auto & model : mModels) model->draw();
for (const auto &mesh : mMeshes) mesh->draw();
Scene::draw();
mTestPhong->mProgram.bind();
mTestPhong->setUniform("uModelInverseTransposed",
@ -616,13 +611,13 @@ void Scene::draw()
"uLightPosition",
MeshRenderer::getInstance("phongLight")->mTransform.translation());
mTestPhong->setUniform("uCameraPosition",
Scene::Camera().transform().translation());
ExampleScene::Camera().transform().translation());
mTestPhong->mProgram.release();
mTestPhong->draw();
mTestAmbient->mProgram.bind();
mTestAmbient->setUniform("uCameraPosition",
Scene::Camera().transform().translation());
ExampleScene::Camera().transform().translation());
mTestAmbient->mProgram.release();
mTestAmbient->draw();
@ -632,7 +627,7 @@ void Scene::draw()
mTestDiffuse->setUniform(
"uLightPosition",
MeshRenderer::getInstance("diffuseLight")->mTransform.translation());
mTestDiffuse->setUniform("uCameraPosition", Scene::Camera().transform().translation());
mTestDiffuse->setUniform("uCameraPosition", ExampleScene::Camera().transform().translation());
mTestDiffuse->mProgram.release();
mTestDiffuse->draw();
@ -643,43 +638,43 @@ void Scene::draw()
mTestSpecular->setUniform(
"uLightPosition",
MeshRenderer::getInstance("specularLight")->mTransform.translation());
mTestSpecular->setUniform("uCameraPosition", Scene::Camera().transform().translation());
mTestSpecular->setUniform("uCameraPosition", ExampleScene::Camera().transform().translation());
mTestSpecular->mProgram.release();
mTestSpecular->draw();
}
void Scene::update()
void ExampleScene::update()
{
auto position = MeshRenderer::getInstance("alienTestLight")->mTransform.translation();
Model::getInstance("alienTest")->setUniform(
"uLight.position", position);
Model::getInstance("alienTest")->setUniform(
"uCameraPosition", Scene::Camera().transform().translation());
"uCameraPosition", ExampleScene::Camera().transform().translation());
auto posMatrix = Model::getInstance("alienTest")->mTransform.toMatrix();
Model::getInstance("alienTest")->setUniform(
"uMVP.normalMatrix", posMatrix.normalMatrix());
Model::getInstance("alienTest")->setUniform(
"uMVP.model", posMatrix);
Model::getInstance("alienTest")->setUniform(
"uMVP.view", Scene::Camera().toMatrix());
"uMVP.view", ExampleScene::Camera().toMatrix());
Model::getInstance("alienTest")->setUniform(
"uMVP.projection", Scene::Projection());
"uMVP.projection", ExampleScene::Projection());
Model::getInstance("alienTest")->mTransform.rotate(0.75f, 0.0f, 1.0f, 0.0f);
position = MeshRenderer::getInstance("spartanTestLight")->mTransform.translation();
Model::getInstance("spartanTest")->setUniform(
"uLight.position", position);
Model::getInstance("spartanTest")->setUniform(
"uCameraPosition", Scene::Camera().transform().translation());
"uCameraPosition", ExampleScene::Camera().transform().translation());
posMatrix = Model::getInstance("spartanTest")->mTransform.toMatrix();
Model::getInstance("spartanTest")->setUniform(
"uMVP.normalMatrix", posMatrix.normalMatrix());
Model::getInstance("spartanTest")->setUniform(
"uMVP.model", posMatrix);
Model::getInstance("spartanTest")->setUniform(
"uMVP.view", Scene::Camera().toMatrix());
"uMVP.view", ExampleScene::Camera().toMatrix());
Model::getInstance("spartanTest")->setUniform(
"uMVP.projection", Scene::Projection());
"uMVP.projection", ExampleScene::Projection());
Model::getInstance("spartanTest")->mTransform.rotate(0.75f, 0.0f, 1.0f, 0.0f);
@ -691,16 +686,16 @@ void Scene::update()
MeshRenderer::getInstance("testPhong")->setUniform(
"uLight.position", position);
MeshRenderer::getInstance("testPhong")->setUniform(
"uCameraPosition", Scene::Camera().transform().translation());
"uCameraPosition", ExampleScene::Camera().transform().translation());
posMatrix = MeshRenderer::getInstance("testPhong")->mTransform.toMatrix();
MeshRenderer::getInstance("testPhong")->setUniform(
"uMVP.normalMatrix", posMatrix.normalMatrix());
MeshRenderer::getInstance("testPhong")->setUniform(
"uMVP.model", posMatrix);
MeshRenderer::getInstance("testPhong")->setUniform(
"uMVP.view", Scene::Camera().toMatrix());
"uMVP.view", ExampleScene::Camera().toMatrix());
MeshRenderer::getInstance("testPhong")->setUniform(
"uMVP.projection", Scene::Projection());
"uMVP.projection", ExampleScene::Projection());
MeshRenderer::getInstance("testPhong")->mProgram.release();
// Rotate lighting example cubes

View File

@ -6,40 +6,31 @@
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_SCENE_H
#define QTK_SCENE_H
#ifndef QTK_EXAMPLE_SCENE_H
#define QTK_EXAMPLE_SCENE_H
#include <abstractscene.h>
#include <camera3d.h>
#include <skybox.h>
#include <QMatrix4x4>
class MeshRenderer;
class Model;
class Scene {
class ExampleScene : public Qtk::Scene {
public:
Scene();
~Scene();
ExampleScene();
~ExampleScene();
void init();
void draw();
void update();
virtual void init();
virtual void draw() override;
virtual void update();
static Camera3D & Camera() { return mCamera;}
static QMatrix4x4 View() { return mCamera.toMatrix();}
static QMatrix4x4 & Projection() { return mProjection;}
private:
static Camera3D mCamera;
static QMatrix4x4 mProjection;
Skybox mSkybox;
MeshRenderer * mTestPhong;
MeshRenderer * mTestSpecular;
MeshRenderer * mTestDiffuse;
MeshRenderer * mTestAmbient;
std::vector<MeshRenderer *> mMeshes;
std::vector<Model *> mModels;
Qtk::MeshRenderer * mTestPhong;
Qtk::MeshRenderer * mTestSpecular;
Qtk::MeshRenderer * mTestDiffuse;
Qtk::MeshRenderer * mTestAmbient;
};
#endif // QTK_SCENE_H
#endif // QTK_EXAMPLE_SCENE_H

View File

@ -9,7 +9,7 @@
#include <QApplication>
#include <QLabel>
#include <mainwidget.h>
#include <qtkwidget.h>
#include <mainwindow.h>

28
app/mainwindow.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <mainwindow.h>
#include <qtkwidget.h>
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// For use in design mode using Qt Creator
// + We can use the `ui` member to access nested widgets by name
for (const auto widget : ui->qWidget->children()) {
auto qtkWidget = dynamic_cast<Qtk::QtkWidget*>(widget);
if (qtkWidget != nullptr) {
std::string key = qtkWidget->objectName().toStdString();
if (mScenes[key] == nullptr) {
mScenes[qtkWidget->objectName().toStdString()] = new ExampleScene();
}
qtkWidget->setScene(mScenes[qtkWidget->objectName().toStdString()]);
}
}
setWindowIcon(QIcon("../resources/icon.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}

View File

@ -1,15 +1,19 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <unordered_map>
#include <QMainWindow>
#include "main-widget_export.h"
#include "qtk-widget_export.h"
#include <examplescene.h>
namespace Ui {
class MainWindow;
}
class MAIN_WIDGET_EXPORT MainWindow : public QMainWindow
class QTK_WIDGET_EXPORT MainWindow : public QMainWindow
{
Q_OBJECT
@ -19,6 +23,7 @@ public:
private:
Ui::MainWindow *ui;
std::unordered_map<std::string, Qtk::Scene*> mScenes;
};
#endif // MAINWINDOW_H

View File

@ -14,17 +14,27 @@
<string>Qtk - MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="MainWidget" name="openGLWidget">
<widget class="QWidget" name="qWidget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>801</width>
<height>561</height>
</rect>
</property>
<widget class="Qtk::QtkWidget" name="openGLWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>775</width>
<height>550</height>
<width>781</width>
<height>541</height>
</rect>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
@ -94,9 +104,9 @@
</widget>
<customwidgets>
<customwidget>
<class>MainWidget</class>
<class>Qtk::QtkWidget</class>
<extends>QOpenGLWidget</extends>
<header>mainwidget.h</header>
<header>qtkwidget.h</header>
</customwidget>
</customwidgets>
<resources/>

View File

@ -8,6 +8,8 @@
#include <string>
#include <src/qtkresources.h>
#ifndef QTK_RESOURCEMANAGER_H
#define QTK_RESOURCEMANAGER_H
@ -26,10 +28,8 @@ public:
*/
static std::string getPath(const std::string & path) {
// Only construct qtk resource path if in qrc format; else return it as-is
return path[0] == ':' ? resourcesDir + path.substr(2) : path;
return path[0] == ':' ? QTK_RESOURCES + path.substr(1) : path;
}
static std::string resourcesDir;
} RM;
#endif //QTK_RESOURCEMANAGER_H

View File

@ -0,0 +1,32 @@
################################################################################
## Author: Shaun Reed | Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
## ##
## CMake function to update git submodules ##
################################################################################
include_guard()
find_package(Git)
# _PATH: Path to git submodule location that we want to update
# + submodule_update(extern/assimp)
function(submodule_update _PATH)
if (NOT QTK_UPDATE_SUBMODULES)
return()
endif()
if (NOT GIT_FOUND)
message(FATAL_ERROR "[Qtk] Error: No git executable found")
endif()
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --force "${_PATH}"
RESULT_VARIABLE result
)
if (NOT result EQUAL 0)
message(
FATAL_ERROR
"[Qtk] Error: Unable to update git submodule at ${_PATH}"
)
endif()
endfunction()

1
extern/assimp/assimp vendored Submodule

@ -0,0 +1 @@
Subproject commit bd64cc88dff17f118ecf32ebcbacaf566f6b6449

BIN
resources/qtk-reference.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

BIN
resources/qtk-views.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 263 KiB

48
src/abstractscene.cpp Normal file
View File

@ -0,0 +1,48 @@
/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Classes for managing objects and data within a scene ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#include <abstractscene.h>
#include <camera3d.h>
#include <resourcemanager.h>
#include <texture.h>
using namespace Qtk;
Camera3D Scene::mCamera;
QMatrix4x4 Scene::mProjection;
/*******************************************************************************
* Constructors, Destructors
******************************************************************************/
Scene::Scene()
{
mCamera.transform().setTranslation(0.0f, 0.0f, 20.0f);
mCamera.transform().setRotation(-5.0f, 0.0f, 1.0f, 0.0f);
}
Scene::~Scene()
{
for (auto & mesh : mMeshes) delete mesh;
for (auto & model : mModels) delete model;
if (mSkybox != Q_NULLPTR) delete mSkybox;
}
void Scene::privDraw()
{
if (!mInit) {
initializeOpenGLFunctions();
init();
mInit = true;
}
if (mSkybox != Q_NULLPTR) mSkybox->draw();
for (auto & model : mModels) model->draw();
for (const auto & mesh : mMeshes) mesh->draw();
}

55
src/abstractscene.h Normal file
View File

@ -0,0 +1,55 @@
/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Classes for managing objects and data within a scene ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_SCENE_H
#define QTK_SCENE_H
#include <camera3d.h>
#include <meshrenderer.h>
#include <model.h>
#include <skybox.h>
#include <QMatrix4x4>
namespace Qtk {
class Scene : protected QOpenGLFunctions {
friend class MainWidget;
public:
Scene();
~Scene();
virtual void init() = 0;
virtual void draw() { privDraw(); };
virtual void update() = 0;
static Camera3D & Camera() { return mCamera;}
static QMatrix4x4 View() { return mCamera.toMatrix();}
static QMatrix4x4 & Projection() { return mProjection;}
inline Skybox * getSkybox() {return mSkybox;}
inline void setSkybox(Skybox * skybox) {
mSkybox = skybox;
}
private:
static Camera3D mCamera;
static QMatrix4x4 mProjection;
bool mInit = false;
void privDraw();
protected:
Skybox * mSkybox;
std::vector<MeshRenderer *> mMeshes;
std::vector<Model *> mModels;
};
}
#endif // QTK_SCENE_H

View File

@ -8,6 +8,7 @@
#include <camera3d.h>
using namespace Qtk;
const QVector3D Camera3D::LocalForward(0.0f, 0.0f, -1.0f);
const QVector3D Camera3D::LocalUp(0.0f, 1.0f, 0.0f);

View File

@ -12,9 +12,10 @@
#include <QDebug>
#include <transform3D.h>
#include <qtkapi.h>
class Camera3D {
namespace Qtk {
class QTKAPI Camera3D {
public:
// Constants
static const QVector3D LocalForward;
@ -47,8 +48,6 @@ private:
#endif
};
Q_DECLARE_TYPEINFO(Camera3D, Q_MOVABLE_TYPE);
// Qt Streams
#ifndef QT_NO_DATASTREAM
QDataStream & operator<<(QDataStream & out, const Camera3D & transform);
@ -58,5 +57,8 @@ QDataStream & operator>>(QDataStream & in, Camera3D & transform);
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const Camera3D & transform);
#endif
}
Q_DECLARE_TYPEINFO(Qtk::Camera3D, Q_MOVABLE_TYPE);
#endif // QTK_CAMERA3D_H

View File

@ -13,6 +13,7 @@
#include <input.h>
using namespace Qtk;
/*******************************************************************************
* Static Helper Structs

View File

@ -12,11 +12,13 @@
#include <QPoint>
#include <Qt>
#include <qtkapi.h>
#include <qtkwidget.h>
class Input {
friend class MainWidget;
namespace Qtk {
class QTKAPI Input {
friend class Qtk::QtkWidget;
public:
// Possible key states
enum InputState
{
@ -50,7 +52,6 @@ public:
static QPoint mouseDelta();
private:
// State updating
static void update();
static void registerKeyPress(int key);
@ -59,5 +60,6 @@ private:
static void registerMouseRelease(Qt::MouseButton button);
static void reset();
};
}
#endif // QTOPENGL_INPUT_H

View File

@ -1,71 +0,0 @@
/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Main window for Qt6 OpenGL widget application ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_MAINWIDGET_H
#define QTK_MAINWIDGET_H
#include <iostream>
#include <QMatrix4x4>
#include <QOpenGLDebugLogger>
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
#define QTK_DEBUG
class MeshRenderer;
class Model;
class Object;
class Scene;
class Skybox;
class Texture;
class MainWidget : public QOpenGLWidget,
protected QOpenGLFunctions {
Q_OBJECT;
public:
// Constructors
MainWidget();
explicit MainWidget(QWidget *parent);
explicit MainWidget(const QSurfaceFormat &format);
~MainWidget() override;
private:
void teardownGL();
void initObjects();
public:
// Inherited virtual Members
void paintGL() override;
void initializeGL() override;
void resizeGL(int width, int height) override;
protected slots:
void update();
void messageLogged(const QOpenGLDebugMessage &msg);
// Protected Helpers
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
// Private helpers
void initializeWidget();
void printContextInformation();
void updateCameraInput();
Scene * mScene;
Object * mObject;
QOpenGLDebugLogger * mDebugLogger;
};
#endif // QTK_MAINWIDGET_H

View File

@ -1,15 +0,0 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon("../resources/icon.png"));
}
MainWindow::~MainWindow()
{
delete ui;
}

View File

@ -8,6 +8,7 @@
#include <mesh.h>
using namespace Qtk;
Cube::Cube(DrawMode mode)
{

View File

@ -12,8 +12,10 @@
#include <QVector2D>
#include <QVector3D>
#include <qtkapi.h>
#include <transform3D.h>
namespace Qtk {
class MeshRenderer;
class Object;
@ -71,7 +73,6 @@ class Object;
#define UV_RIGHT QVector2D(0.0f, 1.0f)
#define UV_CORNER QVector2D(1.0f, 1.0f)
typedef std::vector<QVector3D> Vertices;
typedef std::vector<QVector3D> Colors;
typedef std::vector<GLuint> Indices;
@ -80,7 +81,7 @@ typedef std::vector<QVector3D> Normals;
enum DrawMode { QTK_DRAW_ARRAYS, QTK_DRAW_ELEMENTS, QTK_DRAW_ELEMENTS_NORMALS };
struct ShapeBase {
struct QTKAPI ShapeBase {
ShapeBase(DrawMode mode=QTK_DRAW_ARRAYS, Vertices v={},Indices i={}, Colors c={},
TexCoords t={}, Normals n={})
: mVertices(v), mColors(c), mIndices(i), mTexCoords(t), mNormals(n)
@ -117,16 +118,17 @@ struct Shape : public ShapeBase {
};
// Primitives inherit from ShapeBase, does not allow setting of shape values
class Mesh {
class QTKAPI Mesh {
};
struct Cube : public ShapeBase {
struct QTKAPI Cube : public ShapeBase {
Cube(DrawMode mode=QTK_DRAW_ARRAYS);
};
struct Triangle : public ShapeBase {
struct QTKAPI Triangle : public ShapeBase {
Triangle(DrawMode mode=QTK_DRAW_ARRAYS);
};
}
#endif // QTK_MESH_H

View File

@ -8,14 +8,14 @@
#include <QImageReader>
#include <scene.h>
#include <abstractscene.h>
#include <meshrenderer.h>
#include <texture.h>
#include <meshrenderer.h>
using namespace Qtk;
// Static QHash that holds all MeshRenderer instances using their mName as keys
MeshRenderer::MeshManager MeshRenderer::sInstances;
Qtk::MeshRenderer::MeshManager Qtk::MeshRenderer::sInstances;
MeshRenderer::MeshRenderer(const char * name, const ShapeBase & shape)
: Object(name, shape), mVertexShader(":/multi-color.vert"),

View File

@ -10,9 +10,11 @@
#include <mesh.h>
#include <object.h>
#include <qtkapi.h>
class MeshRenderer : public Object {
namespace Qtk {
class QTKAPI MeshRenderer : public Object {
public:
// Delegate constructors
MeshRenderer(const char * name, Vertices vertices, Indices indices,
@ -73,5 +75,6 @@ private:
bool mHasTexture;
std::string mVertexShader, mFragmentShader;
};
}
#endif // QTK_MESHRENDERER_H

View File

@ -9,12 +9,12 @@
#include <QFileInfo>
#include <scene.h>
#include <texture.h>
#include <resourcemanager.h>
#include <abstractscene.h>
#include <model.h>
#include <resourcemanager.h>
#include <texture.h>
using namespace Qtk;
Model::ModelManager Model::mManager;

View File

@ -16,7 +16,6 @@
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLFunctions>
// Assimp
#include <assimp/Importer.hpp>
@ -24,10 +23,11 @@
#include <assimp/scene.h>
// QTK
#include <qtkapi.h>
#include <transform3D.h>
struct ModelVertex {
namespace Qtk {
struct QTKAPI ModelVertex {
QVector3D mPosition;
QVector3D mNormal;
QVector3D mTangent;
@ -35,7 +35,7 @@ struct ModelVertex {
QVector2D mTextureCoord;
};
struct ModelTexture {
struct QTKAPI ModelTexture {
GLuint mID;
QOpenGLTexture * mTexture;
std::string mType;
@ -44,7 +44,7 @@ struct ModelTexture {
class Model;
class ModelMesh : protected QOpenGLFunctions {
class QTKAPI ModelMesh : protected QOpenGLFunctions {
public:
friend Model;
typedef std::vector<ModelVertex> Vertices;
@ -85,7 +85,7 @@ public:
};
class Model : public QObject {
class QTKAPI Model : public QObject {
Q_OBJECT
public:
@ -136,5 +136,6 @@ private:
std::string mDirectory;
const char * mVertexShader, * mFragmentShader, * mName;
};
}
#endif // QTK_MODEL_H

View File

@ -7,3 +7,5 @@
##############################################################################*/
#include <object.h>
using namespace Qtk;

View File

@ -14,9 +14,10 @@
#include <QOpenGLVertexArrayObject>
#include <mesh.h>
#include <qtkapi.h>
class Object : public QObject {
namespace Qtk {
class QTKAPI Object : public QObject {
Q_OBJECT
public:
@ -57,5 +58,6 @@ public:
private:
QOpenGLTexture * mTexture;
};
}
#endif // QTK_OBJECT_H

View File

@ -1,24 +1,23 @@
/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Manage files and resources used by qtk ##
## About: Main window for Qt6 OpenGL widget application ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_QTKAPI_H
#define QTK_QTKAPI_H
#include "resourcemanager.h"
#include <algorithm>
#include <string>
#include <QtGlobal>
#include <QtCore/QtGlobal>
static std::string nixPath(std::string path)
{
#ifdef Q_OS_WINDOWS
std::replace(path.begin(), path.end(), '\\', '/');
#ifdef QTK_SHARED
# if defined(QTK_EXPORT)
# define QTKAPI Q_DECL_EXPORT
# else
# define QTKAPI Q_DECL_IMPORT
# endif
#else
# define QTKAPI
#endif
return path;
}
std::string RM::resourcesDir =
std::string(__FILE__).substr(0, nixPath(__FILE__).find("src/"))
+ "resources/";
#endif //QTK_QTKAPI_H

2
src/qtkresources.h.in Normal file
View File

@ -0,0 +1,2 @@
#define QTK_RESOURCES "@QTK_RESOURCES@"

View File

@ -9,36 +9,36 @@
#include <QKeyEvent>
#include <input.h>
#include <qtkwidget.h>
#include <mesh.h>
#include <object.h>
#include <scene.h>
#include <mainwidget.h>
#include <abstractscene.h>
using namespace Qtk;
/*******************************************************************************
* Constructors, Destructors
******************************************************************************/
MainWidget::MainWidget() : mDebugLogger(Q_NULLPTR)
QtkWidget::QtkWidget() : mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
{
initializeWidget();
}
// Constructor for using this widget in QtDesigner
MainWidget::MainWidget(QWidget *parent) : QOpenGLWidget(parent), mDebugLogger(Q_NULLPTR)
QtkWidget::QtkWidget(QWidget *parent) : QOpenGLWidget(parent),
mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
{
initializeWidget();
}
MainWidget::MainWidget(const QSurfaceFormat &format)
: mDebugLogger(Q_NULLPTR)
QtkWidget::QtkWidget(const QSurfaceFormat &format)
: mScene(Q_NULLPTR), mDebugLogger(Q_NULLPTR)
{
setFormat(format);
setFocusPolicy(Qt::ClickFocus);
}
MainWidget::~MainWidget()
QtkWidget::~QtkWidget()
{
makeCurrent();
teardownGL();
@ -49,80 +49,26 @@ MainWidget::~MainWidget()
* Private Member Functions
******************************************************************************/
void MainWidget::teardownGL()
void QtkWidget::teardownGL()
{
// Nothing to teardown yet...
}
void MainWidget::initObjects()
{
mScene = new Scene;
// Drawing a primitive object using Qt and OpenGL
// The Object class only stores basic QOpenGL* members and shape data
// + Within mainwidget, mObject serves as a basic QOpenGL example
mObject = new Object("testObject");
mObject->setVertices(Cube(QTK_DRAW_ELEMENTS).vertices());
mObject->setIndices(Cube(QTK_DRAW_ELEMENTS).indices());
mObject->mProgram.create();
mObject->mProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,
":/solid-ambient.vert");
mObject->mProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,
":/solid-ambient.frag");
mObject->mProgram.link();
mObject->mProgram.bind();
mObject->mVAO.create();
mObject->mVAO.bind();
mObject->mVBO.create();
mObject->mVBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mObject->mVBO.bind();
mObject->mVBO.allocate(mObject->vertices().data(),
mObject->vertices().size()
* sizeof(mObject->vertices()[0]));
mObject->mProgram.enableAttributeArray(0);
mObject->mProgram.setAttributeBuffer(0, GL_FLOAT, 0,
3, sizeof(mObject->vertices()[0]));
mObject->mProgram.setUniformValue("uColor", QVector3D(1.0f, 0.0f, 0.0f));
mObject->mProgram.setUniformValue("uLightColor", WHITE);
mObject->mProgram.setUniformValue("uAmbientStrength", 0.75f);
mObject->mVBO.release();
mObject->mVAO.release();
mObject->mProgram.release();
mObject->mTransform.setTranslation(13.0f, 0.0f, -2.0f);
}
/*******************************************************************************
* Inherited Virtual Member Functions
******************************************************************************/
void MainWidget::paintGL()
void QtkWidget::paintGL()
{
// Clear buffers
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// Draw the scene first, since it handles drawing our skybox
mScene->draw();
// Draw any additional objects within mainwidget manually
mObject->mProgram.bind();
mObject->mVAO.bind();
mObject->mProgram.setUniformValue("uModel", mObject->mTransform.toMatrix());
mObject->mProgram.setUniformValue("uView", Scene::Camera().toMatrix());
mObject->mProgram.setUniformValue("uProjection", Scene::Projection());
glDrawElements(GL_TRIANGLES, mObject->indices().size(),
GL_UNSIGNED_INT, mObject->indices().data());
mObject->mVAO.release();
mObject->mProgram.release();
if (mScene != Q_NULLPTR) mScene->draw();
}
void MainWidget::initializeGL()
void QtkWidget::initializeGL()
{
initializeOpenGLFunctions();
// Connect the frameSwapped signal to call the update() function
@ -150,12 +96,9 @@ void MainWidget::initializeGL()
glClearDepth(1.0f);
glClearColor(0.0f, 0.25f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Initialize default objects within the scene
initObjects();
}
void MainWidget::resizeGL(int width, int height)
void QtkWidget::resizeGL(int width, int height)
{
Scene::Projection().setToIdentity();
Scene::Projection().perspective(45.0f,
@ -168,16 +111,16 @@ void MainWidget::resizeGL(int width, int height)
* Protected Slots
******************************************************************************/
void MainWidget::update()
void QtkWidget::update()
{
updateCameraInput();
mScene->update();
if (mScene != Q_NULLPTR) mScene->update();
QWidget::update();
}
void MainWidget::messageLogged(const QOpenGLDebugMessage &msg)
void QtkWidget::messageLogged(const QOpenGLDebugMessage &msg)
{
QString error;
@ -242,7 +185,7 @@ void MainWidget::messageLogged(const QOpenGLDebugMessage &msg)
* Protected Helpers
******************************************************************************/
void MainWidget::keyPressEvent(QKeyEvent *event)
void QtkWidget::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat()) {
// Do not repeat input while a key is held down
@ -252,7 +195,7 @@ void MainWidget::keyPressEvent(QKeyEvent *event)
}
}
void MainWidget::keyReleaseEvent(QKeyEvent *event)
void QtkWidget::keyReleaseEvent(QKeyEvent *event)
{
if (event->isAutoRepeat()) {
event->ignore();
@ -261,12 +204,12 @@ void MainWidget::keyReleaseEvent(QKeyEvent *event)
}
}
void MainWidget::mousePressEvent(QMouseEvent *event)
void QtkWidget::mousePressEvent(QMouseEvent *event)
{
Input::registerMousePress(event->button());
}
void MainWidget::mouseReleaseEvent(QMouseEvent *event)
void QtkWidget::mouseReleaseEvent(QMouseEvent *event)
{
Input::registerMouseRelease(event->button());
}
@ -276,7 +219,7 @@ void MainWidget::mouseReleaseEvent(QMouseEvent *event)
* Private Helpers
******************************************************************************/
void MainWidget::initializeWidget()
void QtkWidget::initializeWidget()
{
QSurfaceFormat format;
format.setRenderableType(QSurfaceFormat::OpenGL);
@ -294,7 +237,7 @@ void MainWidget::initializeWidget()
setFocusPolicy(Qt::ClickFocus);
}
void MainWidget::printContextInformation()
void QtkWidget::printContextInformation()
{
QString glType;
QString glVersion;
@ -330,7 +273,7 @@ void MainWidget::printContextInformation()
}
void MainWidget::updateCameraInput()
void QtkWidget::updateCameraInput()
{
Input::update();
// Camera Transformation

74
src/qtkwidget.h Normal file
View File

@ -0,0 +1,74 @@
/*##############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
## About: Main window for Qt6 OpenGL widget application ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#ifndef QTK_QTKWIDGET_H
#define QTK_QTKWIDGET_H
#include <iostream>
#include <QMatrix4x4>
#include <QOpenGLDebugLogger>
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
#include <qtkapi.h>
#include <abstractscene.h>
namespace Qtk {
class QTKAPI QtkWidget : public QOpenGLWidget,
protected QOpenGLFunctions {
Q_OBJECT;
public:
// Constructors
QtkWidget();
explicit QtkWidget(QWidget *parent);
explicit QtkWidget(const QSurfaceFormat &format);
~QtkWidget() override;
private:
void teardownGL();
public:
// Inherited virtual Members
void paintGL() override;
void initializeGL() override;
void resizeGL(int width, int height) override;
inline Qtk::Scene * getScene() {return mScene;}
inline void setScene(Qtk::Scene * scene) {
if (mScene != Q_NULLPTR) delete mScene;
mScene = scene;
}
protected slots:
void update();
#ifdef QTK_DEBUG
void messageLogged(const QOpenGLDebugMessage &msg);
#endif
// Protected Helpers
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
// Private helpers
void initializeWidget();
void updateCameraInput();
Qtk::Scene * mScene;
#ifdef QTK_DEBUG
void printContextInformation();
QOpenGLDebugLogger * mDebugLogger;
#endif
};
}
#endif // QTK_QTKWIDGET_H

View File

@ -6,23 +6,25 @@
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#include <scene.h>
#include <abstractscene.h>
#include <skybox.h>
#include <texture.h>
#include <skybox.h>
using namespace Qtk;
Skybox::Skybox(std::string right, std::string top, std::string front,
std::string left, std::string bottom, std::string back,
const std::string & name)
: mCubeMap(Texture::initCubeMap(
QImage(right.c_str()).mirrored(), QImage(top.c_str()),
QImage(front.c_str()), QImage(left.c_str()),
QImage(bottom.c_str()), QImage(back.c_str()))),
mVBO(QOpenGLBuffer::VertexBuffer),
: mVBO(QOpenGLBuffer::VertexBuffer),
mVertices(Cube(QTK_DRAW_ELEMENTS).vertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).indices())
{ init();}
{
init();
mCubeMap = Texture::initCubeMap(
QImage(right.c_str()).mirrored(), QImage(top.c_str()),
QImage(front.c_str()), QImage(left.c_str()),
QImage(bottom.c_str()), QImage(back.c_str()));
}
Skybox::Skybox(std::string name)
: Skybox(":/right.png", ":/top.png", ":/front.png",
@ -87,13 +89,11 @@ void Skybox::init()
mVBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mVBO.bind();
// Allocate vertex positions into VBO
mVBO.allocate(mVertices.data(),
mVertices.size() * sizeof(mVertices[0]));
mVBO.allocate(mVertices.data(), mVertices.size() * sizeof(mVertices[0]));
// Enable attribute array for vertex positions
mProgram.enableAttributeArray(0);
mProgram.setAttributeBuffer(0, GL_FLOAT, 0,
3, sizeof(QVector3D));
mProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(QVector3D));
// Set shader texture unit to 0
mProgram.setUniformValue("uTexture", 0);

View File

@ -10,16 +10,18 @@
#include <QImage>
#include <QOpenGLBuffer>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLFunctions>
#include <camera3d.h>
#include <mesh.h>
#include <qtkapi.h>
class Skybox : protected QOpenGLFunctions {
namespace Qtk {
class QTKAPI Skybox : protected QOpenGLFunctions {
public:
// Delegate this constructor to use default skybox images
// + This allows creating a skybox with no arguments ( auto s = new Skybox; )
@ -44,5 +46,6 @@ private:
QOpenGLBuffer mVBO;
QOpenGLTexture * mCubeMap;
};
}
#endif // QTK_SKYBOX_H

View File

@ -11,6 +11,7 @@
#include <texture.h>
using namespace Qtk;
QImage * Texture::initImage(const char * image, bool flipX, bool flipY)
{

View File

@ -11,8 +11,10 @@
#include <QOpenGLTexture>
#include <qtkapi.h>
class Texture {
namespace Qtk {
class QTKAPI Texture {
public:
~Texture() {}
@ -38,5 +40,6 @@ private:
// Private ctor to prevent creating instances of this class
Texture() {}
};
}
#endif // QTOPENGL_TEXTURE_H

View File

@ -9,6 +9,7 @@
#include <transform3D.h>
using namespace Qtk;
const QVector3D Transform3D::LocalForward(0.0f, 0.0f, 1.0f);
const QVector3D Transform3D::LocalUp(0.0f, 1.0f, 0.0f);
@ -110,6 +111,8 @@ QVector3D Transform3D::right() const
* QT Streams
******************************************************************************/
namespace Qtk {
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const Transform3D & transform)
{
dbg << "Transform3D\n{\n";
@ -125,7 +128,9 @@ QDebug operator<<(QDebug dbg, const Transform3D & transform)
transform.rotation().scalar() << ">\n}";
return dbg;
}
#endif
#ifndef QT_NO_DATASTREAM
QDataStream & operator<<(QDataStream & out, const Transform3D & transform)
{
out << transform.mTranslation;
@ -142,3 +147,5 @@ QDataStream & operator>>(QDataStream & in, Transform3D & transform)
transform.m_dirty = true;
return in;
}
#endif
}

View File

@ -10,13 +10,18 @@
#ifndef QTK_TRANSFORM3D_H
#define QTK_TRANSFORM3D_H
#include <QDebug>
#include <QMatrix4x4>
#include <QQuaternion>
#include <QVector3D>
#ifndef QT_NO_DEBUG_STREAM
#include <QDebug>
#endif
class Transform3D
#include <qtkapi.h>
namespace Qtk {
class QTKAPI Transform3D
{
public:
// Constructors
@ -96,13 +101,13 @@ private:
bool m_dirty;
#ifndef QT_NO_DATASTREAM
friend QDataStream &operator<<(QDataStream & out, const Transform3D & transform);
friend QDataStream &operator>>(QDataStream & in, Transform3D & transform);
#endif
};
Q_DECLARE_TYPEINFO(Transform3D, Q_MOVABLE_TYPE);
// Qt Streams
#ifndef QT_NO_DEBUG_STREAM
@ -113,5 +118,8 @@ QDebug operator<<(QDebug dbg, const Transform3D & transform);
QDataStream &operator<<(QDataStream & out, const Transform3D & transform);
QDataStream &operator>>(QDataStream & in, Transform3D & transform);
#endif
}
Q_DECLARE_TYPEINFO(Qtk::Transform3D, Q_MOVABLE_TYPE);
#endif // QTK_TRANSFORM3D_H