00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _CELENGINE_MESH_H_
00011 #define _CELENGINE_MESH_H_
00012
00013 #include <celutil/basictypes.h>
00014 #include <celutil/color.h>
00015 #include <celutil/reshandle.h>
00016 #include <celmath/ray.h>
00017 #include <celmath/aabox.h>
00018 #include <vector>
00019 #include <string>
00020
00021
00022 class RenderContext;
00023
00024 class Mesh
00025 {
00026 public:
00027 enum VertexAttributeSemantic
00028 {
00029 Position = 0,
00030 Color0 = 1,
00031 Color1 = 2,
00032 Normal = 3,
00033 Tangent = 4,
00034 Texture0 = 5,
00035 Texture1 = 6,
00036 Texture2 = 7,
00037 Texture3 = 8,
00038 SemanticMax = 9,
00039 InvalidSemantic = -1,
00040 };
00041
00042 enum VertexAttributeFormat
00043 {
00044 Float1 = 0,
00045 Float2 = 1,
00046 Float3 = 2,
00047 Float4 = 3,
00048 UByte4 = 4,
00049 FormatMax = 5,
00050 InvalidFormat = -1,
00051 };
00052
00053 struct VertexAttribute
00054 {
00055 VertexAttribute() :
00056 semantic(InvalidSemantic),
00057 format(InvalidFormat),
00058 offset(0)
00059 {
00060 }
00061
00062 VertexAttribute(VertexAttributeSemantic _semantic,
00063 VertexAttributeFormat _format,
00064 uint32 _offset) :
00065 semantic(_semantic),
00066 format(_format),
00067 offset(_offset)
00068 {
00069 }
00070
00071 VertexAttributeSemantic semantic;
00072 VertexAttributeFormat format;
00073 uint32 offset;
00074 };
00075
00076 struct VertexDescription
00077 {
00078 VertexDescription(uint32 _stride,
00079 uint32 _nAttributes,
00080 VertexAttribute* _attributes);
00081 VertexDescription(const VertexDescription& desc);
00082 ~VertexDescription();
00083
00084 const VertexAttribute& getAttribute(VertexAttributeSemantic semantic) const
00085 {
00086 return semanticMap[semantic];
00087 }
00088
00089 bool validate() const;
00090
00091 VertexDescription& operator=(const VertexDescription&);
00092
00093 uint32 stride;
00094 uint32 nAttributes;
00095 VertexAttribute* attributes;
00096
00097 private:
00098 void clearSemanticMap();
00099 void buildSemanticMap();
00100
00101
00102 VertexAttribute semanticMap[SemanticMax];
00103 };
00104
00105 enum TextureSemantic
00106 {
00107 DiffuseMap = 0,
00108 NormalMap = 1,
00109 SpecularMap = 2,
00110 EmissiveMap = 3,
00111 TextureSemanticMax = 4,
00112 InvalidTextureSemantic = -1,
00113 };
00114
00115 class Material
00116 {
00117 public:
00118 Material();
00119
00120 Color diffuse;
00121 Color emissive;
00122 Color specular;
00123 float specularPower;
00124 float opacity;
00125 ResourceHandle maps[TextureSemanticMax];
00126 };
00127
00128 enum PrimitiveGroupType
00129 {
00130 TriList = 0,
00131 TriStrip = 1,
00132 TriFan = 2,
00133 LineList = 3,
00134 LineStrip = 4,
00135 PointList = 5,
00136 PrimitiveTypeMax = 6,
00137 InvalidPrimitiveGroupType = -1
00138 };
00139
00140 class PrimitiveGroup
00141 {
00142 public:
00143 PrimitiveGroup();
00144 ~PrimitiveGroup();
00145
00146 PrimitiveGroupType prim;
00147 uint32 materialIndex;
00148 uint32* indices;
00149 uint32 nIndices;
00150 };
00151
00152 Mesh();
00153 ~Mesh();
00154
00155 void setVertices(uint32 _nVertices, void* vertexData);
00156 bool setVertexDescription(const VertexDescription& desc);
00157 const VertexDescription& getVertexDescription() const;
00158
00159 const PrimitiveGroup* getGroup(uint32) const;
00160 uint32 addGroup(PrimitiveGroup* group);
00161 uint32 addGroup(PrimitiveGroupType prim,
00162 uint32 materialIndex,
00163 uint32 nIndices,
00164 uint32* indices);
00165 void remapIndices(const std::vector<uint32>& indexMap);
00166 void clearGroups();
00167
00168 const std::string& getName() const;
00169 void setName(const std::string&);
00170
00171 bool pick(const Ray3d& r, double& distance) const;
00172 void render(const std::vector<const Material*>& materials,
00173 RenderContext&) const;
00174
00175 AxisAlignedBox getBoundingBox() const;
00176 void transform(Vec3f translation, float scale);
00177
00178 const void* getVertexData() const { return vertices; }
00179 uint32 getVertexCount() const { return nVertices; }
00180 uint32 getVertexStride() const { return vertexDesc.stride; }
00181
00182 static PrimitiveGroupType parsePrimitiveGroupType(const std::string&);
00183 static VertexAttributeSemantic parseVertexAttributeSemantic(const std::string&);
00184 static VertexAttributeFormat parseVertexAttributeFormat(const std::string&);
00185 static TextureSemantic parseTextureSemantic(const std::string&);
00186 static uint32 getVertexAttributeSize(VertexAttributeFormat);
00187
00188 private:
00189 void recomputeBoundingBox();
00190
00191 private:
00192 VertexDescription vertexDesc;
00193
00194 uint32 nVertices;
00195 void* vertices;
00196 mutable uint32 vbObject;
00197 mutable bool vbInitialized;
00198
00199 std::vector<PrimitiveGroup*> groups;
00200
00201 std::string name;
00202 };
00203
00204
00205 #if 0
00206 #include <celmath/frustum.h>
00207 #include <celmath/ray.h>
00208
00209
00210 class Mesh
00211 {
00212 public:
00213 virtual ~Mesh() {};
00214 virtual void render(float lod) = 0;
00215 virtual void render(unsigned int attributes, float lod) = 0;
00216 virtual void render(unsigned int attributes, const Frustum&, float lod) = 0;
00217 virtual bool pick(const Ray3d&, double&) { return false; };
00218
00219 enum {
00220 Normals = 0x01,
00221 Tangents = 0x02,
00222 Colors = 0x04,
00223 TexCoords0 = 0x08,
00224 TexCoords1 = 0x10,
00225 VertexProgParams = 0x1000,
00226 Multipass = 0x10000000,
00227 };
00228 };
00229 #endif
00230
00231 #endif // !_CELMESH_MESH_H_
00232