[CORE][ADD] SkyBox Merge

This commit is contained in:
Linloir 2022-12-18 10:24:54 +08:00
commit 12f32f5c07
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
10 changed files with 136 additions and 2 deletions

View File

@ -4,7 +4,6 @@
#include <qresource.h> #include <qresource.h>
#include <qurl.h> #include <qurl.h>
#include <qdir.h> #include <qdir.h>
#include <time.h>
#include "illuminer.h" #include "illuminer.h"

View File

@ -13,7 +13,7 @@
#include "illuminer.h" #include "illuminer.h"
#include "utils.h" #include "utils.h"
#include "logger.h" #include "logger.h"
#include "skybox.h"
class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions class SceneViewer : public QOpenGLWidget, protected QOpenGLFunctions
{ {
@ -27,6 +27,8 @@ private:
DirLight* _dirLight = nullptr; DirLight* _dirLight = nullptr;
// Shader program for objects // Shader program for objects
ShaderProgram _shaderProgram = ShaderProgram::empty(); ShaderProgram _shaderProgram = ShaderProgram::empty();
ShaderProgram _skyShader = ShaderProgram::empty();
skybox* sky;
// Main camera // Main camera
Camera _camera; Camera _camera;
float _cameraMovementSpeed = 0.02f; float _cameraMovementSpeed = 0.02f;

70
FinalProject/skybox.cpp Normal file
View File

@ -0,0 +1,70 @@
#include "utils.h"
#include "skybox.h"
#include "shader.h"
#include "camera.h"
#include <STBImage/stb_image.h>
#include <vector>
#include <string>
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<std::string> 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;
}

63
FinalProject/skybox.h Normal file
View File

@ -0,0 +1,63 @@
#pragma once
#include "utils.h"
#include "shader.h"
#include "camera.h"
#include <vector>
#include <string>
class skybox {
public:
std::vector<std::string> faces;
skybox(std::string path);
unsigned int cubemapTexture, skyboxVAO, skyboxVBO;
void render();
private:
unsigned int loadCubemap(std::vector<std::string> 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
};
};

BIN
skybox/back.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 723 KiB

BIN
skybox/bottom.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

BIN
skybox/front.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 KiB

BIN
skybox/left.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 588 KiB

BIN
skybox/right.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 KiB

BIN
skybox/top.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 KiB