Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Notiooo committed Dec 25, 2023
1 parent 04a7568 commit b52b78b
Show file tree
Hide file tree
Showing 22 changed files with 4,227 additions and 12 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3,852 changes: 3,852 additions & 0 deletions AimGL/resources/Models/button_stand/button-stand.obj

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AimGL/resources/Sounds/button-click.wav
Binary file not shown.
3 changes: 3 additions & 0 deletions AimGL/src/CMakeLists_Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ set(PROJECT_SOURCES
Player/Player.cpp
World/Camera.cpp
World/InfiniteGridFloor.cpp
World/Scene/Nodes/Node.cpp
World/Scene/GameObjects/Rifle.cpp
World/Scene/GameObjects/PreviewTarget.cpp
World/Scene/GameObjects/ButtonStand.cpp
World/Physics/Drawable/AABB.cpp
World/Physics/Drawable/Ray.cpp
World/Physics/Drawable/DrawableCollider.cpp
Expand All @@ -37,5 +39,6 @@ set(PROJECT_SOURCES
World/Physics/RectangleCollider.cpp
World/Physics/SphereCollider.cpp
World/Physics/Collisions.cpp
Utils/Transform.cpp
Utils/Mouse.cpp
)
16 changes: 16 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@ void Mesh::draw(const Renderer& target, const Camera& camera, const Shader& shad
}
target.draw3D(mVAO, mEBO, shader, camera);
}

void Mesh::setTexture(Texture&& newTexture)
{
auto it = std::find_if(textures.begin(), textures.end(),
[&newTexture](const auto& texture)
{
return texture.type() == newTexture.type();
});

if (it != textures.end())
{
*it = std::move(newTexture);
}

textures.push_back(std::move(newTexture));
}
6 changes: 6 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Core/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class Mesh
*/
void draw(const Renderer& target, const Camera& camera, const Shader& shader) const;

/**
* \brief TODO: THIS
* \param newTexture TODO: THIS
*/
void setTexture(Texture&& newTexture);

private:
/**
* \brief Sets the specified texture to the shader
Expand Down
29 changes: 19 additions & 10 deletions AimGL/src/Renderer/Graphics/3D/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Model::Model(const std::string& objFilePath, const std::vector<TextureType>& tex

void Model::draw(const Renderer& target, const Camera& camera) const
{
mShader.bind();
mShader.setUniform("model", mLastCalculatedModel);
mShader.unbind();
mMesh->draw(target, camera, mShader);
}

Expand Down Expand Up @@ -74,6 +77,16 @@ void Model::showDebugImGui(std::string name)
updateModel();
}

Mesh& Model::mesh()
{
return *mMesh;
}

const Mesh& Model::mesh() const
{
return *mMesh;
}

glm::vec3 Model::position() const
{
return mPosition;
Expand All @@ -86,14 +99,10 @@ glm::vec3 Model::dimensions() const

void Model::updateModel()
{
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));

mShader.bind();
mShader.setUniform("model", model);
mShader.unbind();
mLastCalculatedModel = glm::mat4(1.0f);
mLastCalculatedModel = glm::translate(mLastCalculatedModel, glm::vec3(mPosition));
mLastCalculatedModel = glm::translate(mLastCalculatedModel, -mRotationOrigin);
mLastCalculatedModel = mRotation.rotate(mLastCalculatedModel);
mLastCalculatedModel = glm::translate(mLastCalculatedModel, mRotationOrigin);
mLastCalculatedModel = glm::scale(mLastCalculatedModel, glm::vec3(mScale, mScale, mScale));
}
14 changes: 14 additions & 0 deletions AimGL/src/Renderer/Graphics/3D/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <Renderer/Renderer.h>
#include <Resources/ObjLoader.h>

class Transform;
/**
* \brief Texture along with the type of texture it represents
*/
Expand Down Expand Up @@ -100,6 +101,18 @@ class Model
*/
void showDebugImGui(std::string name = "");

/**
* \brief TODO: THIS
* \return TODO: THIs
*/
Mesh& mesh();

/**
* \brief TODO: THIS
* \return TODO: THIS
*/
const Mesh& mesh() const;

