This commit is contained in:
Shaun Reed 2022-07-17 12:53:45 -04:00
parent 0f372f71f4
commit 99b4f9355b
17 changed files with 234 additions and 156 deletions

View File

@ -22,7 +22,7 @@ Be sure to take note of the Qt6 installation directory, as we will need it to co
Once Qt6 is installed, to build and run `qtk` on Ubuntu - Once Qt6 is installed, to build and run `qtk` on Ubuntu -
```bash ```bash
sudo apt update -y && sudo apt install freeglut3-dev libassimp-dev cmake build-essential git sudo apt update -y && sudo apt install libassimp-dev cmake build-essential git
git clone https://gitlab.com/shaunrd0/qtk 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 $(nproc --ignore=2) --target qtk-main 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 ./qtk/build/qtk-main

View File

@ -63,12 +63,11 @@ void ExampleScene::init()
mTestPhong->mNBO.create(); mTestPhong->mNBO.create();
mTestPhong->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw); mTestPhong->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mTestPhong->mNBO.bind(); mTestPhong->mNBO.bind();
mTestPhong->mNBO.allocate(mTestPhong->normals().data(), mTestPhong->mNBO.allocate(mTestPhong->getNormals().data(),
mTestPhong->normals().size() mTestPhong->getNormals().size()
* sizeof(mTestPhong->normals()[0])); * sizeof(mTestPhong->getNormals()[0]));
mTestPhong->mProgram.enableAttributeArray(1); mTestPhong->mProgram.enableAttributeArray(1);
mTestPhong->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mTestPhong->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, 3, sizeof(QVector3D));
3, sizeof(QVector3D));
mTestPhong->mNBO.release(); mTestPhong->mNBO.release();
mTestPhong->mVAO.release(); mTestPhong->mVAO.release();
mTestPhong->mProgram.release(); mTestPhong->mProgram.release();
@ -88,9 +87,9 @@ void ExampleScene::init()
mTestAmbient->mNBO.create(); mTestAmbient->mNBO.create();
mTestAmbient->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw); mTestAmbient->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mTestAmbient->mNBO.bind(); mTestAmbient->mNBO.bind();
mTestAmbient->mNBO.allocate(mTestAmbient->normals().data(), mTestAmbient->mNBO.allocate(mTestAmbient->getNormals().data(),
mTestAmbient->normals().size() mTestAmbient->getNormals().size()
* sizeof(mTestAmbient->normals()[0])); * sizeof(mTestAmbient->getNormals()[0]));
mTestAmbient->mProgram.enableAttributeArray(1); mTestAmbient->mProgram.enableAttributeArray(1);
mTestAmbient->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mTestAmbient->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -112,9 +111,9 @@ void ExampleScene::init()
mTestDiffuse->mNBO.create(); mTestDiffuse->mNBO.create();
mTestDiffuse->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw); mTestDiffuse->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mTestDiffuse->mNBO.bind(); mTestDiffuse->mNBO.bind();
mTestDiffuse->mNBO.allocate(mTestDiffuse->normals().data(), mTestDiffuse->mNBO.allocate(mTestDiffuse->getNormals().data(),
mTestDiffuse->normals().size() mTestDiffuse->getNormals().size()
* sizeof(mTestDiffuse->normals()[0])); * sizeof(mTestDiffuse->getNormals()[0]));
mTestDiffuse->mProgram.enableAttributeArray(1); mTestDiffuse->mProgram.enableAttributeArray(1);
mTestDiffuse->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mTestDiffuse->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -138,9 +137,9 @@ void ExampleScene::init()
mTestSpecular->mNBO.create(); mTestSpecular->mNBO.create();
mTestSpecular->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw); mTestSpecular->mNBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mTestSpecular->mNBO.bind(); mTestSpecular->mNBO.bind();
mTestSpecular->mNBO.allocate(mTestSpecular->normals().data(), mTestSpecular->mNBO.allocate(mTestSpecular->getNormals().data(),
mTestSpecular->normals().size() mTestSpecular->getNormals().size()
* sizeof(mTestSpecular->normals()[0])); * sizeof(mTestSpecular->getNormals()[0]));
mTestSpecular->mProgram.enableAttributeArray(1); mTestSpecular->mProgram.enableAttributeArray(1);
mTestSpecular->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mTestSpecular->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -261,9 +260,9 @@ void ExampleScene::init()
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->normals().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getNormals().data(),
mMeshes.back()->normals().size() mMeshes.back()->getNormals().size()
* sizeof(mMeshes.back()->normals()[0])); * sizeof(mMeshes.back()->getNormals()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -330,9 +329,9 @@ void ExampleScene::init()
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->normals().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getNormals().data(),
mMeshes.back()->normals().size() mMeshes.back()->getNormals().size()
* sizeof(mMeshes.back()->normals()[0])); * sizeof(mMeshes.back()->getNormals()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -352,9 +351,9 @@ void ExampleScene::init()
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->normals().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getNormals().data(),
mMeshes.back()->normals().size() mMeshes.back()->getNormals().size()
* sizeof(mMeshes.back()->normals()[0])); * sizeof(mMeshes.back()->getNormals()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -374,19 +373,16 @@ void ExampleScene::init()
mMeshes.back()->init(); mMeshes.back()->init();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->setTexture(Texture::initTexture2D(":/crate.png")); mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png"));
mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->setUniform("uTexture", 0);
mMeshes.back()->texture().bind();
mMeshes.back()->texture().release();
mMeshes.back()->mVAO.bind(); mMeshes.back()->mVAO.bind();
mMeshes.back()->mNBO.destroy(); mMeshes.back()->mNBO.destroy();
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.texCoords().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.getTexCoords().data(),
mMeshes.back()->mShape.texCoords().size() mMeshes.back()->mShape.getTexCoords().size()
* sizeof(mMeshes.back()->mShape.texCoords()[0])); * sizeof(mMeshes.back()->mShape.getTexCoords()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
2, sizeof(QVector2D)); 2, sizeof(QVector2D));
@ -405,13 +401,13 @@ void ExampleScene::init()
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->texCoords().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getTexCoords().data(),
mMeshes.back()->texCoords().size() mMeshes.back()->getTexCoords().size()
* sizeof(mMeshes.back()->texCoords()[0])); * sizeof(mMeshes.back()->getTexCoords()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
mMeshes.back()->setTexture(Texture::initTexture2D(":/crate.png")); mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png"));
mMeshes.back()->mProgram.setUniformValue("uTexture", 0); mMeshes.back()->mProgram.setUniformValue("uTexture", 0);
mMeshes.back()->mProgram.release(); mMeshes.back()->mProgram.release();
@ -429,16 +425,16 @@ void ExampleScene::init()
mMeshes.back()->init(); mMeshes.back()->init();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->setTexture(Texture::initCubeMap(":/crate.png")); mMeshes.back()->setTexture(OpenGLTextureFactory::initCubeMap(":/crate.png"));
mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->setUniform("uTexture", 0);
mMeshes.back()->mVAO.bind(); mMeshes.back()->mVAO.bind();
mMeshes.back()->mNBO.destroy(); mMeshes.back()->mNBO.destroy();
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.texCoords().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.getTexCoords().data(),
mMeshes.back()->mShape.texCoords().size() mMeshes.back()->mShape.getTexCoords().size()
* sizeof(mMeshes.back()->mShape.texCoords()[0])); * sizeof(mMeshes.back()->mShape.getTexCoords()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
2, sizeof(QVector2D)); 2, sizeof(QVector2D));
@ -458,9 +454,9 @@ void ExampleScene::init()
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->normals().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getNormals().data(),
mMeshes.back()->normals().size() mMeshes.back()->getNormals().size()
* sizeof(mMeshes.back()->normals()[0])); * sizeof(mMeshes.back()->getNormals()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -480,9 +476,9 @@ void ExampleScene::init()
mMeshes.back()->mVAO.bind(); mMeshes.back()->mVAO.bind();
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->normals().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getNormals().data(),
mMeshes.back()->normals().size() mMeshes.back()->getNormals().size()
* sizeof(mMeshes.back()->normals()[0])); * sizeof(mMeshes.back()->getNormals()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -502,9 +498,9 @@ void ExampleScene::init()
mMeshes.back()->mVAO.bind(); mMeshes.back()->mVAO.bind();
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->normals().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->getNormals().data(),
mMeshes.back()->normals().size() mMeshes.back()->getNormals().size()
* sizeof(mMeshes.back()->normals()[0])); * sizeof(mMeshes.back()->getNormals()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
@ -520,19 +516,16 @@ void ExampleScene::init()
mMeshes.back()->init(); mMeshes.back()->init();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->setTexture(Texture::initTexture2D(":/crate.png")); mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png"));
mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->setUniform("uTexture", 0);
mMeshes.back()->texture().bind();
mMeshes.back()->texture().release();
mMeshes.back()->mVAO.bind(); mMeshes.back()->mVAO.bind();
mMeshes.back()->mNBO.destroy(); mMeshes.back()->mNBO.destroy();
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.texCoords().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.getTexCoords().data(),
mMeshes.back()->mShape.texCoords().size() mMeshes.back()->mShape.getTexCoords().size()
* sizeof(mMeshes.back()->mShape.texCoords()[0])); * sizeof(mMeshes.back()->mShape.getTexCoords()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
2, sizeof(QVector2D)); 2, sizeof(QVector2D));
@ -549,19 +542,16 @@ void ExampleScene::init()
mMeshes.back()->init(); mMeshes.back()->init();
mMeshes.back()->mProgram.bind(); mMeshes.back()->mProgram.bind();
mMeshes.back()->setTexture(Texture::initTexture2D(":/crate.png")); mMeshes.back()->setTexture(OpenGLTextureFactory::initTexture2D(":/crate.png"));
mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->setUniform("uTexture", 0);
mMeshes.back()->texture().bind();
mMeshes.back()->texture().release();
mMeshes.back()->mVAO.bind(); mMeshes.back()->mVAO.bind();
mMeshes.back()->mNBO.destroy(); mMeshes.back()->mNBO.destroy();
mMeshes.back()->mNBO.create(); mMeshes.back()->mNBO.create();
mMeshes.back()->mNBO.bind(); mMeshes.back()->mNBO.bind();
mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.texCoords().data(), mMeshes.back()->mNBO.allocate(mMeshes.back()->mShape.getTexCoords().data(),
mMeshes.back()->mShape.texCoords().size() mMeshes.back()->mShape.getTexCoords().size()
* sizeof(mMeshes.back()->mShape.texCoords()[0])); * sizeof(mMeshes.back()->mShape.getTexCoords()[0]));
mMeshes.back()->mProgram.enableAttributeArray(1); mMeshes.back()->mProgram.enableAttributeArray(1);
mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0, mMeshes.back()->mProgram.setAttributeBuffer(1, GL_FLOAT, 0,
2, sizeof(QVector2D)); 2, sizeof(QVector2D));
@ -604,20 +594,24 @@ void ExampleScene::draw()
{ {
Scene::draw(); Scene::draw();
for (const auto & model : mModels) model->draw();
for (const auto & mesh : mMeshes) mesh->draw();
mTestPhong->mProgram.bind(); mTestPhong->mProgram.bind();
mTestPhong->setUniform("uModelInverseTransposed", mTestPhong->setUniform("uModelInverseTransposed",
mTestPhong->mTransform.toMatrix().normalMatrix()); mTestPhong->mTransform.toMatrix().normalMatrix());
mTestPhong->setUniform( mTestPhong->setUniform(
"uLightPosition", "uLightPosition",
MeshRenderer::getInstance("phongLight")->mTransform.translation()); MeshRenderer::getInstance("phongLight")->mTransform.getTranslation());
mTestPhong->setUniform("uCameraPosition", mTestPhong->setUniform("uCameraPosition",
ExampleScene::Camera().transform().translation()); ExampleScene::Camera().transform().getTranslation());
mTestPhong->mProgram.release(); mTestPhong->mProgram.release();
mTestPhong->draw(); mTestPhong->draw();
mTestAmbient->mProgram.bind(); mTestAmbient->mProgram.bind();
mTestAmbient->setUniform("uCameraPosition", mTestAmbient->setUniform("uCameraPosition",
ExampleScene::Camera().transform().translation()); ExampleScene::Camera().transform().getTranslation());
mTestAmbient->mProgram.release(); mTestAmbient->mProgram.release();
mTestAmbient->draw(); mTestAmbient->draw();
@ -626,8 +620,9 @@ void ExampleScene::draw()
mTestDiffuse->mTransform.toMatrix().normalMatrix()); mTestDiffuse->mTransform.toMatrix().normalMatrix());
mTestDiffuse->setUniform( mTestDiffuse->setUniform(
"uLightPosition", "uLightPosition",
MeshRenderer::getInstance("diffuseLight")->mTransform.translation()); MeshRenderer::getInstance("diffuseLight")->mTransform.getTranslation());
mTestDiffuse->setUniform("uCameraPosition", ExampleScene::Camera().transform().translation()); mTestDiffuse->setUniform("uCameraPosition",
ExampleScene::Camera().transform().getTranslation());
mTestDiffuse->mProgram.release(); mTestDiffuse->mProgram.release();
mTestDiffuse->draw(); mTestDiffuse->draw();
@ -637,19 +632,21 @@ void ExampleScene::draw()
mTestSpecular->mTransform.toMatrix().normalMatrix()); mTestSpecular->mTransform.toMatrix().normalMatrix());
mTestSpecular->setUniform( mTestSpecular->setUniform(
"uLightPosition", "uLightPosition",
MeshRenderer::getInstance("specularLight")->mTransform.translation()); MeshRenderer::getInstance("specularLight")->mTransform.getTranslation());
mTestSpecular->setUniform("uCameraPosition", ExampleScene::Camera().transform().translation()); mTestSpecular->setUniform("uCameraPosition",
ExampleScene::Camera().transform().getTranslation());
mTestSpecular->mProgram.release(); mTestSpecular->mProgram.release();
mTestSpecular->draw(); mTestSpecular->draw();
} }
void ExampleScene::update() void ExampleScene::update()
{ {
auto position = MeshRenderer::getInstance("alienTestLight")->mTransform.translation(); auto position = MeshRenderer::getInstance(
"alienTestLight")->mTransform.getTranslation();
Model::getInstance("alienTest")->setUniform( Model::getInstance("alienTest")->setUniform(
"uLight.position", position); "uLight.position", position);
Model::getInstance("alienTest")->setUniform( Model::getInstance("alienTest")->setUniform(
"uCameraPosition", ExampleScene::Camera().transform().translation()); "uCameraPosition", ExampleScene::Camera().transform().getTranslation());
auto posMatrix = Model::getInstance("alienTest")->mTransform.toMatrix(); auto posMatrix = Model::getInstance("alienTest")->mTransform.toMatrix();
Model::getInstance("alienTest")->setUniform( Model::getInstance("alienTest")->setUniform(
"uMVP.normalMatrix", posMatrix.normalMatrix()); "uMVP.normalMatrix", posMatrix.normalMatrix());
@ -661,11 +658,12 @@ void ExampleScene::update()
"uMVP.projection", ExampleScene::Projection()); "uMVP.projection", ExampleScene::Projection());
Model::getInstance("alienTest")->mTransform.rotate(0.75f, 0.0f, 1.0f, 0.0f); Model::getInstance("alienTest")->mTransform.rotate(0.75f, 0.0f, 1.0f, 0.0f);
position = MeshRenderer::getInstance("spartanTestLight")->mTransform.translation(); position = MeshRenderer::getInstance(
"spartanTestLight")->mTransform.getTranslation();
Model::getInstance("spartanTest")->setUniform( Model::getInstance("spartanTest")->setUniform(
"uLight.position", position); "uLight.position", position);
Model::getInstance("spartanTest")->setUniform( Model::getInstance("spartanTest")->setUniform(
"uCameraPosition", ExampleScene::Camera().transform().translation()); "uCameraPosition", ExampleScene::Camera().transform().getTranslation());
posMatrix = Model::getInstance("spartanTest")->mTransform.toMatrix(); posMatrix = Model::getInstance("spartanTest")->mTransform.toMatrix();
Model::getInstance("spartanTest")->setUniform( Model::getInstance("spartanTest")->setUniform(
"uMVP.normalMatrix", posMatrix.normalMatrix()); "uMVP.normalMatrix", posMatrix.normalMatrix());
@ -682,11 +680,11 @@ void ExampleScene::update()
MeshRenderer::getInstance("testPhong")->mTransform.rotate( MeshRenderer::getInstance("testPhong")->mTransform.rotate(
0.75f, 1.0f, 0.5f, 0.0f); 0.75f, 1.0f, 0.5f, 0.0f);
MeshRenderer::getInstance("testPhong")->mProgram.bind(); MeshRenderer::getInstance("testPhong")->mProgram.bind();
position = MeshRenderer::getInstance("testLight")->mTransform.translation(); position = MeshRenderer::getInstance("testLight")->mTransform.getTranslation();
MeshRenderer::getInstance("testPhong")->setUniform( MeshRenderer::getInstance("testPhong")->setUniform(
"uLight.position", position); "uLight.position", position);
MeshRenderer::getInstance("testPhong")->setUniform( MeshRenderer::getInstance("testPhong")->setUniform(
"uCameraPosition", ExampleScene::Camera().transform().translation()); "uCameraPosition", ExampleScene::Camera().transform().getTranslation());
posMatrix = MeshRenderer::getInstance("testPhong")->mTransform.toMatrix(); posMatrix = MeshRenderer::getInstance("testPhong")->mTransform.toMatrix();
MeshRenderer::getInstance("testPhong")->setUniform( MeshRenderer::getInstance("testPhong")->setUniform(
"uMVP.normalMatrix", posMatrix.normalMatrix()); "uMVP.normalMatrix", posMatrix.normalMatrix());
@ -722,7 +720,7 @@ void ExampleScene::update()
static float translateX = 0.025f; static float translateX = 0.025f;
float limit = -9.0f; // Origin position.x - 2.0f float limit = -9.0f; // Origin position.x - 2.0f
float posX = float posX =
MeshRenderer::getInstance("topTriangle")->mTransform.translation().x(); MeshRenderer::getInstance("topTriangle")->mTransform.getTranslation().x();
if (posX < limit || posX > limit + 4.0f) { if (posX < limit || posX > limit + 4.0f) {
translateX = -translateX; translateX = -translateX;
} }

