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

BlenderFileLoader.cpp « blender_interface « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab7e58287cf2ac53c0f2d34d9dc3f0a8b11a79dd (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
417
418
419
420
421
422
423
424
425
426
427
#include "BlenderFileLoader.h"

#include <assert.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;

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

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

	_viewplane_left=   _re->viewplane.xmin;
	_viewplane_right=  _re->viewplane.xmax;
	_viewplane_bottom= _re->viewplane.ymin;
	_viewplane_top=    _re->viewplane.ymax;
	_z_near= -_re->clipsta;
	_z_far=  -_re->clipend;
#if 0
	cout << "frustrum: l " << _viewplane_left << " r " << _viewplane_right
		<< " b " << _viewplane_bottom << " t " << _viewplane_top
		<< " n " << _z_near << " f " << _z_far << endl;
#endif

	int id = 0;
	for(obi= (ObjectInstanceRen *) _re->instancetable.first; obi; obi=obi->next) {
		if (!(obi->lay & _re->scene->lay & _srl->lay))
			continue;
		char *name = obi->ob->id.name;
		//cout << name[0] << name[1] << ":" << (name+2) <<;
		//print_m4("obi->mat", obi->mat);

		if( obi->obr->totvlak > 0)
			insertShapeNode(obi, ++id);
		else
			cout << "Warning: " << (name+2) << " is not a vlak-based object (ignored)" << endl;
	}

	//Returns the built scene.
	return _Scene;
}

#define CLIPPED_BY_NEAR -1
#define NOT_CLIPPED      0
#define CLIPPED_BY_FAR   1

// check if each vertex of a triangle (V1, V2, V3) is clipped by the near/far plane
// and calculate the number of triangles to be generated by clipping
int BlenderFileLoader::countClippedFaces(float v1[3], float v2[3], float v3[3], int clip[3])
{
	float *v[3];
	int numClipped, sum, numTris;

	v[0] = v1;
	v[1] = v2;
	v[2] = v3;
	numClipped = sum = 0;
	for (int i = 0; i < 3; i++) {
		if (v[i][2] > _z_near) {
			clip[i] = CLIPPED_BY_NEAR;
			numClipped++;
		} else if (v[i][2] < _z_far) {
			clip[i] = CLIPPED_BY_FAR;
			numClipped++;
		} else {
			clip[i] = NOT_CLIPPED;
		}
//		printf("%d %s\n", i, (clip[i] == NOT_CLIPPED) ? "not" : (clip[i] == CLIPPED_BY_NEAR) ? "near" : "far");
		sum += clip[i];
	}
	switch (numClipped) {
	case 0:
		numTris = 1; // triangle
		break;
	case 1:
		numTris = 2; // tetragon
		break;
	case 2:
		if (sum == 0)
			numTris = 3; // pentagon
		else
			numTris = 1; // triangle
		break;
	case 3:
		if (sum == 3 || sum == -3)
			numTris = 0;
		else
			numTris = 2; // tetragon
		break;
	}
	return numTris;
}

// find the intersection point C between the line segment from V1 to V2 and
// a clipping plane at depth Z (i.e., the Z component of C is known, while
// the X and Y components are unknown).
void BlenderFileLoader::clipLine(float v1[3], float v2[3], float c[3], float z)
{
	double d[3];
	for (int i = 0; i < 3; i++)
		d[i] = v2[i] - v1[i];
	double t = (z - v1[2]) / d[2];
	c[0] = v1[0] + t * d[0];
	c[1] = v1[1] + t * d[1];
	c[2] = z;
}

