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 
6 #include <vrecko/Ability.h>
7 #include <osg/Node>
8 #include <osg/Geode>
9 #include <osg/Geometry>
10 
11 using namespace vrecko;
12 
13 namespace APSpacePartitioning {
14 
15 #ifdef AP_SPACEPARTITIONING_EXPORTS
16  #define SPACEPARTITIONING_IMP_EXP __declspec(dllexport)
17 #else
18  #define SPACEPARTITIONING_IMP_EXP __declspec(dllimport)
19 #endif
20 
21 
33 public:
39 
40  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node);
41 
45  virtual bool loadGeometry(osg::Node *pNode);
46 
50  struct VertexBase {
51  osg::Vec3 pos;
52  osg::Vec3 normal;
53  };
54  virtual unsigned long getVertexStructureSize() { return sizeof(VertexBase); }
55  virtual void allocateVertexArray(unsigned long numVertices) { vertices = new VertexBase[numVertices]; vertex_size = getVertexStructureSize(); }
56  virtual void deallocateVertexArray() { if (vertices) { delete[] ((VertexBase*)vertices); } }
57  virtual bool saveVerticesToFile(vrecko::BufferedFileWrite *bfile);
58  virtual bool loadVerticesFromFile(vrecko::BufferedFile *bfile);
63  struct FaceBase {
64  unsigned long vIndex[3];
65  osg::Vec3 min;
66  osg::Vec3 normal;
67  };
68 
69  virtual unsigned long getFaceStructureSize() { return sizeof(FaceBase); }
70  virtual void allocateFaceArray(unsigned long numFaces) { faces = new FaceBase[numFaces]; face_size = getFaceStructureSize(); }
71  virtual void deallocateFaceArray() { if (faces) { delete[] ((FaceBase*)faces); }}
72  virtual bool saveFacesToFile(vrecko::BufferedFileWrite *bfile);
73  virtual bool loadFacesFromFile(vrecko::BufferedFile *bfile);
75  inline void* getFace(unsigned long index) { return (void*)(((unsigned char*)faces) + index * face_size); }
76  inline void* getVertex(unsigned long index) { return (void*)(((unsigned char*)vertices) + index * vertex_size); }
78  inline unsigned long getFaceCount() { return face_count; }
79  inline unsigned long getVertexCount() { return vertex_count; }
81  void debugWriteNormals();
83 protected:
84  void *faces;
85  unsigned long face_count;
86  unsigned long face_size;
88  void *vertices;
89  unsigned long vertex_count;
90  unsigned long vertex_size;
94  bool getOSGInitCounts(osg::Node *pNode, unsigned long &pVertexCount, unsigned long &pPrimitiveCount);
96  bool loadGeometryHelper(osg::Node *pNode, osg::Matrix &transform, unsigned long &vertex_pos, unsigned long &face_pos);
98  void sortVerticesClockwise();
99  virtual void destroyGeometry();
100  void recalculateNormals();
102  /*
103  * returns a normal by ID
104  */
105  inline osg::Vec3 * getNormalByIndex(osg::Geometry::AttributeBinding normalBinding, osg::Vec3Array * pNormArray, osg::IndexArray * pNormIndices, unsigned long index) {
106  if (osg::Geometry::BIND_OFF == normalBinding)
107  return ((osg::Vec3*)NULL);
108 
109  if (pNormIndices) {
110  switch (pNormIndices->getDataType()) {
111  case osg::Array::ByteArrayType:
112  index = *(((char*)pNormIndices->getDataPointer()) + index);
113  break;
114  case osg::Array::ShortArrayType:
115  index = *(((short*)pNormIndices->getDataPointer()) + index);
116  break;
117  case osg::Array::IntArrayType:
118  index = *(((int*)pNormIndices->getDataPointer()) + index);
119  break;
120  case osg::Array::UByteArrayType:
121  index = *(((unsigned char*)pNormIndices->getDataPointer()) + index);
122  break;
123  case osg::Array::UShortArrayType:
124  index = *(((unsigned short*)pNormIndices->getDataPointer()) + index);
125  break;
126  case osg::Array::UIntArrayType:
127  index = *(((unsigned int*)pNormIndices->getDataPointer()) + index);
128  break;
129  default:
130  logger.debugLog("GeometryLoaderBase error: Unsupported data type for normals array");
131  return (osg::Vec3*)NULL;
132  }
133  }
134  return ((osg::Vec3*)pNormArray->getDataPointer()) + index;
135 }
136 };
137 
138 }
139 
140 #endif
141 
142