Compare commits

...

3 Commits

14 changed files with 156 additions and 133 deletions

View File

@ -22,18 +22,7 @@ ExampleScene::~ExampleScene() = default;
void ExampleScene::init()
{
setSkybox(new Qtk::Skybox(":/textures/skybox/right.png",
":/textures/skybox/top.png",
":/textures/skybox/front.png",
":/textures/skybox/left.png",
":/textures/skybox/bottom.png",
":/textures/skybox/back.png",
"Skybox"));
std::string spartanPath = QTK_EXAMPLE_SOURCE_DIR;
spartanPath += "/../resources/models/spartan/spartan.obj";
auto spartan = addObject(new Model("spartan", spartanPath.c_str()));
spartan->getTransform().setTranslation(-4.0f, 0.0f, 0.0f);
setSkybox(new Qtk::Skybox);
auto mesh = addObject(
new Qtk::MeshRenderer("rightTriangle", Triangle(QTK_DRAW_ELEMENTS)));

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 MiB

View File

@ -1,6 +1,5 @@
<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>

View File

@ -1,6 +1,5 @@
<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>

View File

@ -62,7 +62,7 @@ MainWindow::MainWindow(QWidget * parent) : QMainWindow(parent)
ui_->menuView->addAction(ui_->qtk__TreeView->toggleViewAction());
// Set the window icon used for Qtk.
setWindowIcon(Qtk::getIcon());
setWindowIcon(getIcon());
}
MainWindow::~MainWindow()

View File

