raylib_impl 0.1.0
Vendor raylib 5.5 : IGraphic2Module / IGraphic3Module / IAudioModule
Loading...
Searching...
No Matches
RayModel.hpp
Go to the documentation of this file.
1
11#ifndef RAYMODEL_HPP_
12#define RAYMODEL_HPP_
13
14//Raylib (without conflict)
15#include "RaylibAliases.hpp"
16namespace raylib {
17 #include "raymath.h"
18}
19
20//Interface
21#include "IModel.hpp"
22
23//encapsulation
24#include "RayAnimationSet.hpp"
25#include "RayMesh.hpp"
26#include "RayTexture.hpp"
27
28#include <utility>
29
30
47class RayModel : public graphic::IModel {
48
49 public:
50 RayModel(RayMesh &mesh) : _mesh(mesh), _position{}, _rotation{}, _scale{1, 1, 1},
51 _animations(nullptr), _animId(0), _animFrame(0) {
52 }
53
54 ~RayModel() = default;
55
56 void setTexture(graphic::ITexture *texture) override {
57 auto *rayTexture = static_cast<RayTexture *>(texture);
58 raylib::Model &model = _mesh.handle();
59 for (int i = 0; i < model.materialCount; i++) {
60 raylib::SetMaterialTexture(&model.materials[i], raylib::MATERIAL_MAP_DIFFUSE, rayTexture->handle());
61 }
62 }
63
64 void setAnimations(graphic::IAnimationSet *animations) override {
65 _animations = static_cast<RayAnimationSet *>(animations);
66 _animFrame = 0;
67 _animId = 0;
68 }
69 int getAnimationsSize() const override {
70 return _animations ? _animations->getSize() : 0;
71 }
72 void setAnimation(int pos) override {
73 _animId = pos;
74 // changer de clip repart du debut, sur le clip entier
75 _first = 0;
76 _last = frameCount() > 0 ? frameCount() - 1 : 0;
77 _animFrame = 0;
78 }
79 int getAnimation() const override {
80 return _animId;
81 }
82 bool updateAnimation() override {
83 if (frameCount() == 0)
84 return false;
85
86 const bool wrapped = (_animFrame >= _last);
87
88 _animFrame = wrapped ? _first : _animFrame + 1;
89 apply();
90 return wrapped;
91 }
92
93 void setFrameRange(int first, int last) override {
94 const int frames = frameCount();
95 if (frames == 0)
96 return;
97
98 _first = clamp(first, frames);
99 _last = clamp(last, frames);
100 if (_last < _first)
101 std::swap(_first, _last);
102 setFrame(_first);
103 }
104
105 int getFirstFrame() const override { return _first; }
106 int getLastFrame() const override { return _last; }
107
108 void setFrame(int frame) override {
109 if (frameCount() == 0)
110 return;
111
112 _animFrame = (frame < _first || frame > _last) ? _first : frame;
113 apply();
114 }
115
116 int getFrame() const override {
117 return _animFrame;
118 }
119
120 Vector3f getPosition() const override {
121 return {_position.x, _position.y, _position.z};
122 }
123
124 void setPosition(Vector3f position) override {
125 _position = {float(position.x), float(position.y), float(position.z)};
126 }
127
128 Vector3f getRotation() const override {
129 return {_rotation.x, _rotation.y, _rotation.z};
130 }
131
132 void setRotation(Vector3f rotation) override {
133 _rotation = {float(rotation.x), float(rotation.y), float(rotation.z)};
134 _mesh.handle().transform = raylib::MatrixRotateXYZ(
135 (raylib::Vector3){DEG2RAD * _rotation.x, DEG2RAD * _rotation.y, DEG2RAD * _rotation.z});
136 }
137
138 Vector3f getSize() const override {
139 raylib::BoundingBox box = _mesh.boundingBox();
140 raylib::Vector3 s = {box.max.x - box.min.x, box.max.y - box.min.y, box.max.z - box.min.z};
141 return {s.x * _scale.x, s.y * _scale.y, s.z * _scale.z};
142 }
143
144 void setSize(Vector3f size) override {
145 raylib::BoundingBox box = _mesh.boundingBox();
146 raylib::Vector3 s = {box.max.x - box.min.x, box.max.y - box.min.y, box.max.z - box.min.z};
147 if (s.x == 0 || s.y == 0 || s.z == 0) return;
148
149 _scale.x = size.x / s.x;
150 _scale.y = size.y / s.y;
151 _scale.z = size.z / s.z;
152 }
153
154 friend class RayWindow;
155
156 private:
157 static int clamp(int frame, int frames) {
158 return frame < 0 ? 0 : (frame >= frames ? frames - 1 : frame);
159 }
160
161 void apply() {
162 raylib::UpdateModelAnimation(_mesh.handle(), *(_animations->handle() + _animId), _animFrame);
163 }
164
165 int frameCount() const {
166 if (!_animations || _animId < 0 || _animId >= _animations->getSize())
167 return 0;
168 return (_animations->handle() + _animId)->frameCount;
169 }
170
171 RayMesh &_mesh;
172 raylib::Vector3 _rotation;
173 raylib::Vector3 _position;
174 raylib::Vector3 _scale;
175 RayAnimationSet *_animations;
176 int _animId;
177 int _animFrame;
178 int _first = 0;
179 int _last = 0;
180};
181
184#endif /* !RAYMODEL_HPP_ */
Raylib AnimationSet class - owns the loaded clips, independent of any RayModel bound to it.
Definition RayAnimationSet.hpp:26
raylib::ModelAnimation * handle() const
Definition RayAnimationSet.hpp:54
int getSize() const override
Definition RayAnimationSet.hpp:42
Raylib Mesh class - owns the loaded geometry (raylib's own Model struct is the mesh+material data,...
Definition RayMesh.hpp:26
raylib::Model & handle()
Definition RayMesh.hpp:42
raylib::BoundingBox boundingBox() const
Definition RayMesh.hpp:46
Raylib Model class - references a RayMesh, does not own it.
Definition RayModel.hpp:47
int getAnimationsSize() const override
Definition RayModel.hpp:69
int getFirstFrame() const override
Definition RayModel.hpp:105
Vector3f getSize() const override
Definition RayModel.hpp:138
void setFrameRange(int first, int last) override
Definition RayModel.hpp:93
void setAnimations(graphic::IAnimationSet *animations) override
Definition RayModel.hpp:64
void setTexture(graphic::ITexture *texture) override
Definition RayModel.hpp:56
Vector3f getPosition() const override
Definition RayModel.hpp:120
void setFrame(int frame) override
Definition RayModel.hpp:108
void setRotation(Vector3f rotation) override
Definition RayModel.hpp:132
Vector3f getRotation() const override
Definition RayModel.hpp:128
void setPosition(Vector3f position) override
Definition RayModel.hpp:124
bool updateAnimation() override
Definition RayModel.hpp:82
void setSize(Vector3f size) override
Definition RayModel.hpp:144
RayModel(RayMesh &mesh)
Definition RayModel.hpp:50
void setAnimation(int pos) override
Definition RayModel.hpp:72
int getFrame() const override
Definition RayModel.hpp:116
int getAnimation() const override
Definition RayModel.hpp:79
int getLastFrame() const override
Definition RayModel.hpp:106
~RayModel()=default
Raylib Texture class - owns the GPU texture. RaySprite/RayModel only reference it,...
Definition RayTexture.hpp:26
Raylib Window class - a single concrete class implementing IWindow3 fully (which already IS-A IWindow...
Definition RayWindow.hpp:37
Definition RayModel.hpp:16