Cafu Engine
Mesh.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 /************/
8 /*** Mesh ***/
9 /************/
10 
11 #ifndef CAFU_MATSYS_MESH_HPP_INCLUDED
12 #define CAFU_MATSYS_MESH_HPP_INCLUDED
13 
14 #include "Templates/Array.hpp"
15 #include "Math3D/Vector3.hpp"
16 
17 
18 namespace MatSys
19 {
20  /// This class represents a polygonal mesh.
21  //
22  // Current problems with meshes:
23  // - Would like to have Origin, Normal, Tangent etc. as Vector3T<float>s.
24  // - But Origin needs 4 components e.g. for stencil shadows.
25  // - Don't really want to add a w-component for Vector3T<float>s (although realistically possible).
26  // - Who says that the representation of a Vertex with Vector3T<float> members matches the packing and alignment requirements of vertex-arrays and VBOs?
27  // Don't want to (and can't) enforce a particular struct-alignment on Vector3T<float>s that's MatSys specific,
28  // because Vector3T<>s are intended for universal employment, not just for the MatSys.
29  // - In the future, different Renderers might require different internal Mesh representations,
30  // e.g. as a GLfloat[24] for each vertex.
31  // - The requirements of vertex-arrays or especially VBOs (locking, handles, etc.) cannot really be foreseen yet,
32  // e.g., do we have to have something like RendererI::GetVBOMesh() ?
33  //
34  // ==> Abstract meshes, hide their details behind a pure virtual interface, implemented by each Renderer.
35  // They might be constructed by more concrete meshes, similar to the current MeshT struct below,
36  // or "mesh builders" that are free to construct meshes in any way they like.
37  // I don't know yet whether the future mesh interface should have methods for manipulating its vertices or not
38  // (e.g. SetOrigin(VertexNr, const Vector3T<float>& Pos)). Having such methods might be complicated to implement (e.g. with VBOs).
39  // Not having such methods would leave us with a handler-type pointer to mesh interfaces, similar to RenderMaterials.
40  //
41  // ==> TODO: 1. Clarify the exact requirements of vertex-arrays for each Renderer, inclusive OpenGL 2.0.
42  // 2. Clarify the exact requirements of VBOs for each Renderer, inclusive OpenGL 2.0.
43  // 3. Keep the differences/requirements/existance between static and animated meshes in mind.
44  // 4. Armed with that knowledge, revise the meshes.
45  class MeshT
46  {
47  public:
48 
49  enum TypeT { Points, Lines, LineStrip, LineLoop, Triangles, TriangleStrip, TriangleFan, Quads, QuadStrip, Polygon };
50  enum WindingT { CW, CCW };
51 
52  struct VertexT
53  {
54  double Origin[4]; // The position (xyzw) in model space.
55  float Color[4]; // The color (rgba).
56 
57  float TextureCoord[2];
58  float LightMapCoord[2];
59  float SHLMapCoord[2];
60 
61  float Normal[3]; // The normal vector in model space.
62  float Tangent[3];
63  float BiNormal[3];
64 
65  float UserAttribF[4];
66  int UserAttribI[4];
67 
68 
69  // Methods for conveniently setting the components.
70  void SetOrigin(double x=0.0, double y=0.0, double z=0.0, double w=1.0) { Origin[0]=x; Origin[1]=y; Origin[2]=z; Origin[3]=w; }
71  void SetOrigin(const Vector3fT& Pos, float w=1.0) { Origin[0]=Pos.x; Origin[1]=Pos.y; Origin[2]=Pos.z; Origin[3]=w; }
72  void SetColor(float r, float g, float b, float a=1.0) { Color[0]=r; Color[1]=g; Color[2]=b; Color[3]=a; }
73  void SetTextureCoord(float s, float t) { TextureCoord[0]=s; TextureCoord[1]=t; }
74  void SetLightMapCoord(float s, float t) { LightMapCoord[0]=s; LightMapCoord[1]=t; }
75  void SetSHLMapCoord(float s, float t) { SHLMapCoord[0]=s; SHLMapCoord[1]=t; }
76  void SetNormal(float x, float y, float z) { Normal[0]=x; Normal[1]=y; Normal[2]=z; }
77  void SetNormal(const Vector3T<float>& N) { Normal[0]=N.x; Normal[1]=N.y; Normal[2]=N.z; }
78  void SetTangent(float x, float y, float z) { Tangent[0]=x; Tangent[1]=y; Tangent[2]=z; }
79  void SetTangent(const Vector3T<float>& T) { Tangent[0]=T.x; Tangent[1]=T.y; Tangent[2]=T.z; }
80  void SetBiNormal(float x, float y, float z) { BiNormal[0]=x; BiNormal[1]=y; BiNormal[2]=z; }
81  void SetBiNormal(const Vector3T<float>& B) { BiNormal[0]=B.x; BiNormal[1]=B.y; BiNormal[2]=B.z; }
82  };
83 
84  TypeT Type;
85  WindingT Winding; ///< The orientation (cw or ccw) of front faces.
86  ArrayT<VertexT> Vertices;
87 
88 
89  /// Constructor.
90  MeshT(TypeT T=Points, WindingT W=CW) : Type(T), Winding(W)
91  {
92  }
93  };
94 }
95 
96 #endif
WindingT Winding
The orientation (cw or ccw) of front faces.
Definition: Mesh.hpp:85
T y
The y-component of this vector.
Definition: Vector3.hpp:41
Definition: Mesh.hpp:52
T z
The z-component of this vector.
Definition: Vector3.hpp:42
T x
The x-component of this vector.
Definition: Vector3.hpp:40
Definition: Renderer.hpp:16
This class represents a polygonal mesh.
Definition: Mesh.hpp:45
MeshT(TypeT T=Points, WindingT W=CW)
Constructor.
Definition: Mesh.hpp:90