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 170e228
Show file tree
Hide file tree
Showing 25 changed files with 276 additions and 13 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);
}
3 changes: 2 additions & 1 deletion AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(PROJECT_SOURCES
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/GameObjects/Rifle.cpp
World/Scene/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
2 changes: 1 addition & 1 deletion AimGL/src/Player/Player.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include <Renderer/Graphics/2D/Sprite2D.h>
#include <World/Camera.h>
#include <World/GameObjects/Rifle.h>
#include <World/Scene/GameObjects/Rifle.h>

class Renderer;
class Camera;
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
33 changes: 31 additions & 2 deletions AimGL/src/Renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,48 @@ class Renderer
explicit Renderer(sf::Window& window);

/**
* Draws the data given in VertexArray, IndexBuffer to the screen using the interpretation given
* in Shader.
* \brief Draws the data given in VertexArray, IndexBuffer to the screen using
* the interpretation given in Shader.
* @param va Stores all Vertex Data.
* @param ib Specifies the drawing order of the VertexArray.
* @param shader Shader telling how to draw data.
*/
void draw2D(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const DrawMode& drawMode = DrawMode::Triangles) const;

/**
* \brief Draws the data given in VertexArray, IndexBuffer to the screen using
* the interpretation given in Shader. In additon information about view, projection
* and position are passed to the shader from camera.
* \param va Vertex array containing the object's vertices.
* \param ib Index buffer defining the order to draw the vertices.
* \param shader Shader program to apply during rendering.
* \param camera Camera object defining the view and projection matrices.
* \param drawMode (Optional) Specifies the mode of drawing (e.g., triangles, lines).
*/
void draw3D(const VertexArray& va, const IndexBuffer& ib, const Shader& shader,
const Camera& camera, const DrawMode& drawMode = DrawMode::Triangles) const;

/**
* \brief Draws the data given in VertexArray with specified number of vertices to
* the screen using the interpretation given in Shader. In additon information about
* view, projection and position are passed to the shader from camera.
* \param va Vertex array containing the object's vertices.
* \param numberOfVertices Number of vertices to draw.
* \param shader Shader program to use during rendering.
* \param camera Camera object for view and projection.
* \param drawMode Specifies the drawing mode (e.g., points, lines).
*/
void draw3D(const VertexArray& vb, int numberOfVertices, const Shader& shader,
const Camera& camera, const DrawMode& drawMode) const;

private:
/**
* \brief Convert a DrawMode enum value to the corresponding OpenGL mode.
* \param drawMode DrawMode enum value.
* \return Corresponding OpenGL drawing mode (e.g., GL_TRIANGLES).
*/
unsigned toOpenGl(const DrawMode& drawMode) const;

sf::Window& mWindow;
};
1 change: 1 addition & 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
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);
}
47 changes: 47 additions & 0 deletions AimGL/src/World/Physics/Ray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#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;

/**
* \brief Represents a ray in 3D space, defined by an origin point, direction vector, and length.
*/
class Ray
{
public:
/**
* \brief Constructs a Ray object.
* \param origin The starting point of the ray in 3D space.
* \param direction The direction vector of the ray. This vector should be normalized.
* \param length (Optional) The length of the ray. Default is 100 units.
*/
Ray(const glm::vec3& origin, const glm::vec3& direction, float length = 100);
Ray(Ray&&) noexcept = default;

/**
* \brief Draws a Ray 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 Ray 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);
}
};
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);
if (mLatelyShotRay.has_value())
{
mLatelyShotRay.value().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();
mLatelyShotRay.emplace(mCamera.cameraPosition(), mCamera.direction());
}
}
}
Loading

0 comments on commit 170e228

Please sign in to comment.