Commits

Anonymous committed 9a1803e

instances duuplicated and transformed at the right place (most of the time)

Comments (0)

Files changed (3)

media/gray.material

     {
         pass
         {
+			//cull_hardware none
             lightning on
-			shading flat
+			shading phong
             ambient 0.5 0.5 0.5
             diffuse 0.5 0.5 0.5
         }

ogre-lib3ds/Test3DSViewerApp.cpp

 
 
 Test3DSViewerApp::Test3DSViewerApp(void)
+:mDummyCnt(0)
+,mNodeCnt(0)
 {
 }
 
 //------------------------------------------------------------------------------
 void Test3DSViewerApp::createScene()
 {
+    m3dsBuildLog = LogManager::getSingleton().createLog("3dsbuild.log");
+
+    mSceneMgr->setNormaliseNormalsOnScale(true);
     _createGrid(500);
     mCamera->setPosition(100, 100, 100);
     mCamera->lookAt(Vector3::ZERO);
     mLightNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("light node");
     mLightNode->attachObject(mLight);
     mLightNode->attachObject(mBBset);
-    mLightNode->setPosition(-400, 100, 200);
+    mLightNode->setPosition(-300, 100, 200);
     
 }
 //------------------------------------------------------------------------------
 {
     SceneNode *modelNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("3ds model");
     int result;
-    //mFile = fopen("cube.3ds", "rb");
+    //mFile = fopen("../media/3ds/cube.3ds", "rb");
     //mFile = fopen("../media/3ds/Modern-home-interior1.3DS", "rb");
-    mFile = fopen("../media/3ds/indochine.3DS", "rb");
+   // mFile = fopen("../media/3ds/indochine.3DS", "rb");
+    //mFile = fopen("../media/3ds/monaco.3DS", "rb");
 
-    m3dsFile = lib3ds_file_new();
+    //m3dsFile = lib3ds_file_new();
 
-    memset(&m3dsIo, 0, sizeof(m3dsIo));
-    m3dsIo.self = mFile;
-    m3dsIo.seek_func = fileio_seek_func;
-    m3dsIo.tell_func = fileio_tell_func;
-    m3dsIo.read_func = fileio_read_func;
-    m3dsIo.write_func = fileio_write_func;
-    m3dsIo.log_func = fileio_log_func;
+    //memset(&m3dsIo, 0, sizeof(m3dsIo));
+    //m3dsIo.self = mFile;
+    //m3dsIo.seek_func = fileio_seek_func;
+    //m3dsIo.tell_func = fileio_tell_func;
+    //m3dsIo.read_func = fileio_read_func;
+    //m3dsIo.write_func = fileio_write_func;
+    //m3dsIo.log_func = fileio_log_func;
 
-    result =  lib3ds_file_read(m3dsFile, &m3dsIo);
+    //result =  lib3ds_file_read(m3dsFile, &m3dsIo);
 
-    mObjectBuilder = mSceneMgr->createManualObject("3ds cube");
 
+   // mObjectBuilder = mSceneMgr->createManualObject("3ds cube");
 
-    
-    for(int j=0 ; j<m3dsFile->nmeshes ; ++j)
-    {
-        Lib3dsMesh *mesh = m3dsFile->meshes[j];
-    
-        float (*normals)[3] = (float(*)[3])malloc(sizeof(float) * 9 * mesh->nfaces);
-        lib3ds_mesh_calculate_vertex_normals(mesh, normals);
 
+   // 
+   // for(int j=0 ; j<m3dsFile->nmeshes ; ++j)
+   // {
+   //     Lib3dsMesh *mesh = m3dsFile->meshes[j];
+   // 
+   //     float (*normals)[3] = (float(*)[3])malloc(sizeof(float) * 9 * mesh->nfaces);
+   //     lib3ds_mesh_calculate_vertex_normals(mesh, normals);
 
-        mObjectBuilder->begin("Gray", RenderOperation::OT_TRIANGLE_LIST);
-        float p[3], t[2];
-        for (int i = 0; i < mesh->nvertices; ++i) {
-            lib3ds_vector_copy(p, mesh->vertices[i]);
-            mObjectBuilder->position(p[1], p[2], p[0]);
-            if (mesh->texcos) {
-                mObjectBuilder->textureCoord(mesh->texcos[i][0], mesh->texcos[i][1]);
-                
-            }
-            Vector3 n(normals[i][0], normals[i][2], normals[i][1]);
-            n.normalise();
-            mObjectBuilder->normal(n);
-        }
-        for(int i=0 ; i<mesh->nfaces ; i++)
-        {
-            mObjectBuilder->index(mesh->faces[i].index[0]);
-            mObjectBuilder->index(mesh->faces[i].index[1]);
-            mObjectBuilder->index(mesh->faces[i].index[2]);
-        }
 
-        mObjectBuilder->end();
-    }
-   /* MeshPtr ogreMesh = mObjectBuilder->convertToMesh("indochine.3ds");
-    ogreMesh->buildEdgeList();
-    
-    Entity *ent = mSceneMgr->createEntity("indo", "indochine.3ds");*/
+   //     mObjectBuilder->begin("Gray", RenderOperation::OT_TRIANGLE_LIST);
+   //     float p[3], t[2];
+   //     for (int i = 0; i < mesh->nvertices; ++i) {
+   //         lib3ds_vector_copy(p, mesh->vertices[i]);
+   //         mObjectBuilder->position(p[1], p[2], p[0]);
+   //         if (mesh->texcos) {
+   //             mObjectBuilder->textureCoord(mesh->texcos[i][0], mesh->texcos[i][1]);
+   //             
+   //         }
+   //         Vector3 n(normals[i][0], normals[i][2], normals[i][1]);
+   //         n.normalise();
+   //         mObjectBuilder->normal(n);
+   //     }
+   //     for(int i=0 ; i<mesh->nfaces ; i++)
+   //     {
+   //         mObjectBuilder->index(mesh->faces[i].index[0]);
+   //         mObjectBuilder->index(mesh->faces[i].index[1]);
+   //         mObjectBuilder->index(mesh->faces[i].index[2]);
+   //     }
 
-    modelNode->attachObject(mObjectBuilder);
+   //     mObjectBuilder->end();
+   // }
+   ///* MeshPtr ogreMesh = mObjectBuilder->convertToMesh("indochine.3ds");
+   // ogreMesh->buildEdgeList();
+   // 
+   // Entity *ent = mSceneMgr->createEntity("indo", "indochine.3ds");*/
+
+   // modelNode->attachObject(mObjectBuilder);
 
 
 
     Lib3dsNode *first = m3dsFile->nodes;
     _dumpNode(log, first, 0, "");*/
 
+    //m3dsFile =  lib3ds_file_open("../media/3ds/monaco.3DS");
+    m3dsFile =  lib3ds_file_open("../media/3ds/Modern-home-interior1.3DS");
 
-    fclose(mFile);
+    if (!m3dsFile->nodes)
+        lib3ds_file_create_nodes_for_meshes(m3dsFile);
+
+    lib3ds_file_eval(m3dsFile, 0);
+
+    _buildSubtree( m3dsFile->nodes, "/", modelNode);
+
+    modelNode->scale(0.1, 0.1, 0.1);
+    modelNode->pitch(Degree(-90));
+
+    lib3ds_file_free(m3dsFile);
+ 
 }
-
+//------------------------------------------------------------------------------
 void Test3DSViewerApp::_dumpNode(Log *_log, Lib3dsNode *_node
                                 , int _level, std::string _basename)
 {
             _dumpNode(_log, p->childs, _level+1, fullName);
         }
     }
