Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Nov 11, 2023
1 parent babe040 commit ee98984
Show file tree
Hide file tree
Showing 31 changed files with 1,309 additions and 14 deletions.
3 changes: 2 additions & 1 deletion AimGL/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
out/
.vshistory/
.vshistory/
CMakeSettings.json
48 changes: 48 additions & 0 deletions AimGL/resources/Shaders/Graphics/InfiniteGridFloor.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#version 330 core
// THANK YOU https://asliceofrendering.com/scene%20helper/2020/01/05/InfiniteGrid/!!!

out vec4 FragColor;

float near = 0.01f;
float far = 20.f;

in vec3 nearPoint;
in vec3 farPoint;
in mat4 fragView;
in mat4 fragProjection;

vec4 grid(vec3 fragPos3D, float scale) {
vec2 coord = fragPos3D.xz * scale;
vec2 derivative = fwidth(coord);
vec2 grid = abs(fract(coord - 0.5) - 0.5) / derivative;
float line = min(grid.x, grid.y);
float minimumz = min(derivative.y, 1);
float minimumx = min(derivative.x, 1);
vec4 color = vec4(0.3, 0.3, 0.3, 0.0 + min(line, 1.0));
return color;
}

float computeDepth(vec3 position) {
vec4 clip_space_position = fragProjection * fragView * vec4 (position.xyz, 1.0);
return 0.5 + 0.5 * (clip_space_position.z / clip_space_position.w);
}

float computeLinearDepth(vec3 pos) {
vec4 clip_space_pos = fragProjection * fragView * vec4(pos.xyz, 1.0);
float clip_space_depth = (clip_space_pos.z / clip_space_pos.w) * 2.0 - 1.0; // put back between -1 and 1
float linearDepth = (2.0 * near * far) / (far + near - clip_space_depth * (far - near)); // get linear value between 0.01 and 100
return linearDepth / far; // normalize
}

void main()
{
float t = -nearPoint.y / (farPoint.y - nearPoint.y);
vec3 fragPos3D = nearPoint + t * (farPoint - nearPoint);
gl_FragDepth = computeDepth(fragPos3D);

float linearDepth = computeLinearDepth(fragPos3D);
float fading = max(0, (0.5 - linearDepth));

FragColor = (grid(fragPos3D, 10) + grid(fragPos3D, 1))* float(t > 0); // adding multiple resolution for the grid
FragColor.a *= fading;
}
32 changes: 32 additions & 0 deletions AimGL/resources/Shaders/Graphics/InfiniteGridFloor.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#version 330
// THANK YOU https://asliceofrendering.com/scene%20helper/2020/01/05/InfiniteGrid/!!!

uniform mat4 view;
uniform mat4 projection;

out vec3 nearPoint;
out vec3 farPoint;
out mat4 fragView;
out mat4 fragProjection;

vec3 UnprojectPoint(float x, float y, float z, mat4 view, mat4 projection) {
mat4 viewInv = inverse(view);
mat4 projInv = inverse(projection);
vec4 unprojectedPoint = viewInv * projInv * vec4(x, y, z, 1.0);
return unprojectedPoint.xyz / unprojectedPoint.w;
}


vec3 gridPlane[6] = vec3[](
vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0)
);

void main() {
vec3 p = gridPlane[gl_VertexID].xyz;
nearPoint = UnprojectPoint(p.x, p.y, 0.0, view, projection).xyz; // unprojecting on the near plane
farPoint = UnprojectPoint(p.x, p.y, 1.0, view, projection).xyz; // unprojecting on the far plane
gl_Position = vec4(p, 1.0); // using directly the clipped coordinates
fragView = view;
fragProjection = projection;
}
12 changes: 12 additions & 0 deletions AimGL/resources/Shaders/Graphics/Rectangle2D.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 330 core

out vec4 FragColor;

uniform vec4 color = vec4(1,0,0, 1.f);

void main()
{
FragColor = color;
if(FragColor.a == 0.0)
discard;
}
13 changes: 13 additions & 0 deletions AimGL/resources/Shaders/Graphics/Rectangle2D.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330 core

layout(location = 0) in vec2 position; // Vertex position

uniform mat4 model;
uniform mat4 projection = mat4(1.0); // For 2D it is identity
uniform mat4 windowOrthoProjection;

void main()
{
mat4 mvp = windowOrthoProjection * model;
gl_Position = mvp * vec4(position, 0.0, 1.0);
}
2 changes: 1 addition & 1 deletion AimGL/resources/Shaders/Graphics/Sprite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ in vec2 TexCoords;
out vec4 color;

uniform sampler2D spriteTexture;
uniform float opacity = 0.f;
uniform float opacity = 1.f;