View File

@ -11,6 +11,7 @@
#include <qtkwidget.h> #include <qtkwidget.h>
#include <mainwindow.h> #include <mainwindow.h>
#include <QSurfaceFormat>
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -24,8 +24,8 @@ const QMatrix4x4 & Camera3D::toMatrix()
{ {
mWorld.setToIdentity(); mWorld.setToIdentity();
// Qt6 renamed QMatrix4x4::conjugate() to conjugated() // Qt6 renamed QMatrix4x4::conjugate() to conjugated()
mWorld.rotate(mTransform.rotation().conjugated()); mWorld.rotate(mTransform.getRotation().conjugated());
mWorld.translate(-mTransform.translation()); mWorld.translate(-mTransform.getTranslation());
return mWorld; return mWorld;
} }

View File

@ -25,18 +25,18 @@ namespace Qtk {
// Accessors // Accessors
inline Transform3D & transform() { return mTransform;} inline Transform3D & transform() { return mTransform;}
inline const QVector3D & translation() const inline const QVector3D & translation() const
{ return mTransform.translation();} { return mTransform.getTranslation();}
inline const QQuaternion & rotation() const inline const QQuaternion & rotation() const
{ return mTransform.rotation();} { return mTransform.getRotation();}
const QMatrix4x4 & toMatrix(); const QMatrix4x4 & toMatrix();
// Queries // Queries
inline QVector3D forward() const inline QVector3D forward() const
{ return mTransform.rotation().rotatedVector(LocalForward);} { return mTransform.getRotation().rotatedVector(LocalForward);}
inline QVector3D right() const inline QVector3D right() const
{ return mTransform.rotation().rotatedVector(LocalRight);} { return mTransform.getRotation().rotatedVector(LocalRight);}
inline QVector3D up() const inline QVector3D up() const
{ return mTransform.rotation().rotatedVector(LocalUp);} { return mTransform.getRotation().rotatedVector(LocalUp);}
private: private:
Transform3D mTransform; Transform3D mTransform;

71
src/mainwidget.h Normal file
View File

@ -0,0 +1,71 @@
/*##############################################################################
## 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 OpenGLTextureFactory;
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

@ -87,11 +87,11 @@ namespace Qtk {
: mVertices(v), mColors(c), mIndices(i), mTexCoords(t), mNormals(n) : mVertices(v), mColors(c), mIndices(i), mTexCoords(t), mNormals(n)
{} {}
inline const Vertices & vertices() const { return mVertices;} inline const Vertices & getVertices() const { return mVertices;}
inline const Indices & indices() const { return mIndices;} inline const Indices & getIndexData() const { return mIndices;}
inline const Colors & colors() const { return mColors;} inline const Colors & getColors() const { return mColors;}
inline const TexCoords & texCoords() const { return mTexCoords;} inline const TexCoords & getTexCoords() const { return mTexCoords;}
inline const Normals & normals() const { return mNormals;} inline const Normals & getNormals() const { return mNormals;}
protected: protected:
DrawMode mDrawMode; DrawMode mDrawMode;

View File

@ -68,9 +68,9 @@ void MeshRenderer::init()
// Combine position and color data into one vector, allowing us to use one VBO // Combine position and color data into one vector, allowing us to use one VBO
Vertices combined; Vertices combined;
combined.reserve(vertices().size() + colors().size()); combined.reserve(getVertices().size() + getColors().size());
combined.insert(combined.end(), vertices().begin(), vertices().end()); combined.insert(combined.end(), getVertices().begin(), getVertices().end());
combined.insert(combined.end(), colors().begin(), colors().end()); combined.insert(combined.end(), getColors().begin(), getColors().end());
mVBO.allocate(combined.data(), mVBO.allocate(combined.data(),
combined.size() * sizeof(combined[0])); combined.size() * sizeof(combined[0]));
@ -82,7 +82,7 @@ void MeshRenderer::init()
// Enable color attribute, setting offset to total size of vertices() // Enable color attribute, setting offset to total size of vertices()
mProgram.enableAttributeArray(1); mProgram.enableAttributeArray(1);
mProgram.setAttributeBuffer(1, GL_FLOAT, mProgram.setAttributeBuffer(1, GL_FLOAT,
vertices().size() * sizeof(vertices()[0]), getVertices().size() * sizeof(getVertices()[0]),
3, sizeof(QVector3D)); 3, sizeof(QVector3D));
mVBO.release(); mVBO.release();
@ -104,7 +104,7 @@ void MeshRenderer::draw()
setUniformMVP(); setUniformMVP();
if (mShape.mDrawMode == QTK_DRAW_ARRAYS) { if (mShape.mDrawMode == QTK_DRAW_ARRAYS) {
glDrawArrays(mDrawType, 0, vertices().size()); glDrawArrays(mDrawType, 0, getVertices().size());
} }
else if (mShape.mDrawMode == QTK_DRAW_ELEMENTS else if (mShape.mDrawMode == QTK_DRAW_ELEMENTS
|| mShape.mDrawMode == QTK_DRAW_ELEMENTS_NORMALS) { || mShape.mDrawMode == QTK_DRAW_ELEMENTS_NORMALS) {
@ -137,12 +137,12 @@ void MeshRenderer::setUniformMVP(const char * model, const char * view,
void MeshRenderer::setColor(const QVector3D & color) void MeshRenderer::setColor(const QVector3D & color)
{ {
if (mShape.mColors.empty()) { if (mShape.mColors.empty()) {
for (const auto & vertex : mShape.vertices()) { for (const auto & vertex : mShape.getVertices()) {
mShape.mColors.push_back(color); mShape.mColors.push_back(color);
} }
} }
else { else {
for (int i = 0; i < mShape.colors().size(); i++) { for (int i = 0; i < mShape.getColors().size(); i++) {
mShape.mColors[i] = color; mShape.mColors[i] = color;
} }
} }
@ -150,7 +150,7 @@ void MeshRenderer::setColor(const QVector3D & color)
void MeshRenderer::setTexture(const char * path) void MeshRenderer::setTexture(const char * path)
{ {
mTexture = new QOpenGLTexture(*Texture::initImage(path)); mTexture = new QOpenGLTexture(*OpenGLTextureFactory::initImage(path));
mHasTexture = true; mHasTexture = true;
} }

View File

@ -12,6 +12,8 @@
#include <object.h> #include <object.h>
#include <qtkapi.h> #include <qtkapi.h>
#include <utility>
namespace Qtk { namespace Qtk {
class QTKAPI MeshRenderer : public Object { class QTKAPI MeshRenderer : public Object {
@ -19,9 +21,9 @@ namespace Qtk {
// Delegate constructors // Delegate constructors
MeshRenderer(const char * name, Vertices vertices, Indices indices, MeshRenderer(const char * name, Vertices vertices, Indices indices,
DrawMode mode=QTK_DRAW_ARRAYS) DrawMode mode=QTK_DRAW_ARRAYS)
: MeshRenderer(name, ShapeBase(mode, vertices, indices)) : MeshRenderer(name, ShapeBase(mode, std::move(vertices), std::move(indices)))
{} {}
MeshRenderer(const char * name) explicit MeshRenderer(const char * name)
: MeshRenderer(name, Cube(QTK_DRAW_ELEMENTS)) : MeshRenderer(name, Cube(QTK_DRAW_ELEMENTS))
{} {}
// Constructor // Constructor

View File

@ -175,7 +175,7 @@ void Model::flipTexture(const std::string & fileName, bool flipX, bool flipY)
texture.mTexture->destroy(); texture.mTexture->destroy();
texture.mTexture->create(); texture.mTexture->create();
texture.mTexture->setData( texture.mTexture->setData(
*Texture::initImage(fullPath.c_str(), flipX, flipY)); *OpenGLTextureFactory::initImage(fullPath.c_str(), flipX, flipY));
modified = true; modified = true;
} }
} }
@ -388,7 +388,7 @@ ModelMesh::Textures Model::loadMaterialTextures(
// If the texture has not yet been loaded // If the texture has not yet been loaded
if (!skip) { if (!skip) {
ModelTexture texture; ModelTexture texture;
texture.mTexture = Texture::initTexture2D( texture.mTexture = OpenGLTextureFactory::initTexture2D(
std::string(mDirectory + '/' + fileName.C_Str()).c_str(), std::string(mDirectory + '/' + fileName.C_Str()).c_str(),
false, false); false, false);
texture.mID = texture.mTexture->textureId(); texture.mID = texture.mTexture->textureId();
@ -412,8 +412,8 @@ void Model::sortModels()
auto cameraDistance = [&cameraPos](const ModelMesh &a, const ModelMesh &b) auto cameraDistance = [&cameraPos](const ModelMesh &a, const ModelMesh &b)
{ {
// Sort by the first vertex position, since all transforms will be the same // Sort by the first vertex position, since all transforms will be the same
return (cameraPos.translation().distanceToPoint(a.mVertices[0].mPosition)) return (cameraPos.getTranslation().distanceToPoint(a.mVertices[0].mPosition))
< (cameraPos.translation().distanceToPoint(b.mVertices[0].mPosition)); < (cameraPos.getTranslation().distanceToPoint(b.mVertices[0].mPosition));
}; };
std::sort(mMeshes.begin(), mMeshes.end(), cameraDistance); std::sort(mMeshes.begin(), mMeshes.end(), cameraDistance);
} }

View File

@ -25,6 +25,7 @@
// QTK // QTK
#include <qtkapi.h> #include <qtkapi.h>
#include <transform3D.h> #include <transform3D.h>
#include <object.h>
namespace Qtk { namespace Qtk {
struct QTKAPI ModelVertex { struct QTKAPI ModelVertex {

View File

@ -33,19 +33,24 @@ namespace Qtk {
~Object() {} ~Object() {}
inline const Vertices & vertices() { return mShape.mVertices;} // Look to MeshRenderer for wrapped texture functionality
inline const Indices & indices() { return mShape.mIndices;} // + Object only exposes a QOpenGLTexture object with no wrapped features
inline const Colors & colors() { return mShape.mColors;}
inline const TexCoords & texCoords() { return mShape.mTexCoords;}
inline const Normals & normals() { return mShape.mNormals;}
inline QOpenGLTexture & texture() const { return *mTexture;} inline QOpenGLTexture & texture() const { return *mTexture;}
// These custom types are wrapped for Qtk
inline const Colors & getColors() { return mShape.mColors;}
inline const Indices & getIndexData() { return mShape.mIndices;}
inline const Normals & getNormals() { return mShape.mNormals;}
inline const Shape & getShape() { return mShape;}
inline const TexCoords & getTexCoords() { return mShape.mTexCoords;}
inline const Vertices & getVertices() { return mShape.mVertices;}
virtual inline void setVertices(const Vertices & value) { mShape.mVertices = value;}
virtual inline void setIndices(const Indices & value) { mShape.mIndices = value;}
virtual inline void setColors(const Colors & value) { mShape.mColors = value;} virtual inline void setColors(const Colors & value) { mShape.mColors = value;}
virtual inline void setTexCoords(const TexCoords & value) { mShape.mTexCoords = value;} virtual inline void setIndices(const Indices & value) { mShape.mIndices = value;}
virtual inline void setNormals(const Normals & value) { mShape.mNormals = value;} virtual inline void setNormals(const Normals & value) { mShape.mNormals = value;}
virtual inline void setShape(const Shape & value) { mShape = value;} virtual inline void setShape(const Shape & value) { mShape = value;}
virtual inline void setTexCoords(const TexCoords & value) { mShape.mTexCoords = value;}
virtual inline void setVertices(const Vertices & value) { mShape.mVertices = value;}
// To set mTexture use the accessor and QOpenGLTexture API
QOpenGLBuffer mVBO, mNBO; QOpenGLBuffer mVBO, mNBO;
QOpenGLVertexArrayObject mVAO; QOpenGLVertexArrayObject mVAO;

View File

@ -16,11 +16,11 @@ Skybox::Skybox(std::string right, std::string top, std::string front,
std::string left, std::string bottom, std::string back, std::string left, std::string bottom, std::string back,
const std::string & name) const std::string & name)
: mVBO(QOpenGLBuffer::VertexBuffer), : mVBO(QOpenGLBuffer::VertexBuffer),
mVertices(Cube(QTK_DRAW_ELEMENTS).vertices()), mVertices(Cube(QTK_DRAW_ELEMENTS).getVertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).indices()) mIndices(Cube(QTK_DRAW_ELEMENTS).getIndexData())
{ {
init(); init();
mCubeMap = Texture::initCubeMap( mCubeMap = OpenGLTextureFactory::initCubeMap(
QImage(right.c_str()).mirrored(), QImage(top.c_str()), QImage(right.c_str()).mirrored(), QImage(top.c_str()),
QImage(front.c_str()), QImage(left.c_str()), QImage(front.c_str()), QImage(left.c_str()),
QImage(bottom.c_str()), QImage(back.c_str())); QImage(bottom.c_str()), QImage(back.c_str()));

View File

@ -13,7 +13,7 @@
using namespace Qtk; using namespace Qtk;
QImage * Texture::initImage(const char * image, bool flipX, bool flipY) QImage * OpenGLTextureFactory::initImage(const char * image, bool flipX, bool flipY)
{ {
// Qt6 limits loaded images to 256MB by default // Qt6 limits loaded images to 256MB by default
QImageReader::setAllocationLimit(512); QImageReader::setAllocationLimit(512);
@ -27,7 +27,7 @@ QImage * Texture::initImage(const char * image, bool flipX, bool flipY)
return loadedImage; return loadedImage;
} }
QOpenGLTexture * Texture::initTexture2D(const char * texture, QOpenGLTexture * OpenGLTextureFactory::initTexture2D(const char * texture,
bool flipX, bool flipY) bool flipX, bool flipY)
{ {
QImage * image = initImage(texture, flipX, flipY); QImage * image = initImage(texture, flipX, flipY);
@ -40,14 +40,14 @@ QOpenGLTexture * Texture::initTexture2D(const char * texture,
return newTexture; return newTexture;
} }
QOpenGLTexture * Texture::initCubeMap(const char * tile) QOpenGLTexture * OpenGLTextureFactory::initCubeMap(const char * tile)
{ {
return initCubeMap(QImage(tile), QImage(tile), return initCubeMap(QImage(tile), QImage(tile),
QImage(tile), QImage(tile), QImage(tile), QImage(tile),
QImage(tile), QImage(tile)); QImage(tile), QImage(tile));
} }
QOpenGLTexture * Texture::initCubeMap( QOpenGLTexture * OpenGLTextureFactory::initCubeMap(
const char * right, const char * top, const char * right, const char * top,
const char * front, const char * left, const char * front, const char * left,
const char * bottom, const char * back) const char * bottom, const char * back)
@ -57,7 +57,7 @@ QOpenGLTexture * Texture::initCubeMap(
QImage(bottom), QImage(back)); QImage(bottom), QImage(back));
} }
QOpenGLTexture * Texture::initCubeMap( QOpenGLTexture * OpenGLTextureFactory::initCubeMap(
QImage right, QImage top, QImage right, QImage top,
QImage front, QImage left, QImage front, QImage left,
QImage bottom, QImage back) QImage bottom, QImage back)

View File

@ -14,9 +14,9 @@
#include <qtkapi.h> #include <qtkapi.h>
namespace Qtk { namespace Qtk {
class QTKAPI Texture { class QTKAPI OpenGLTextureFactory {
public: public:
~Texture() {} ~OpenGLTextureFactory() {}
// QImage // QImage
static QImage * initImage(const char * image, static QImage * initImage(const char * image,
@ -38,7 +38,7 @@ namespace Qtk {
private: private:
// Private ctor to prevent creating instances of this class // Private ctor to prevent creating instances of this class
Texture() {} OpenGLTextureFactory() {}
}; };
} }

View File

@ -91,17 +91,17 @@ const QMatrix4x4 & Transform3D::toMatrix()
* Queries * Queries
******************************************************************************/ ******************************************************************************/
QVector3D Transform3D::forward() const QVector3D Transform3D::getForward() const
{ {
return mRotation.rotatedVector(LocalForward); return mRotation.rotatedVector(LocalForward);
} }
QVector3D Transform3D::up() const QVector3D Transform3D::getUp() const
{ {
return mRotation.rotatedVector(LocalUp); return mRotation.rotatedVector(LocalUp);
} }
QVector3D Transform3D::right() const QVector3D Transform3D::getRight() const
{ {
return mRotation.rotatedVector(LocalRight); return mRotation.rotatedVector(LocalRight);
} }
@ -116,16 +116,16 @@ namespace Qtk {
QDebug operator<<(QDebug dbg, const Transform3D & transform) QDebug operator<<(QDebug dbg, const Transform3D & transform)
{ {
dbg << "Transform3D\n{\n"; dbg << "Transform3D\n{\n";
dbg << "Position: <" << transform.translation().x() << ", " dbg << "Position: <" << transform.getTranslation().x() << ", "
<< transform.translation().y() << ", " << transform.getTranslation().y() << ", "
<< transform.translation().z() << ">\n"; << transform.getTranslation().z() << ">\n";
dbg << "Scale: <" << transform.scale().x() << ", " dbg << "Scale: <" << transform.getScale().x() << ", "
<< transform.scale().y() << ", " << transform.getScale().y() << ", "
<< transform.scale().z() << ">\n"; << transform.getScale().z() << ">\n";
dbg << "Rotation: <" << transform.rotation().x() << ", " dbg << "Rotation: <" << transform.getRotation().x() << ", "
<< transform.rotation().y() << ", " << transform.getRotation().y() << ", "
<< transform.rotation().z() << " | " << << transform.getRotation().z() << " | " <<
transform.rotation().scalar() << ">\n}"; transform.getRotation().scalar() << ">\n}";
return dbg; return dbg;
} }
#endif #endif

View File

@ -82,14 +82,14 @@ namespace Qtk {
// //
// Accessors // Accessors
inline const QVector3D & translation() const { return mTranslation;} inline const QVector3D & getTranslation() const { return mTranslation;}
inline const QVector3D & scale() const { return mScale; } inline const QVector3D & getScale() const { return mScale; }
inline const QQuaternion & rotation() const { return mRotation; } inline const QQuaternion & getRotation() const { return mRotation; }
const QMatrix4x4 & toMatrix(); const QMatrix4x4 & toMatrix();
QVector3D forward() const; QVector3D getForward() const;
QVector3D up() const; QVector3D getUp() const;
QVector3D right() const; QVector3D getRight() const;
static const QVector3D LocalForward, LocalUp, LocalRight; static const QVector3D LocalForward, LocalUp, LocalRight;