+}
+
+//------------------------------------------------------------------------------
+void Test3DSViewerApp::_buildSubtree(Lib3dsNode *_node
+                                    ,const std::string &_basename
+                                    ,SceneNode *_parentNode)
+{
+    boost::format fullNameFmt("%s/%06d%s");
+
+    Lib3dsNode *p;
+    for(p = _node ; p ; p=p->next)
+    {
+        
+        if (p->type == LIB3DS_NODE_MESH_INSTANCE) 
+        {
+            mNodeCnt++;
+            fullNameFmt % _basename % p->node_id % p->name;
+
+            std::string fullName = fullNameFmt.str();
+
+            Lib3dsMeshInstanceNode *n = (Lib3dsMeshInstanceNode*) p;
+
+            m3dsBuildLog->logMessage(boost::str(boost::format("building new node (%d) : %s") % mNodeCnt % fullName));
+
+            SceneNode *newNode = _parentNode->createChildSceneNode(fullName + " Node");
+            //newNode->setPosition(n->pivot[0], n->pivot[1], n->pivot[2]);
+
+            Lib3dsMesh *mesh = lib3ds_file_mesh_for_node(m3dsFile, (Lib3dsNode*)n);
+            
+            if(mesh && mesh->nvertices)
+            {
+
+                MeshPtr newMesh = _convert3dsMeshToOgreMesh(mesh, n, fullName);
+                mMeshVect.push_back(newMesh);
+                mMeshes[newMesh->getName()] = newMesh;
+                    
+                m3dsBuildLog->logMessage(boost::str(boost::format("attaching %s to node %s")% newMesh->getName() % fullName));
+                Entity *ent = mSceneMgr->createEntity(fullName+" Ent", newMesh->getName());
+                
+                newNode->attachObject(ent);
+            }
+            
+            _buildSubtree(p->childs, fullName, newNode);
+        }
+    }
+}
+//------------------------------------------------------------------------------
+MeshPtr Test3DSViewerApp::_convert3dsMeshToOgreMesh(Lib3dsMesh *_mesh
+                                                    , Lib3dsMeshInstanceNode *_node
+                                                    , const std::string &_basename)
+{
+    boost::format fmt("%s -- %s");
+    fmt % _basename % _mesh->name;
+    std::string fullMeshName = fmt.str();
+
+    m3dsBuildLog->logMessage(std::string("building new mesh : ") + fullMeshName+".mesh");
+    ManualObject *newObject = mSceneMgr->createManualObject(fullMeshName);
+    Lib3dsMesh *mesh = _mesh;
+    Lib3dsMeshInstanceNode *node = _node;
+
+
+    float (*orig_vertices)[3];
+
+    //save mesh
+    orig_vertices = (float(*)[3])malloc(sizeof(float) * 3 * mesh->nvertices);
+    memcpy(orig_vertices, mesh->vertices, sizeof(float) * 3 * mesh->nvertices);
+    
+    // create translated ogre manualobject
+    {
+        float inv_matrix[4][4], M[4][4];
+        float tmp[3];
+        int i;
+
+        lib3ds_matrix_copy(M, node->base.matrix);
+        lib3ds_matrix_translate(M, -node->pivot[0], -node->pivot[1], -node->pivot[2]);
+        lib3ds_matrix_copy(inv_matrix, mesh->matrix);
+        lib3ds_matrix_inv(inv_matrix);
+        lib3ds_matrix_mult(M, M, inv_matrix);
+
+        for (i = 0; i < mesh->nvertices; ++i) {
+            lib3ds_vector_transform(tmp, M, mesh->vertices[i]);
+            lib3ds_vector_copy(mesh->vertices[i], tmp);
+        }
+   
+        float (*normals)[3] = (float(*)[3])malloc(sizeof(float) * 9 * mesh->nfaces);
+        lib3ds_mesh_calculate_vertex_normals(mesh, normals);
+        
+        // copy everything to vertexbuffers
+        newObject->begin("Gray", RenderOperation::OT_TRIANGLE_LIST);
+
+        
+        for (int i = 0; i < mesh->nvertices; ++i) 
+        {
+            
+            newObject->position( mesh->vertices[i][0]
+                               , mesh->vertices[i][1]
+                               , mesh->vertices[i][2]);
+            if (mesh->texcos) 
+            {
+                newObject->textureCoord(mesh->texcos[i][0], mesh->texcos[i][1]);
+            }
+            Vector3 n(normals[i][0], normals[i][1], normals[i][2]);
+            
+            newObject->normal(n);
+        }
+
+        for(int i=0 ; i<mesh->nfaces ; i++)
+        {
+            newObject->index(mesh->faces[i].index[0]);
+            newObject->index(mesh->faces[i].index[1]);
+            newObject->index(mesh->faces[i].index[2]);
+        }
+
+        newObject->end();
+        free(normals); 
+    }
+    //restore mesh for future use
+    memcpy(mesh->vertices, orig_vertices, sizeof(float) * 3 * mesh->nvertices);
+    free(orig_vertices);
+
+    // create ogre mesh from manualobject
+    MeshPtr newMesh = newObject->convertToMesh(fullMeshName + ".mesh");
+    //newMesh->buildTangentVectors();
+    mSceneMgr->destroyManualObject(newObject);
+    return newMesh;
 }

ogre-lib3ds/Test3DSViewerApp.h

     void createScene();
     void _createGrid(int);
     void _build3dsModel();
+    void _buildSubtree(Lib3dsNode*, const std::string&
+                      ,SceneNode*);
+    MeshPtr _convert3dsMeshToOgreMesh(Lib3dsMesh*
+                                     ,Lib3dsMeshInstanceNode*
+                                     ,const std::string&);
 
     void _dumpNode(Log*, Lib3dsNode*, int, std::string);
 
     Light *mLight;
     SceneNode *mLightNode;
 
-    std::map<std::string, Entity*> mMeshes;
+    std::map<std::string, MeshPtr> mMeshes;
+    std::list<MeshPtr> mMeshVect;
+
+    Log *m3dsBuildLog;
+    int mDummyCnt, mNodeCnt;
 };