Umasoft
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Mesh.h
1 //
2 // Mesh.h for OSG
3 //
4 // Copyright (c) 2001-2011 Virtual Terrain Project
5 // Free for all uses, see license.txt for details.
6 //
7 
8 #ifndef VTOSG_MESHH
9 #define VTOSG_MESHH
10 
11 #include <osg/PrimitiveSet>
12 #include <osg/Geometry>
13 
14 #include <osgText/Font>
15 #include <osgText/Text>
16 
17 #ifdef AVOID_OSG_INDICES
18 //#define USE_OPENGL_BUFFER_OBJECTS
19 #endif
20 
21 // Shorthand
22 #define FAB osg::Material::FRONT_AND_BACK
23 
24 // Vertex values
25 #define VT_Normals 1
26 #define VT_Colors 2
27 #define VT_TexCoords 4
28 #ifdef USE_OPENGL_BUFFER_OBJECTS
29 #define VT_VBO 8
30 #endif
31 class vtImage;
32 
35 
45 class vtMesh : public osg::Geometry
46 {
47  friend class vtGeode;
48 
49 public:
50  typedef osg::PrimitiveSet::Mode PrimType;
51 
52  vtMesh(PrimType ePrimType, int VertType, int NumVertices);
53 
54  // Get bounding box
55  void GetBoundBox(FBox3 &box) const;
56 
57  // Adding primitives
58  void AddTri(int p0, int p1, int p2);
59  void AddFan(int p0, int p1, int p2 = -1, int p3 = -1, int p4 = -1, int p5 = -1);
60  void AddFan(int *idx, int iNVerts);
61  void AddStrip(int iNVerts, unsigned short *pIndices);
62  void AddLine(int p0, int p1);
63  int AddLine(const FPoint3 &pos1, const FPoint3 &pos2);
64  void AddQuad(int p0, int p1, int p2, int p3);
65 
66  // Accessors
67 #ifdef AVOID_OSG_INDICES
68  PrimType getPrimType() const { return m_PrimType; }
69 #else
70  PrimType getPrimType() const { return (PrimType) getPrimSet()->getMode(); }
71 #endif
72 
73  void SetMatIndex(int i) { m_iMatIdx = i; }
74  int GetMatIndex() const { return m_iMatIdx; }
75 
76  // Adding vertices
77  int AddVertex(float x, float y, float z);
78  int AddVertexN(float x, float y, float z, float nx, float ny, float nz);
79  int AddVertexUV(float x, float y, float z, float u, float v);
80 
81  int AddVertex(const FPoint3 &p);
82  int AddVertexN(const FPoint3 &p, const FPoint3 &n);
83  int AddVertexUV(const FPoint3 &p, float u, float v);
84  int AddVertexUV(const FPoint3 &p, const FPoint2 &uv);
85  int AddVertexNUV(const FPoint3 &p, const FPoint3 &n, const FPoint2 &uv);
86 
87  // Adding primitives
88  void AddStrip2(int iNVerts, int iStartIndex);
89 
90  void TransformVertices(const FMatrix4 &mat);
91 
92  void CreateEllipsoid(const FPoint3 &center, const FPoint3 &size,
93  int res, bool hemi = false, bool bNormalsIn = false);
94  void CreateBlock(const FPoint3& size);
95  void CreateOptimizedBlock(const FPoint3& size);
96  void CreatePrism(const FPoint3 &base, const FPoint3 &vector_up,
97  const FPoint2 &size1, const FPoint2 &size2);
98  void CreateRectangularMesh(int xsize, int ysize, bool bReverseNormals = false);
99  void CreateCylinder(float height, float radius, int res,
100  bool bTop = true, bool bBottom = true, bool bCentered = true,
101  int direction = 1);
102  void CreateTetrahedron(const FPoint3 &center, float fRadius);
103 
104  void AddRectangleXZ(float xsize, float zsize);
105  void AddRectangleXY(float x, float y, float xsize, float ysize,
106  float z=0.0f, bool bCentered=false);
107 
108  void CreateConicalSurface(const FPoint3 &tip, double radial_angle,
109  double theta1, double theta2,
110  double r1, double r2, int res = 40);
111  void CreateRectangle(int iQuads1, int iQuads2,
112  int Axis1, int Axis2, int Axis3,
113  const FPoint2 &min1, const FPoint2 &max1, float fLevel, float fTiling);
114 
115  // Access vertex properties
116  uint GetNumVertices() const;
117 
118  void SetVtxPos(uint, const FPoint3&);
119  FPoint3 GetVtxPos(uint i) const;
120 
121  void SetVtxNormal(uint, const FPoint3&);
122  FPoint3 GetVtxNormal(uint i) const;
123 
124  void SetVtxColor(uint, const RGBAf&);
125  RGBAf GetVtxColor(uint i) const;
126 
127  void SetVtxTexCoord(uint, const FPoint2&);
128  FPoint2 GetVtxTexCoord(uint i) const;
129 
130  void SetLineWidth(float fWidth);
131 
132  void SetVtxPUV(uint i, const FPoint3 &pos, float u, float v)
133  {
134  SetVtxPos(i, pos);
135  SetVtxTexCoord(i, FPoint2(u, v));
136  }
137  void SetVtxPN(uint i, const FPoint3 &pos, const FPoint3 &norm)
138  {
139  SetVtxPos(i, pos);
140  SetVtxNormal(i, norm);
141  }
142 
143  // Control rendering optimization ("display lists")
144  void ReOptimize();
145  void AllowOptimize(bool bAllow);
146 
147  // Access values
148  int GetNumPrims() const;
149  int GetNumIndices() const { return getVertexIndices()->getNumElements(); }
150  short GetIndex(int i) const { return getIndices()->at(i); }
151  int GetPrimLen(int i) const { return dynamic_cast<const osg::DrawArrayLengths*>(getPrimitiveSet(0))->at(i); }
152 
154 
155  bool hasVertexNormals() const { return getNormalArray() != NULL; }
156  bool hasVertexColors() const { return getColorArray() != NULL; }
157  bool hasVertexTexCoords() const { return getTexCoordArray(0) != NULL; }
158 
159 protected:
160  // Implementation
161  void _AddStripNormals();
162  void _AddPolyNormals();
163  void _AddTriangleNormals();
164  void _AddQuadNormals();
165 
166  osg::PrimitiveSet *getPrimSet() { return getPrimitiveSet(0); }
167  const osg::PrimitiveSet *getPrimSet() const { return getPrimitiveSet(0); }
168 
169  // might not need dynamic_cast here; could be simpler/faster with static cast?
170  osg::DrawArrays *getDrawArrays() { return dynamic_cast<osg::DrawArrays*>(getPrimitiveSet(0)); }
171  osg::DrawArrayLengths *getDrawArrayLengths() { return dynamic_cast<osg::DrawArrayLengths*>(getPrimitiveSet(0)); }
172 
173  const osg::DrawArrays *getDrawArrays() const { return dynamic_cast<const osg::DrawArrays*>(getPrimitiveSet(0)); }
174  const osg::DrawArrayLengths *getDrawArrayLengths() const { return dynamic_cast<const osg::DrawArrayLengths*>(getPrimitiveSet(0)); }
175 
176  osg::UIntArray *getIndices() { return (osg::UIntArray*) getVertexIndices(); }
177  const osg::UIntArray *getIndices() const { return (const osg::UIntArray*) getVertexIndices(); }
178 
179  osg::Vec3Array *getVerts() { return (osg::Vec3Array*) getVertexArray(); }
180  const osg::Vec3Array *getVerts() const { return (const osg::Vec3Array*) getVertexArray(); }
181 
182  osg::Vec3Array *getNormals() { return (osg::Vec3Array*) getNormalArray(); }
183  const osg::Vec3Array *getNormals() const { return (const osg::Vec3Array*) getNormalArray(); }
184 
185  osg::Vec4Array *getColors() { return (osg::Vec4Array*) getColorArray(); }
186  const osg::Vec4Array *getColors() const { return (const osg::Vec4Array*) getColorArray(); }
187 
188  osg::Vec2Array *getTexCoords() { return (osg::Vec2Array*) getTexCoordArray(0); }
189  const osg::Vec2Array *getTexCoords() const { return (const osg::Vec2Array*) getTexCoordArray(0); }
190 
191  int m_iMatIdx;
192 #ifdef AVOID_OSG_INDICES
193  PrimType m_PrimType;
194 #endif
195 };
196 
198 typedef osg::ref_ptr<osgText::Font> vtFontPtr;
199 
205 class vtTextMesh : public osgText::Text
206 {
207 public:
208  vtTextMesh(osgText::Font *font, float fSize = 1, bool bCenter = false);
209 
210  // Override with ability to get OSG bounding box
211  void GetBoundBox(FBox3 &box) const;
212 
214  void SetText(const char *text);
215 
217  void SetText(const wchar_t *text);
218 #if SUPPORT_WSTRING
219  void SetText(const std::wstring &text);
221 #endif
222  void SetPosition(const FPoint3 &pos);
224 
226  void SetRotation(const FQuat &rot);
227 
229  void SetAlignment(int align);
230 
232  void SetColor(const RGBAf &rgba);
233 
234  void SetMatIndex(int i) { m_iMatIdx = i; }
235  int GetMatIndex() const { return m_iMatIdx; }
236 
237 protected:
238  int m_iMatIdx;
239 };
240  // Group sg
242 
243 #endif // VTOSG_MESHH
244