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

BlenderFileLoader.cpp « scene_graph « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4dc92755ad98e345b8c0d5cdd5a3c3c11ed504c2 (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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "BlenderFileLoader.h"

BlenderFileLoader::BlenderFileLoader(Render *re, SceneRenderLayer* srl)
{
	_re = re;
	_srl = srl;
  _Scene = NULL;
  _numFacesRead = 0;
  _minEdgeSize = DBL_MAX;
}

BlenderFileLoader::~BlenderFileLoader()
{
  _Scene = NULL;
}

NodeGroup* BlenderFileLoader::Load()
{
	ObjectInstanceRen *obi;
	ObjectRen *obr;

	cout << "\n===  Importing triangular meshes into Blender  ===" << endl;

  // creation of the scene root node
  _Scene = new NodeGroup;

	int id = 0;
	for(obi= (ObjectInstanceRen *) _re->instancetable.first; obi; obi=obi->next) {
		if (!(obi->lay & _re->scene->lay & _srl->lay))
			continue;

		obr= obi->obr;
		
		if( obr->totvlak > 0)
			insertShapeNode(obr, ++id);
		else
			cout << "  Sorry, only vlak-based shapes are supported." << endl;
	}

  //Returns the built scene.
  return _Scene;
}

void BlenderFileLoader::insertShapeNode(ObjectRen *obr, int id)
{
		VlakRen *vlr;
	
	float minBBox[3];
	float maxBBox[3];
	
	NodeTransform *currentMesh = new NodeTransform;
	NodeShape * shape;
	
	// Mesh *mesh = (Mesh *)ob->data;
	//---------------------
	// mesh => obr
	
	// builds the shape:
	shape = new NodeShape;
	
	// 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);
	//---------------------
	// M allows to recover world coordinates from camera coordinates
	// M => obr->ob->imat * obr->obmat  (multiplication from left to right)
	float M[4][4];
	MTC_Mat4MulMat4(M, obr->ob->imat, obr->ob->obmat); 
	
	// 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);
	// mesh_calc_normals(mesh->mvert, mesh->totvert, mesh->mface, mesh->totface, NULL);
	//---------------------
	// already calculated and availabe in vlak ?	
	
	// We build the rep:
	IndexedFaceSet *rep;
	unsigned numFaces = 0;
	for(int a=0; a < obr->totvlak; a++) {
		if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
		else vlr++;
	
		if(vlr->v4)
			numFaces += 2;
		else
			numFaces++;
	}
	
	unsigned vSize = 3*3*numFaces;
	float *vertices = new float[vSize];
	unsigned nSize = vSize;
	float *normals = new float[nSize];
	unsigned *numVertexPerFaces = new unsigned[numFaces];
	vector<FrsMaterial> meshFrsMaterials;
	
	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;
	
	FrsMaterial 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.
	//lib3ds_vector_transform(pvtmp, M, mesh->pointL[mesh->faceL[0].points[0]].pos);
	float pvtmp[3];
	pvtmp[0] = obr->vertnodes[0].vert->co[0];
	pvtmp[1] = obr->vertnodes[0].vert->co[1];
	pvtmp[2] = obr->vertnodes[0].vert->co[2];
	
	MTC_Mat4MulVecfl( M, pvtmp);
	
	minBBox[0] = pvtmp[0];
	maxBBox[0] = pvtmp[0];
	minBBox[1] = pvtmp[1];
	maxBBox[1] = pvtmp[1];
	minBBox[2] = pvtmp[2];
	maxBBox[2] = pvtmp[2];
	
	int p;
	real vert[3][3];
	real norm;
	for(p=0; p < obr->totvlak; ++p) // we parse the faces of the mesh
	{
			VertRen * fv[3];
		
		  // Lib3dsFace *f=&mesh->faceL[p];
		  // Lib3dsMaterial *mat=0;
			if((p & 255)==0) vlr = obr->vlaknodes[p>>8].vlak;
			else vlr++;
			Material *mat = vlr->mat;
	
			if (mat) 
			{
			    tmpMat.setDiffuse( mat->r, mat->g, mat->b, mat->alpha );
			    tmpMat.setSpecular( mat->specr, mat->specg, mat->specb, mat->spectra);
			    float s = 1.0 * (mat->har + 1) / 4 ; // in Blender: [1;511] => in OpenGL: [0;128]
			    if(s > 128.f)
			      s = 128.f;
			    tmpMat.setShininess(s);
			}
	  
			if(meshFrsMaterials.empty())
			{
				meshFrsMaterials.push_back(tmpMat);
		    	shape->setFrsMaterial(tmpMat);
		  	} else {
		    	// find if the material is aleady in the list
		    	unsigned i=0;
		    	bool found = false;
		
		    	for(vector<FrsMaterial>::iterator it=meshFrsMaterials.begin(), itend=meshFrsMaterials.end();
		    		it!=itend;
		    		++it){
		      			if(*it == tmpMat){
		        			currentMIndex = i;
		        			found = true;
		        			break;
		      			}
		      			++i;
		    	}
		
		    	if(!found){
		      		meshFrsMaterials.push_back(tmpMat);
		      		currentMIndex = meshFrsMaterials.size()-1;
		    	}
	  		}
	
			unsigned j;
			fv[0] = vlr->v1;
			fv[1] = vlr->v2;
			fv[2] = vlr->v3;
			float *pv_ptr[3];
			for(i=0; i<3; ++i) // we parse the vertices of the face f
			{
	
				//lib3ds_vector_transform(pv, M, mesh->pointL[f->points[i]].pos); //fills the cells of the pv array
				for(j=0; j<3; j++)
					pv[j] = fv[i]->co[j];
				MTC_Mat4MulVecfl( M, pv);
	
				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];
				}
				
				pv_ptr[i] = pv;
				*pvi = currentIndex;
				*pmi = currentMIndex;

				currentIndex +=3;
				pv += 3;

				pvi++;
				pmi++;
			}
			
			currentIndex -= 9;
						
			float vec01[3];
			vec01[0] = pv_ptr[1][0] - pv_ptr[0][0]; 
			vec01[1] = pv_ptr[1][1] - pv_ptr[0][1];
			vec01[2] = pv_ptr[1][2] - pv_ptr[0][2];
			
			float vec02[3];
			vec02[0] = pv_ptr[2][0] - pv_ptr[0][0]; 
			vec02[1] = pv_ptr[2][1] - pv_ptr[0][1];
			vec02[2] = pv_ptr[2][2] - pv_ptr[0][2];
			
			float n[3];
			MTC_cross3Float(n, vec01, vec02);
			MTC_normalize3DF(n);
			
			for(i=0; i<3; ++i) {
				for(j=0; j<3; ++j) {
					pn[j] = n[j];
				}
				*pni = currentIndex;

				pn += 3;
				pni++;

				currentIndex +=3;
			}
	
			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;

			
			if(vlr->v4){

				unsigned j;
				fv[0] = vlr->v1;
				fv[1] = vlr->v3;
				fv[2] = vlr->v4;
				float *pv_ptr[3];
				for(i=0; i<3; ++i) // we parse the vertices of the face f
				{

					//lib3ds_vector_transform(pv, M, mesh->pointL[f->points[i]].pos); //fills the cells of the pv array
					for(j=0; j<3; j++)
						pv[j] = fv[i]->co[j];
					MTC_Mat4MulVecfl( M, pv);

					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];
					}

					pv_ptr[i] = pv;
					*pvi = currentIndex;
					*pmi = currentMIndex;

					currentIndex +=3;
					pv += 3;

					pvi++;
					pmi++;
				}

				currentIndex -= 9;

				float vec01[3];
				vec01[0] = pv_ptr[1][0] - pv_ptr[0][0]; 
				vec01[1] = pv_ptr[1][1] - pv_ptr[0][1];
				vec01[2] = pv_ptr[1][2] - pv_ptr[0][2];

				float vec02[3];
				vec02[0] = pv_ptr[2][0] - pv_ptr[0][0]; 
				vec02[1] = pv_ptr[2][1] - pv_ptr[0][1];
				vec02[2] = pv_ptr[2][2] - pv_ptr[0][2];

				float n[3];
				MTC_cross3Float(n, vec01, vec02);
				MTC_normalize3DF(n);
				
				for(i=0; i<3; ++i) {
					for(j=0; j<3; ++j) {
						pn[j] = n[j];
					}
					*pni = currentIndex;

					pn += 3;
					pni++;

					currentIndex +=3;
				}

				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;



			}
	
	}
	
	// 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
	FrsMaterial** marray = new FrsMaterial*[meshFrsMaterials.size()];
	unsigned mindex=0;
	for(vector<FrsMaterial>::iterator m=meshFrsMaterials.begin(), mend=meshFrsMaterials.end();
	    m!=mend;
	    ++m){
	  marray[mindex] = new FrsMaterial(*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, meshFrsMaterials.size(),
	                         0, 0,
	                         numFaces, numVertexPerFaces, faceStyle,
	                         cleanVIndices, viSize,
	                         cleanNIndices, niSize,
	                         MIndices, viSize,
	                         0,0,
	                         0);
	// sets the id of the rep
	rep->setId(Id(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);
	
	Matrix44r meshMat = Matrix44r::identity();
	currentMesh->setMatrix(meshMat);
	currentMesh->Translate(0,0,0);
	
	currentMesh->AddChild(shape);
	_Scene->AddChild(currentMesh);
	
}