source: GTP/trunk/Lib/Vis/OnlineCullingCHC/ObjReader/src/ObjReader.cpp @ 2111

Revision 2111, 11.2 KB checked in by mattausch, 17 years ago (diff)
Line 
1#include "ObjReader.h"
2#include "gzstream.h"
3#include "Triangle3.h"
4
5
6ObjReader::ObjReader()
7{}
8
9ObjReader::~ObjReader()
10{}
11
12
13bool ObjReader::LoadFile(const string &filename,
14                                                 TriangleContainer &triangles) const
15{
16        igzstream samplesIn(filename.c_str());
17       
18        if (!samplesIn.is_open())
19                return false;
20       
21        // read in triangle size
22        int numTriangles;
23
24        samplesIn.read(reinterpret_cast<char *>(&numTriangles), sizeof(int));
25        triangles.reserve(numTriangles);
26       
27        while (1)
28        {
29                GtpVisibilityPreprocessor::Triangle3 *tri =
30                        new GtpVisibilityPreprocessor::Triangle3();
31               
32                samplesIn.read(reinterpret_cast<char *>((*tri).mVertices + 0),
33                                           sizeof(GtpVisibilityPreprocessor::Vector3));
34                samplesIn.read(reinterpret_cast<char *>((*tri).mVertices + 1),
35                                           sizeof(GtpVisibilityPreprocessor::Vector3));
36                samplesIn.read(reinterpret_cast<char *>((*tri).mVertices + 2),
37                                           sizeof(GtpVisibilityPreprocessor::Vector3));
38
39                // end of file reached
40                if (samplesIn.eof())
41                        break;
42
43                triangles.push_back(tri);
44        }
45       
46        return true;
47}
48
49
50const std::vector<Ogre::Entity *> &ObjReader::GetObjects() const
51{
52        return mObjects;
53}
54
55
56#if TODO
57
58Ogre::Entity *IVReader::createEntity(Ogre::SceneManager* sceneMgr, std::string name, ObjMeshData *mData, Ogre::Vector3 *translation)
59{
60        using namespace Ogre;
61
62        std::string meshName = name.substr(name.find('/',0), name.length()) + "/Mesh";
63        std::string entityName = name + "/Entity";
64
65        Mesh* pMesh = NULL;
66        if ((pMesh = (Mesh *) MeshManager::getSingleton().getByName(meshName).getPointer()) == NULL)
67        {
68
69                ObjMeshData *data = mData->expand();
70
71                ObjManualMeshLoader *loader     = new ObjManualMeshLoader(data);
72
73                if (translation != NULL) *translation = data->boundingBox->getCenter();
74
75//              data->collapse();
76//              delete data;
77
78
79                addMeshToList(meshName, loader);
80
81                Mesh* pMesh = MeshManager::getSingleton().createManual(meshName, "IVGroup", loader).getPointer();
82
83                pMesh->load();
84                pMesh->touch();
85        }
86        else
87        {
88                if (translation != NULL)
89                {
90                        *translation = meshList->getManualMeshLoader(meshName)->getBoundingBox()->getCenter();
91                }
92        }
93
94        Entity *pEntity = sceneMgr->createEntity(entityName, meshName);
95
96
97        if (0) pEntity->setRenderQueueGroup(Ogre::RENDER_QUEUE_MAIN);
98
99        return pEntity;
100}
101
102
103void IVReader::buildTree(Ogre::SceneManager* sceneMgr, Ogre::SceneNode *sceneNodeRoot)
104{
105        using namespace Ogre;
106        if (root == NULL) return;
107
108        std::string s = intToStr(treeCnt) + "/" + fileName + "/Root";
109        SceneNode *actualSceneNode = sceneNodeRoot->createChildSceneNode(s);
110        s = "/"; s = s + fileName + "/RootMaterial";
111       
112        Material* pMaterial = NULL;
113        if ((pMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
114        {
115                pMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
116        }
117        pMaterial->load();
118
119        nodeCnt = 1; matCnt = 1;
120       
121
122        buildTree(sceneMgr, actualSceneNode, root, NULL, pMaterial);   
123        treeCnt++;
124
125
126        std::string message = "Tree of "; message = message + fileName + " built (" + intToStr(nodeCnt) + " Nodes)";
127        if (IVLog != NULL) IVLog->logMessage(message);
128
129}
130
131void IVReader::buildTree(Ogre::SceneManager* sceneMgr, 
132                                                 Ogre::SceneNode *sceneNodeRoot,
133                                                 ObjNode *IVReaderoot, ObjMeshData *mData,
134                                                 Ogre::Material *material)
135{
136        using namespace Ogre;
137
138        Material *newMaterial = NULL;
139
140        bool meshDataCreated = false;
141        if (mData == NULL)
142        {
143                mData = new ObjMeshData();
144                meshDataCreated = true;
145        }
146
147        ObjType t; int i;
148       
149        ObjNode *help = IVReaderoot->getNextChildNode(true);
150        while (help != NULL)
151        {       
152                if (strcmp(help->getName(), "USE") == 0)
153                {
154                        char *defLabel = (char *) help->getField("label", &t, &i);
155                        if (t == IV_STRING)
156                        {
157                                help = defList->getDef(defLabel);
158                        }
159                }
160                if ((strcmp(help->getName(), "Separator") == 0) || (strcmp(help->getName(), "TransformSeparator") == 0))
161                {
162/*                      std::string s = sceneNodeRoot->getName() + "/Sep" + intToStr(nodeCnt);
163                        nodeCnt++;
164                        SceneNode *actualSceneNode = sceneNodeRoot->createChildSceneNode(s);
165                        buildTree(sceneMgr, actualSceneNode, help, mData, actualMaterial);
166*/
167                        if (newMaterial == NULL) newMaterial = material;
168                        buildTree(sceneMgr, sceneNodeRoot, help, mData, newMaterial);
169                }
170
171                if (strcmp(help->getName(), "Texture2") == 0)
172                {
173                        char *textureFileName = (char *) help->getField("filename", &t, &i);
174                        if (t == IV_STRING)
175                        { 
176                                std::string s = sceneNodeRoot->getName().substr(sceneNodeRoot->getName().find('/',0), sceneNodeRoot->getName().length()) + "/Material" + intToStr(matCnt);
177                                matCnt++;
178                                if ((newMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
179                                {
180                                        newMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
181                                        newMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(textureFileName);
182                                }
183                                newMaterial->load();
184                                newMaterial->reload();
185                        }
186                }
187// can be included
188                if (strcmp(help->getName(), "Material") == 0)
189                {
190                        Real *ambientColor = (Real *) help->getField("ambientColor", &t, &i);
191                        if ((i != 3) || (t != IV_REAL)) ambientColor = NULL;
192                        Real *diffuseColor = (Real *) help->getField("diffuseColor", &t, &i);
193                        if ((i != 3) || (t != IV_REAL)) diffuseColor = NULL;
194                        Real *specularColor = (Real *) help->getField("specularColor", &t, &i);
195                        if ((i != 3) || (t != IV_REAL)) specularColor = NULL;
196                        Real *emissiveColor = (Real *) help->getField("emissiveColor", &t, &i);
197                        if ((i != 3) || (t != IV_REAL)) emissiveColor = NULL;
198                        Real *shininess = (Real *) help->getField("shininess", &t, &i);
199                        if ((i != 1) || (t != IV_REAL)) shininess = NULL;
200                        Real *transparency = (Real *) help->getField("transparency", &t, &i);
201                        if ((i != 1) || (t != IV_REAL)) transparency = NULL;
202
203                        if (newMaterial == NULL)
204                        {
205                                std::string s = sceneNodeRoot->getName().substr(sceneNodeRoot->getName().find('/',0), sceneNodeRoot->getName().length()) + "/Material" + intToStr(matCnt);
206                                matCnt++;
207                                if ((newMaterial = (Material *) MaterialManager::getSingleton().getByName(s).getPointer()) == NULL)
208                                {
209                                        if (material == NULL) newMaterial = (Material *)MaterialManager::getSingleton().create(s, "General").getPointer();
210                                        else newMaterial = material->clone(s).getPointer();
211
212                                        Real alpha = 1.f;
213                                        if (transparency != NULL) alpha = 1.f - transparency[0];
214                                        if (ambientColor != NULL) newMaterial->setAmbient(ambientColor[0], ambientColor[1], ambientColor[2]);
215                                        if (diffuseColor != NULL) newMaterial->setDiffuse(diffuseColor[0], diffuseColor[1], diffuseColor[2], alpha);
216                                        if (specularColor != NULL) newMaterial->setSpecular(specularColor[0], specularColor[1], specularColor[2], alpha);
217                                        if (emissiveColor != NULL) newMaterial->setSelfIllumination(emissiveColor[0], emissiveColor[1], emissiveColor[2]);
218                                        if (shininess != NULL) newMaterial->setShininess(shininess[0]);
219                                       
220//                                      if (material->getTechnique(0)->getPass(0)->getNumTextureUnitStates() > 0)                                                       
221//                                      {
222//                                              newMaterial->getTechnique(0)->getPass(0)->createTextureUnitState(material->getTechnique(0)->getPass(0)->getTextureUnitState(0)->getTextureName());
223//                                      }
224                                }
225                                newMaterial->load();
226                                newMaterial->reload();
227                        }
228                        else
229                        {
230                                Real alpha = 1.f;
231                                if (transparency != NULL) alpha = 1.f - transparency[0];
232                                if (ambientColor != NULL) newMaterial->setAmbient(ambientColor[0], ambientColor[1], ambientColor[2]);
233                                if (diffuseColor != NULL) newMaterial->setDiffuse(diffuseColor[0], diffuseColor[1], diffuseColor[2], alpha);
234                                if (specularColor != NULL) newMaterial->setSpecular(specularColor[0], specularColor[1], specularColor[2], alpha);
235                                if (emissiveColor != NULL) newMaterial->setSelfIllumination(emissiveColor[0], emissiveColor[1], emissiveColor[2]);
236                                if (shininess != NULL) newMaterial->setShininess(shininess[0]);
237                                newMaterial->load();
238                        }
239                }
240
241                if (strcmp(help->getName(), "Coordinate3") == 0)
242                {
243                        mData->vertices = (Real *) help->getField("point", &t, &mData->vcnt);
244                        if ((t != IV_REAL) && (t != IV_INT)) mData->vertices = NULL;
245                }
246                if (strcmp(help->getName(), "Normal") == 0)
247                {
248                        mData->normals = (Real *) help->getField("vector", &t, &mData->ncnt);
249                        if ((t != IV_REAL) && (t != IV_INT)) mData->normals = NULL;
250                }
251                if (strcmp(help->getName(), "TextureCoordinate2") == 0)
252                {
253                        mData->texCoords = (Real *) help->getField("point", &t, &mData->tcnt);
254                        if ((t != IV_REAL) && (t != IV_INT)) mData->texCoords = NULL;
255                }
256
257                if (strcmp(help->getName(), "IndexedFaceSet") == 0)
258                {
259                        std::string s = sceneNodeRoot->getName() + "/IFS" + intToStr(nodeCnt);
260                        nodeCnt++;
261                        SceneNode *triangleStripSceneNode = sceneNodeRoot->createChildSceneNode(s);
262
263                        mData->indices = (Real *) help->getField("coordIndex", &t, &mData->icnt);
264                        if (t != IV_INT) mData->indices = NULL;
265
266                        mData->normalIndices = (Real *) help->getField("normalIndex", &t, &mData->nicnt);
267                        if (t != IV_INT) mData->normalIndices = NULL;
268               
269                        mData->texCoordIndices = (Real *) help->getField("textureCoordIndex", &t, &mData->ticnt);
270                        if (t != IV_INT) mData->texCoordIndices = NULL;
271               
272
273                        mData->roType = IV_ROT_FACE_SET;               
274       
275                        Vector3 translation = Vector3(0,0,0);
276                        Entity *pEntity = createEntity(sceneMgr, s, mData, &translation);
277                        if (newMaterial == NULL) newMaterial = material;
278                        if (newMaterial != NULL) pEntity->setMaterialName(newMaterial->getName());
279                               
280                        triangleStripSceneNode->attachObject(pEntity);
281                        triangleStripSceneNode->translate(translation);         
282
283                }
284
285
286                if (strcmp(help->getName(), "IndexedTriangleStripSet") == 0)
287                {
288                        std::string s = sceneNodeRoot->getName() + "/ITS" + intToStr(nodeCnt);
289                        nodeCnt++;
290                        SceneNode *triangleStripSceneNode = sceneNodeRoot->createChildSceneNode(s);
291               
292                        mData->indices = (Real *) help->getField("coordIndex", &t, &mData->icnt);
293                        if (t != IV_INT) mData->indices = NULL;
294
295                        mData->normalIndices = (Real *) help->getField("normalIndex", &t, &mData->nicnt);
296                        if (t != IV_INT) mData->normalIndices = NULL;
297
298                        mData->texCoordIndices = (Real *) help->getField("textureCoordIndex", &t, &mData->ticnt);
299                        if (t != IV_INT) mData->texCoordIndices = NULL;
300
301                        ObjNode *n = help->getNextChildNode(true);
302                        while ((n != NULL) && (strcmp(help->getName(), "VertexProperty") == 0))
303                        {
304                                n = help->getNextChildNode();
305                        }
306                        if (n != NULL)
307                        {
308                                mData->vertices = (Real *) n->getField("vertex", &t, &mData->vcnt);
309                                if ((t != IV_REAL) && (t != IV_INT)) mData->vertices = NULL;
310
311                                mData->normals = (Real *) n->getField("normal", &t, &mData->ncnt);
312                                if ((t != IV_REAL) && (t != IV_INT)) mData->normals = NULL;
313                                mData->texCoords = (Real *) n->getField("texCoord", &t, &mData->tcnt);
314                                if ((t != IV_REAL) && (t != IV_INT)) mData->texCoords = NULL;
315                        }
316
317                        mData->roType = IV_ROT_TRIANGLE_STRIP;         
318
319                        Vector3 translation = Vector3(0,0,0);
320                        Entity *pEntity = createEntity(sceneMgr, s, mData, &translation);
321                        if (newMaterial == NULL) newMaterial = material;
322                        if (newMaterial != NULL) pEntity->setMaterialName(newMaterial->getName());
323                               
324                        triangleStripSceneNode->attachObject(pEntity);
325                        triangleStripSceneNode->translate(translation);         
326
327                }
328
329                help = IVReaderoot->getNextChildNode();
330        }
331        if (meshDataCreated) if (mData != NULL) delete mData;
332}
333#endif
Note: See TracBrowser for help on using the repository browser.