@ -132,6 +132,11 @@ class MainWindow : public QMainWindow
*/
void setScene(Qtk::Scene * scene);
/**
* @return Default icon to use for Qtk desktop application.
*/
static QIcon getIcon() { return QIcon(":/icons/icon.png"); }
public slots:
/**
* Trigger a refresh for widgets related to a scene that has been updated.

View File

@ -426,6 +426,7 @@ void QtkScene::draw()
// WARNING: We must call the base class draw() function first.
// + This will handle rendering core scene components like the Skybox.
Scene::draw();
const QVector3D cameraPosition = getCamera().getTransform().getTranslation();
mTestPhong->bindShaders();
mTestPhong->setUniform("uModelInverseTransposed",
@ -433,14 +434,12 @@ void QtkScene::draw()
mTestPhong->setUniform(
"uLightPosition",
MeshRenderer::getInstance("phongLight")->getTransform().getTranslation());
mTestPhong->setUniform("uCameraPosition",
QtkScene::getCamera().getTransform().getTranslation());
mTestPhong->setUniform("uCameraPosition", cameraPosition);
mTestPhong->releaseShaders();
mTestPhong->draw();
mTestAmbient->bindShaders();
mTestAmbient->setUniform(
"uCameraPosition", QtkScene::getCamera().getTransform().getTranslation());
mTestAmbient->setUniform("uCameraPosition", cameraPosition);
mTestAmbient->releaseShaders();
mTestAmbient->draw();
@ -452,8 +451,7 @@ void QtkScene::draw()
MeshRenderer::getInstance("diffuseLight")
->getTransform()
.getTranslation());
mTestDiffuse->setUniform(
"uCameraPosition", QtkScene::getCamera().getTransform().getTranslation());
mTestDiffuse->setUniform("uCameraPosition", cameraPosition);
mTestDiffuse->releaseShaders();
mTestDiffuse->draw();
@ -465,67 +463,82 @@ void QtkScene::draw()
MeshRenderer::getInstance("specularLight")
->getTransform()
.getTranslation());
mTestSpecular->setUniform(
"uCameraPosition", QtkScene::getCamera().getTransform().getTranslation());
mTestSpecular->setUniform("uCameraPosition", cameraPosition);
mTestSpecular->releaseShaders();
mTestSpecular->draw();
}
void QtkScene::update()
{
auto mySpartan = Model::getInstance("My spartan");
mySpartan->getTransform().rotate(0.75f, 0.0f, 1.0f, 0.0f);
auto getModel = Model::getInstance;
const QVector3D cameraPosition = getCamera().getTransform().getTranslation();
auto myCube = MeshRenderer::getInstance("My cube");
myCube->getTransform().rotate(-0.75f, 0.0f, 1.0f, 0.0f);
// Models may have failed to load, so we should check before accessing.
if (auto mySpartan = getModel("My spartan"); mySpartan) {
mySpartan->getTransform().rotate(0.75f, 0.0f, 1.0f, 0.0f);
}
auto position = MeshRenderer::getInstance("alienTestLight")
->getTransform()
.getTranslation();
auto alien = Model::getInstance("alienTest");
alien->setUniform("uLight.position", position);
alien->setUniform("uCameraPosition",
QtkScene::getCamera().getTransform().getTranslation());
auto posMatrix = alien->getTransform().toMatrix();
alien->setUniform("uMVP.normalMatrix", posMatrix.normalMatrix());
alien->setUniform("uMVP.model", posMatrix);
alien->setUniform("uMVP.view", QtkScene::getCamera().toMatrix());
alien->setUniform("uMVP.projection", QtkScene::getProjectionMatrix());
alien->getTransform().rotate(0.75f, 0.0f, 1.0f, 0.0f);
if (auto myCube = getModel("My cube"); myCube) {
myCube->getTransform().rotate(-0.75f, 0.0f, 1.0f, 0.0f);
}
position = MeshRenderer::getInstance("spartanTestLight")
->getTransform()
.getTranslation();
auto spartan = Model::getInstance("spartanTest");
spartan->setUniform("uLight.position", position);
spartan->setUniform("uCameraPosition",
QtkScene::getCamera().getTransform().getTranslation());
posMatrix = spartan->getTransform().toMatrix();
spartan->setUniform("uMVP.normalMatrix", posMatrix.normalMatrix());
spartan->setUniform("uMVP.model", posMatrix);
spartan->setUniform("uMVP.view", QtkScene::getCamera().toMatrix());
spartan->setUniform("uMVP.projection", QtkScene::getProjectionMatrix());
spartan->getTransform().rotate(0.75f, 0.0f, 1.0f, 0.0f);
// Helper lambda to set the light position used by GLSL shaders on the model.
// TODO: This could be a helper function on the Model class.
auto setLightPosition = [](const std::string & lightName, Model * model) {
if (auto light = Model::getInstance(lightName.c_str()); light) {
QVector3D position = light->getTransform().getTranslation();
model->setUniform("uLight.position", position);
} else {
qDebug() << "[QtkScene] Failed to set light position: "
<< lightName.c_str();
}
};
auto phong = MeshRenderer::getInstance("testPhong");
phong->getTransform().rotate(0.75f, 1.0f, 0.5f, 0.0f);
phong->bindShaders();
position =
MeshRenderer::getInstance("testLight")->getTransform().getTranslation();
phong->setUniform("uLight.position", position);
phong->setUniform("uCameraPosition",
QtkScene::getCamera().getTransform().getTranslation());
posMatrix = phong->getTransform().toMatrix();
phong->setUniform("uMVP.normalMatrix", posMatrix.normalMatrix());
phong->setUniform("uMVP.model", posMatrix);
phong->setUniform("uMVP.view", QtkScene::getCamera().toMatrix());
phong->setUniform("uMVP.projection", QtkScene::getProjectionMatrix());
phong->releaseShaders();
QMatrix4x4 posMatrix;
if (auto alien = getModel("alienTest"); alien) {
setLightPosition("alienTestLight", alien);
alien->setUniform("uCameraPosition", cameraPosition);
posMatrix = alien->getTransform().toMatrix();
alien->setUniform("uMVP.normalMatrix", posMatrix.normalMatrix());
alien->setUniform("uMVP.model", posMatrix);
alien->setUniform("uMVP.view", QtkScene::getCamera().toMatrix());
alien->setUniform("uMVP.projection", QtkScene::getProjectionMatrix());
alien->getTransform().rotate(0.75f, 0.0f, 1.0f, 0.0f);
}
if (auto spartan = getModel("spartanTest"); spartan) {
setLightPosition("spartanTestLight", spartan);
spartan->setUniform("uCameraPosition", cameraPosition);
posMatrix = spartan->getTransform().toMatrix();
spartan->setUniform("uMVP.normalMatrix", posMatrix.normalMatrix());
spartan->setUniform("uMVP.model", posMatrix);
spartan->setUniform("uMVP.view", QtkScene::getCamera().toMatrix());
spartan->setUniform("uMVP.projection", QtkScene::getProjectionMatrix());
spartan->getTransform().rotate(0.75f, 0.0f, 1.0f, 0.0f);
}
if (auto phong = getModel("testPhong"); phong) {
setLightPosition("testLight", phong);
phong->getTransform().rotate(0.75f, 1.0f, 0.5f, 0.0f);
phong->bindShaders();
phong->setUniform("uCameraPosition", cameraPosition);
posMatrix = phong->getTransform().toMatrix();
phong->setUniform("uMVP.normalMatrix", posMatrix.normalMatrix());
phong->setUniform("uMVP.model", posMatrix);
phong->setUniform("uMVP.view", QtkScene::getCamera().toMatrix());
phong->setUniform("uMVP.projection", QtkScene::getProjectionMatrix());
phong->releaseShaders();
}
// MeshRenderers are lower level opengl objects baked into the source code.
auto getMesh = MeshRenderer::getInstance;
// Rotate lighting example cubes
mTestPhong->getTransform().rotate(0.75f, 0.5f, 0.3f, 0.2f);
MeshRenderer::getInstance("noLight")->getTransform().rotate(
0.75f, 0.5f, 0.3f, 0.2f);
getMesh("noLight")->getTransform().rotate(0.75f, 0.5f, 0.3f, 0.2f);
mTestAmbient->getTransform().rotate(0.75f, 0.5f, 0.3f, 0.2f);
mTestDiffuse->getTransform().rotate(0.75f, 0.5f, 0.3f, 0.2f);
mTestSpecular->getTransform().rotate(0.75f, 0.5f, 0.3f, 0.2f);
@ -533,46 +546,27 @@ void QtkScene::update()
// Examples of various translations and rotations
// Rotate in multiple directions simultaneously
MeshRenderer::getInstance("rgbNormalsCube")
->getTransform()
.rotate(0.75f, 0.2f, 0.4f, 0.6f);
getMesh("rgbNormalsCube")->getTransform().rotate(0.75f, 0.2f, 0.4f, 0.6f);
// Pitch forward and roll sideways
MeshRenderer::getInstance("leftTriangle")
->getTransform()
.rotate(0.75f, 1.0f, 0.0f, 0.0f);
MeshRenderer::getInstance("rightTriangle")
->getTransform()
.rotate(0.75f, 0.0f, 0.0f, 1.0f);
getMesh("leftTriangle")->getTransform().rotate(0.75f, 1.0f, 0.0f, 0.0f);
getMesh("rightTriangle")->getTransform().rotate(0.75f, 0.0f, 0.0f, 1.0f);
// Move between two positions over time
static float translateX = 0.025f;
float limit = -9.0f; // Origin position.x - 2.0f
float posX = MeshRenderer::getInstance("topTriangle")
->getTransform()
.getTranslation()
.x();
float posX = getMesh("topTriangle")->getTransform().getTranslation().x();
if (posX < limit || posX > limit + 4.0f) {
translateX = -translateX;
}
MeshRenderer::getInstance("topTriangle")
->getTransform()
.translate(translateX, 0.0f, 0.0f);
MeshRenderer::getInstance("bottomTriangle")
->getTransform()
.translate(-translateX, 0.0f, 0.0f);
getMesh("topTriangle")->getTransform().translate(translateX, 0.0f, 0.0f);
getMesh("bottomTriangle")->getTransform().translate(-translateX, 0.0f, 0.0f);
// And lets rotate the triangles in two directions at once
MeshRenderer::getInstance("topTriangle")
->getTransform()
.rotate(0.75f, 0.2f, 0.0f, 0.4f);
MeshRenderer::getInstance("bottomTriangle")
->getTransform()
.rotate(0.75f, 0.0f, 0.2f, 0.4f);
getMesh("topTriangle")->getTransform().rotate(0.75f, 0.2f, 0.0f, 0.4f);
getMesh("bottomTriangle")->getTransform().rotate(0.75f, 0.0f, 0.2f, 0.4f);
// And make the bottom triangle green, instead of RGB
// Rotate center cube in several directions simultaneously
// + Not subject to gimbal lock since we are using quaternions :)
MeshRenderer::getInstance("centerCube")
->getTransform()
.rotate(0.75f, 0.2f, 0.4f, 0.6f);
getMesh("centerCube")->getTransform().rotate(0.75f, 0.2f, 0.4f, 0.6f);
}

View File

@ -67,7 +67,7 @@ QString WidgetPlugin::whatsThis() const
QIcon WidgetPlugin::icon() const
{
return Qtk::getIcon();
return QIcon(":/icons/icon.png");
}
bool WidgetPlugin::isContainer() const

View File

@ -121,9 +121,7 @@ void MeshRenderer::draw()
bindShaders();
mVAO.bind();
if (mTexture.hasTexture()) {
mTexture.getOpenGLTexture().bind();
}
mTexture.bind();
// TODO: Automate uniforms some other way
setUniformMVP();
@ -138,9 +136,7 @@ void MeshRenderer::draw()
mShape.mIndices.data());
}
if (mTexture.hasTexture()) {
mTexture.getOpenGLTexture().release();
}
mTexture.bind();
mVAO.release();
releaseShaders();

View File

@ -43,14 +43,6 @@ namespace Qtk
}
return widget;
}
/**
* @return Default icon to use for Qtk desktop application.
*/
static QIcon getIcon()
{
return QIcon(":/icons/icon.png");
}
} // namespace Qtk
#endif // QTK_QTKAPI_H

