diff --git a/FinalProject/FinalProject.vcxproj b/FinalProject/FinalProject.vcxproj index b3ece53..d73cb8a 100644 --- a/FinalProject/FinalProject.vcxproj +++ b/FinalProject/FinalProject.vcxproj @@ -114,6 +114,7 @@ + diff --git a/FinalProject/FinalProject.vcxproj.filters b/FinalProject/FinalProject.vcxproj.filters index adf8bd8..cda208c 100644 --- a/FinalProject/FinalProject.vcxproj.filters +++ b/FinalProject/FinalProject.vcxproj.filters @@ -100,6 +100,9 @@ Source Files\Utils + + Source Files\OpenGL Abstractions + diff --git a/FinalProject/sceneviewer.cpp b/FinalProject/sceneviewer.cpp index aa6bfef..2896afe 100644 --- a/FinalProject/sceneviewer.cpp +++ b/FinalProject/sceneviewer.cpp @@ -11,6 +11,7 @@ #include "shader.h" #include "logger.h" #include "model.h" +#include "skybox.h" using std::vector; @@ -65,10 +66,22 @@ void SceneViewer::initializeGL() { vertexShader.dispose(); fragmentShader.dispose(); + skyShader.ensureInitialized(); + + VertexShader vertexShader_sky("./temp/shaders/skyboxShader.vs"); + FragmentShader fragmentShader_sky("./temp/shaders/skyboxShader.fs"); + skyShader.attachShader(vertexShader_sky); + skyShader.attachShader(fragmentShader_sky); + vertexShader_sky.dispose(); + fragmentShader_sky.dispose(); + + Model* backpackModel = new Model("D:\\ProgrammingFile\\SceneEditor\\Models\\backpack\\backpack.obj"); Logger::info("Model loaded"); Renderable backpack(backpackModel); _objects.push_back(backpack); + + sky = new skybox("D:/ProgrammingFile/SceneEditor/skybox"); _camera.setPosition(glm::vec3(0.0f, 0.0f, 5.0f)); } @@ -93,6 +106,13 @@ void SceneViewer::paintGL() { } _shaderProgram.unbind(); + + skyShader.bind(); + view = glm::mat4(glm::mat3(view)); + skyShader.setUniform("view", view); + skyShader.setUniform("projection", projection); + sky->render(); + skyShader.unbind(); } void SceneViewer::mousePressEvent(QMouseEvent* event) { diff --git a/FinalProject/sceneviewer.h b/FinalProject/sceneviewer.h index a854b32..6ad5eb6 100644 --- a/FinalProject/sceneviewer.h +++ b/FinalProject/sceneviewer.h @@ -12,6 +12,7 @@ #include "renderable.h" #include "vao.h" #include "utils.h" +#include "skybox.h" class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions { @@ -23,6 +24,8 @@ private: std::vector _objects; // 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..3242858 --- /dev/null +++ b/FinalProject/skybox.cpp @@ -0,0 +1,69 @@ +#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 + "/top.jpg"); + faces.push_back(path + "/bottom.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); + + 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/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