celestia/src/celmodel/mesh.h

226 lines
6.3 KiB
C
Raw Normal View History

2010-02-05 13:10:32 -07:00
// mesh.h
//
// Copyright (C) 2004-2010, Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELMODEL_MESH_H_
#define _CELMODEL_MESH_H_
#include "material.h"
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <vector>
#include <string>
namespace cmod
{
class Mesh
{
public:
// 32-bit index type
typedef unsigned int index32;
class BufferResource
{
};
enum VertexAttributeSemantic
{
Position = 0,
Color0 = 1,
Color1 = 2,
Normal = 3,
Tangent = 4,
Texture0 = 5,
Texture1 = 6,
Texture2 = 7,
Texture3 = 8,
PointSize = 9,
2020-12-11 06:38:12 -07:00
NextPosition = 10,
ScaleFactor = 11,
SemanticMax = 12,
2010-02-05 13:10:32 -07:00
InvalidSemantic = -1,
};
enum VertexAttributeFormat
{
Float1 = 0,
Float2 = 1,
Float3 = 2,
Float4 = 3,
UByte4 = 4,
FormatMax = 5,
InvalidFormat = -1,
};
struct VertexAttribute
{
VertexAttribute() :
semantic(InvalidSemantic),
format(InvalidFormat),
offset(0)
{
}
VertexAttribute(VertexAttributeSemantic _semantic,
VertexAttributeFormat _format,
unsigned int _offset) :
semantic(_semantic),
format(_format),
offset(_offset)
{
}
VertexAttributeSemantic semantic;
VertexAttributeFormat format;
unsigned int offset;
};
struct VertexDescription
{
VertexDescription(unsigned int _stride,
unsigned int _nAttributes,
VertexAttribute* _attributes);
VertexDescription(const VertexDescription& desc);
~VertexDescription();
const VertexAttribute& getAttribute(VertexAttributeSemantic semantic) const
{
return semanticMap[semantic];
}
2020-12-11 06:38:12 -07:00
VertexDescription appendingAttributes(const VertexAttribute* newAttributes, int count) const;
2010-02-05 13:10:32 -07:00
bool validate() const;
VertexDescription& operator=(const VertexDescription&);
unsigned int stride;
unsigned int nAttributes;
VertexAttribute* attributes;
private:
void clearSemanticMap();
void buildSemanticMap();
2010-02-05 13:10:32 -07:00
// Vertex attributes indexed by semantic
VertexAttribute semanticMap[SemanticMax];
};
enum PrimitiveGroupType
{
TriList = 0,
TriStrip = 1,
TriFan = 2,
LineList = 3,
LineStrip = 4,
PointList = 5,
SpriteList = 6,
PrimitiveTypeMax = 7,
InvalidPrimitiveGroupType = -1
};
class PrimitiveGroup
{
public:
PrimitiveGroup() = default;
~PrimitiveGroup() = default;
2010-02-05 13:10:32 -07:00
unsigned int getPrimitiveCount() const;
2010-02-05 13:10:32 -07:00
PrimitiveGroupType prim;
unsigned int materialIndex;
index32* indices;
unsigned int nIndices;
2020-12-11 06:38:12 -07:00
PrimitiveGroupType primOverride;
void* vertexOverride;
unsigned int vertexCountOverride;
index32* indicesOverride;
unsigned int nIndicesOverride;
VertexDescription vertexDescriptionOverride { 0, 0, nullptr };
2010-02-05 13:10:32 -07:00
};
class PickResult
{
public:
PickResult() = default;
Mesh* mesh{ nullptr };
PrimitiveGroup* group{ nullptr };
unsigned int primitiveIndex{ 0 };
double distance{ -1.0 };
};
Mesh() = default;
2010-02-05 13:10:32 -07:00
~Mesh();
void setVertices(unsigned int _nVertices, void* vertexData);
bool setVertexDescription(const VertexDescription& desc);
const VertexDescription& getVertexDescription() const;
2020-12-11 06:38:12 -07:00
PrimitiveGroup* createLinePrimitiveGroup(bool lineStrip, unsigned int nIndices, Mesh::index32* indices);
2010-02-05 13:10:32 -07:00
const PrimitiveGroup* getGroup(unsigned int index) const;
PrimitiveGroup* getGroup(unsigned int index);
2010-02-05 13:10:32 -07:00
unsigned int addGroup(PrimitiveGroup* group);
unsigned int addGroup(PrimitiveGroupType prim,
unsigned int materialIndex,
unsigned int nIndices,
index32* indices);
unsigned int getGroupCount() const;
void remapIndices(const std::vector<index32>& indexMap);
void clearGroups();
void remapMaterials(const std::vector<unsigned int>& materialMap);
/*! Reorder primitive groups so that groups with identical materials
* appear sequentially in the primitive group list. This will reduce
* the number of graphics state changes at render time.
*/
void aggregateByMaterial();
const std::string& getName() const;
void setName(const std::string&);
bool pick(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction, PickResult* result) const;
2010-02-05 13:10:32 -07:00
bool pick(const Eigen::Vector3d& origin, const Eigen::Vector3d& direction, double& distance) const;
Eigen::AlignedBox<float, 3> getBoundingBox() const;
void transform(const Eigen::Vector3f& translation, float scale);
const void* getVertexData() const { return vertices; }
unsigned int getVertexCount() const { return nVertices; }
unsigned int getVertexStride() const { return vertexDesc.stride; }
unsigned int getPrimitiveCount() const;
static PrimitiveGroupType parsePrimitiveGroupType(const std::string&);
static VertexAttributeSemantic parseVertexAttributeSemantic(const std::string&);
static VertexAttributeFormat parseVertexAttributeFormat(const std::string&);
static Material::TextureSemantic parseTextureSemantic(const std::string&);
static unsigned int getVertexAttributeSize(VertexAttributeFormat);
private:
void recomputeBoundingBox();
private:
VertexDescription vertexDesc{ 0, 0, nullptr };
2010-02-05 13:10:32 -07:00
unsigned int nVertices{ 0 };
void* vertices{ nullptr };
mutable BufferResource* vbResource{ nullptr };
2010-02-05 13:10:32 -07:00
std::vector<PrimitiveGroup*> groups;
std::string name;
};
} // namespace cmod
#endif // !_CELMESH_MESH_H_