Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Dec 1, 2023
1 parent b6e7aec commit f2db0d4
Show file tree
Hide file tree
Showing 30 changed files with 429 additions and 44 deletions.
Binary file added AimGL/resources/Sounds/footsteps.wav
Binary file not shown.
Binary file added AimGL/resources/Sounds/gunshot.wav
Binary file not shown.
Binary file added AimGL/resources/Textures/muzzle_flash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions AimGL/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ target_include_directories(AimGLSrc PUBLIC
${CUSTOM_INCLUDES_DIR}
)


set_target_properties(sfml-audio PROPERTIES CXX_STANDARD 98 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)

target_link_libraries(AimGLSrc
PUBLIC
sfml-graphics
sfml-audio
ImGui-SFML::ImGui-SFML
glew_s
glm
Expand All @@ -53,11 +57,21 @@ add_custom_command(TARGET AimGLSrc POST_BUILD
$<TARGET_FILE:sfml-graphics>
$<TARGET_FILE:sfml-window>
$<TARGET_FILE:sfml-system>
$<TARGET_FILE:sfml-audio>
$<TARGET_FILE:spdlog>
$<TARGET_FILE:minitrace>
$<TARGET_FILE_DIR:AimGLSrc>
)

# Copy OpenAL32.dll for SFML AUDIO
if (WIN32 AND SFML_BUILD_AUDIO)
add_custom_command(TARGET AimGLSrc POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${FETCHCONTENT_BASE_DIR}/sfml-src/extlibs/bin/x64/openal32.dll
$<TARGET_FILE_DIR:AimGLSrc>
)
endif()

# Remove previous game resources

add_custom_command(TARGET AimGLSrc PRE_BUILD
Expand Down
2 changes: 2 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ set(PROJECT_SOURCES
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/GameObjects/Target.cpp
World/GameObjects/Rifle.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
51 changes: 27 additions & 24 deletions AimGL/src/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,34 @@

Player::Player(WindowToRender& window)
: mCamera(window)
, mGun("resources/Models/ak47/ak47.obj",
{{"resources/Models/ak47/ak47-alternative.png", Texture::Type::Diffuse},
{"resources/Models/ak47/ak47-alternative-specular.png", Texture::Type::Specular}})
, mCrosshairTexture("resources/Textures/crosshair.png")
, mCrosshair(mCrosshairTexture)
, mRifle(mCamera)
{
mCrosshair.setPosition({window.getSize().x / 2.f, window.getSize().y / 2.f},
Sprite2D::Origin::Center);
mCrosshair.setOpacity(0.8f);
mCamera.cameraPosition({mPosition.x, mPosition.y + PLAYER_HEGIHT, mPosition.z});
mGun.setPosition(mCamera.cameraPosition(), Model::Origin::Center);
mSoundBuffer.loadFromFile("resources/Sounds/footsteps.wav");
mWalkingSound.setBuffer(mSoundBuffer);
mWalkingSound.setVolume(50);
mRifle.updateAttachToCamera(mCamera, 1);
}

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

mCrosshair.draw(target);
}

void Player::updateGunPosition(const float& deltaTime)
{
const glm::vec3 rotationOrigin = {0.3f, -0.2f, -0.35f};
const glm::vec3 targetPosition = {mPosition.x + rotationOrigin.x,
mPosition.y + PLAYER_HEGIHT * 5 / 8,
mPosition.z + rotationOrigin.z};

const auto gunPosition = lerp(mGun.position(), targetPosition, deltaTime * 30.f);
mGun.setPosition(gunPosition, Model::Origin::Center);
mGun.setRotationOrigin(rotationOrigin);

const auto gunTargetPitch =
lerp(mGun.rotation().pitch, mCamera.rotation().pitch, deltaTime * 20.f);
const auto gunTargetYaw =
lerp(mGun.rotation().yaw, -(mCamera.rotation().yaw + 90), deltaTime * 20.f);
mGun.setRotation({gunTargetYaw, gunTargetPitch, 0});
}

void Player::update(const float& deltaTime)
{
mCamera.cameraPosition({mPosition.x, mPosition.y + PLAYER_HEGIHT, mPosition.z});
mCamera.update(deltaTime);
updateGunPosition(deltaTime);
mRifle.updateAttachToCamera(mCamera, deltaTime);
}

void Player::updatePhysics(float deltaTime)
Expand Down Expand Up @@ -130,6 +113,25 @@ void Player::handleMovementKeyboardInputs(const float& deltaTime)
direction -= mCamera.rightDirectionWithoutPitch();
}


auto noDirection = glm::vec3(0.0f, 0.0f, 0.0f);
switch (mWalkingSound.getStatus())
{
case sf::Sound::Status::Playing:
if (direction == noDirection or not isOnGround())
{
mWalkingSound.stop();
}
break;
case sf::Sound::Status::Paused:
case sf::Sound::Status::Stopped:
if (direction != noDirection and isOnGround())
{
mWalkingSound.play();
}
break;
}

if (glm::length(direction) > 0.0f)
{
direction = glm::normalize(direction);// Normalize the combined direction
Expand All @@ -152,6 +154,7 @@ void Player::handleEvent(const sf::Event& event)
{
case sf::Keyboard::Space: tryJump(); break;
}
mRifle.handleEvent(event);
}

Camera& Player::camera()
Expand Down
13 changes: 5 additions & 8 deletions 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 <Renderer/Graphics/3D/Model.h>
#include <World/Camera.h>
#include <World/GameObjects/Rifle.h>

class Renderer;
class Camera;
Expand Down Expand Up @@ -113,17 +113,14 @@ class Player
*/
bool isOnGround() const;

/**
* \brief Updates the position of the player's weapon
* \param deltaTime the time that has passed since the game was last updated.
*/
void updateGunPosition(const float& deltaTime);

private:
Camera mCamera;
glm::vec3 mPosition{0, 0, 0};
glm::vec3 mVelocity{0, 0, 0};
Model mGun;
Rifle mRifle;
Texture mCrosshairTexture;
Sprite2D mCrosshair;
sf::SoundBuffer mSoundBuffer;
sf::Sound mWalkingSound;
bool isWalking{false};
};
12 changes: 12 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ glm::vec3 Model::position() const
return mPosition;
}

