qtk/src/skybox.cpp

102 lines
3.3 KiB
C++
Raw Normal View History

2021-09-03 16:56:57 +00:00
/*##############################################################################
## Author: Shaun Reed ##
2022-03-06 16:54:05 +00:00
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
2021-09-03 16:56:57 +00:00
## About: Skybox class using QtOpenGL ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################*/
#include <abstractscene.h>
2021-09-03 16:56:57 +00:00
#include <skybox.h>
#include <texture.h>
2021-09-03 16:56:57 +00:00
using namespace Qtk;
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
Skybox::Skybox(
const std::string & right, const std::string & top,
const std::string & front, const std::string & left,
const std::string & bottom, const std::string & back,
const std::string & name) :
mVBO(QOpenGLBuffer::VertexBuffer),
mVertices(Cube(QTK_DRAW_ELEMENTS).getVertices()),
mIndices(Cube(QTK_DRAW_ELEMENTS).getIndexData()) {
2022-08-14 21:02:50 +00:00
init();
2022-11-24 22:26:53 +00:00
mTexture.setCubeMap(
2022-08-14 21:02:50 +00:00
QImage(right.c_str()).mirrored(), QImage(top.c_str()),
2022-11-24 22:26:53 +00:00
QImage(front.c_str()), QImage(left.c_str()), QImage(bottom.c_str()),
QImage(back.c_str()));
2022-08-14 21:02:50 +00:00
}
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
Skybox::Skybox(const std::string & name) :
Skybox(
":/right.png", ":/top.png", ":/front.png", ":/left.png", ":/bottom.png",
":/back.png", name) {}
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
Skybox::Skybox(QOpenGLTexture * cubeMap, const std::string & name) :
mTexture(cubeMap) {
init();
}
2021-09-03 16:56:57 +00:00
/*******************************************************************************
* Public Member Functions
******************************************************************************/
2022-11-24 22:26:53 +00:00
void Skybox::draw() {
2021-09-03 16:56:57 +00:00
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
mVAO.bind();
mProgram.bind();
2022-11-24 22:26:53 +00:00
mTexture.getOpenGLTexture().bind();
2021-09-03 16:56:57 +00:00
mProgram.setUniformValue("uProjectionMatrix", Scene::Projection());
mProgram.setUniformValue("uViewMatrix", Scene::Camera().toMatrix());
2021-09-03 16:56:57 +00:00
mProgram.setUniformValue("uTexture", 0);
2022-11-24 22:26:53 +00:00
glDrawElements(
GL_TRIANGLES, mIndices.size(), GL_UNSIGNED_INT, mIndices.data());
2021-09-03 16:56:57 +00:00
2022-11-24 22:26:53 +00:00
mTexture.getOpenGLTexture().bind();
2021-09-03 16:56:57 +00:00
mProgram.release();
mVAO.release();
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
glActiveTexture(GL_TEXTURE0);
}
/*******************************************************************************
* Private Member Functions
******************************************************************************/
2022-11-24 22:26:53 +00:00
void Skybox::init() {
initializeOpenGLFunctions();
2021-09-03 16:56:57 +00:00
// Set up shader program
mProgram.create();
mProgram.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/skybox.vert");
mProgram.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/skybox.frag");
mProgram.link();
mProgram.bind();
// Setup VAO
mVAO.create();
mVAO.bind();
// Setup VBO for vertex position data
mVBO.create();
mVBO.setUsagePattern(QOpenGLBuffer::StaticDraw);
mVBO.bind();
// Allocate vertex positions into VBO
2022-08-14 21:02:50 +00:00
mVBO.allocate(mVertices.data(), mVertices.size() * sizeof(mVertices[0]));
2021-09-03 16:56:57 +00:00
// Enable attribute array for vertex positions
mProgram.enableAttributeArray(0);
2022-08-14 21:02:50 +00:00
mProgram.setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(QVector3D));
2021-09-03 16:56:57 +00:00
// Set shader texture unit to 0
mProgram.setUniformValue("uTexture", 0);
mVAO.release();
mVBO.release();
mProgram.release();
}