Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Nov 10, 2023
1 parent babe040 commit 276e675
Show file tree
Hide file tree
Showing 21 changed files with 985 additions and 11 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 = 10.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.2, 0.2, 0.2, 1.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;
}
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.
4 changes: 4 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ set(PROJECT_SOURCES
Renderer3D/OpenglUtils.cpp
Renderer3D/Texture.cpp
Renderer3D/Sprite2D.cpp
Renderer3D/Sprite3D.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
21 changes: 21 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Renderer3D.h"
#include "pch.h"
#include "World/Camera.h"

Renderer3D::Renderer3D(sf::Window& window)
: mWindow(window)
Expand All @@ -25,6 +26,26 @@ void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const Shader
#endif
}

void Renderer3D::draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const Camera& camera, const DrawMode& drawMode) const
{
shader.bind();
va.bind();
ib.bind();

const auto view = camera.view();
const auto projection = camera.projection();
shader.setUniform("view", view);
shader.setUniform("projection", projection);
GLCall(glDrawElements(toOpenGl(drawMode), ib.size(), GL_UNSIGNED_INT, nullptr));

#ifdef _DEBUG
shader.unbind();
va.unbind();
ib.unbind();
#endif
}

unsigned Renderer3D::toOpenGl(const Renderer3D::DrawMode& drawMode) const
{
switch (drawMode)
Expand Down
5 changes: 5 additions & 0 deletions AimGL/src/Renderer3D/Renderer3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Renderer3D/Shader.h"
#include "Renderer3D/VertexArray.h"

class Camera;

/**
* 3D Renderer allows to draw 3D objects to the screen using appropriate buffers and shaders
*/
Expand All @@ -29,6 +31,9 @@ class Renderer3D
void draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode = DrawMode::Triangles) const;

void draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const Camera& camera, const DrawMode& drawMode = DrawMode::Triangles) const;

private:
unsigned toOpenGl(const DrawMode& drawMode) const;
sf::Window& mWindow;
Expand Down
109 changes: 109 additions & 0 deletions AimGL/src/Renderer3D/Sprite3D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "Sprite3D.h"
#include "pch.h"
#include "World/Camera.h"

Sprite3D::Sprite3D(const Texture& texture)
: mTexture(texture)
, mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Sprite3D.vs"},
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Sprite3D.fs"}}
, mPosition(0.0f)
, mScale(1.0f)
, mRotation(0.f)
{

initializeBuffers();
//setScale(1);
setPosition({0.f, 0.f, 0.f});
}

void Sprite3D::initializeBuffers()
{
float vertices[] = {
// positions // texture coords
1.f, 1.f, 0.0f, 1.0f, 1.0f,// top right
1.f, -1.f, 0.0f, 1.0f, 0.0f,// bottom right
-1.f, -1.f, 0.0f, 0.0f, 0.0f,// bottom left
-1.f, 1.f, 0.0f, 0.0f, 1.0f // 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>(3);
mBufferLayout.push<float>(2);
mVAO.setBuffer(mVBO, mBufferLayout);
mVAO.unbind();
}

void Sprite3D::draw(const Renderer3D& target, const Camera& camera) const
{
// Prepare transformations
mShader.bind();
mTexture.bind(0);
target.draw(mVAO, mEBO, mShader, camera);
}

void Sprite3D::updateModel()
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(mPosition));
//model = glm::rotate(model, glm::radians(mRotation), glm::vec3(0.0f, 0.0f, 1.0f));
//model = glm::scale(model, glm::vec3(mScale, 1.0f));
mShader.bind();
mShader.setUniform("model", model);
mShader.unbind();
}

void Sprite3D::setPosition(const glm::vec3& newPosition, Origin origin)
{
switch (origin)
{
case Origin::Center: mPosition = newPosition; break;
case Origin::LeftBottom:
mPosition = newPosition;
mPosition += glm::vec3{1, 1, 0};
//mPosition += glm::vec3{mTexture.width(), mTexture.height(), 0.f};
break;
}
updateModel();
}

void Sprite3D::setScale(float newScale)
{
mScale = {mTexture.width(), mTexture.height()};
mScale *= newScale;
}

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

void Sprite3D::setOpacity(float opacity)
{
mOpacity = opacity;
mShader.bind();
mShader.setUniform("opacity", mOpacity);
mShader.unbind();
}

void Sprite3D::setWidth(float width)
{
mScale = {width, width / mTexture.aspectRatio()};
mScale /= 2.f;
updateModel();
}

void Sprite3D::setHeight(float height)
{
mScale = {height * mTexture.aspectRatio(), height};
mScale /= 2.f;
updateModel();
}
Loading

0 comments on commit 276e675

Please sign in to comment.