private:
/**
* \brief Updates the model in the shader
Expand All @@ -114,4 +127,5 @@ class Model
glm::vec3 mRotationOrigin{0, 0, 0};
float mScale{1};
Rotation3D mRotation;
glm::mat4 mLastCalculatedModel;
};
2 changes: 1 addition & 1 deletion AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Rotation3D& Rotation3D::operator*=(const Rotation3D& rhs)
return *this;
}

Rotation3D::operator glm::vec<3, float>()
Rotation3D::operator glm::vec<3, float>() const
{
return mRotation;
}
Expand Down
2 changes: 1 addition & 1 deletion AimGL/src/Renderer/Graphics/3D/Utils/Rotation3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Rotation3D
Rotation3D& operator-=(const Rotation3D&);
Rotation3D& operator*=(const Rotation3D&);
friend Rotation3D operator*(const Rotation3D& lhs, float rhs);
explicit operator glm::vec3();
explicit operator glm::vec3() const;

/**
* \brief Angles of Euler rotation.
Expand Down
19 changes: 19 additions & 0 deletions AimGL/src/Renderer/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ Texture::Texture(Texture&& rhs) noexcept
mTextureType = rhs.mTextureType;
}

Texture& Texture::operator=(Texture&& rhs) noexcept
{
if (this != &rhs)
{
GLCall(glDeleteTextures(1, &mTextureId));

mTextureId = rhs.mTextureId;
rhs.mTextureId = 0;
mFilePath = std::move(rhs.mFilePath);
mAspectRatio = rhs.mAspectRatio;
mWidth = rhs.mWidth;
mHeight = rhs.mHeight;
mNrChannels = rhs.mNrChannels;
mTextureType = rhs.mTextureType;
}

return *this;
}

void Texture::bind(unsigned int slot) const
{
GLCall(glActiveTexture(GL_TEXTURE0 + slot));
Expand Down
6 changes: 6 additions & 0 deletions AimGL/src/Renderer/Graphics/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class Texture
*/
Texture(Texture&& rhs) noexcept;

/**
* \brief Move assignment of texture class
* \param rhs Other r-value texture object
*/
Texture& operator=(Texture&& rhs) noexcept;