glm::vec3 Model::realPosition() const
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(mPosition));
model = glm::translate(model, -mRotationOrigin);
model = mRotation.rotate(model);
model = glm::translate(model, mRotationOrigin);
model = glm::scale(model, glm::vec3(mScale, mScale, mScale));

return glm::vec3(model[3]);
}

glm::vec3 Model::dimensions() const
{
return mObjLoader.dimensions() * mScale;
Expand Down
2 changes: 2 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class Model
*/
glm::vec3 position() const;

glm::vec3 realPosition() const;

/**
* \brief Gets the model's dimensions.
* \return The 3D vector representing the model's dimensions.
Expand Down
19 changes: 19 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Sprite3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,22 @@ void Sprite3D::showDebugImGui(std::string name)
updateModel();
updateOpacity();
}

void Sprite3D::faceToCamera(const Camera& camera)
{
glm::vec3 upDirection = camera.upwardDirection();// Standard up direction in Y

// Create a lookAt matrix. This matrix makes the sprite look at the camera.
glm::mat4 lookAtMatrix = glm::lookAt(mPosition, camera.cameraPosition(), upDirection);

// Extract the rotation from the matrix
glm::quat rotationQuat = glm::quat_cast(glm::inverse(lookAtMatrix));

// Convert the quaternion to Euler angles
glm::vec3 eulerRotation = glm::eulerAngles(rotationQuat);

// Set the sprite's rotation. GLM returns the rotation in radians.
auto rotation = glm::degrees(eulerRotation);
mRotation = {rotation.y, rotation.x, rotation.z};
updateModel();
}
3 changes: 3 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Sprite3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class Sprite3D
*/
void showDebugImGui(std::string name = "");

void faceToCamera(const Camera& camera);

private:
/**
* \brief Updates the model in the shader (model of MVP)
Expand Down Expand Up @@ -114,5 +116,6 @@ class Sprite3D
glm::vec2 mScale;
glm::vec2 mDimensionsNormalized;
Rotation3D mRotation;
std::optional<glm::mat4> mBillboardMatrix;
float mOpacity;
};
69 changes: 69 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
#include "Rotation3D.h"
#include "pch.h"

Rotation3D::Rotation3D()
: mRotation(0, 0, 0)
{
}

Rotation3D::Rotation3D(float yaw, float pitch, float roll)
: mRotation(yaw, pitch, roll)
{
}

Rotation3D::Rotation3D(const Rotation3D& rotation)
: mRotation(rotation.mRotation)
{
}

Rotation3D& Rotation3D::operator=(const Rotation3D& rotation)
{
mRotation = rotation.mRotation;
return *this;
}

glm::mat4 Rotation3D::rotate(glm::mat4 model, std::initializer_list<EulerAngle> ordering) const
{
for (const auto axisRotation: ordering)
Expand All @@ -27,3 +48,51 @@ void Rotation3D::imGuiRotationSlider()
ImGui::SliderFloat("Pitch", &pitch, -360, 360.0f);
ImGui::SliderFloat("Yaw", &yaw, -360, 360.0f);
}

bool Rotation3D::operator==(const Rotation3D& rhs) const
{
return mRotation == rhs.mRotation;
}

Rotation3D Rotation3D::operator+(const Rotation3D& rhs) const
{
return Rotation3D(mRotation + rhs.mRotation);
}

Rotation3D Rotation3D::operator-(const Rotation3D& rhs) const
{
return Rotation3D(mRotation - rhs.mRotation);
}

Rotation3D Rotation3D::operator*(const Rotation3D& rhs) const
{
return Rotation3D(mRotation * rhs.mRotation);
}

Rotation3D& Rotation3D::operator+=(const Rotation3D& rhs)
{
mRotation += rhs.mRotation;
return *this;
}

Rotation3D& Rotation3D::operator-=(const Rotation3D& rhs)
{
mRotation -= rhs.mRotation;
return *this;
}

Rotation3D& Rotation3D::operator*=(const Rotation3D& rhs)
{
mRotation *= rhs.mRotation;
return *this;
}

Rotation3D::operator glm::vec<3, float>()
{
return mRotation;
}

Rotation3D::Rotation3D(const glm::vec3& rotation)
: mRotation(rotation)
{
}
39 changes: 35 additions & 4 deletions AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@
/**
* \brief Represents a 3D rotation using Euler angles.
*/
struct Rotation3D
class Rotation3D
{
glm::vec3 mRotation{0, 0, 0};

public:
Rotation3D();
Rotation3D(float yaw, float pitch, float roll);
Rotation3D(const Rotation3D& rotation);
Rotation3D& operator=(const Rotation3D& rotation);
bool operator==(const Rotation3D&) const;
Rotation3D operator+(const Rotation3D&) const;
Rotation3D operator-(const Rotation3D&) const;
Rotation3D operator*(const Rotation3D&) const;
Rotation3D& operator+=(const Rotation3D&);
Rotation3D& operator-=(const Rotation3D&);
Rotation3D& operator*=(const Rotation3D&);
friend Rotation3D operator*(const Rotation3D& lhs, float rhs);
explicit operator glm::vec3();

/**
* \brief Angles of Euler rotation.
*/
Expand All @@ -15,9 +32,9 @@ struct Rotation3D
YAW
};

float yaw{0};
float pitch{0};
float roll{0};
float& yaw = mRotation.x;
float& pitch = mRotation.y;
float& roll = mRotation.z;

/**
* \brief Applies rotation to a 3D model matrix in a specified order.
Expand All @@ -33,4 +50,18 @@ struct Rotation3D
* \brief Renders an ImGui slider interface for adjusting the rotation.
*/
void imGuiRotationSlider();

private:
explicit Rotation3D(const glm::vec3& rotation);
};


inline Rotation3D operator*(const Rotation3D& lhs, float rhs)
{
return Rotation3D(lhs.mRotation * rhs);
}

inline Rotation3D operator*(float lhs, const Rotation3D& rhs)
{
return rhs * lhs;
}
Loading

0 comments on commit f2db0d4

Please sign in to comment.