void main()
{
Expand Down
15 changes: 15 additions & 0 deletions AimGL/resources/Shaders/Graphics/Sprite3D.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 330 core

in vec2 TexCoords;
out vec4 color;

uniform sampler2D spriteTexture;
uniform float opacity = 1.f;

void main()
{
color = texture(spriteTexture, TexCoords);
color.a = min(color.a, opacity);
if(color.a == 0.0)
discard;
}
17 changes: 17 additions & 0 deletions AimGL/resources/Shaders/Graphics/Sprite3D.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#version 330 core

layout(location = 0) in vec3 position; // Vertex position
layout(location = 1) in vec2 texCoords; // Texture coordinates

out vec2 TexCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
TexCoords = texCoords;
mat4 mvp = projection * view * model;
gl_Position = mvp * vec4(position, 1.0);
}
Binary file added AimGL/resources/Textures/logo_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ set(PROJECT_SOURCES
Renderer3D/OpenglUtils.cpp
Renderer3D/Texture.cpp
Renderer3D/Sprite2D.cpp
Renderer3D/Sprite3D.cpp
Renderer3D/Rectangle2D.cpp
States/State.cpp
States/StateStack.cpp
States/CustomStates/LogoState.cpp
States/CustomStates/ExitGameState.cpp
States/CustomStates/GameState.cpp
World/Camera.cpp
Utils/Mouse.cpp
)
7 changes: 6 additions & 1 deletion AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "Game.h"
#include "States/CustomStates/ExitGameState.h"
#include "States/CustomStates/LogoState.h"
#include "States/CustomStates/GameState.h"
#include "constants.h"
#include "pch.h"
#include "Utils/Mouse.h"


constexpr int FRAMES_PER_SECOND = 120;
Expand Down Expand Up @@ -60,15 +62,17 @@ Game::Game()
throw std::runtime_error("Failed to initialize GLEW");
}

glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Setup all application-flow states
mAppStack.saveState<LogoState>(State_ID::LogoState, *mGameWindow);
mAppStack.saveState<ExitGameState>(State_ID::ExitGameState);
mAppStack.saveState<GameState>(State_ID::GameState, *mGameWindow);

// Initial state of the statestack is TitleState
mAppStack.push(State_ID::LogoState);
mAppStack.push(State_ID::GameState);
}

void Game::run()
Expand Down Expand Up @@ -206,6 +210,7 @@ void Game::update(const sf::Time& deltaTime)
{
MTR_SCOPE("Game", "Game::update");
auto deltaTimeInSeconds = deltaTime.asSeconds();
Mouse::update(deltaTimeInSeconds, *mGameWindow);

updateImGui(deltaTime);

Expand Down
129 changes: 129 additions & 0 deletions AimGL/src/Renderer3D/Rectangle2D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include "Rectangle2D.h"
#include "pch.h"

Rectangle2D::Rectangle2D(const glm::vec2 size, const glm::vec4& color)
: mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Rectangle2D.vs"},
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Rectangle2D.fs"}}
, mColor(color)
, mPosition(0.0f)
, mRotation(0.f)
, mDimensions(size)
{

initializeBuffers();
setPosition({0.f, 0.f}, Origin::Center);

updateColor();
}

void Rectangle2D::initializeBuffers()
{
float vertices[] = {
// positions
1.f, 1.f, // top right
1.f, -1.f,// bottom right
-1.f, -1.f,// bottom left
-1.f, 1.f // top left
};

unsigned int indices[] = {
0, 1, 3,// first Triangle
1, 2, 3 // second Triangle
};

mVBO.setBuffer(vertices, sizeof(vertices));
mEBO.setBuffer(indices, sizeof(indices));

mVAO.bind();
mVBO.bind();
mBufferLayout.push<float>(2);
mVAO.setBuffer(mVBO, mBufferLayout);
mVAO.unbind();
}

void Rectangle2D::draw(const Renderer3D& target) const
{
target.draw(mVAO, mEBO, mShader);
}

void Rectangle2D::updateColor()
{
mShader.bind();
mShader.setUniform("color", mColor);
mShader.unbind();
}

void Rectangle2D::updateModel()
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(mPosition));

// Negative mRotation makes it rotate clockwise for positive angles
model = glm::rotate(model, glm::radians(-mRotation), glm::vec3(0.0f, 0.0f, 1.0f));

// Dividing dimensions by two is needed as vertices are from -1 to 1
model = glm::scale(model, glm::vec3(mDimensions.x / 2.f, mDimensions.y / 2.f, 1.0f));
mShader.bind();
mShader.setUniform("model", model);
mShader.unbind();
}

void Rectangle2D::setPosition(const glm::vec2& newPosition, Origin origin)
{
switch (origin)
{
case Origin::Center: mPosition = glm::vec3(newPosition, 0.f); break;
case Origin::LeftBottom:
mPosition = glm::vec3(newPosition, 0.f);
mPosition += glm::vec3{mDimensions.x / 2.f, mDimensions.y / 2.f, 0.f};
break;
}
updateModel();
}

void Rectangle2D::setColor(glm::vec4 color)
{
mColor = color;
updateColor();
}

void Rectangle2D::setRotation(float angle)
{
mRotation = angle;
updateModel();
}

void Rectangle2D::setOpacity(float opacity)
{
mColor.a = opacity;
updateColor();
}

void Rectangle2D::setWidth(float width)
{
mDimensions.x = width;
updateModel();
}

void Rectangle2D::setHeight(float height)
{
mDimensions.y = height;
updateModel();
}

void Rectangle2D::showDebugImGui(std::string name)
{
name = "[Rectangle2D] " + name;
ImGui::Begin(name.c_str());
ImGui::SliderFloat4("Color", &mColor[0], 0.0f, 1.0f);
ImGui::SliderFloat2("Position", &mPosition[0], -1500.f, 1500.f);
ImGui::SliderFloat("Rotation", &mRotation, 0.0f, 360.f);
ImGui::End();
updateModel();
updateColor();
}

float Rectangle2D::opacity()
{
return mColor.a;
}
Loading

0 comments on commit ee98984

Please sign in to comment.