/**
* \brief Binds a texture for rendering
* \param slot Slot to which the texture will be binded (can be obtained from the shader level)
Expand Down
4 changes: 4 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}})
, mButtonStand(mColliderRegister, {0, 0, 0})
{
Mouse::lockMouseAtCenter(mWindow);
mTree.setScale(0.2f);
Expand Down Expand Up @@ -47,6 +48,7 @@ void GameState::draw(sf::Window& target) const
mTree.draw(mRenderer, mPlayer.camera());
mLogo.draw(mRenderer, mPlayer.camera());
mPhaseInLogoColor.draw(mRenderer);
mButtonStand.draw(mRenderer, mPlayer.camera());

for (auto& previewTarget: mPreviewTargets)
{
Expand All @@ -70,6 +72,7 @@ bool GameState::update(const float& deltaTime)
{
previewTarget->update(deltaTime);
}
mButtonStand.update(deltaTime);
mPlayer.update(deltaTime);

if (mPhaseInLogoColor.opacity() > 0)
Expand All @@ -83,6 +86,7 @@ bool GameState::handleEvent(const sf::Event& event)
{
MTR_SCOPE("GameState", "GameState::handleEvent");
mPlayer.handleEvent(event);
mButtonStand.handleEvent(event);

if (event.type == sf::Event::KeyPressed)
{
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 @@ -10,6 +10,7 @@
#include <Player/Player.h>
#include <Renderer/Graphics/3D/Model.h>
#include <World/Physics/ColliderRegister.h>
#include <World/Scene/GameObjects/ButtonStand.h>
#include <World/Scene/GameObjects/PreviewTarget.h>

class StateStack;
Expand Down Expand Up @@ -66,4 +67,5 @@ class GameState : public State
InfiniteGridFloor mInfiniteGridFloor;
Model mTree;
std::vector<std::unique_ptr<PreviewTarget>> mPreviewTargets;
ButtonStand mButtonStand;
};
27 changes: 27 additions & 0 deletions AimGL/src/Utils/Transform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Transform.h"
#include "pch.h"

#include <Renderer/Graphics/3D/Utils/Rotation3D.h>

glm::mat4 Transform::matrix() const
{
return mTransform;
}

Transform& Transform::translate(const glm::vec3& vec)
{
glm::translate(mTransform, vec);
return *this;
}

Transform& Transform::rotate(const Rotation3D& rotation)
{
mTransform = rotation.rotate(mTransform);
return *this;
}

Transform& Transform::operator*=(const Transform& rhs)
{
mTransform *= rhs.mTransform;
return *this;
}
17 changes: 17 additions & 0 deletions AimGL/src/Utils/Transform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#pragma once

class Rotation3D;

class Transform
{
public:
[[nodiscard]] glm::mat4 matrix() const;
Transform& translate(const glm::vec3& vec);
Transform& rotate(const Rotation3D& rotation);
Transform& operator*=(const Transform& rhs);


private:
glm::mat4 mTransform{1};
};
86 changes: 86 additions & 0 deletions AimGL/src/World/Scene/GameObjects/ButtonStand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "ButtonStand.h"
#include "pch.h"

ButtonStand::ButtonStand(ColliderRegister& colliderRegister, const glm::vec3& position)
: mButtonStand("resources/Models/button_stand/button-stand.obj",
{{"resources/Models/button_stand/button-stand.png", Texture::Type::Diffuse}})
, mColliderRegister(colliderRegister)
, mCollisionBox(mColliderRegister, {0, 0, 0}, mButtonStand.dimensions())
, mButtonShotTriggerBox(
colliderRegister, {0, 0, 0},
{mButtonStand.dimensions().x / 2.f, 0.1, mButtonStand.dimensions().z / 2.f})
{
mSoundBuffer.loadFromFile("resources/Sounds/button-click.wav");
mClickSound.setBuffer(mSoundBuffer);
mButtonStand.setPosition(position);
mCollisionBox.setPosition(mButtonStand.position() - mButtonStand.dimensions() / 2.f);

auto shotTriggerBoxPosition = mButtonStand.position();
shotTriggerBoxPosition.x -= mButtonStand.dimensions().x / 4.f;
shotTriggerBoxPosition.z -= mButtonStand.dimensions().z / 4.f;
shotTriggerBoxPosition.y += (mButtonStand.dimensions().y / 2.f) - 0.1;
mButtonShotTriggerBox.setPosition(shotTriggerBoxPosition);

mButtonShotTriggerBox.callback(
[this](const Collider& collider)
{
if (collider.colliderTag() == ColliderTag::GunShot)
{
if (not isAnimationBeingPlayed)
{
signalizeButtonClick();
mOnShotFunction();
}
}
});
}

void ButtonStand::draw(const Renderer& target, const Camera& camera) const
{
mButtonStand.draw(target, camera);
mCollisionBox.draw(target, camera);
mButtonShotTriggerBox.draw(target, camera);
}

void ButtonStand::update(const float& deltaTime)
{
if (isAnimationBeingPlayed)
{
if (mAnimationClock.getElapsedTime() > mTimeOfClickAnimation)
{
isAnimationBeingPlayed = false;
mButtonStand.mesh().setTexture(
{"resources/Models/button_stand/button-stand.png", Texture::Type::Diffuse});
}
}
}

void ButtonStand::onClick(std::function<void()> onShotFunction)
{
mOnShotFunction = std::move(onShotFunction);
}

void ButtonStand::handleEvent(const sf::Event& event)
{
// TODO: Meh... I think it is unexpected to check
// whether mouse is clicked :/

if (event.type == sf::Event::MouseButtonPressed && event.key.code == sf::Mouse::Left &&
not isAnimationBeingPlayed)
{
hasNewMouseClickOccured = true;
}
}

void ButtonStand::signalizeButtonClick()
{
if (hasNewMouseClickOccured)
{
isAnimationBeingPlayed = true;
mAnimationClock.restart();
mButtonStand.mesh().setTexture(
{"resources/Models/button_stand/button-stand-clicked.png", Texture::Type::Diffuse});
hasNewMouseClickOccured = false;
mClickSound.play();
}
}
Loading

0 comments on commit b52b78b

Please sign in to comment.