diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index 8fc139c..5ef2e92 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "illuminer.h" diff --git a/FinalProject/sceneviewer.h b/FinalProject/sceneviewer.h index 18df691..8b1c9dd 100644 --- a/FinalProject/sceneviewer.h +++ b/FinalProject/sceneviewer.h @@ -13,7 +13,7 @@ #include "illuminer.h" #include "utils.h" #include "logger.h" - +#include "skybox.h" class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions { @@ -27,6 +27,8 @@ private: DirLight* _dirLight = nullptr; // Shader program for objects ShaderProgram _shaderProgram = ShaderProgram::empty(); + ShaderProgram _skyShader = ShaderProgram::empty(); + skybox* sky; // Main camera Camera _camera; float _cameraMovementSpeed = 0.02f; diff --git a/FinalProject/skybox.cpp b/FinalProject/skybox.cpp new file mode 100644 index 0000000..ba2b009 --- /dev/null +++ b/FinalProject/skybox.cpp @@ -0,0 +1,70 @@ +#include "utils.h" +#include "skybox.h" +#include "shader.h" +#include "camera.h" + +#include +#include +#include + +skybox::skybox(std::string path){ + faces.clear(); + faces.push_back(path + "/right.jpg"); + faces.push_back(path + "/left.jpg"); + faces.push_back(path + "/bottom.jpg"); + faces.push_back(path + "/top.jpg"); + faces.push_back(path + "/front.jpg"); + faces.push_back(path + "/back.jpg"); + + cubemapTexture = loadCubemap(faces); + + OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &skyboxVAO); + OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &skyboxVBO); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(skyboxVAO); + OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO); + OPENGL_EXTRA_FUNCTIONS->glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW); + OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(0); + OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); +} + +void skybox::render() { + OPENGL_EXTRA_FUNCTIONS->glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content + // skybox cube + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(skyboxVAO); + OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0); + OPENGL_EXTRA_FUNCTIONS->glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture); + OPENGL_EXTRA_FUNCTIONS->glDrawArrays(GL_TRIANGLES, 0, 36); + OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); + OPENGL_EXTRA_FUNCTIONS->glDepthFunc(GL_LESS); // set depth function back to default +} + +unsigned int skybox::loadCubemap(std::vector faces) +{ + unsigned int textureID; + OPENGL_FUNCTIONS->glGenTextures(1, &textureID); + OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_CUBE_MAP, textureID); + stbi_set_flip_vertically_on_load(true); + + int width, height, nrChannels; + for (unsigned int i = 0; i < faces.size(); i++) + { + unsigned char* data = stbi_load(faces[i].c_str(), &width, &height, &nrChannels, 0); + if (data) + { + OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + stbi_image_free(data); + } + else + { + std::cout << "Cubemap texture failed to load at path: " << faces[i] << std::endl; + stbi_image_free(data); + } + } + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); + + return textureID; +} \ No newline at end of file diff --git a/FinalProject/skybox.h b/FinalProject/skybox.h new file mode 100644 index 0000000..ba0f1ec --- /dev/null +++ b/FinalProject/skybox.h @@ -0,0 +1,63 @@ +#pragma once + +#include "utils.h" +#include "shader.h" +#include "camera.h" + +#include +#include + + +class skybox { +public: + std::vector faces; + skybox(std::string path); + unsigned int cubemapTexture, skyboxVAO, skyboxVBO; + void render(); +private: + unsigned int loadCubemap(std::vector faces); + float skyboxVertices[108] = { + // positions + -1.0f, 1.0f, -1.0f, + -1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, -1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + + -1.0f, -1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, -1.0f, 1.0f, + -1.0f, -1.0f, 1.0f, + + -1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, -1.0f, + 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, 1.0f, + -1.0f, 1.0f, -1.0f, + + -1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, -1.0f, + 1.0f, -1.0f, -1.0f, + -1.0f, -1.0f, 1.0f, + 1.0f, -1.0f, 1.0f + }; +}; \ No newline at end of file diff --git a/skybox/back.jpg b/skybox/back.jpg new file mode 100644 index 0000000..470a679 Binary files /dev/null and b/skybox/back.jpg differ diff --git a/skybox/bottom.jpg b/skybox/bottom.jpg new file mode 100644 index 0000000..893f394 Binary files /dev/null and b/skybox/bottom.jpg differ diff --git a/skybox/front.jpg b/skybox/front.jpg new file mode 100644 index 0000000..4e17b77 Binary files /dev/null and b/skybox/front.jpg differ diff --git a/skybox/left.jpg b/skybox/left.jpg new file mode 100644 index 0000000..5750b91 Binary files /dev/null and b/skybox/left.jpg differ diff --git a/skybox/right.jpg b/skybox/right.jpg new file mode 100644 index 0000000..8963037 Binary files /dev/null and b/skybox/right.jpg differ diff --git a/skybox/top.jpg b/skybox/top.jpg new file mode 100644 index 0000000..4db3c2a Binary files /dev/null and b/skybox/top.jpg differ