Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Dec 2, 2023
1 parent 0a8f72b commit bb3bbdd
Show file tree
Hide file tree
Showing 25 changed files with 259 additions and 8 deletions.
8 changes: 8 additions & 0 deletions AimGL/resources/Shaders/Graphics/Physics/Ray.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#version 330 core
out vec4 FragColor;

uniform vec3 rayColor = vec3(0,1,0);

void main() {
FragColor = vec4(rayColor, 1.0);
}
9 changes: 9 additions & 0 deletions AimGL/resources/Shaders/Graphics/Physics/Ray.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#version 330 core
layout (location = 0) in vec3 aPos;

uniform mat4 view;
uniform mat4 projection;

void main() {
gl_Position = projection * view * vec4(aPos, 1.0);
}
1 change: 1 addition & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ set(PROJECT_SOURCES
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/GameObjects/Rifle.cpp
World/Physics/Ray.cpp
Utils/Mouse.cpp
)
2 changes: 1 addition & 1 deletion AimGL/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Game::Game()
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
3 changes: 0 additions & 3 deletions AimGL/src/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ Player::Player(WindowToRender& window)

void Player::draw(const Renderer& target) const
{
glDepthRange(0.0, 0.01);
mRifle.draw(target);
glDepthRange(0.0, 1.0);

mCrosshair.draw(target);
}

Expand Down
17 changes: 16 additions & 1 deletion AimGL/src/Renderer/Core/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ Shader::Shader(std::initializer_list<ShaderSource> shaders)
GLCall(glUseProgram(mRendererId));
}

Shader::Shader(Shader&& rhs) noexcept
: mRendererId(rhs.mRendererId)
, mUniformLocationCache(std::move(rhs.mUniformLocationCache))
{
rhs.mRendererId = 0;
}

Shader& Shader::operator=(Shader&& rhs) noexcept
{
mRendererId = rhs.mRendererId;
mUniformLocationCache = std::move(rhs.mUniformLocationCache);
rhs.mRendererId = 0;
return *this;
}

Shader::~Shader()
{
GLCall(glDeleteProgram(mRendererId));
Expand Down Expand Up @@ -93,7 +108,7 @@ unsigned Shader::getUniformLocation(const std::string& name) const
GLCall(auto location = glGetUniformLocation(mRendererId, name.c_str()));
if (location == -1)
{
spdlog::warn("No uniform with name {} exist", name);
spdlog::debug("No uniform with name {} exist", name);
}

mUniformLocationCache[name] = location;
Expand Down
4 changes: 2 additions & 2 deletions AimGL/src/Renderer/Core/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Shader
Shader(std::initializer_list<ShaderSource> shaders);

Shader(const Shader&) = delete;
Shader(Shader&&) noexcept = default;
Shader(Shader&&) noexcept;
Shader& operator=(const Shader&) = delete;
Shader& operator=(Shader&&) noexcept = default;
Shader& operator=(Shader&&) noexcept;
~Shader();

/**
Expand Down
19 changes: 19 additions & 0 deletions AimGL/src/Renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ void Renderer::draw3D(const VertexArray& va, const IndexBuffer& ib, const Shader
#endif
}

void Renderer::draw3D(const VertexArray& va, int numberOfVertices, const Shader& shader,
const Camera& camera, const DrawMode& drawMode) const
{
shader.bind();
va.bind();

const auto view = camera.view();
const auto projection = camera.projection();
shader.setUniform("view", view);
shader.setUniform("projection", projection);
shader.setUniform("cameraPosition", camera.cameraPosition());
GLCall(glDrawArrays(toOpenGl(drawMode), 0, numberOfVertices));

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

unsigned Renderer::toOpenGl(const Renderer::DrawMode& drawMode) const
{
switch (drawMode)
Expand Down
25 changes: 25 additions & 0 deletions AimGL/src/Renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,35 @@ class Renderer
void draw2D(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode = DrawMode::Triangles) const;

/**
* \brief TODO: THIS
* \param va TODO: THIS
* \param ib TODO: THIS
* \param shader TODO: THIS
* \param camera TODO: THIS
* \param drawMode TODO: THIS
*/
void draw3D(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const Camera& camera, const DrawMode& drawMode = DrawMode::Triangles) const;

/**
* \brief TODO: THIS
* \param va TODO: THIS
* \param numberOfVertices TODO: THIS
* \param shader TODO: THIS
* \param camera TODO: THIS
* \param drawMode TODO: THIS
*/
void draw3D(const VertexArray& vb, int numberOfVertices, const Shader& shader,
const Camera& camera, const DrawMode& drawMode) const;

private:
/**
* \brief TODO: THIS
* \param drawMode TODO: THIS
* \return TODO: THIS
*/
unsigned toOpenGl(const DrawMode& drawMode) const;

sf::Window& mWindow;
};
2 changes: 2 additions & 0 deletions AimGL/src/States/CustomStates/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GameState::GameState(StateStack& stack, WindowToRender& window)
, mPhaseInLogoColor({window.getSize().x, window.getSize().y}, {0.067f, 0.11f, 0.18f, 1.1f})
, mTree("resources/Models/tree/tree.obj",
{{"resources/Models/tree/tree_combined.png", Texture::Type::Diffuse}})
, mRay({0, 0, 0}, {1, 1, 0})
{
Mouse::lockMouseAtCenter(mWindow);
mTree.setScale(0.2f);
Expand All @@ -39,6 +40,7 @@ void GameState::draw(sf::Window& target) const
mLogo.draw(mRenderer, mPlayer.camera());
mPhaseInLogoColor.draw(mRenderer);
mPlayer.draw(mRenderer);
mRay.draw(mRenderer, mPlayer.camera());
}

