Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MaxFileLoader.cpp « scene_graph « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 515874c40213e9ba66877c76d8e3cd19320ee5d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

//
//  Copyright (C) : Please refer to the COPYRIGHT file distributed 
//   with this source distribution. 
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
///////////////////////////////////////////////////////////////////////////////

#include "MaxFileLoader.h"

MaxFileLoader::MaxFileLoader()
{
  _FileName = NULL;
  _3dsFile = NULL;
  _Scene = NULL;
  _numFacesRead = 0;
  _minEdgeSize = DBL_MAX;
}

MaxFileLoader::MaxFileLoader(const char *iFileName)
{
  _FileName = new char[strlen(iFileName)+1];
  strcpy(_FileName, iFileName);

  _3dsFile = NULL;
  _Scene = NULL;
  _numFacesRead = 0;
  _minEdgeSize = DBL_MAX;
}

MaxFileLoader::~MaxFileLoader()
{
  if(NULL != _FileName)
  {
    delete [] _FileName;
    _FileName = NULL;
  }

  if(NULL != _3dsFile)
  {
    lib3ds_file_free(_3dsFile);
    _3dsFile = NULL;
  }

  _Scene = NULL;
}

void MaxFileLoader::SetFileName(const char *iFileName)
{
  if(NULL != _FileName)
    delete [] _FileName;

  _FileName = new char[strlen(iFileName)+1];
  strcpy(_FileName, iFileName);
}

NodeGroup* MaxFileLoader::Load()
{
  _3dsFile=lib3ds_file_load(_FileName);
  if(NULL == _3dsFile)
    return NULL;

	/* No nodes?  Fabricate nodes to display all the meshes. */
  if( !_3dsFile->nodes )
  {
    Lib3dsMesh *mesh;
    Lib3dsNode *node;

    for(mesh = _3dsFile->meshes; mesh != NULL; mesh = mesh->next)
    {
      node = lib3ds_node_new_object();
      strcpy(node->name, mesh->name);
      node->parent_id = LIB3DS_NO_PARENT;
      node->data.object.scl_track.keyL = lib3ds_lin3_key_new();
      node->data.object.scl_track.keyL->value[0] = 1.;
      node->data.object.scl_track.keyL->value[1] = 1.;
      node->data.object.scl_track.keyL->value[2] = 1.;
      lib3ds_file_insert_node(_3dsFile, node);
    }
  }

  lib3ds_file_eval(_3dsFile, 0);
  
  // creation of the scene root node
  _Scene = new NodeGroup;

  // converts the 3ds format to the scene format
  // the RenderNode method modifies _Scene.
  Lib3dsNode *p;
  for (p=_3dsFile->nodes; p!=0; p=p->next) {
    RenderNode(p);
  }
  //Returns the built scene.
  return _Scene;
}

void lib3ds_normal_transform(Lib3dsVector c, Lib3dsMatrix m, Lib3dsVector a)
{
  c[0]= (m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2]);
  c[1]= (m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2]);
  c[2]= (m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2]);

  //  c[0]= (m[0][0]*a[0] + m[1][0]*a[1] + m[2][0]*a[2])/m[0][0];
  //  c[1]= (m[0][1]*a[0] + m[1][1]*a[1] + m[2][1]*a[2])/m[1][1];
  //  c[2]= (m[0][2]*a[0] + m[1][2]*a[1] + m[2][2]*a[2])/m[2][2];

  //lib3ds_vector_normalize(c);

  //  c[0] = c[0]*m[0][0];
  //  c[1] = c[1]*m[1][1];
  //  c[2] = c[2]*m[2][2];

}



