Commits

Anonymous committed 1ea1483

fixed normals

Comments (0)

Files changed (2)

 
 # Define plugins
 Plugin=RenderSystem_Direct3D9_d
-Plugin=RenderSystem_GL_d
-Plugin=Plugin_ParticleFX_d
 Plugin=Plugin_CgProgramManager_d
 Plugin=Plugin_OctreeSceneManager_d
 

ogre-lib3ds/Test3DSViewerApp.cpp

     mLightNode->attachObject(mLight);
     mLightNode->attachObject(mBBset);
     mLightNode->setPosition(-300, 100, 200);
+    //mLightNode->setPosition(0, 100, 0);
     
 }
 //------------------------------------------------------------------------------
 void Test3DSViewerApp::_build3dsModel()
 {
     SceneNode *modelNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("3ds model");
-    int result;
-    //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/monaco.3DS", "rb");
-
-    //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;
-
-    //result =  lib3ds_file_read(m3dsFile, &m3dsIo);
-
-
-   // 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);
-
-
-   //     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");*/
-
-   // modelNode->attachObject(mObjectBuilder);
-
-
-
-    /*Log *log = LogManager::getSingleton().createLog("3dsdump.log");
-
-    Lib3dsNode *first = m3dsFile->nodes;
-    _dumpNode(log, first, 0, "");*/
-
+   
+    m3dsFile =  lib3ds_file_open("../media/3ds/indochine.3DS");
     //m3dsFile =  lib3ds_file_open("../media/3ds/monaco.3DS");
-    m3dsFile =  lib3ds_file_open("../media/3ds/Modern-home-interior1.3DS");
-
+    //m3dsFile =  lib3ds_file_open("../media/3ds/amphimath2.3DS");
+    //m3dsFile =  lib3ds_file_open("../media/3ds/Modern-home-interior1.3DS");
     if (!m3dsFile->nodes)
         lib3ds_file_create_nodes_for_meshes(m3dsFile);
 
 
     _buildSubtree( m3dsFile->nodes, "/", modelNode);
 
-    modelNode->scale(0.1, 0.1, 0.1);
+    //modelNode->scale(0.1, 0.1, 0.1);
     modelNode->pitch(Degree(-90));
 
     lib3ds_file_free(m3dsFile);
             {
 
                 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());
+                if(!newMesh.isNull())
+                {
+                    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);
+                }
                 
-                newNode->attachObject(ent);
             }
             
             _buildSubtree(p->childs, fullName, newNode);
     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;
+        //int i;
 
         lib3ds_matrix_copy(M, node->base.matrix);
         lib3ds_matrix_translate(M, -node->pivot[0], -node->pivot[1], -node->pivot[2]);
         lib3ds_matrix_inv(inv_matrix);
         lib3ds_matrix_mult(M, M, inv_matrix);
 
-        for (i = 0; i < mesh->nvertices; ++i) {
+        for (int 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
+        // copy everything to vertex buffers
         newObject->begin("Gray", RenderOperation::OT_TRIANGLE_LIST);
 
-        
-        for (int i = 0; i < mesh->nvertices; ++i) 
+        int idx = 0;
+
+        // foreach tri
+        for(int tri_idx = 0 ; tri_idx < mesh->nfaces ; ++tri_idx)
         {
-            
-            newObject->position( mesh->vertices[i][0]
-                               , mesh->vertices[i][1]
-                               , mesh->vertices[i][2]);
-            if (mesh->texcos) 
+           
+            // foreach vertex in tri
+            for(int j=0 ; j<3 ; ++j)
             {
-                newObject->textureCoord(mesh->texcos[i][0], mesh->texcos[i][1]);
+                Vector3 pos, norm;
+                Vector2 tc;
+                   
+                pos = Vector3(mesh->vertices[mesh->faces[tri_idx].index[j]]);
+                newObject->position(pos);
+                                
+                if(mesh->texcos)
+                    tc = Vector2(mesh->texcos[mesh->faces[tri_idx].index[j]]);
+                norm = Vector3(normals[idx]);
+
+
+                newObject->normal(norm);
+
+                newObject->index(idx++);
             }
-            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);
+    
+    MeshPtr newMesh;
+    if(idx)
+    {
+        // create ogre mesh from manualobject
+        newMesh = newObject->convertToMesh(fullMeshName + ".mesh");
+        //newMesh->buildTangentVectors();
+        mSceneMgr->destroyManualObject(newObject);
+    }
+    else
+        newMesh.setNull();
+
     return newMesh;
 }