vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GeometryLoaderBase.h
Go to the documentation of this file.
1 #ifndef GEOMETRY_LOADER_BASE_H
2 #define GEOMETRY_LOADER_BASE_H
3 
4 
5 // THIS IS A DERIVATE of the original GeometryLoaderBase from the Phantom library (also in Vrecko solution)...
6 // try to use THAT ONE as a reference.
7 
8 
9 
10 #include <vrecko/Ability.h>
11 #include <osg/Node>
12 #include <osg/Geode>
13 #include <osg/Geometry>
14 
15 using namespace vrecko;
16 
17 namespace APFFDEditor {
18 
19 /*
20  Basic class to load geometry from OpenSceneGraph node
21 */
22 
24 public:
27  // NOTE: Don't forget to call destroyGeometry() in your destructor.
28 
29  // The idea:
30  // This class contains the basic loading routines.
31  // Descendant classes can create their descendant versions of _VertexBase_ and _FaceBase_ classes
32  // but in that case it is IMPORTANT to also override methods that take care of allocation
33  // and returns the correct size of the used structure!
34  // You have to also call destroyGeometry() in your destructor (of the GeometryLoaderBase descendant).
35 
36  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node);
37 
38  struct VertexBase {
39  osg::Vec3 pos;
40  osg::Vec3 realPos;
41  osg::Vec3 normal;
42  osg::Vec3 relPosInGrid;
43  };
44  virtual unsigned long getVertexStructureSize() { return sizeof(VertexBase); }
45  virtual void allocateVertexArray(unsigned long numVertices) { vertices = new VertexBase[numVertices]; vertex_size = getVertexStructureSize(); }
46  virtual void deallocateVertexArray() { if (vertices) { delete[] ((VertexBase*)vertices); } }
47 
48  struct FaceBase {
49  unsigned long vIndex[3]; // vertex indices (ordered clockwise)
50  osg::Vec3 min; // minimum coordinate (used for sorting)
51  osg::Vec3 normal;
52 // int index_to_geometry;
53  };
54  virtual unsigned long getFaceStructureSize() { return sizeof(FaceBase); }
55  virtual void allocateFaceArray(unsigned long numFaces) { faces = new FaceBase[numFaces]; face_size = getFaceStructureSize(); }
56  virtual void deallocateFaceArray() { if (faces) { delete[] ((FaceBase*)faces); }}
57 
58  inline void* getFace(unsigned long index) { return (void*)(((unsigned char*)faces) + index * face_size); }
59  inline void* getVertex(unsigned long index) { return (void*)(((unsigned char*)vertices) + index * vertex_size); }
60 
61  inline unsigned long getFaceCount() { return face_count; }
62  inline unsigned long getVertexCount() { return vertex_count; }
63 
64  void debugWriteNormals();
65 
66 protected:
67  void *faces;
68  unsigned long face_count;
69  unsigned long face_size; // cached number from getFaceStructureSize()
70 
71  void *vertices;
72  unsigned long vertex_count;
73  unsigned long vertex_size; // cached number from getVertexStructureSize()
74 
78 
81  bool getOSGInitCounts(osg::Node *pNode, unsigned long &pVertexCount, unsigned long &pPrimitiveCount);
82 
83  // Fills the buffers with vertices and faces loaded from a given OSG node.
84  virtual bool loadGeometry(osg::Node *pNode);
85  bool loadGeometryHelper(osg::Node *pNode, osg::Matrix &transform, unsigned long &vertex_pos, unsigned long &face_pos);
86  inline osg::Vec3 * getNormalByIndex(osg::Geometry::AttributeBinding normalBinding, osg::Vec3Array * pNormArray, osg::IndexArray * pNormIndices, unsigned long index);
87 
88  // Iterates throught all the faces and sorts vIndex-es clockwise
89  void sortVerticesClockwise();
90 
91  // Deallocates loaded faces/vertices
92  virtual void destroyGeometry();
93 
94  // Ignores the old normals and creates new ones.
95  void recalculateNormals();
96 
97 
98 };
99 
100 inline osg::Vec3 * GeometryLoaderBase::getNormalByIndex(osg::Geometry::AttributeBinding normalBinding, osg::Vec3Array * pNormArray, osg::IndexArray * pNormIndices, unsigned long index) {
101  if (osg::Geometry::BIND_OFF == normalBinding)
102  return ((osg::Vec3*)NULL);
103 
104  if (pNormIndices) {
105  switch (pNormIndices->getDataType()) {
106  case osg::Array::ByteArrayType:
107  index = *(((char*)pNormIndices->getDataPointer()) + index);
108  break;
109  case osg::Array::ShortArrayType:
110  index = *(((short*)pNormIndices->getDataPointer()) + index);
111  break;
112  case osg::Array::IntArrayType:
113  index = *(((int*)pNormIndices->getDataPointer()) + index);
114  break;
115  case osg::Array::UByteArrayType:
116  index = *(((unsigned char*)pNormIndices->getDataPointer()) + index);
117  break;
118  case osg::Array::UShortArrayType:
119  index = *(((unsigned short*)pNormIndices->getDataPointer()) + index);
120  break;
121  case osg::Array::UIntArrayType:
122  index = *(((unsigned int*)pNormIndices->getDataPointer()) + index);
123  break;
124  default:
125 
126  return (osg::Vec3*)NULL;
127  }
128  }
129 
130  return ((osg::Vec3*)pNormArray->getDataPointer()) + index;
131 }
132 
133 }
134 
135 #endif