void MaxFileLoader::RenderNode(Lib3dsNode *iNode)
{
  Lib3dsNode *p;
  for (p=iNode->childs; p!=0; p=p->next) 
    RenderNode(p);

  float minBBox[3];
  float maxBBox[3];
  if (iNode->type==LIB3DS_OBJECT_NODE) 
  {
    if (strcmp(iNode->name,"$$$DUMMY")==0) 
      return;

    NodeTransform *currentMesh = new NodeTransform;
    NodeShape * shape;
    
    if (!iNode->user.d) // If the shape is not built yet, just do it !
    {
      Lib3dsMesh *mesh=lib3ds_file_mesh_by_name(_3dsFile, iNode->name);
      ASSERT(mesh);
      if (!mesh) 
        return;

      // builds the shape:
      shape = new NodeShape;
      iNode->user.d=(unsigned long)shape; // We store as user data the NodeShape address

      // We invert the matrix in order to 
      // be able to retrieve the shape's coordinates 
      // in its local coordinates system (origin is the iNode pivot)
      Lib3dsMatrix M;
      lib3ds_matrix_copy(M, mesh->matrix);
      lib3ds_matrix_inv(M);
      
      // We compute a normal per vertex and manages the smoothing of the shape:
      Lib3dsVector *normalL=(Lib3dsVector*)malloc(3*sizeof(Lib3dsVector)*mesh->faces);
      lib3ds_mesh_calculate_normals(mesh, normalL);

      // We build the rep:
      IndexedFaceSet *rep;
      unsigned numFaces = mesh->faces;
      
      unsigned vSize = 3*3*numFaces;
      float *vertices = new float[vSize];
      unsigned nSize = vSize;
      float *normals = new float[nSize];
      unsigned *numVertexPerFaces = new unsigned[numFaces];
      vector<Material> meshMaterials;

      IndexedFaceSet::TRIANGLES_STYLE *faceStyle = new IndexedFaceSet::TRIANGLES_STYLE[numFaces];
      unsigned i;
      for (i = 0; i <numFaces; i++) {
        faceStyle[i] = IndexedFaceSet::TRIANGLES;
        numVertexPerFaces[i] = 3;
      }
      
      unsigned viSize = 3*numFaces;
      unsigned *VIndices = new unsigned[viSize];
      unsigned niSize = viSize;
      unsigned *NIndices = new unsigned[niSize];
      unsigned *MIndices = new unsigned[viSize]; // Material Indices
     

      float *pv = vertices;
      float *pn = normals;
      unsigned *pvi = VIndices;
      unsigned *pni = NIndices;
      unsigned *pmi = MIndices;
      
      unsigned currentIndex = 0;
      unsigned currentMIndex = 0;

      Material tmpMat;
      
      // we want to find the min and max coordinates as we build the rep. 
      // We initialize the min and max values whith the first vertex.
      float pvtmp[3];
      lib3ds_vector_transform(pvtmp, M, mesh->pointL[mesh->faceL[0].points[0]].pos);
      minBBox[0] = pvtmp[0];
      maxBBox[0] = pvtmp[0];
      minBBox[1] = pvtmp[1];
      maxBBox[1] = pvtmp[1];
      minBBox[2] = pvtmp[2];
      maxBBox[2] = pvtmp[2];

      unsigned p;
      real vert[3][3];
      real norm;
      for(p=0; p<mesh->faces; ++p) // we parse the faces of the mesh
      {
        Lib3dsFace *f=&mesh->faceL[p];
        Lib3dsMaterial *mat=0;
        if (f->material[0]) 
          mat=lib3ds_file_material_by_name(_3dsFile, f->material);

        if (mat) 
        {
          tmpMat.SetDiffuse(mat->diffuse[0], mat->diffuse[1], mat->diffuse[2], mat->diffuse[3]);
          tmpMat.SetSpecular(mat->specular[0], mat->specular[1], mat->specular[2], mat->specular[3]);
          float s = (float)pow(2.0, 10.0*mat->shininess);
          if(s > 128.f)
            s = 128.f;
          tmpMat.SetShininess(s);
        }
        
        if(meshMaterials.empty()){
          meshMaterials.push_back(tmpMat);
          shape->SetMaterial(tmpMat);
        }else{
          // find if the material is aleady in the list
          unsigned i=0;
          bool found = false;
          for(vector<Material>::iterator it=meshMaterials.begin(), itend=meshMaterials.end();
          it!=itend;
          ++it){
            if(*it == tmpMat){
              currentMIndex = i;
              found = true;
              break;
            }
            ++i;
          }
          if(!found){
            meshMaterials.push_back(tmpMat);
            currentMIndex = meshMaterials.size()-1;
          }
        }
        

        for(i=0; i<3; ++i) // we parse the vertices of the face f
        {
          unsigned j;
          lib3ds_vector_transform(pv, M, mesh->pointL[f->points[i]].pos); //fills the cells of the pv array
          for(j=0; j<3; j++) // we parse the xyz coordinates of the vertex i
          {
            if(minBBox[j] > pv[j])
              minBBox[j] = pv[j];

            if(maxBBox[j] < pv[j])
              maxBBox[j] = pv[j];

            vert[i][j] = pv[j];
          }
          
          for(j=0; j<3; j++)
            pn[j] = f->normal[j];

          lib3ds_normal_transform(pn, M, normalL[3*p+i]); //fills the cells of the pv array
          //lib3ds_vector_normalize(pn);

          
          *pvi = currentIndex;
          *pni = currentIndex;
          *pmi = currentMIndex;
          

          currentIndex +=3;
          pv += 3;
          pn += 3;
          pvi++;
          pni++;
          pmi++;
          
        }

        for(i=0; i<3; i++)
        {
          norm = 0.0;
          for (unsigned j = 0; j < 3; j++)
            norm += (vert[i][j] - vert[(i+1)%3][j])*(vert[i][j] - vert[(i+1)%3][j]);
          norm = sqrt(norm);
          if(_minEdgeSize > norm)
            _minEdgeSize = norm;
        }

        _numFacesRead++;
      }

      free(normalL);

      // We might have several times the same vertex. We want a clean 
      // shape with no real-vertex. Here, we are making a cleaning 
      // pass.
      real *cleanVertices = NULL;
      unsigned   cvSize;
      unsigned   *cleanVIndices = NULL;
      
      GeomCleaner::CleanIndexedVertexArray(
        vertices, vSize, 
        VIndices, viSize,
        &cleanVertices, &cvSize, 
        &cleanVIndices);

      real *cleanNormals = NULL;
      unsigned   cnSize;
      unsigned   *cleanNIndices = NULL;

      GeomCleaner::CleanIndexedVertexArray(
        normals, nSize, 
        NIndices, niSize,
        &cleanNormals, &cnSize, 
        &cleanNIndices);

      // format materials array
      Material** marray = new Material*[meshMaterials.size()];
      unsigned mindex=0;
      for(vector<Material>::iterator m=meshMaterials.begin(), mend=meshMaterials.end();
          m!=mend;
          ++m){
        marray[mindex] = new Material(*m);
        ++mindex;
      }
      // deallocates memory:
      delete [] vertices;
      delete [] normals;
      delete [] VIndices;
      delete [] NIndices;
      
      // Create the IndexedFaceSet with the retrieved attributes
      rep = new IndexedFaceSet(cleanVertices, cvSize, 
                               cleanNormals, cnSize,
                               marray, meshMaterials.size(),
                               0, 0,
                               numFaces, numVertexPerFaces, faceStyle,
                               cleanVIndices, viSize,
                               cleanNIndices, niSize,
                               MIndices, viSize,
                               0,0,
                               0);
	  // sets the id of the rep
	  rep->SetId(Id(iNode->node_id, 0));


      const BBox<Vec3r> bbox = BBox<Vec3r>(Vec3r(minBBox[0], minBBox[1], minBBox[2]), 
                                           Vec3r(maxBBox[0], maxBBox[1], maxBBox[2]));
      rep->SetBBox(bbox);
      shape->AddRep(rep);
    }

    if (iNode->user.d) 
    {
      if(NULL != iNode->matrix)
      {
        Lib3dsObjectData *d = &iNode->data.object;
        Matrix44r M44f;
        for(unsigned i=0; i<4; i++)
          for(unsigned j=0; j<4; j++)
            M44f(i,j) = iNode->matrix[j][i];
          
          currentMesh->SetMatrix(Matrix44r(M44f));
          currentMesh->Translate(-d->pivot[0], -d->pivot[1], -d->pivot[2]);
      }
      shape = (NodeShape*)iNode->user.d;
      currentMesh->AddChild(shape);
      _Scene->AddChild(currentMesh);
    }
  }

}