-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds skybox to the game. This was solved somewhat by shortcuts due to a coming deadline.
- Loading branch information
Showing
16 changed files
with
250 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#version 330 core | ||
out vec4 FragColor; | ||
|
||
in vec3 TexCoords; | ||
|
||
uniform samplerCube skybox; | ||
|
||
void main() | ||
{ | ||
FragColor = texture(skybox, TexCoords); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#version 330 core | ||
layout (location = 0) in vec3 aPos; | ||
|
||
out vec3 TexCoords; | ||
|
||
uniform mat4 projection; | ||
uniform mat4 view; | ||
|
||
void main() | ||
{ | ||
TexCoords = vec3(-aPos.xy, aPos.z); | ||
gl_Position = projection * view * vec4(aPos, 1.0); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include "Skybox.h" | ||
#include "pch.h" | ||
|
||
#include "World/Camera.h" | ||
|
||
Skybox::Skybox() | ||
: mTextureId(0) | ||
, mWidth(0) | ||
, mHeight(0) | ||
, mNrChannels(0) | ||
, mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Skybox.vs"}, | ||
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Skybox.fs"}} | ||
{ | ||
auto vertices = skyboxVertices(); | ||
|
||
mBufferLayout.push<float>(3); | ||
mVBO.setBuffer(vertices); | ||
mVAO.setBuffer(mVBO, mBufferLayout); | ||
|
||
auto faces = skyboxFacesTextures(); | ||
glGenTextures(1, &mTextureId); | ||
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureId); | ||
for (auto i = 0; i < faces.size(); i++) | ||
{ | ||
loadTextureFace(faces, i); | ||
} | ||
GLCall(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); | ||
GLCall(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); | ||
GLCall(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); | ||
GLCall(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); | ||
GLCall(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)); | ||
} | ||
|
||
Skybox::~Skybox() | ||
{ | ||
GLCall(glDeleteTextures(1, &mTextureId)); | ||
} | ||
|
||
void Skybox::bind() const | ||
{ | ||
GLCall(glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureId)); | ||
} | ||
|
||
void Skybox::unbind() const | ||
{ | ||
GLCall(glBindTexture(GL_TEXTURE_CUBE_MAP, 0)); | ||
} | ||
|
||
void Skybox::draw(const Camera& camera) const | ||
{ | ||
glDepthMask(GL_FALSE); | ||
constexpr auto numberOfVertices = 36; | ||
mShader.bind(); | ||
mVAO.bind(); | ||
bind(); | ||
const auto view = glm::mat4(glm::mat3(camera.view())); | ||
const auto projection = camera.projection(); | ||
mShader.setUniform("view", view); | ||
mShader.setUniform("projection", projection); | ||
GLCall(glDrawArrays(GL_TRIANGLES, 0, numberOfVertices)); | ||
glDepthMask(GL_TRUE); | ||
} | ||
|
||
std::vector<std::string> Skybox::skyboxFacesTextures() | ||
{ | ||
return {"resources/Textures/Skybox/right.jpg", "resources/Textures/Skybox/left.jpg", | ||
"resources/Textures/Skybox/bottom.jpg", "resources/Textures/Skybox/top.jpg", | ||
"resources/Textures/Skybox/front.jpg", "resources/Textures/Skybox/back.jpg"}; | ||
} | ||
|
||
std::vector<float> Skybox::skyboxVertices() | ||
{ | ||
return { | ||
// 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 // | ||
}; | ||
} | ||
|
||
GLenum Skybox::deduceImageFormat() | ||
{ | ||
switch (mNrChannels) | ||
{ | ||
case 1: return GL_RED; | ||
case 3: return GL_RGB; | ||
case 4: return GL_RGBA; | ||
} | ||
} | ||
|
||
void Skybox::loadTextureFace(const std::vector<std::string>& faces, unsigned faceId) | ||
{ | ||
unsigned char* mData = nullptr; | ||
mData = stbi_load(faces[faceId].c_str(), &mWidth, &mHeight, &mNrChannels, 0); | ||
auto format = deduceImageFormat(); | ||
if (mData) | ||
{ | ||
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + faceId, 0, format, mWidth, mHeight, 0, format, | ||
GL_UNSIGNED_BYTE, mData); | ||
} | ||
else | ||
{ | ||
spdlog::error("Failed to load a texture: {}", faces[faceId]); | ||
} | ||
stbi_image_free(mData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#pragma once | ||
#include <Renderer/Core/Buffers/BufferLayout.h> | ||
#include <Renderer/Core/Shader.h> | ||
#include <Renderer/Core/VertexArray.h> | ||
|
||
class Camera; | ||
|
||
class Skybox | ||
{ | ||
public: | ||
/** | ||
* \brief Constructor for the Skybox class, initializes the skybox environment. | ||
*/ | ||
Skybox(); | ||
|
||
/** | ||
* \brief Destructor for the Skybox class. | ||
*/ | ||
~Skybox(); | ||
|
||
/** | ||
* \brief Binds the skybox's textures and resources for rendering. | ||
*/ | ||
void bind() const; | ||
|
||
/** | ||
* \brief Unbinds the skybox's textures and resources. | ||
*/ | ||
void unbind() const; | ||
|
||
/** | ||
* \brief Draws the skybox using a camera's perspective. | ||
* \param camera The camera viewing the skybox. | ||
*/ | ||
void draw(const Camera& camera) const; | ||
|
||
private: | ||
/** | ||
* \brief Retrieves the file paths of the skybox's face textures. | ||
* \return Vector of strings representing paths to skybox face textures. | ||
*/ | ||
std::vector<std::string> skyboxFacesTextures(); | ||
|
||
/** | ||
* \brief Provides the vertices for the skybox. | ||
* \return Vector of floats representing the skybox vertices. | ||
*/ | ||
std::vector<float> skyboxVertices(); | ||
|
||
/** | ||
* \brief Determines the image format for texture loading. | ||
* \return The GLenum representing the image format. | ||
*/ | ||
GLenum deduceImageFormat(); | ||
|
||
/** | ||
* \brief Loads a texture of one face of the skybox. TODO: Make it more readable | ||
* \param faces The texture file paths for the skybox faces. | ||
* \param faceId The ID of the face to which the texture is applied. | ||
*/ | ||
void loadTextureFace(const std::vector<std::string>& faces, unsigned faceId); | ||
|
||
private: | ||
unsigned int mTextureId; | ||
float mAspectRatio; | ||
int mWidth, mHeight, mNrChannels; | ||
|
||
VertexArray mVAO; | ||
VertexBuffer mVBO; | ||
BufferLayout mBufferLayout; | ||
Shader mShader; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
#include "pch.h" | ||
#include "pch.h" | ||
|
||
#define STB_IMAGE_IMPLEMENTATION | ||
#include "stb_image.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,5 @@ | |
#include "spdlog/spdlog.h" | ||
|
||
// Other | ||
#include <result.hpp> | ||
#include "stb_image.h" | ||
#include <result.hpp> |