View File

@ -17,9 +17,28 @@ using namespace Qtk;
* Constructors / Destructors
******************************************************************************/
Skybox::Skybox(QOpenGLTexture * cubeMap, const std::string & name)
Skybox::Skybox(const std::string & name) :
mVBO(QOpenGLBuffer::VertexBuffer),
mVertices(Cube(QTK_DRAW_ELEMENTS).getVertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).getIndexData())
{
mTexture.setTexture(cubeMap);
QImage image({1024, 1024}, QImage::Format_RGBA8888);
image.fill(Qt::darkGray);
mTexture.setCubeMap(image, image, image, image, image, image);
init();
}
Skybox::Skybox(QOpenGLTexture * cubeMap, const std::string & name) :
mVBO(QOpenGLBuffer::VertexBuffer),
mVertices(Cube(QTK_DRAW_ELEMENTS).getVertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).getIndexData())
{
if (cubeMap == Q_NULLPTR) {
qDebug()
<< "[Qtk] Failed to set cubemap for skybox with null QOpenGLTexture.";
} else {
mTexture.setTexture(cubeMap);
}
init();
}
@ -34,13 +53,13 @@ Skybox::Skybox(const std::string & right,
mVertices(Cube(QTK_DRAW_ELEMENTS).getVertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).getIndexData())
{
init();
mTexture.setCubeMap(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()));
init();
}
/*******************************************************************************
@ -54,7 +73,7 @@ void Skybox::draw()
mVAO.bind();
mProgram.bind();
mTexture.getOpenGLTexture().bind();
mTexture.bind();
mProgram.setUniformValue("uProjectionMatrix", Scene::getProjectionMatrix());
mProgram.setUniformValue("uViewMatrix", Scene::getCamera().toMatrix());
@ -62,7 +81,7 @@ void Skybox::draw()
glDrawElements(
GL_TRIANGLES, mIndices.size(), GL_UNSIGNED_INT, mIndices.data());
mTexture.getOpenGLTexture().bind();
mTexture.bind();
mProgram.release();
mVAO.release();

View File

@ -34,7 +34,12 @@ namespace Qtk
* Constructors / Destructors
************************************************************************/
// Delegate this constructor to use default skybox images
/**
* Construct a skybox with a default texture.
*
* @param name The objectName to use for the Skybox.
*/
explicit Skybox(const std::string & name = "Skybox");
/**
* Construct a skybox with an existing QOpenGLTexture.

View File

@ -8,6 +8,7 @@
#include <QDebug>
#include <QImageReader>
#include <QPainter>
#include "texture.h"
@ -65,6 +66,22 @@ QOpenGLTexture * OpenGLTextureFactory::initCubeMap(const char * right,
QImage(back));
}
QImage OpenGLTextureFactory::defaultTexture()
{
QImage image({256, 256}, QImage::Format_RGBA8888);
image.fill(Qt::lightGray);
// Draw a red '?' to the center of the image.
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::red);
painter.setFont({"Helvetica", 100, QFont::Bold});
constexpr QRect rect(0, 0, 256, 256);
painter.drawText(rect, Qt::AlignCenter, "?");
return image;
}
QOpenGLTexture * OpenGLTextureFactory::initCubeMap(const QImage & right,
const QImage & top,
const QImage & front,
@ -87,9 +104,9 @@ QOpenGLTexture * OpenGLTextureFactory::initCubeMap(const QImage & right,
QOpenGLTexture::CubeMapNegativeZ};
int i = 0;
for (const auto & face : faces) {
QImage faceImage(faceTextures[i]);
QImage & faceImage = faceTextures[i];
if (faceImage.isNull()) {
qDebug() << "Error loading cube map image\n";
qDebug() << "[libqtk] Error loading cube map image\n";
faceImage = defaultTexture();
}
faceImage = faceImage.convertToFormat(QImage::Format_RGBA8888);

View File

@ -145,13 +145,7 @@ namespace Qtk
const char * back);
/// The texture used in place of a missing texture.
static QImage defaultTexture()
{
// Use plaster for default texture if image fails to load.
// This prevents segfaults when loading a texture that doesn't exist.
// TODO: Replace with a '?' texture to indicate missing texture.
return QImage(":/textures/plaster.png");
}
static QImage defaultTexture();
private:
// Private ctor to prevent creating instances of this class
@ -223,6 +217,20 @@ namespace Qtk
return mOpenGLTexture != Q_NULLPTR;
}
/**
* Bind the OpenGL texture if it exists, avoiding segmentation faults.
*/
bool bind() const
{
if (hasTexture()) {
// TODO: It would be nice to warn here but some objects may not have
// a texture. Factor Texture out of those objects so we don't bind.
mOpenGLTexture->bind();
return true;
}
return false;
}
/*************************************************************************
* Accessors
************************************************************************/
@ -306,12 +314,12 @@ namespace Qtk
/**
* Sets this Texture to be a cube map with provided sides.
*
* @param right Path to texture to use for right cube map side.
* @param top Path to texture to use for top cube map side.
* @param front Path to texture to use for front cube map side.
* @param left Path to texture to use for left cube map side.
* @param bottom Path to texture to use for bottom cube map side.
* @param back Path to texture to use for back cube map side.
* @param right QImage texture to use for right cube map side.
* @param top QImage texture to use for top cube map side.
* @param front QImage texture to use for front cube map side.
* @param left QImage texture to use for left cube map side.
* @param bottom QImage texture to use for bottom cube map side.
* @param back QImage texture to use for back cube map side.
*/
virtual inline void setCubeMap(const QImage & right,
const QImage & top,