Skip to content

Commit

Permalink
bone animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daivuk committed Jun 21, 2020
1 parent 9d83817 commit 65982be
Show file tree
Hide file tree
Showing 11 changed files with 962 additions and 96 deletions.
55 changes: 46 additions & 9 deletions include/onut/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

// STL
#include <vector>
#include <string>
#include <unordered_map>

// Forward
#include <onut/ForwardDeclaration.h>
Expand All @@ -17,29 +19,50 @@ OForwardDeclare(IndexBuffer);
OForwardDeclare(Model);
OForwardDeclare(Texture);
OForwardDeclare(VertexBuffer);
OForwardDeclare(Shader);

#define ONUT_MODEL_VERTEX_HAS_POSITION 0x01
#define ONUT_MODEL_VERTEX_HAS_NORMAL 0x02
#define ONUT_MODEL_VERTEX_HAS_COLOR 0x04
#define ONUT_MODEL_VERTEX_HAS_UV 0x08
#define ONUT_MODEL_VERTEX_HAS_WEIGHTS 0x10

namespace onut
{
class Model final : public Resource, public std::enable_shared_from_this<Model>
{
public:
struct MeshVertex
struct AnimFrame
{
std::vector<Matrix> bones;
};

struct AnimMesh
{
std::vector<AnimFrame> frames;
};

struct Anim
{
float position[3];
float normal[3];
float color[4];
float uv[2];
double duration;
int frameCount;
std::vector<AnimMesh> animMeshes;
std::string name;
};

struct Mesh
{
OIndexBufferRef pIndexBuffer;
OVertexBufferRef pVertexBuffer;
OShaderRef pVS;
OShaderRef pPS;
OTextureRef pTexture;
uint32_t elementCount;

// Raw data. We keep that for batching
std::vector<MeshVertex> vertices;
std::vector<Matrix> bones;
std::unordered_map<std::string, int> boneMapping;
int vertexFlags = 0;
int vertexSize = 6;
std::vector<float> vertices;
std::vector<uint16_t> indices16;
std::vector<uint32_t> indices32;
};
Expand All @@ -50,18 +73,32 @@ namespace onut
Matrix transform;
};

struct Node
{
Matrix transform;
std::vector<Node> chidren;
std::string name;
};

static OModelRef createFromFile(const std::string& filename, const OContentManagerRef& pContentManager = nullptr);
static OModelRef createFromBatch(const std::vector<Batch>& batch);

~Model();
int getMeshCount() const;
Mesh* getMesh(int index);
const Vector3* getBoundingBox() const;
const Node& getRoot() const;
int getAnimId(const std::string& name) const;
const Anim* getAnim(int index) const;
const Anim* getAnim(const std::string& name) const;

void render(const Matrix& transform);
void render(const Matrix& transform, const OShaderRef& customVS = nullptr, const OShaderRef& customPS = nullptr);
void render(const Matrix& transform, const Anim* pAnim, double time, const OShaderRef& customVS = nullptr, const OShaderRef& customPS = nullptr);

private:
std::vector<Mesh> m_meshes;
std::vector<Anim> m_anims;
Node m_root;
Vector3 m_boundingBox[2];
};
}
Expand Down
15 changes: 13 additions & 2 deletions include/onut/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ namespace onut

RenderStates renderStates;

OShaderRef get3DVSForInput(bool hasColor, bool hasTexture, bool hasWeights);
OShaderRef get3DPSForInput(bool hasTexture);

protected:
Renderer();

Expand All @@ -213,8 +216,16 @@ namespace onut

OShaderRef m_p2DVertexShader;
OShaderRef m_p2DPixelShader;
OShaderRef m_p3DVertexShader;
OShaderRef m_p3DPixelShader;
OShaderRef m_p3DVertexShaderPNCT;
OShaderRef m_p3DVertexShaderPNT;
OShaderRef m_p3DVertexShaderPNC;
OShaderRef m_p3DVertexShaderPN;
OShaderRef m_p3DVertexShaderPNCTW;
OShaderRef m_p3DVertexShaderPNTW;
OShaderRef m_p3DVertexShaderPNCW;
OShaderRef m_p3DVertexShaderPNW;
OShaderRef m_p3DPixelShaderCT;
OShaderRef m_p3DPixelShaderC;
OShaderRef m_pEffectsVertexShader;
OShaderRef m_pBlurHPixelShader;
OShaderRef m_pBlurVPixelShader;
Expand Down
3 changes: 3 additions & 0 deletions include/onut/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ namespace onut
virtual void setVector3(int varId, const Vector3& value) = 0;
virtual void setVector4(int varId, const Vector4& value) = 0;
virtual void setMatrix(int varId, const Matrix& value) = 0;
virtual void setMatrixArray(int varId, const Matrix* values, int count) = 0;
virtual void setFloat(const std::string& varName, float value) = 0;
virtual void setVector2(const std::string& varName, const Vector2& value) = 0;
virtual void setVector3(const std::string& varName, const Vector3& value) = 0;
virtual void setVector4(const std::string& varName, const Vector4& value) = 0;
virtual void setMatrix(const std::string& varName, const Matrix& value) = 0;
virtual void setMatrixArray(const std::string& varName, const Matrix* values, int count) = 0;

protected:
Shader();
Expand All @@ -83,6 +85,7 @@ namespace onut
{
VarType type;
std::string name;
int count = 1;
};
using ParsedUniforms = std::vector<ParsedUniform>;

Expand Down
8 changes: 4 additions & 4 deletions src/Images.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace onut
{
std::vector<uint8_t> ret;
lodepng::encode(ret, data, size.x, size.y);
return ret;
return std::move(ret);
}

std::vector<uint8_t> convertToPNG(const uint8_t* pData, const Point& size)
{
std::vector<uint8_t> ret;
lodepng::encode(ret, pData, size.x, size.y);
return ret;
return std::move(ret);
}

std::vector<uint8_t> loadPNG(const std::string& filename, Point& size)
Expand All @@ -32,7 +32,7 @@ namespace onut
lodepng::decode(ret, w, h, filename);
size.x = static_cast<int>(w);
size.y = static_cast<int>(h);
return ret;
return std::move(ret);
}

std::vector<uint8_t> loadPNG(const std::vector<uint8_t>& data, Point& size)
Expand All @@ -42,7 +42,7 @@ namespace onut
lodepng::decode(ret, w, h, data);
size.x = static_cast<int>(w);
size.y = static_cast<int>(h);
return ret;
return std::move(ret);
}

#if defined(WIN32)
Expand Down
Loading

0 comments on commit 65982be

Please sign in to comment.