1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
#pragma once
#include "BaseSprite.h"
#include <Containers/SmallVector.h>
using namespace Death::Containers;
namespace nCine
{
/// A scene node representing a mesh with vertices and UVs
class MeshSprite : public BaseSprite
{
public:
/// Vertex data for the mesh
struct Vertex
{
float x, y;
float u, v;
Vertex()
: x(0.0f), y(0.0f), u(0.0f), v(0.0f) {}
Vertex(float xx, float yy, float uu, float vv)
: x(xx), y(yy), u(uu), v(vv) {}
};
static const std::uint32_t VertexBytes = sizeof(Vertex);
static const std::uint32_t VertexFloats = VertexBytes / sizeof(float);
/// Vertex data for the mesh when no texture is specified
struct VertexNoTexture
{
float x, y;
VertexNoTexture()
: x(0.0f), y(0.0f) {}
VertexNoTexture(float xx, float yy)
: x(xx), y(yy) {}
};
static const std::uint32_t VertexNoTextureBytes = sizeof(VertexNoTexture);
static const std::uint32_t VertexNoTextureFloats = VertexNoTextureBytes / sizeof(float);
enum class TextureCutMode
{
RESIZE,
CROP
};
/// Default constructor for a sprite with no parent and no texture, positioned in the origin
MeshSprite();
/// Constructor for a sprite with a parent and texture, positioned in the relative origin
MeshSprite(SceneNode* parent, Texture* texture);
/// Constructor for a sprite with a texture but no parent, positioned in the origin
explicit MeshSprite(Texture* texture);
/// Constructor for a sprite with a parent, a texture and a specified relative position
MeshSprite(SceneNode* parent, Texture* texture, float xx, float yy);
/// Constructor for a sprite with a parent, a texture and a specified relative position as a vector
MeshSprite(SceneNode* parent, Texture* texture, Vector2f position);
/// Constructor for a sprite with a texture and a specified position but no parent
MeshSprite(Texture* texture, float xx, float yy);
/// Constructor for a sprite with a texture and a specified position as a vector but no parent
MeshSprite(Texture* texture, Vector2f position);
MeshSprite& operator=(const MeshSprite&) = delete;
MeshSprite(MeshSprite&&) = default;
MeshSprite& operator=(MeshSprite&&) = default;
/// Returns a copy of this object
inline MeshSprite clone() const {
return MeshSprite(*this);
}
/// Returns the number of bytes used by each vertex
inline std::uint32_t bytesPerVertex() const {
return bytesPerVertex_;
}
/// Returns the number of vertices of the sprite mesh
inline std::uint32_t numVertices() const {
return numVertices_;
}
/// Returns the total number of bytes used by all sprite's vertices
inline std::uint32_t numBytes() const {
return numVertices_ * bytesPerVertex_;
}
/// Returns the vertices data of the sprite mesh
inline const float* vertices() const {
return vertexDataPointer_;
}
/// Returns true if the vertices belong to the sprite and are not stored externally
inline bool uniqueVertices() const {
return vertexDataPointer_ == vertices_.data();
}
/// Copies the vertices data with a custom format from a pointer into the sprite
void copyVertices(std::uint32_t numVertices, std::uint32_t bytesPerVertex, const void* vertexData);
/// Copies the vertices data from a pointer into the sprite
void copyVertices(std::uint32_t numVertices, const Vertex* vertices);
/// Copies the vertices data from a pointer into the sprite (no texture version)
void copyVertices(std::uint32_t numVertices, const VertexNoTexture* vertices);
/// Copies the vertices data from another sprite and sets the same size
void copyVertices(const MeshSprite& meshSprite);
/// Sets the vertices data to point to an external array with a custom format
void setVertices(std::uint32_t numVertices, std::uint32_t bytesPerVertex, const void* vertexData);
/// Sets the vertices data to point to an external array
void setVertices(std::uint32_t numVertices, const Vertex* vertices);
/// Sets the vertices data to point to an external array (no texture version)
void setVertices(std::uint32_t numVertices, const VertexNoTexture* vertices);
/// Sets the vertices data to the data used by another sprite and sets the same size
void setVertices(const MeshSprite& meshSprite);
/// Returns the internal vertices data, cleared and set to the required size (custom format version)
float* emplaceVertices(std::uint32_t numElements, std::uint32_t bytesPerVertex);
/// Returns the internal vertices data, cleared and set to the required size
float* emplaceVertices(std::uint32_t numElements);
/// Creates an internal set of vertices from an external array of points in texture space, with optional texture cut mode
void createVerticesFromTexels(std::uint32_t numVertices, const Vector2f* points, TextureCutMode cutMode);
/// Creates an internal set of vertices from an external array of points in texture space
void createVerticesFromTexels(std::uint32_t numVertices, const Vector2f* points);
/// Returns the number of indices used to draw the sprite mesh
inline std::uint32_t numIndices() const {
return numIndices_;
}
/// Returns the indices used to draw the sprite mesh
inline const std::uint16_t* indices() const {
return indexDataPointer_;
}
/// Returns true if the indices belong to the sprite and are not stored externally
inline bool uniqueIndices() const {
return indexDataPointer_ == indices_.data();
}
/// Copies the indices from a pointer into the sprite
void copyIndices(std::uint32_t numIndices, const std::uint16_t* indices);
/// Copies the indices from another sprite
void copyIndices(const MeshSprite& meshSprite);
/// Sets the indices data to point to an external array
void setIndices(std::uint32_t numIndices, const std::uint16_t* indices);
/// Sets the indices data to the data used by another sprite
void setIndices(const MeshSprite& meshSprite);
/// Returns the internal indices data, cleared and set to the required size
std::uint16_t* emplaceIndices(std::uint32_t numIndices);
inline static ObjectType sType() {
return ObjectType::MeshSprite;
}
protected:
/// Protected copy constructor used to clone objects
MeshSprite(const MeshSprite& other);
private:
/// The array of vertex positions, interleaved with texture coordinates when a texture is attached
SmallVector<float, 0> vertices_;
/// Pointer to vertex data, either from a shared array or unique to this sprite
const float* vertexDataPointer_;
/// The number of bytes used by each vertex
std::uint32_t bytesPerVertex_;
/// The number of vertices, either shared or not, that composes the mesh
std::uint32_t numVertices_;
/// The array of indices used to draw the sprite mesh
SmallVector<std::uint16_t, 0> indices_;
/// Pointer to index data, either from a shared array or unique to this sprite
const std::uint16_t* indexDataPointer_;
/// The number of indices, either shared or not, that composes the mesh
std::uint32_t numIndices_;
/// Initializer method for constructors and the copy constructor
void init();
void shaderHasChanged() override;
void textureHasChanged(Texture* newTexture) override;
};
}
|