bool GameState::fixedUpdate(const float& deltaTime)
Expand Down
2 changes: 2 additions & 0 deletions AimGL/src/States/CustomStates/GameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <Player/Player.h>
#include <Renderer/Graphics/3D/Model.h>
#include <World/Physics/Ray.h>

class StateStack;

Expand Down Expand Up @@ -62,4 +63,5 @@ class GameState : public State
Rectangle2D mPhaseInLogoColor;
InfiniteGridFloor mInfiniteGridFloor;
Model mTree;
Ray mRay;
};
7 changes: 7 additions & 0 deletions AimGL/src/World/GameObjects/Rifle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ Rifle::Rifle(Camera& camera)

void Rifle::draw(const Renderer& target) const
{
glDepthRange(0.0, 0.01);
mGun.draw(target, mCamera);
glDepthRange(0.0, 1.0);
for (const auto& ray: mRays)
{
ray.draw(target, mCamera);
}
}

void Rifle::update(const float& deltaTime)
Expand Down Expand Up @@ -58,5 +64,6 @@ void Rifle::handleEvent(const sf::Event& event)
mCurrentRecoil = std::min(mCurrentRecoil, RECOIL_OFFSET_MAX);
mCamera.shake();
mGunShotSound.play();
mRays.emplace_back(mCamera.cameraPosition(), mCamera.direction());
}
}
2 changes: 2 additions & 0 deletions AimGL/src/World/GameObjects/Rifle.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "World/Physics/Ray.h"
#include <Renderer/Graphics/3D/Model.h>
#include <SFML/Audio/Sound.hpp>
#include <SFML/Audio/SoundBuffer.hpp>
Expand Down Expand Up @@ -51,4 +52,5 @@ class Rifle
float mCurrentRecoil{0};
sf::SoundBuffer mSoundBuffer;
sf::Sound mGunShotSound;
std::vector<Ray> mRays;
};
2 changes: 2 additions & 0 deletions AimGL/src/World/GameObjects/Target.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "Target.h"
#include "pch.h"
5 changes: 5 additions & 0 deletions AimGL/src/World/GameObjects/Target.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

class Target
{
};
2 changes: 2 additions & 0 deletions AimGL/src/World/Physics/Collider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "Collider.h"
#include "pch.h"
13 changes: 13 additions & 0 deletions AimGL/src/World/Physics/Collider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

class SphereCollider;
class RectangleCollider;

class Collider
{
public:
virtual ~Collider() = default;
virtual bool checkCollision(const Collider& other) const = 0;
virtual bool checkCollisionWith(const SphereCollider& other) const = 0;
virtual bool checkCollisionWith(const RectangleCollider& other) const = 0;
};
8 changes: 8 additions & 0 deletions AimGL/src/World/Physics/Collisions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

