creation of animation containers

This commit is contained in:
arthur.jamet
2021-05-22 11:55:44 +02:00
parent fc46529232
commit 66c33b403c
21 changed files with 354 additions and 131 deletions
+9 -5
View File
@@ -18,7 +18,6 @@ set(HEADERS
sources/IRessource.hpp
sources/Matrix.hpp
sources/Mesh.hpp
sources/Model.hpp
sources/Window.hpp
sources/Audio/IAudio.hpp
sources/Audio/Music.hpp
@@ -52,11 +51,20 @@ set(HEADERS
sources/Drawables/3D/Ray.hpp
sources/Drawables/3D/Sphere.hpp
sources/Drawables/3D/Triangle.hpp
sources/Model/Model.hpp
sources/Model/ModelAnimation.hpp
sources/Model/ModelAnimations.hpp
sources/Vector/Vector2.hpp
sources/Vector/Vector3.hpp
)
set(SRC
sources/Color.cpp
sources/Font.cpp
sources/Window.cpp
sources/Model/Model.cpp
sources/Model/ModelAnimation.cpp
sources/Model/ModelAnimations.cpp
sources/Audio/Music.cpp
sources/Audio/Sound.cpp
sources/Camera/Camera2D.cpp
@@ -86,10 +94,6 @@ set(SRC
sources/Drawables/Texture.cpp
sources/Vector/Vector2.cpp
sources/Vector/Vector3.cpp
sources/Color.cpp
sources/Font.cpp
sources/Model.cpp
sources/Window.cpp
)
find_package(raylib QUIET)
+5
View File
@@ -72,6 +72,11 @@ void RAY::Camera::Camera3D::setMode(Mode mode)
this->_mode = mode;
}
void RAY::Camera::Camera3D::update(void)
{
UpdateCamera(&this->_camera);
}
RAY::Camera::Camera3D::operator ::Camera3D() const
{
return this->_camera;
+4 -1
View File
@@ -59,9 +59,12 @@ namespace RAY::Camera {
//! @brief Returns camera 3d transform matrix
Matrix getMatrix(void) const override;
// Set camera mode (multiple camera modes available)
//! @brief Set camera mode (multiple camera modes available)
void setMode(Mode mode);
//! @brief Update camera position for selected mode
void update(void);
//! @brief get camera struct
operator ::Camera3D() const;
+1 -2
View File
@@ -52,9 +52,8 @@ void RAY::Drawables::Drawables2D::Triangle::drawOn(RAY::Window &)
DrawTriangle(this->_position, this->_posB, this->_posC, this->_color);
}
void RAY::Drawables::Drawables2D::Triangle::drawOn(RAY::Image &image)
void RAY::Drawables::Drawables2D::Triangle::drawOn(RAY::Image &)
{
(void)image;
throw std::exception();
}
+1 -1
View File
@@ -54,7 +54,7 @@ namespace RAY::Drawables::Drawables2D {
//! @brief Draw point on window
void drawOn(RAY::Window &) override;
//! @brief Draw point on image
//! @brief A Triangle cannot be drawn on an image, an exception will be thrown
void drawOn(RAY::Image &image) override;
private:
+1 -1
View File
@@ -52,7 +52,7 @@ namespace RAY
bool exportTo(const std::string &outputPath);
//! @brief unload ressources
bool unload() override;
bool unload();;
//! @brief get image
operator ::Image() const;
+1 -1
View File
@@ -43,7 +43,7 @@ namespace RAY
bool load(const std::string &filename) override;
//! @brief unload ressources
bool unload() override;
bool unload();;
//! @return libray Texture struct
operator ::Texture() const;
-2
View File
@@ -17,8 +17,6 @@ namespace RAY {
virtual ~IRessource() = default;
virtual bool load(const std::string &filePath) = 0;
virtual bool unload() = 0;
};
};
-47
View File
@@ -1,47 +0,0 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** Model
*/
#include "Model.hpp"
RAY::Model::Model(const std::string &filename):
_model(LoadModel(filename.c_str()))
{
}
RAY::Model::Model(const Mesh &mesh):
_model(LoadModelFromMesh(mesh))
{
}
RAY::Model::~Model()
{
this->unload();
}
bool RAY::Model::load(const std::string &filename)
{
this->_model = LoadModel(filename.c_str());
return true;
}
bool RAY::Model::load(const Mesh &mesh)
{
this->_model = LoadModelFromMesh(mesh);
return true;
}
bool RAY::Model::unload()
{
UnloadModel(_model);
return true;
}
bool RAY::Model::unloadKeepMeshes()
{
UnloadModelKeepMeshes(_model);
return true;
}
+65
View File
@@ -0,0 +1,65 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** Model
*/
#include "Model/Model.hpp"
RAY::Model::Model(const std::string &filename):
_model(LoadModel(filename.c_str()))
{
}
RAY::Model::Model(const Mesh &mesh):
_model(LoadModelFromMesh(mesh))
{
}
RAY::Model::~Model()
{
UnloadModel(this->_model);
}
bool RAY::Model::load(const std::string &filename)
{
this->_model = LoadModel(filename.c_str());
return true;
}
bool RAY::Model::load(const Mesh &mesh)
{
this->_model = LoadModelFromMesh(mesh);
return true;
}
bool RAY::Model::unloadKeepMeshes()
{
UnloadModelKeepMeshes(_model);
return true;
}
bool RAY::Model::setAnimation(const RAY::ModelAnimation &animation)
{
if (!IsModelAnimationValid(this->_model, animation))
return false;
UpdateModelAnimation(this->_model, animation, animation.getFrameCounter());
return true;
}
bool RAY::Model::setTextureToMaterial(RAY::Model::MaterialType materialType, const RAY::Texture &texture)
{
SetMaterialTexture(&this->_model.materials[materialType], materialType, texture);
return true;
}
RAY::Model::operator ::Model() const
{
return this->_model;
}
int RAY::Model::getBoneCount() const
{
return this->_model.boneCount;
}
@@ -9,12 +9,19 @@
#define MODEL_HPP_
#include "IRessource.hpp"
#include "Drawables/Texture.hpp"
#include "Drawables/IDrawable.hpp"
#include "Model/ModelAnimation.hpp"
#include <raylib.h>
#include <vector>
namespace RAY {
//! @brief Basic 3d Model type
//! @brief Basic 3D Model type
class Model: public IRessource {
public:
typedef ::MaterialMapIndex MaterialType;
//! @brief Create an model, loading a file
//! @param filePath: path to file to load
Model(const std::string &filePath);
@@ -29,6 +36,7 @@ namespace RAY {
//! @brief A model is assignable
Model& operator=(const Model &model) = default;
//! @brief Model destructor, unloads all related data
~Model();
//! @brief Load model from file (meshes and materials)
@@ -37,13 +45,27 @@ namespace RAY {
//! @brief Load model from mesh (default materials)
bool load(const Mesh &mesh);
//! @brief Unload model (including meshes) from memory (RAM and/or VRAM)
bool unload();
//! @brief Unload model (excluding meshes) from memory (RAM and/or VRAM)
bool unloadKeepMeshes();
//! @brief Update model animation pose
//! @return false if animation is not compatible
bool setAnimation(const RAY::ModelAnimation &animation);
//! @brief Sets a texture to the Nth material
//! @param materielIndex The type of material to apply the texture to (serves as an index)
//! @param texture the texture to apply
bool setTextureToMaterial(MaterialType materialType, const RAY::Texture &texture);
//! @brief A RAY Model is cast-able in libray's model
operator ::Model() const;
//! @return The number of bones in the model
int getBoneCount() const;
private:
//! @brief Raw data from raylib
::Model _model;
};
};
#endif /* !Model_HPP_ */
+46
View File
@@ -0,0 +1,46 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** ModelAnimation
*/
#include "Model/ModelAnimation.hpp"
RAY::ModelAnimation::ModelAnimation(::ModelAnimation &animation):
_animation(animation), _frameCounter(0)
{
}
size_t RAY::ModelAnimation::getFrameCounter() const
{
return this->_frameCounter;
}
size_t RAY::ModelAnimation::getFrameCount() const
{
return this->_animation.frameCount;
}
RAY::ModelAnimation &RAY::ModelAnimation::setFrameCounter(size_t frameCounter)
{
this->_frameCounter = frameCounter % this->_animation.frameCount;
return *this;
}
RAY::ModelAnimation &RAY::ModelAnimation::incrementFrameCounter()
{
this->_frameCounter = (this->_frameCounter + 1) % this->_animation.frameCount;
return *this;
}
RAY::ModelAnimation::operator ::ModelAnimation() const
{
return this->_animation;
}
RAY::ModelAnimation::operator ::ModelAnimation *()
{
return &this->_animation;
}
+57
View File
@@ -0,0 +1,57 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** ModelAnimation
*/
#ifndef MODELANIMATION_HPP_
#define MODELANIMATION_HPP_
#include <raylib.h>
#include <memory>
namespace RAY {
//! @brief A Holder for Model Animations
class ModelAnimation {
public:
//! @brief A Model animation constructor
//! @param animationPtr an animation pointer, returned by the nimation-loading function
ModelAnimation(::ModelAnimation &animationPtr);
//! @brief A default copy-constructor
ModelAnimation(const ModelAnimation &) = default;
//! @brief A model animation is assignable
ModelAnimation &operator=(const ModelAnimation &) = default;
//! @brief Castin Object to raw model animation pointer
operator ::ModelAnimation *();
//! @brief Castin Object to raw model animation pointer
operator ::ModelAnimation() const;
//! @brief Returns the current frame the animation is at
size_t getFrameCounter() const;
//! @brief Returns the number of frame in the animation
size_t getFrameCount() const;
//! @brief Set the frame the position is at
ModelAnimation &setFrameCounter(size_t frameCounter);
//! @brief Increment the frame counter
ModelAnimation &incrementFrameCounter();
//! @brief Default destructor
~ModelAnimation() = default;
private:
::ModelAnimation &_animation;
size_t _frameCounter;
};
}
#endif /* !MODELANIMATION_HPP_ */
+32
View File
@@ -0,0 +1,32 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** ModelAnimations
*/
#include "Model/ModelAnimations.hpp"
RAY::ModelAnimations::ModelAnimations(const std::string &filePath):
_animationsPtr(LoadModelAnimations(filePath.c_str(), &this->_animationCount))
{
::ModelAnimation *ptr = this->_animationsPtr.get();
for (int i = 0; i < this->_animationCount; i++)
this->_animations.push_back(RAY::ModelAnimation(ptr[i]));
}
RAY::ModelAnimations::~ModelAnimations()
{
UnloadModelAnimations(this->_animationsPtr.release(), this->_animationCount);
}
RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index)
{
return this->_animations[index];
}
size_t RAY::ModelAnimations::getAnimationsCount() const
{
return this->_animationCount;
}
+49
View File
@@ -0,0 +1,49 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** ModelAnimations
*/
#ifndef MODELANIMATIONS_HPP_
#define MODELANIMATIONS_HPP_
#include "Model/ModelAnimation.hpp"
#include <vector>
namespace RAY {
//! @brief A Holder for Model Animations
class ModelAnimations {
public:
//! @brief A Model animation constructor
//! @param filePath an animation pointer, returned by the nimation-loading function
ModelAnimations(const std::string &filePath);
//! @brief Only single entity can hold these animations pointers
ModelAnimations(const ModelAnimations &) = delete;
//! @brief Unloads all animations
~ModelAnimations();
//! @brief Only single entity can hold these animations pointers
ModelAnimations &operator=(const ModelAnimations &) = delete;
//! @brief Castin Object to raw model animation pointer
ModelAnimation &operator[](int index);
//! @return the number of loaded animations
size_t getAnimationsCount() const;
private:
//! @brief Holds the pointer returned by the loading function
std::unique_ptr<::ModelAnimation> _animationsPtr;
//! @brief A holder for animations
std::vector<ModelAnimation> _animations;
//! @brief the number of loaded animations
int _animationCount;
};
}
#endif /* !MODELANIMATIONS_HPP_ */
+5
View File
@@ -158,6 +158,11 @@ void RAY::Window::draw(const Mesh &mesh, const Material &material, const Matrix
DrawMesh(mesh, material, transform);
}
void RAY::Window::draw(const RAY::Model &model, const RAY::Vector3 &position, const RAY::Vector3 &rotationAxis, float rotationAngle, const RAY::Vector3 &scale, const RAY::Color &tint)
{
DrawModelEx(model, position, rotationAxis, rotationAngle, scale, tint);
}
void RAY::Window::setIcon(RAY::Image &img)
{
SetWindowIcon(img);
+9 -2
View File
@@ -10,7 +10,8 @@
#include <raylib.h>
#include <string>
#include <Vector/Vector2.hpp>
#include "Vector/Vector2.hpp"
#include "Vector/Vector3.hpp"
#include "Controllers/Keyboard.hpp"
#include "Camera/Camera2D.hpp"
#include "Camera/Camera3D.hpp"
@@ -18,8 +19,10 @@
#include "Canvas.hpp"
#include "Drawables/IDrawable.hpp"
#include "Drawables/Texture.hpp"
#include "Model/Model.hpp"
namespace RAY {
class Model;
//! @brief Window manager
class Window: public Canvas {
public:
@@ -116,11 +119,15 @@ namespace RAY {
//! @param texture The object to render
//! @param position The position of the texture relative to the top left window corner
//! @param tint
void draw(const RAY::Texture &texture, const Vector2 &position, const Color &tint);
void draw(const Texture &texture, const Vector2 &position, const Color &tint);
//! @brief Draw a 3d mesh with material and transform
void draw(const Mesh &mesh, const Material &material, const Matrix &transform);
//! @brief Draw a model
void draw(const Model &model, const Vector3 &position, const Vector3 &rotationAxis = Vector3(0, 0, 0),
float rotationAngle = 0, const Vector3 &scale = Vector3(1, 1, 1), const Color &tint = WHITE);
private:
//! @brief Creates window, and opens it if openNow is set to true
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

+1 -1
View File
@@ -10,5 +10,5 @@
RAY::Vector3 toRAY(const WAL::Vector3f &wal)
{
return RAY::Vector3(wal.x, wal.y, wal.y);
return RAY::Vector3(wal.x, wal.y, wal.y);
}
+42 -64
View File
@@ -6,98 +6,75 @@
*/
#include <iostream>
#include "Window.hpp"
#include "Drawables/2D/Text.hpp"
#include "Drawables/2D/Circle.hpp"
#include "Controllers/Keyboard.hpp"
#include "Camera/Camera3D.hpp"
#include "Drawables/3D/Grid.hpp"
#include "Drawables/3D/Cube.hpp"
#include <cmath>
#include "Wal.hpp"
#include "Camera/Camera3D.hpp"
#include "Controllers/Keyboard.hpp"
#include "Drawables/2D/Text.hpp"
#include "Drawables/3D/Grid.hpp"
#include "Drawables/Texture.hpp"
#include "Model/Model.hpp"
#include "Model/ModelAnimations.hpp"
#include "Vector/Vector3.hpp"
#include "Window.hpp"
int main()
{
WAL::Wal wal;
SetTraceLogLevel(LOG_WARNING);
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
RAY::Window &window = RAY::Window::getInstance(screenWidth, screenHeight, "Bidibidibop", FLAG_WINDOW_RESIZABLE);
RAY::Camera::Camera3D camera(RAY::Vector3(10.0f, 10.0f, 10.0f),
RAY::Vector3(0.0f, 0.0f, 0.0f),
RAY::Vector3(0.0f, 1.0f, 0.0f),
45.0f, CAMERA_PERSPECTIVE
);
RAY::Model model("guy.iqm");
RAY::Texture texture("guytex.png");
RAY::ModelAnimations animations("guy.iqm");
RAY::Drawables::Drawables3D::Grid grid(10, 1.0f);
RAY::Drawables::Drawables2D::Text instructionText("PRESS SPACE to PLAY MODEL ANIMATION", 10, {10, 20} , MAROON);
model.setTextureToMaterial(MAP_DIFFUSE, texture);
RAY::Vector2 ballPosition = {(float)screenWidth / 2, (float)screenHeight / 2};
RAY::Window &window = RAY::Window::getInstance(screenWidth, screenHeight, "Ta mère en slip", FLAG_WINDOW_RESIZABLE);
RAY::Camera::Camera3D camera(RAY::Vector3{30.0f, 20.0f, 30.0f},
RAY::Vector3{0.0f, 0.0f, 0.0f},
RAY::Vector3{0.0f, 1.0f, 0.0f},
70.0,
CAMERA_PERSPECTIVE);
RAY::Drawables::Drawables3D::Grid grid(10, 5.0f);
RAY::Drawables::Drawables3D::Cube cube(RAY::Vector3(0, 0, 0),
RAY::Vector3(0, 0, 0),
RAY::Color(0, 0, 0, 0));
RAY::Vector3 position(0.0f, 0.0f, 0.0f); // Set model position
// Specify the amount of blocks in each direction
const int numBlocks = 15;
camera.setMode(CAMERA_FREE); // Set free camera mode
window.setFPS(60);
window.setFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!window.shouldClose()) // Detect window close button or ESC key
while (!window.shouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
double time = GetTime();
camera.update();
// Calculate time scale for cube position and size
float scale = (2.0f + (float)sin(time)) * 0.7f;
// Move camera around the scene
double cameraTime = time * 0.3;
camera.setPosition({(float)cos(cameraTime) * 40.0f, 20.0f, (float)sin(cameraTime) * 40.0f});
// Play animation when spacebar is held down
if (RAY::Controller::Keyboard::isDown(KEY_SPACE))
{
animations[0].incrementFrameCounter();
model.setAnimation(animations[0]);
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
window.setDrawingState(RAY::Window::DRAWING);
window.clear(RAYWHITE);
window.clear();
window.useCamera(camera);
window.useCamera(camera);
window.draw(grid);
window.draw(model, position, RAY::Vector3(1.0f, 0.0f, 0.0f), -90.0f, RAY::Vector3( 1.0f, 1.0f, 1.0f ));
for (int x = 0; x < numBlocks; x++) {
for (int y = 0; y < numBlocks; y++) {
for (int z = 0; z < numBlocks; z++) {
// Scale of the blocks depends on x/y/z positions
float blockScale = (x + y + z) / 30.0f;
window.draw(grid);
// Scatter makes the waving effect by adding blockScale over time
float scatter = sinf(blockScale * 20.0f + (float)(time * 4.0f));
window.unuseCamera();
// Calculate the cube position
cube.setPosition(RAY::Vector3{
static_cast<float>(x - numBlocks / 2) * (scale * 3.0f) + scatter,
static_cast<float>(y - numBlocks / 2) * (scale * 2.0f) + scatter,
static_cast<float>(z - numBlocks / 2) * (scale * 3.0f) + scatter
});
// Pick a color with a hue depending on cube position for the rainbow color effect
cube.setColor(RAY::Color(static_cast<float>(((x + y + z) * 18) % 360), 0.75f, 0.9f));
// Calculate cube size
float cubeSize = (2.4f - scale) * blockScale;
cube.setDimensions({cubeSize, cubeSize, cubeSize});
// And finally, draw the cube!
window.draw(cube);
}
}
}
window.unuseCamera();
window.draw(instructionText);
window.setDrawingState(RAY::Window::IDLE);
//----------------------------------------------------------------------------------
@@ -105,7 +82,8 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
window.close(); // Close window and OpenGL context
window.close(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;