// clip the triangle (V1, V2, V3) by the near and far clipping plane and
// obtain a set of vertices after the clipping.  The number of vertices
// is at most 5.
void BlenderFileLoader::clipTriangle(int numTris, float triCoords[][3], float v1[3], float v2[3], float v3[3], int clip[3])
{
	float *v[3];
	int i, j, k;

	v[0] = v1;
	v[1] = v2;
	v[2] = v3;
	k = 0;
	for (i = 0; i < 3; i++) {
		j = (i + 1) % 3;
		if (clip[i] == NOT_CLIPPED) {
			copy_v3_v3(triCoords[k++], v[i]);
			if (clip[j] != NOT_CLIPPED) {
				clipLine(v[i], v[j], triCoords[k++], (clip[j] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
			}
		} else if (clip[i] != clip[j]) {
			if (clip[j] == NOT_CLIPPED) {
				clipLine(v[i], v[j], triCoords[k++], (clip[i] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
			} else {
				clipLine(v[i], v[j], triCoords[k++], (clip[i] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
				clipLine(v[i], v[j], triCoords[k++], (clip[j] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
			}
		}
	}
	assert (k == 2 + numTris);
}

void BlenderFileLoader::addTriangle(struct LoaderState *ls, float v1[3], float v2[3], float v3[3])
{
	float v12[3], v13[3], n[3];
	float *fv[3], len;
	unsigned i, j;

	// initialize the bounding box by the first vertex
	if (ls->currentIndex == 0) {
		copy_v3_v3(ls->minBBox, v1);
		copy_v3_v3(ls->maxBBox, v1);
	}

	// compute the normal of the triangle
	sub_v3_v3v3(v12, v1, v2);
	sub_v3_v3v3(v13, v1, v3);
	cross_v3_v3v3(n, v12, v13);
	normalize_v3(n);

	fv[0] = v1;
	fv[1] = v2;
	fv[2] = v3;
	for (i = 0; i < 3; i++) {

		copy_v3_v3(ls->pv, fv[i]);
		copy_v3_v3(ls->pn, n);

		// update the bounding box
		for (j = 0; j < 3; j++)
		{
	  		if (ls->minBBox[j] > ls->pv[j])
	    		ls->minBBox[j] = ls->pv[j];

	  		if (ls->maxBBox[j] < ls->pv[j])
	    		ls->maxBBox[j] = ls->pv[j];
		}

		len = len_v3v3(fv[i], fv[(i + 1) % 3]);
		if (_minEdgeSize > len)
	  		_minEdgeSize = len;
		
		*ls->pvi = ls->currentIndex;
		*ls->pni = ls->currentIndex;
		*ls->pmi = ls->currentMIndex;

		ls->currentIndex +=3;
		ls->pv += 3;
		ls->pn += 3;

		ls->pvi++;
		ls->pni++;
		ls->pmi++;
	}
}

void BlenderFileLoader::insertShapeNode(ObjectInstanceRen *obi, int id)
{
	ObjectRen *obr = obi->obr;

	// We parse vlak nodes and count the number of faces after the clipping by
	// the near and far view planes is applied (Note: mesh vertices are in the
	// camera coordinate system).
	VlakRen *vlr;
	unsigned numFaces = 0;
	float v1[3], v2[3], v3[3], v4[3];
	int clip_1[3], clip_2[3];
	for(int a=0; a < obr->totvlak; a++) {
		if((a & 255)==0) vlr= obr->vlaknodes[a>>8].vlak;
		else vlr++;
		copy_v3_v3(v1, vlr->v1->co);
		copy_v3_v3(v2, vlr->v2->co);
		copy_v3_v3(v3, vlr->v3->co);
		if (vlr->v4) copy_v3_v3(v4, vlr->v4->co);
		if (obi->flag & R_TRANSFORMED) {
			mul_m4_v3(obi->mat, v1);
			mul_m4_v3(obi->mat, v2);
			mul_m4_v3(obi->mat, v3);
			if (vlr->v4) mul_m4_v3(obi->mat, v4);
		}
//		print_v3("v1", v1);
//		print_v3("v2", v2);
//		print_v3("v3", v3);
//		if (vlr->v4) print_v3("v4", v4);
		numFaces += countClippedFaces(v1, v2, v3, clip_1);
		if (vlr->v4)
			numFaces += countClippedFaces(v1, v3, v4, clip_2);
	}
//	cout <<"numFaces " <<numFaces<<endl;
	if (numFaces == 0)
		return;

	// We allocate memory for the meshes to be imported
	NodeTransform *currentMesh = new NodeTransform;
	NodeShape * shape = new NodeShape;

	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
	
	struct LoaderState ls;
	ls.pv = vertices;
	ls.pn = normals;
	ls.pvi = VIndices;
	ls.pni = NIndices;
	ls.pmi = MIndices;
	ls.currentIndex = 0;
	ls.currentMIndex = 0;
	
	FrsMaterial tmpMat;

	// We parse the vlak nodes again and import meshes while applying the clipping
	// by the near and far view planes.
	int p;
	for(p=0; p < obr->totvlak; ++p) // we parse the faces of the mesh
	{
			// Lib3dsFace *f=&mesh->faceL[p];
			// Lib3dsMaterial *mat=0;
			if((p & 255)==0) vlr = obr->vlaknodes[p>>8].vlak;
			else vlr++;
			copy_v3_v3(v1, vlr->v1->co);
			copy_v3_v3(v2, vlr->v2->co);
			copy_v3_v3(v3, vlr->v3->co);
			if (vlr->v4) copy_v3_v3(v4, vlr->v4->co);
			if (obi->flag & R_TRANSFORMED) {
				mul_m4_v3(obi->mat, v1);
				mul_m4_v3(obi->mat, v2);
				mul_m4_v3(obi->mat, v3);
				if (vlr->v4) mul_m4_v3(obi->mat, v4);
			}

			unsigned numTris_1, numTris_2;
			numTris_1 = countClippedFaces(v1, v2, v3, clip_1);
			numTris_2 = (vlr->v4) ? countClippedFaces(v1, v3, v4, clip_2) : 0;
			if (numTris_1 == 0 && numTris_2 == 0)
				continue;

			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){
		        			ls.currentMIndex = i;
		        			found = true;
		        			break;
		      			}
		      			++i;
		    	}
		
		    	if(!found){
		      		meshFrsMaterials.push_back(tmpMat);
		      		ls.currentMIndex = meshFrsMaterials.size()-1;
		    	}
	  		}

			float triCoords[5][3];

			if (numTris_1 > 0) {
				clipTriangle(numTris_1, triCoords, v1, v2, v3, clip_1);
				for (i = 0; i < numTris_1; i++) {
					addTriangle(&ls, triCoords[0], triCoords[i+1], triCoords[i+2]);
					_numFacesRead++;
				}
			}

			if (numTris_2 > 0) {
				clipTriangle(numTris_2, triCoords, v1, v3, v4, clip_2);
				for (i = 0; i < numTris_2; i++) {
					addTriangle(&ls, triCoords[0], triCoords[i+1], triCoords[i+2]);
					_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
	IndexedFaceSet *rep;
	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(ls.minBBox[0], ls.minBBox[1], ls.minBBox[2]), 
	                                     Vec3r(ls.maxBBox[0], ls.maxBBox[1], ls.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);
	
}