Skip to content

Commit

Permalink
Add skybox
Browse files Browse the repository at this point in the history
Adds skybox to the game. This was solved somewhat
by shortcuts due to a coming deadline.
  • Loading branch information
Notiooo committed Jan 13, 2024
1 parent 1043b7a commit eaaa067
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 9 deletions.
11 changes: 11 additions & 0 deletions AimGL/resources/Shaders/Graphics/Skybox.fs
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);
}
13 changes: 13 additions & 0 deletions AimGL/resources/Shaders/Graphics/Skybox.vs
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);
}
Binary file added AimGL/resources/Textures/Skybox/back.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Textures/Skybox/bottom.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Textures/Skybox/front.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Textures/Skybox/left.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Textures/Skybox/right.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Textures/Skybox/top.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(PROJECT_SOURCES
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/Skybox.cpp
World/Scene/GameObjects/Rifle.cpp
World/Scene/GameObjects/Target.cpp
World/Scene/GameObjects/TargetManager.cpp
Expand Down
3 changes: 0 additions & 3 deletions AimGL/src/Renderer/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

#include "Renderer/Core/OpenglUtils.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

Texture::Texture(const std::string& filePath, Type type)
: mTextureId(0)
, mFilePath(filePath)
Expand Down
4 changes: 1 addition & 3 deletions AimGL/src/States/CustomStates/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ GameState::GameState(StateStack& stack, WindowToRender& window)
, mWindow(window)
, mPlayer(window, mColliderRegister)
, mRenderer(mWindow)
, mGameBackground(glm::vec2(1280, 720.f), glm::vec4(0.85f, 0.85f, 0.85f, 1.f))
, mPhaseInLogoColor({window.getSize().x, window.getSize().y}, {0.067f, 0.11f, 0.18f, 1.1f})
, mShootingRange(mColliderRegister, {-2, 0, 5.5})
, mSidewayMovingTargetsRange(mColliderRegister, {3, 0, 5.5})
Expand All @@ -22,15 +21,14 @@ GameState::GameState(StateStack& stack, WindowToRender& window)
Mouse::lockMouseAtCenter(mWindow);
mWelcomeScreen.setScale(2);
mWelcomeScreen.setPosition({0, 1, -2}, Sprite3D::Origin::Center);
mGameBackground.setPosition({0, 0});
mPhaseInLogoColor.setPosition({0, 0});
mPhaseInClock.restart();
}

void GameState::draw(sf::Window& target) const
{
MTR_SCOPE("GameState", "GameState::draw");
mGameBackground.draw(mRenderer);
mSkybox.draw(mPlayer.camera());
mInfiniteGridFloor.draw(target, mPlayer.camera());
mWelcomeScreen.draw(mRenderer, mPlayer.camera());
mPhaseInLogoColor.draw(mRenderer);
Expand Down
3 changes: 2 additions & 1 deletion AimGL/src/States/CustomStates/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <World/Physics/ColliderRegister.h>
#include <World/Scene/GameObjects/ShootingRange.h>
#include <World/Scene/GameObjects/SidewayMovingTargetsRange.h>
#include <World/Skybox.h>

class StateStack;

Expand Down Expand Up @@ -83,13 +84,13 @@ class GameState : public State
ColliderRegister mColliderRegister;
Player mPlayer;
Renderer mRenderer;
Rectangle2D mGameBackground;
sf::Clock mPhaseInClock;
Rectangle2D mPhaseInLogoColor;
InfiniteGridFloor mInfiniteGridFloor;
ShootingRange mShootingRange;
SidewayMovingTargetsRange mSidewayMovingTargetsRange;
Texture mWelcomeScreenTexture;
Sprite3D mWelcomeScreen;
Skybox mSkybox;
bool mDrawImgui{true};
};
144 changes: 144 additions & 0 deletions AimGL/src/World/Skybox.cpp
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);
}
72 changes: 72 additions & 0 deletions AimGL/src/World/Skybox.h
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;
};
5 changes: 4 additions & 1 deletion AimGL/src/pch.cpp
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"
3 changes: 2 additions & 1 deletion AimGL/src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
#include "spdlog/spdlog.h"

// Other
#include <result.hpp>
#include "stb_image.h"
#include <result.hpp>

0 comments on commit eaaa067

Please sign in to comment.