Kemena3D
Loading...
Searching...
No Matches
kmesh.h
Go to the documentation of this file.
1
5
6#ifndef KMESH_H
7#define KMESH_H
8
9#include "kexport.h"
10#include "kdriver.h"
11
12#include <map>
13#include <vector>
14#include <string>
15
16#include "kobject.h"
17#include "kbone.h"
18// (Skeletal animation is reached through the forward-declared kAnimator
19// pointer below — no animation header needed at the mesh-header level.)
20#include "kanimator.h"
21
22namespace kemena
23{
24 class kAnimator;
25
34 class KEMENA3D_API kMesh : public kObject
35 {
36 public:
41 kMesh(kObject *parentNode = nullptr);
42
47
52 void setLoaded(bool newLoaded);
53
58 bool getLoaded();
59
64 void setFileName(kString newFileName);
65
71
76 void setRefName(kString newRefName);
77
83
93
99
101 void setPosition(kVec3 newPosition);
103 void setRotation(kQuat newRotation);
105 void setScale(kVec3 newScale);
106
111 void reserveBoneData(size_t vertexCount);
112
117 void reserveSpace(size_t vertexCount);
118
123 void setBoneInfoMap(std::map<kString, kBoneInfo> newBoneInfoMap);
124
129 std::map<kString, kBoneInfo> &getBoneInfoMap();
130
136
141 void setBoneCount(int newBoneCount);
142
147 void addIndex(uint32_t index);
148
153 std::vector<uint32_t> getIndices();
154
159 void addVertex(kVec3 vertex);
160
165 std::vector<kVec3> getVertices();
166
171 void addUV(kVec2 uv);
172
177 std::vector<kVec2> getUVs();
178
184
189 std::vector<kVec3> getVertexColors();
190
195 void addNormal(kVec3 normal);
196
201 std::vector<kVec3> getNormals();
202
207 void addTangent(kVec3 tangent);
208
213 std::vector<kVec3> getTangents();
214
219 void addBitangent(kVec3 bitangent);
220
225 std::vector<kVec3> getBitangents();
226
231 void addBoneID(const kIvec4 &boneID);
232
238 void setBoneID(size_t vertexIndex, const kIvec4 &boneID);
239
245 kIvec4 getBoneID(size_t vertexIndex);
246
251 std::vector<kIvec4> getBoneIDs();
252
257 void setBoneIDs(std::vector<kIvec4> newBoneIDs);
258
263 void addWeight(const kVec4 &weight);
264
270 void setWeight(size_t vertexIndex, const kVec4 &weight);
271
277 kVec4 getWeight(size_t vertexIndex);
278
283 std::vector<kVec4> getWeights();
284
289 void setWeights(std::vector<kVec4> newWeights);
290
296
302
307 uint32_t getVertexBuffer();
308
314
319 void setNormalMatrix(kMat4 newNormalMatrix);
320
326
334
339
348
357
364
369
373 void draw();
374
380
385 void deserialize(json data);
386
391 void setVisible(bool newVisible);
392
398
403 void setCastShadow(bool newCastShadow);
404
410
415 void setReceiveShadow(bool newReceiveShadow);
416
422
429 void setVertexBoneData(size_t vertexID, int boneID, float weight);
430
435 void setAnimator(kAnimator *newAnimator);
436
442
447 void setSkinned(bool newSkinned);
448
454
455 protected:
456 private:
457 bool loaded = false;
458
459 kString fileName;
460 kString refName;
461 kString primitiveType;
462
463 std::vector<kVec3> vertices;
464 std::vector<uint32_t> indices;
465 std::vector<kVec2> uvs;
466 std::vector<kVec3> vertexColors;
467 std::vector<kVec3> normals;
468 std::vector<kVec3> tangents;
469 std::vector<kVec3> bitangents;
470 std::vector<kIvec4> boneIDs;
471 std::vector<kVec4> weights;
472
473 uint32_t vao = 0;
474 uint32_t indicesEbo = 0;
475
476 uint32_t vertexBuffer = 0;
477 uint32_t vertexColorBuffer = 0;
478 uint32_t uvBuffer = 0;
479 uint32_t normalBuffer = 0;
480 uint32_t tangentBuffer = 0;
481 uint32_t bitangentBuffer = 0;
482 uint32_t boneIDBuffer = 0;
483 uint32_t weightBuffer = 0;
484
485 kMat3 normalMatrix;
486
487 kAABB localAABB;
488
489 bool isVisible = true;
490 bool isCastShadow = true;
491 bool isReceiveShadow = true;
492
493 std::map<kString, kBoneInfo> boneInfoMap;
494 int boneCount = 0;
495
496 kAnimator *animator = nullptr;
497 bool isSkinned = false;
498 };
499}
500
501#endif // KMESH_H
Controls playback of animation clips.
Definition kanimator.h:36
void addNormal(kVec3 normal)
Appends a vertex normal.
int getVertexCount()
Returns the number of vertices in this mesh.
kIvec4 getBoneID(size_t vertexIndex)
Returns the bone-ID tuple for a vertex.
void setVertexBoneData(size_t vertexID, int boneID, float weight)
Assigns a single bone influence to a vertex (used during loading).
kVec4 getWeight(size_t vertexIndex)
Returns the bone-weight tuple for a vertex.
void generateVbo()
Uploads all vertex attribute data to the GPU.
kString getRefName()
Returns the reference name.
int & getBoneCount()
Returns a reference to the bone counter used during loading.
std::vector< kVec4 > getWeights()
Returns a copy of the bone-weight buffer.
~kMesh()
Destroys the mesh and releases its GPU buffers.
bool getReceiveShadow()
Returns whether the mesh receives shadows.
void addBitangent(kVec3 bitangent)
Appends a vertex bitangent.
void setWeight(size_t vertexIndex, const kVec4 &weight)
Overwrites the bone-weight tuple for a specific vertex.
void setBoneInfoMap(std::map< kString, kBoneInfo > newBoneInfoMap)
Replaces the bone-name-to-info map.
void setRefName(kString newRefName)
Sets the reference name used to identify shared mesh data.
kMat4 getNormalMatrix()
Returns the stored normal matrix.
kAnimator * getAnimator()
Returns the attached animator.
void computeLocalAABB()
Computes the local-space AABB from the vertex position buffer.
void setPrimitiveType(kString type)
Marks this mesh as one of the built-in procedural primitives ("cube", "sphere", "cylinder",...
void setFileName(kString newFileName)
Sets the source asset file path.
void setReceiveShadow(bool newReceiveShadow)
Controls whether the mesh receives shadows.
kAABB getWorldAABB()
Returns the AABB transformed into world space.
json serialize()
Serialises the mesh to JSON.
void addBoneID(const kIvec4 &boneID)
Appends a bone-ID tuple for the next vertex.
void setWeights(std::vector< kVec4 > newWeights)
Replaces the entire bone-weight buffer.
void setBoneCount(int newBoneCount)
Sets the total number of bones.
void reserveSpace(size_t vertexCount)
Pre-allocates all per-vertex attribute vectors.
bool getCastShadow()
Returns whether the mesh casts shadows.
void generateTangents()
Computes per-vertex tangents and bitangents from positions, UVs, and indices. Must be called after al...
std::vector< kVec2 > getUVs()
Returns a copy of the UV coordinate buffer.
void setNormalMatrix(kMat4 newNormalMatrix)
Stores a precomputed normal matrix.
bool getLoaded()
Returns whether the mesh geometry is fully loaded.
uint32_t getVertexColorBuffer()
Returns the GPU handle for the vertex-colour VBO.
std::vector< kIvec4 > getBoneIDs()
Returns a copy of the bone-ID buffer.
void setCastShadow(bool newCastShadow)
Controls whether the mesh casts shadows.
std::vector< kVec3 > getVertexColors()
Returns a copy of the per-vertex colour buffer.
std::vector< kVec3 > getBitangents()
Returns a copy of the bitangent buffer.
void setBoneIDs(std::vector< kIvec4 > newBoneIDs)
Replaces the entire bone-ID buffer.
void setSkinned(bool newSkinned)
Sets whether this mesh is driven by skeletal animation.
void calculateNormalMatrix()
Recomputes the normal matrix from the current world transform.
void addUV(kVec2 uv)
Appends a UV coordinate.
std::vector< uint32_t > getIndices()
Returns a copy of the index buffer.
void setScale(kVec3 newScale)
Propagates a local scale change to this mesh and its children.
void setRotation(kQuat newRotation)
Propagates a local rotation change to this mesh and its children.
void setPosition(kVec3 newPosition)
Propagates a local position change to this mesh and its children.
void setAnimator(kAnimator *newAnimator)
Attaches a skeletal animator to this mesh.
void addWeight(const kVec4 &weight)
Appends a bone-weight tuple for the next vertex.
void addVertex(kVec3 vertex)
Appends a vertex position.
kAABB getLocalAABB() const
Returns the axis-aligned bounding box in local (object) space.
void draw()
Issues a draw call for this mesh via the current kDriver.
std::vector< kVec3 > getNormals()
Returns a copy of the normal buffer.
bool getVisible()
Returns whether the mesh is visible.
void setVisible(bool newVisible)
Controls the mesh's render visibility.
void setBoneID(size_t vertexIndex, const kIvec4 &boneID)
Overwrites the bone-ID tuple for a specific vertex.
void reserveBoneData(size_t vertexCount)
Pre-allocates bone ID and weight arrays for a given vertex count.
std::vector< kVec3 > getVertices()
Returns a copy of the vertex position buffer.
kMesh(kObject *parentNode=nullptr)
Constructs a mesh node and optionally attaches it to a parent.
std::vector< kVec3 > getTangents()
Returns a copy of the tangent buffer.
void addTangent(kVec3 tangent)
Appends a vertex tangent.
uint32_t getVertexBuffer()
Returns the GPU handle for the position VBO.
void setLoaded(bool newLoaded)
Marks whether the mesh geometry has been fully loaded.
void addIndex(uint32_t index)
Appends an index to the index buffer.
kString getFileName()
Returns the source asset file path.
void addVertexColor(kVec3 color)
Appends a per-vertex colour.
void deserialize(json data)
Restores the mesh from a JSON object.
std::map< kString, kBoneInfo > & getBoneInfoMap()
Returns a reference to the bone-name-to-info map.
bool getSkinned()
Returns whether skeletal skinning is enabled.
uint32_t getVertexArrayObject()
Returns the GPU handle for the Vertex Array Object.
kString getPrimitiveType() const
Returns the procedural-primitive marker, if any.
kObject(kObject *parentNode=nullptr)
Constructs a kObject and optionally attaches it to a parent.
Drives playback of skeletal (bone) animation, and reserves a hook for future non-skeletal (object-tra...
Single skeletal bone with position, rotation, and scale keyframe channels.
Abstract graphics driver interface used by the engine.
Symbol visibility / linkage macro for the Kemena3D library.
#define KEMENA3D_API
Definition kexport.h:35
Base class for all scene-graph nodes in the Kemena3D engine.
nlohmann::json json
Definition kobject.h:30
Top-level Kemena3D engine namespace.
Definition kanimation.h:23
std::string kString
Standard string alias.
Definition kdatatype.h:42
glm::mat3 kMat3
3x3 float matrix.
Definition kdatatype.h:38
glm::quat kQuat
Unit quaternion.
Definition kdatatype.h:40
glm::vec2 kVec2
2-component float vector.
Definition kdatatype.h:32
glm::vec4 kVec4
4-component float vector.
Definition kdatatype.h:34
glm::vec3 kVec3
3-component float vector.
Definition kdatatype.h:33
glm::ivec4 kIvec4
4-component integer vector.
Definition kdatatype.h:37
glm::mat4 kMat4
4x4 float matrix.
Definition kdatatype.h:39
Axis-aligned bounding box (min/max in the same space).
Definition kdatatype.h:572