bool checkSphereRectangleCollision(const SphereCollider& sphere, const RectangleCollider& rectangle)
{
glm::vec3 closestPoint = glm::clamp(sphere.center, aabb.min, aabb.max);
float distanceSquared = glm::distance2(sphere.center, closestPoint);
return distanceSquared < (sphere.radius * sphere.radius);
}
27 changes: 27 additions & 0 deletions AimGL/src/World/Physics/Ray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Ray.h"
#include "pch.h"

#include <Renderer/Renderer.h>

Ray::Ray(const glm::vec3& origin, const glm::vec3& direction, float length)
: mOrigin(origin)
, mDirection(glm::normalize(direction))
, mLength(length)
, mShader{{ShaderType::VertexShader, "resources/Shaders/Graphics/Physics/Ray.vs"},
{ShaderType::FragmentShader, "resources/Shaders/Graphics/Physics/Ray.fs"}}
{
glm::vec3 endPoint = mOrigin + mDirection * mLength;

std::vector<float> vertices = {mOrigin.x, mOrigin.y, mOrigin.z,
endPoint.x, endPoint.y, endPoint.z};
mBufferLayout.push<float>(3);

mVBO.setBuffer(vertices);
mVAO.setBuffer(mVBO, mBufferLayout);
}

void Ray::draw(const Renderer& target, const Camera& camera) const
{
constexpr auto numberOfVertices = 6;
target.draw3D(mVAO, numberOfVertices, mShader, camera, Renderer::DrawMode::Lines);
}
38 changes: 38 additions & 0 deletions AimGL/src/World/Physics/Ray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include "Renderer/Core/Buffers/BufferLayout.h"
#include "Renderer/Core/Buffers/VertexBuffer.h"
#include "Renderer/Core/VertexArray.h"

#include <Renderer/Core/Shader.h>

class Camera;
class Renderer;

class Ray
{
public:
Ray(const glm::vec3& origin, const glm::vec3& direction, float length = 100);

/**
* \brief Draws a model for a given target
* \param target The target to which the model is drawn
* \param camera A camera in 3D space that looks at this object
*/
void draw(const Renderer& target, const Camera& camera) const;


/**
* Updates the Player logic dependent, or independent of time, every rendered frame.
* \param deltaTime the time that has passed since the game was last updated.
*/
void update(const float& deltaTime);

private:
VertexArray mVAO;
VertexBuffer mVBO;
BufferLayout mBufferLayout;
glm::vec3 mOrigin;
glm::vec3 mDirection;
float mLength;
Shader mShader;
};
2 changes: 2 additions & 0 deletions AimGL/src/World/Physics/RectangleCollider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "RectangleCollider.h"
#include "pch.h"
31 changes: 31 additions & 0 deletions AimGL/src/World/Physics/RectangleCollider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

class RectangleCollider : public Collider
{
public:
glm::vec3 min;// Minimum point
glm::vec3 max;// Maximum point

RectangleCollider(const glm::vec3& min, const glm::vec3& max)
: min(min)
, max(max)
{
}

bool checkCollision(const Collider& other) const override
{
return other.checkCollisionWith(*this);
}

bool checkCollisionWith(const SphereCollider& other) const override
{
return checkSphereRectangleCollision(other, *this);
}

bool checkCollisionWith(const RectangleCollider& other) const override
{
return (min.x <= other.max.x && max.x >= other.min.x) &&
(min.y <= other.max.y && max.y >= other.min.y) &&
(min.z <= other.max.z && max.z >= other.min.z);
}
};
2 changes: 2 additions & 0 deletions AimGL/src/World/Physics/SphereCollider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "SphereCollider.h"
#include "pch.h"
31 changes: 31 additions & 0 deletions AimGL/src/World/Physics/SphereCollider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

class SphereCollider : public Collider
{
public:
glm::vec3 center;
float radius;

SphereCollider(const glm::vec3& center, float radius)
: center(center)
, radius(radius)
{
}

bool checkCollision(const Collider& other) const override
{
return other.checkCollisionWith(*this);
}

bool checkCollisionWith(const SphereCollider& other) const override
{
float distanceSquared = glm::distance2(center, other.center);
float radiusSum = radius + other.radius;
return distanceSquared < (radiusSum * radiusSum);
}

bool checkCollisionWith(const RectangleCollider& other) const override
{
return checkSphereRectangleCollision(*this, other);
}
};
Loading

0 comments on commit bb3bbdd

Please sign in to comment.