Compare commits
4 Commits
8d7bbb75fb
...
51643c4019
Author | SHA1 | Date | |
---|---|---|---|
51643c4019 | |||
f7b807d3a5 | |||
d6dbb5c2ff | |||
dab3fdc9bd |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@ -198,13 +198,13 @@ jobs:
|
||||
|
||||
- name: Build Qtk Library
|
||||
shell: bash
|
||||
run: cmake --build build/ --config Release --target qtk_library -- ${{ matrix.flags }}
|
||||
run: cmake --build build/ --config Release --target qtk -- ${{ matrix.flags }}
|
||||
|
||||
# Packaging
|
||||
|
||||
- name: Install Qtk Library
|
||||
shell: bash
|
||||
run: cmake --install build/ --config Release --prefix=$(pwd)/install --component qtk_library
|
||||
run: cmake --install build/ --config Release --prefix=$(pwd)/install --component qtk
|
||||
|
||||
- name: Package Qtk Library
|
||||
shell: bash
|
||||
|
@ -65,7 +65,7 @@ option(QTK_PLUGINS "Install Qtk plugins to Qt Designer path." OFF)
|
||||
# Options for qtk_gui
|
||||
option(QTK_GUI "Build the Qtk desktop application" ON)
|
||||
option(QTK_GUI_SCENE
|
||||
"Fetch model resources and build the GUI with an example scene." OFF
|
||||
"Fetch model resources and build the GUI with an example scene." ON
|
||||
)
|
||||
|
||||
if (QTK_CCACHE)
|
||||
@ -189,8 +189,8 @@ endif()
|
||||
add_subdirectory(src)
|
||||
|
||||
if(QTK_EXAMPLE)
|
||||
# Create a namespaced alias for linking with qtk_library in the example.
|
||||
add_library(${PROJECT_NAME}::qtk_library ALIAS qtk_library)
|
||||
# Create a namespaced alias for linking with libqtk in the example.
|
||||
add_library(${PROJECT_NAME}::qtk ALIAS qtk)
|
||||
add_subdirectory(example-app EXCLUDE_FROM_ALL)
|
||||
endif()
|
||||
|
||||
|
16
README.md
16
README.md
@ -1,9 +1,9 @@
|
||||
# Qtk
|
||||
|
||||
[](https://github.com/shaunrd0/qtk/actions/workflows/all-builds.yml)
|
||||
[](https://github.com/shaunrd0/qtk/actions/workflows/build.yml)
|
||||
[](https://github.com/shaunrd0/qtk/actions/workflows/linting.yml)
|
||||
|
||||
Qtk desktop application provides a model loader using [Assimp](https://assimp.org/) within a Qt widget application.
|
||||
The Qtk desktop application provides a model loader using [Assimp](https://assimp.org/) within a Qt widget application.
|
||||
You can fly around the scene using WASD while holding down the left or right mouse button.
|
||||
[QtkWidget](./src/designer-plugins/qtkwidget.h) is the primary QOpenGLWidget used to render the scene and handle input.
|
||||
|
||||
@ -72,7 +72,7 @@ project in the root of this repository.
|
||||
|
||||
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.
|
||||
and see some examples at [qtk-resources/resources/models](https://git.shaunreed.com/shaunrd0/qtk-resources/src/branch/master/models).
|
||||
|
||||
### Source Builds
|
||||
|
||||
@ -97,7 +97,7 @@ Qtk is composed of three separate components.
|
||||
* The shared library [libqtk](./src/qtk) provides classes that leverage QOpenGL functionality
|
||||
while still using lower-level OpenGL APIs to customize the rendering process.
|
||||
Many of these classes can be further expanded, such as [Qtk::Scene](./src/qtk/scene.h).
|
||||
This taget, `qtk_library` in cmake, is always selected to build and install as
|
||||
This taget, `qtk` in cmake, is always selected to build and install as
|
||||
it is required by all other components in this project.
|
||||
* The [Qtk desktop application](./src/app) is built using libqtk within a Qt application.
|
||||
This target, `qtk_gui` in cmake, is optional and can be controlled using the `QTK_GUI` option below.
|
||||
@ -174,14 +174,14 @@ Here we will install to the `/usr/local/` path.
|
||||
|
||||
```bash
|
||||
# Install libqtk only
|
||||
cmake --build build-all/ --target qtk_library -- -j $(nproc)
|
||||
cmake --install build-all/ --component qtk_library --prefix=/usr/local
|
||||
cmake --build build-all/ --target qtk -- -j $(nproc)
|
||||
cmake --install build-all/ --component qtk --prefix=/usr/local
|
||||
-- Install configuration: "Release"
|
||||
-- Installing: /usr/local/lib/cmake/Qtk/QtkConfig.cmake
|
||||
-- Installing: /usr/local/lib/cmake/Qtk/QtkConfigVersion.cmake
|
||||
-- Installing: /usr/local/lib/cmake/Qtk/QtkTargets.cmake
|
||||
-- Installing: /usr/local/lib/cmake/Qtk/QtkTargets-release.cmake
|
||||
-- Installing: /usr/local/lib/static/libqtk_library.a
|
||||
-- Installing: /usr/local/lib/static/libqtk.a
|
||||
-- Installing: /usr/local/include/qtk/camera3d.h
|
||||
-- Installing: /usr/local/include/qtk/input.h
|
||||
-- Installing: /usr/local/include/qtk/meshrenderer.h
|
||||
@ -238,7 +238,7 @@ cmake --build build-all/ --target qtk_plugins -- -j $(nproc)
|
||||
# The path here should be initialized during build configuration, so no need for --prefix
|
||||
cmake --install build-all/ --component qtk_plugins
|
||||
-- Install configuration: "Release"
|
||||
-- Up-to-date: /home/shaun/Qt/6.6.0/gcc_64/../../Tools/QtCreator/lib/Qt/lib/libqtk_library.a
|
||||
-- Up-to-date: /home/shaun/Qt/6.6.0/gcc_64/../../Tools/QtCreator/lib/Qt/lib/libqtk.a
|
||||
-- Up-to-date: /home/shaun/Qt/6.6.0/gcc_64/../../Tools/QtCreator/lib/Qt/lib/libqtk_plugin_library.a
|
||||
-- Up-to-date: /home/shaun/Qt/6.6.0/gcc_64/../../Tools/QtCreator/lib/Qt/plugins/designer/libqtk_collection.so
|
||||
```
|
||||
|
@ -56,7 +56,7 @@ endif()
|
||||
|
||||
# Allow add_subdirectory on this project to use target ALIAS if available.
|
||||
# If this example project is opened standalone we will use find_package.
|
||||
if(NOT TARGET Qtk::qtk_library)
|
||||
if(NOT TARGET Qtk::qtk)
|
||||
find_package(Qtk 0.2 REQUIRED)
|
||||
endif()
|
||||
|
||||
@ -77,7 +77,7 @@ configure_file(
|
||||
|
||||
qt_add_executable(qtk_example ${EXAMPLE_SOURCES})
|
||||
target_link_libraries(qtk_example PUBLIC Qt6::Widgets Qt6::OpenGLWidgets Qt6::Core)
|
||||
target_link_libraries(qtk_example PUBLIC Qtk::qtk_library)
|
||||
target_link_libraries(qtk_example PUBLIC Qtk::qtk)
|
||||
target_include_directories(qtk_example PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
|
||||
|
||||
install(
|
||||
|
22
resources/minimal_resources.qrc
Normal file
22
resources/minimal_resources.qrc
Normal file
@ -0,0 +1,22 @@
|
||||
<RCC>
|
||||
<qresource prefix="/textures">
|
||||
<file alias="plaster.png">images/plaster.png</file>
|
||||
<file alias="crate.png">images/crate.png</file>
|
||||
<file alias="stone.png">images/stone.png</file>
|
||||
<file alias="wood.png">images/wood.png</file>
|
||||
<file>skybox/back.png</file>
|
||||
<file>skybox/bottom.png</file>
|
||||
<file>skybox/front.png</file>
|
||||
<file>skybox/left.png</file>
|
||||
<file>skybox/right.png</file>
|
||||
<file>skybox/top.png</file>
|
||||
</qresource>
|
||||
<qresource prefix="/icons">
|
||||
<file>fontawesome-free-6.2.1-desktop/svgs/solid/cube.svg</file>
|
||||
<file>fontawesome-free-6.2.1-desktop/svgs/regular/trash-can.svg</file>
|
||||
<file>fontawesome-free-6.2.1-desktop/svgs/regular/folder-open.svg</file>
|
||||
<file>fontawesome-free-6.2.1-desktop/svgs/regular/floppy-disk.svg</file>
|
||||
<file>fontawesome-free-6.2.1-desktop/svgs/brands/git-alt.svg</file>
|
||||
<file alias="icon.png">icons/icon.png</file>
|
||||
</qresource>
|
||||
</RCC>
|
@ -28,8 +28,6 @@
|
||||
<file alias="solid.vert">shaders/vertex/solid.vert</file>
|
||||
<file alias="solid-perspective.frag">shaders/fragment/solid-perspective.frag</file>
|
||||
<file alias="solid-perspective.vert">shaders/vertex/solid-perspective.vert</file>
|
||||
<file alias="multi-color.frag">shaders/fragment/multi-color.frag</file>
|
||||
<file alias="multi-color.vert">shaders/vertex/multi-color.vert</file>
|
||||
<file alias="rgb-normals.frag">shaders/fragment/rgb-normals.frag</file>
|
||||
<file alias="rgb-normals.vert">shaders/vertex/rgb-normals.vert</file>
|
||||
<file alias="texture-cubemap.frag">shaders/fragment/texture-cubemap.frag</file>
|
||||
@ -44,13 +42,9 @@
|
||||
<file alias="solid-specular.vert">shaders/vertex/solid-specular.vert</file>
|
||||
<file alias="solid-phong.frag">shaders/fragment/solid-phong.frag</file>
|
||||
<file alias="solid-phong.vert">shaders/vertex/solid-phong.vert</file>
|
||||
<file alias="model-basic.frag">shaders/fragment/model-basic.frag</file>
|
||||
<file alias="model-basic.vert">shaders/vertex/model-basic.vert</file>
|
||||
<file alias="model-phong.frag">shaders/fragment/model-phong.frag</file>
|
||||
<file alias="model-phong.vert">shaders/vertex/model-phong.vert</file>
|
||||
<file alias="model-normals.frag">shaders/fragment/model-normals.frag</file>
|
||||
<file alias="model-normals.vert">shaders/vertex/model-normals.vert</file>
|
||||
<file alias="skybox.frag">skybox/skybox.frag</file>
|
||||
<file alias="skybox.vert">skybox/skybox.vert</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
@ -1,11 +0,0 @@
|
||||
#version 330 core
|
||||
out vec4 fColor;
|
||||
|
||||
in vec2 vTextureCoord;
|
||||
|
||||
uniform sampler2D texture_diffuse1;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = texture(texture_diffuse1, vTextureCoord);
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
#version 330
|
||||
in vec4 vColor;
|
||||
|
||||
out vec4 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTextureCoord;
|
||||
|
||||
out vec2 vTextureCoord;
|
||||
|
||||
uniform mat4 uModel;
|
||||
uniform mat4 uView;
|
||||
uniform mat4 uProjection;
|
||||
|
||||
void main()
|
||||
{
|
||||
vTextureCoord = aTextureCoord;
|
||||
gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
#version 330
|
||||
layout(location = 0) in vec3 aPosition;
|
||||
layout(location = 1) in vec3 aColor;
|
||||
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModel; // Model
|
||||
uniform mat4 uView; // View
|
||||
uniform mat4 uProjection; // Projection
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
|
||||
|
||||
vColor = vec4(aColor, 1.0f);
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
#version 330
|
||||
uniform samplerCube uTexture;
|
||||
|
||||
varying vec3 vTexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = texture(uTexture, vTexCoord);
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
#version 330
|
||||
layout(location = 0) in vec3 aPosition;
|
||||
|
||||
out vec3 vTexCoord;
|
||||
|
||||
uniform mat4 uProjectionMatrix;
|
||||
uniform mat4 uViewMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Strip translation column from camera's 4x4 matrix
|
||||
mat4 view = mat4(mat3(uViewMatrix));
|
||||
gl_Position = uProjectionMatrix * view * vec4(aPosition, 1.0);
|
||||
vTexCoord = aPosition;
|
||||
}
|
@ -13,22 +13,22 @@ install(
|
||||
FILES
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
|
||||
COMPONENT qtk_library
|
||||
COMPONENT qtk
|
||||
DESTINATION lib/cmake/${PROJECT_NAME}
|
||||
)
|
||||
install(
|
||||
EXPORT qtk_export
|
||||
FILE ${PROJECT_NAME}Targets.cmake
|
||||
NAMESPACE ${PROJECT_NAME}::
|
||||
COMPONENT qtk_library
|
||||
COMPONENT qtk
|
||||
DESTINATION lib/cmake/${PROJECT_NAME}
|
||||
)
|
||||
# System install for qtk_library
|
||||
# System install for libqtk
|
||||
install(
|
||||
TARGETS qtk_library
|
||||
# Associate qtk_library target with qtk-export
|
||||
TARGETS qtk
|
||||
# Associate libqtk target with qtk-export
|
||||
EXPORT qtk_export
|
||||
COMPONENT qtk_library
|
||||
COMPONENT qtk
|
||||
FILE_SET HEADERS DESTINATION include
|
||||
INCLUDES DESTINATION include
|
||||
LIBRARY DESTINATION lib
|
||||
|
@ -26,7 +26,17 @@ else()
|
||||
)
|
||||
endif()
|
||||
|
||||
if(QTK_GUI_SCENE)
|
||||
qt6_add_big_resources(QTK_GUI_SOURCES "${QTK_RESOURCES}/resources.qrc")
|
||||
else()
|
||||
qt6_add_big_resources(
|
||||
QTK_GUI_SOURCES
|
||||
"${QTK_RESOURCES}/minimal_resources.qrc"
|
||||
)
|
||||
endif()
|
||||
|
||||
qt_add_executable(qtk_gui ${QTK_GUI_SOURCES})
|
||||
|
||||
target_link_libraries(qtk_gui PRIVATE qtk_plugin_library)
|
||||
|
||||
if (QTK_GUI_SCENE)
|
||||
|
@ -9,12 +9,10 @@
|
||||
#include <QApplication>
|
||||
|
||||
#include "qtkmainwindow.h"
|
||||
#include "qtkscene.h"
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
Q_INIT_RESOURCE(resources);
|
||||
|
||||
initResources();
|
||||
QApplication a(argc, argv);
|
||||
|
||||
auto window = MainWindow::getMainWindow();
|
||||
@ -24,11 +22,7 @@ int main(int argc, char * argv[])
|
||||
// NOTE: We set the scene here and not in QtkMainWindow to detach the scene
|
||||
// from the QtkWidget plugin (qtk_plugin_library build target).
|
||||
// Once we can save / load scenes, this call, and QtkScene, can be removed.
|
||||
#ifdef QTK_GUI_SCENE
|
||||
window->setScene(new QtkScene);
|
||||
#else
|
||||
window->setScene(new EmptyScene);
|
||||
#endif
|
||||
window->setScene(new AppScene);
|
||||
|
||||
window->show();
|
||||
|
||||
|
@ -16,6 +16,21 @@
|
||||
|
||||
#include "designer-plugins/debugconsole.h"
|
||||
|
||||
#ifdef QTK_GUI_SCENE
|
||||
#include "qtkscene.h"
|
||||
using AppScene = QtkScene;
|
||||
inline void initResources()
|
||||
{
|
||||
Q_INIT_RESOURCE(resources);
|
||||
}
|
||||
#else
|
||||
using AppScene = EmptyScene;
|
||||
inline void initResources()
|
||||
{
|
||||
Q_INIT_RESOURCE(minimal_resources);
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class MainWindow;
|
||||
|
@ -42,8 +42,8 @@ void QtkScene::init()
|
||||
// Clone qtk-resources if it doesn't already exist.
|
||||
QDir repoDir("resources/");
|
||||
if (!repoDir.exists()) {
|
||||
qDebug() << "Cloning qtk-resources repository to '"
|
||||
<< repoDir.absolutePath() << "'...";
|
||||
qDebug() << "Cloning qtk-resources repository to " << repoDir.absolutePath()
|
||||
<< "...";
|
||||
|
||||
// Run git clone
|
||||
QProcess gitProcess;
|
||||
|
@ -29,7 +29,7 @@ target_sources(
|
||||
"${QTK_PLUGIN_LIBRARY_SOURCES}"
|
||||
"${QTK_PLUGIN_LIBRARY_HEADERS}"
|
||||
)
|
||||
target_link_libraries(qtk_plugin_library PUBLIC Qt6::UiPlugin qtk_library)
|
||||
target_link_libraries(qtk_plugin_library PUBLIC Qt6::UiPlugin qtk)
|
||||
|
||||
################################################################################
|
||||
# Qtk Widget Plugins
|
||||
@ -47,7 +47,7 @@ target_link_libraries(qtk_plugins PUBLIC qtk_plugin_library)
|
||||
# Otherwise, we just use them for building the Qtk desktop application.
|
||||
if(QTK_PLUGINS)
|
||||
install(
|
||||
TARGETS qtk_plugins qtk_library qtk_plugin_library
|
||||
TARGETS qtk_plugins qtk qtk_plugin_library
|
||||
COMPONENT qtk_plugins
|
||||
LIBRARY DESTINATION "${QTK_PLUGIN_INSTALL_DIR}"
|
||||
ARCHIVE DESTINATION "${QTK_PLUGIN_INSTALL_DIR}"
|
||||
|
@ -130,13 +130,8 @@ void ToolBox::createPageShader(const Object * object)
|
||||
|
||||
auto shaderView = new QTextEdit;
|
||||
shaderView->setReadOnly(true);
|
||||
auto vertexFile = QFile(object->getVertexShader().c_str());
|
||||
if (vertexFile.exists()) {
|
||||
vertexFile.open(QIODeviceBase::ReadOnly);
|
||||
shaderView->setText(vertexFile.readAll());
|
||||
vertexFile.close();
|
||||
mainLayout->addRow(shaderView);
|
||||
}
|
||||
shaderView->setText(object->getVertexShaderSourceCode().c_str());
|
||||
mainLayout->addRow(shaderView);
|
||||
|
||||
rowLayout = new QHBoxLayout;
|
||||
rowLayout->addWidget(new QLabel("Fragment Shader:"));
|
||||
@ -145,13 +140,8 @@ void ToolBox::createPageShader(const Object * object)
|
||||
|
||||
shaderView = new QTextEdit;
|
||||
shaderView->setReadOnly(true);
|
||||
auto fragmentfile = QFile(object->getFragmentShader().c_str());
|
||||
if (fragmentfile.exists()) {
|
||||
fragmentfile.open(QIODeviceBase::ReadOnly);
|
||||
shaderView->setText(fragmentfile.readAll());
|
||||
fragmentfile.close();
|
||||
mainLayout->addRow(shaderView);
|
||||
}
|
||||
shaderView->setText(object->getFragmentShaderSourceCode().c_str());
|
||||
mainLayout->addRow(shaderView);
|
||||
|
||||
widget->setLayout(mainLayout);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ set(
|
||||
skybox.h
|
||||
texture.h
|
||||
transform3D.h
|
||||
shaders.h
|
||||
)
|
||||
|
||||
set(
|
||||
@ -43,11 +44,10 @@ set(
|
||||
transform3D.cpp
|
||||
)
|
||||
|
||||
qt6_add_big_resources(QTK_LIBRARY_SOURCES "${QTK_RESOURCES}/resources.qrc")
|
||||
qt_add_library(qtk_library STATIC EXCLUDE_FROM_ALL)
|
||||
target_sources(qtk_library PRIVATE ${QTK_LIBRARY_SOURCES})
|
||||
qt_add_library(qtk STATIC EXCLUDE_FROM_ALL)
|
||||
target_sources(qtk PRIVATE ${QTK_LIBRARY_SOURCES})
|
||||
target_sources(
|
||||
qtk_library PUBLIC
|
||||
qtk PUBLIC
|
||||
FILE_SET HEADERS
|
||||
BASE_DIRS $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
|
||||
BASE_DIRS $<INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/include>
|
||||
@ -55,25 +55,25 @@ target_sources(
|
||||
)
|
||||
|
||||
if(QTK_DEBUG)
|
||||
target_compile_definitions(qtk_library PUBLIC QTK_DEBUG)
|
||||
target_compile_definitions(qtk PUBLIC QTK_DEBUG)
|
||||
endif()
|
||||
|
||||
set_target_properties(
|
||||
qtk_library PROPERTIES
|
||||
qtk PROPERTIES
|
||||
VERSION ${PROJECT_VERSION}
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
qtk_library PUBLIC
|
||||
qtk PUBLIC
|
||||
Qt6::Core Qt6::OpenGLWidgets Qt6::Widgets
|
||||
)
|
||||
|
||||
if(QTK_SUBMODULES OR NOT QTK_ASSIMP_NEW_INTERFACE)
|
||||
target_link_libraries(qtk_library PUBLIC assimp)
|
||||
target_link_libraries(qtk PUBLIC assimp)
|
||||
elseif(QTK_ASSIMP_NEW_INTERFACE)
|
||||
target_link_libraries(qtk_library PUBLIC assimp::assimp)
|
||||
target_link_libraries(qtk PUBLIC assimp::assimp)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(qtk_library PUBLIC OpenGL::GL)
|
||||
target_link_libraries(qtk PUBLIC OpenGL::GL)
|
||||
endif()
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
#include "meshrenderer.h"
|
||||
#include "scene.h"
|
||||
#include "shaders.h"
|
||||
#include "texture.h"
|
||||
|
||||
using namespace Qtk;
|
||||
@ -35,8 +36,7 @@ MeshRenderer::MeshRenderer(const char * name) :
|
||||
}
|
||||
|
||||
MeshRenderer::MeshRenderer(const char * name, const ShapeBase & shape) :
|
||||
Object(name, shape, QTK_MESH), mVertexShader(":/shaders/multi-color.vert"),
|
||||
mFragmentShader(":/shaders/multi-color.frag"), mDrawType(GL_TRIANGLES)
|
||||
Object(name, shape, QTK_MESH), mDrawType(GL_TRIANGLES)
|
||||
{
|
||||
mShape = Shape(shape);
|
||||
init();
|
||||
@ -68,10 +68,22 @@ void MeshRenderer::init()
|
||||
mVAO.bind();
|
||||
|
||||
mProgram.create();
|
||||
mProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,
|
||||
mVertexShader.c_str());
|
||||
mProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,
|
||||
mFragmentShader.c_str());
|
||||
// If no shader is provided, use a default one.
|
||||
if (mVertexShader.empty()) {
|
||||
mProgram.addShaderFromSourceCode(QOpenGLShader::Vertex,
|
||||
QTK_SHADER_VERTEX_MESH);
|
||||
} else {
|
||||
mProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,
|
||||
mVertexShader.c_str());
|
||||
}
|
||||
|
||||
if (mFragmentShader.empty()) {
|
||||
mProgram.addShaderFromSourceCode(QOpenGLShader::Fragment,
|
||||
QTK_SHADER_FRAGMENT_MESH);
|
||||
} else {
|
||||
mProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,
|
||||
mFragmentShader.c_str());
|
||||
}
|
||||
mProgram.link();
|
||||
mProgram.bind();
|
||||
|
||||
|
@ -55,8 +55,8 @@ namespace Qtk
|
||||
*/
|
||||
inline Model(const char * name,
|
||||
const char * path,
|
||||
const char * vertexShader = ":/shaders/model-basic.vert",
|
||||
const char * fragmentShader = ":/shaders/model-basic.frag") :
|
||||
const char * vertexShader = "",
|
||||
const char * fragmentShader = "") :
|
||||
Object(name, QTK_MODEL), mModelPath(path),
|
||||
mVertexShader(vertexShader), mFragmentShader(fragmentShader)
|
||||
{
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "modelmesh.h"
|
||||
#include "scene.h"
|
||||
#include "shaders.h"
|
||||
|
||||
using namespace Qtk;
|
||||
|
||||
@ -73,12 +74,12 @@ void ModelMesh::draw(QOpenGLShaderProgram & shader)
|
||||
* Private Member Functions
|
||||
******************************************************************************/
|
||||
|
||||
void ModelMesh::initMesh(const char * vert, const char * frag)
|
||||
void ModelMesh::initMesh(const std::string & vert, const std::string & frag)
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
|
||||
// Create VAO, VBO, EBO
|
||||
bool status = mVAO->create();
|
||||
mVAO->create();
|
||||
mVBO->create();
|
||||
mEBO->create();
|
||||
|
||||
@ -97,10 +98,26 @@ void ModelMesh::initMesh(const char * vert, const char * frag)
|
||||
mEBO->release();
|
||||
|
||||
// Load and link shaders
|
||||
mProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, vert);
|
||||
mProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, frag);
|
||||
mProgram->link();
|
||||
mProgram->bind();
|
||||
if (!vert.empty()) {
|
||||
mProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, vert.c_str());
|
||||
} else {
|
||||
mProgram->addShaderFromSourceCode(QOpenGLShader::Vertex,
|
||||
QTK_SHADER_VERTEX_MODEL);
|
||||
}
|
||||
|
||||
if (!frag.empty()) {
|
||||
mProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, frag.c_str());
|
||||
} else {
|
||||
mProgram->addShaderFromSourceCode(QOpenGLShader::Fragment,
|
||||
QTK_SHADER_FRAGMENT_MODEL);
|
||||
}
|
||||
|
||||
if (!mProgram->link()) {
|
||||
qDebug() << "Failed to link shader: " << mProgram->log();
|
||||
}
|
||||
if (!mProgram->bind()) {
|
||||
qDebug() << "Failed to bind shader: " << mProgram->log();
|
||||
}
|
||||
|
||||
// Positions
|
||||
mProgram->enableAttributeArray(0);
|
||||
|
@ -97,8 +97,8 @@ namespace Qtk
|
||||
ModelMesh(Vertices vertices,
|
||||
Indices indices,
|
||||
Textures textures,
|
||||
const char * vertexShader = ":/model-basic.vert",
|
||||
const char * fragmentShader = ":/model-basic.frag") :
|
||||
const char * vertexShader = "",
|
||||
const char * fragmentShader = "") :
|
||||
mProgram(new QOpenGLShaderProgram),
|
||||
mVAO(new QOpenGLVertexArrayObject),
|
||||
mVBO(new QOpenGLBuffer(QOpenGLBuffer::VertexBuffer)),
|
||||
@ -146,7 +146,7 @@ namespace Qtk
|
||||
* @param vert Path to vertex shader to use for this model.
|
||||
* @param frag Path to fragment shader to use for this model.
|
||||
*/
|
||||
void initMesh(const char * vert, const char * frag);
|
||||
void initMesh(const std::string & vert, const std::string & frag);
|
||||
|
||||
/*************************************************************************
|
||||
* Private Members
|
||||
|
@ -9,3 +9,15 @@
|
||||
#include "object.h"
|
||||
|
||||
using namespace Qtk;
|
||||
|
||||
std::string Object::getShaderSourceCode(
|
||||
QOpenGLShader::ShaderType shader_type) const
|
||||
{
|
||||
for (const auto & shader : mProgram.shaders()) {
|
||||
if (shader->shaderType() == shader_type) {
|
||||
return shader->sourceCode().toStdString();
|
||||
}
|
||||
}
|
||||
qDebug() << "Failed to find shader of type " << shader_type;
|
||||
return "";
|
||||
}
|
||||
|
@ -51,7 +51,6 @@ namespace Qtk
|
||||
mName(name), mVBO(QOpenGLBuffer::VertexBuffer), mBound(false),
|
||||
mType(type)
|
||||
{
|
||||
initResources();
|
||||
setObjectName(name);
|
||||
}
|
||||
|
||||
@ -60,7 +59,6 @@ namespace Qtk
|
||||
mName(name), mVBO(QOpenGLBuffer::VertexBuffer), mShape(shape),
|
||||
mBound(false), mType(type)
|
||||
{
|
||||
initResources();
|
||||
setObjectName(name);
|
||||
}
|
||||
|
||||
@ -126,6 +124,21 @@ namespace Qtk
|
||||
return "Base Object has no fragment shader.";
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual std::string getShaderSourceCode(
|
||||
QOpenGLShader::ShaderType shader_type) const;
|
||||
|
||||
|
||||
[[nodiscard]] virtual inline std::string getVertexShaderSourceCode() const
|
||||
{
|
||||
return getShaderSourceCode(QOpenGLShader::Vertex);
|
||||
}
|
||||
|
||||
[[nodiscard]] virtual inline std::string getFragmentShaderSourceCode()
|
||||
const
|
||||
{
|
||||
return getShaderSourceCode(QOpenGLShader::Fragment);
|
||||
}
|
||||
|
||||
/*************************************************************************
|
||||
* Setters
|
||||
************************************************************************/
|
||||
|
@ -22,16 +22,6 @@
|
||||
#define QTKAPI
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Initialize Qt resources required by the Qtk library.
|
||||
* This cannot be defined within any namespace, but can be called by ctors.
|
||||
* See object.h for example.
|
||||
*/
|
||||
inline void initResources()
|
||||
{
|
||||
Q_INIT_RESOURCE(resources);
|
||||
}
|
||||
|
||||
namespace Qtk
|
||||
{
|
||||
/**
|
||||
|
120
src/qtk/shaders.h
Normal file
120
src/qtk/shaders.h
Normal file
@ -0,0 +1,120 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
|
||||
## About: Default GLSL shaders to use for objects if no shader if provided. ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################*/
|
||||
#ifndef QTK_SHADERS_H
|
||||
#define QTK_SHADERS_H
|
||||
|
||||
//
|
||||
// Model
|
||||
|
||||
#define QTK_SHADER_VERTEX_MODEL \
|
||||
R"(
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPosition;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTextureCoord;
|
||||
|
||||
out vec2 vTextureCoord;
|
||||
|
||||
uniform mat4 uModel;
|
||||
uniform mat4 uView;
|
||||
uniform mat4 uProjection;
|
||||
|
||||
void main()
|
||||
{
|
||||
vTextureCoord = aTextureCoord;
|
||||
gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
|
||||
}
|
||||
)"
|
||||
|
||||
#define QTK_SHADER_FRAGMENT_MODEL \
|
||||
R"(
|
||||
#version 330 core
|
||||
out vec4 fColor;
|
||||
|
||||
in vec2 vTextureCoord;
|
||||
|
||||
uniform sampler2D texture_diffuse1;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = texture(texture_diffuse1, vTextureCoord);
|
||||
}
|
||||
)"
|
||||
|
||||
//
|
||||
// MeshRenderer
|
||||
|
||||
#define QTK_SHADER_VERTEX_MESH \
|
||||
R"(
|
||||
#version 330
|
||||
layout(location = 0) in vec3 aPosition;
|
||||
layout(location = 1) in vec3 aColor;
|
||||
|
||||
out vec4 vColor;
|
||||
|
||||
uniform mat4 uModel; // Model
|
||||
uniform mat4 uView; // View
|
||||
uniform mat4 uProjection; // Projection
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
|
||||
|
||||
vColor = vec4(aColor, 1.0f);
|
||||
}
|
||||
)"
|
||||
|
||||
#define QTK_SHADER_FRAGMENT_MESH \
|
||||
R"(
|
||||
#version 330
|
||||
in vec4 vColor;
|
||||
|
||||
out vec4 fColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
fColor = vColor;
|
||||
}
|
||||
)"
|
||||
|
||||
//
|
||||
// Skybox
|
||||
|
||||
#define QTK_SHADER_VERTEX_SKYBOX \
|
||||
R"(
|
||||
#version 330
|
||||
layout(location = 0) in vec3 aPosition;
|
||||
|
||||
out vec3 vTexCoord;
|
||||
|
||||
uniform mat4 uProjectionMatrix;
|
||||
uniform mat4 uViewMatrix;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Strip translation column from camera's 4x4 matrix
|
||||
mat4 view = mat4(mat3(uViewMatrix));
|
||||
gl_Position = uProjectionMatrix * view * vec4(aPosition, 1.0);
|
||||
vTexCoord = aPosition;
|
||||
}
|
||||
)"
|
||||
|
||||
#define QTK_SHADER_FRAGMENT_SKYBOX \
|
||||
R"(
|
||||
#version 330
|
||||
uniform samplerCube uTexture;
|
||||
|
||||
varying vec3 vTexCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_FragColor = texture(uTexture, vTexCoord);
|
||||
}
|
||||
)"
|
||||
|
||||
#endif // QTK_SHADERS_H
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "skybox.h"
|
||||
#include "scene.h"
|
||||
#include "shaders.h"
|
||||
#include "texture.h"
|
||||
|
||||
using namespace Qtk;
|
||||
@ -80,10 +81,10 @@ void Skybox::init()
|
||||
|
||||
// Set up shader program
|
||||
mProgram.create();
|
||||
mProgram.addShaderFromSourceFile(QOpenGLShader::Vertex,
|
||||
":/shaders/skybox.vert");
|
||||
mProgram.addShaderFromSourceFile(QOpenGLShader::Fragment,
|
||||
":/shaders/skybox.frag");
|
||||
mProgram.addShaderFromSourceCode(QOpenGLShader::Fragment,
|
||||
QTK_SHADER_FRAGMENT_SKYBOX);
|
||||
mProgram.addShaderFromSourceCode(QOpenGLShader::Vertex,
|
||||
QTK_SHADER_VERTEX_SKYBOX);
|
||||
mProgram.link();
|
||||
mProgram.bind();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user