aboutsummaryrefslogtreecommitdiff
path: root/source/Irrlicht/CShadowVolumeSceneNode.cpp
blob: da38c223af7bff3ea85fac6c7822ab7c65f7fca2 (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "IrrCompileConfig.h"

#ifdef _IRR_COMPILE_WITH_SHADOW_VOLUME_SCENENODE_

#include "CShadowVolumeSceneNode.h"
#include "ISceneManager.h"
#include "IMesh.h"
#include "IVideoDriver.h"
#include "ICameraSceneNode.h"
#include "SViewFrustum.h"
#include "SLight.h"
#include "os.h"

namespace irr
{
namespace scene
{


//! constructor
CShadowVolumeSceneNode::CShadowVolumeSceneNode(const IMesh* shadowMesh, ISceneNode* parent,
		ISceneManager* mgr, s32 id, bool zfailmethod, f32 infinity)
: IShadowVolumeSceneNode(parent, mgr, id),
	AdjacencyDirtyFlag(true),
	ShadowMesh(0), IndexCount(0), VertexCount(0), ShadowVolumesUsed(0),
	Infinity(infinity), UseZFailMethod(zfailmethod), Optimization(ESV_SILHOUETTE_BY_POS)
{
	#ifdef _DEBUG
	setDebugName("CShadowVolumeSceneNode");
	#endif
	setShadowMesh(shadowMesh);
	setAutomaticCulling(scene::EAC_OFF);
}


//! destructor
CShadowVolumeSceneNode::~CShadowVolumeSceneNode()
{
	if (ShadowMesh)
		ShadowMesh->drop();
}


void CShadowVolumeSceneNode::createShadowVolume(const core::vector3df& light, bool isDirectional)
{
	SShadowVolume* svp = 0;
	core::aabbox3d<f32>* bb = 0;

	// builds the shadow volume and adds it to the shadow volume list.

	if (ShadowVolumes.size() > ShadowVolumesUsed)
	{
		// get the next unused buffer

		svp = &ShadowVolumes[ShadowVolumesUsed];
		svp->set_used(0);

		bb = &ShadowBBox[ShadowVolumesUsed];
	}
	else
	{
		ShadowVolumes.push_back(SShadowVolume());
		svp = &ShadowVolumes.getLast();

		ShadowBBox.push_back(core::aabbox3d<f32>());
		bb = &ShadowBBox.getLast();
	}
	svp->reallocate(IndexCount*5);
	++ShadowVolumesUsed;

	// We use triangle lists
	Edges.set_used(IndexCount*2);
	u32 numEdges = 0;

	numEdges=createEdgesAndCaps(light, isDirectional, svp, bb);

	// for all edges add the near->far quads
	core::vector3df lightDir1(light*Infinity);
	core::vector3df lightDir2(light*Infinity);
	for (u32 i=0; i<numEdges; ++i)
	{
		const core::vector3df &v1 = Vertices[Edges[2*i+0]];
		const core::vector3df &v2 = Vertices[Edges[2*i+1]];
		if ( !isDirectional )
		{
			lightDir1 = (v1 - light).normalize()*Infinity;
			lightDir2 = (v2 - light).normalize()*Infinity;
		}
		const core::vector3df v3(v1+lightDir1);
		const core::vector3df v4(v2+lightDir2);

		// Add a quad (two triangles) to the vertex list
#ifdef _DEBUG
		if (svp->size() >= svp->allocated_size()-5)
			os::Printer::log("Allocation too small.", ELL_DEBUG);
#endif
		svp->push_back(v1);
		svp->push_back(v2);
		svp->push_back(v3);

		svp->push_back(v2);
		svp->push_back(v4);
		svp->push_back(v3);
	}
}

// TODO.
// Not sure what's going on. Either FaceData should mean the opposite and true should mean facing away from light
// or I'm missing something else. Anyway - when not setting this then Shadows will look wrong on Burnings driver
// while they seem to look OK on first view either way on other drivers. Only tested with z-fail so far.
// Maybe errors only show up close to near/far plane on other drivers as otherwise the stencil-buffer-count 
// is probably ending up with same value anyway 
#define IRR_USE_REVERSE_EXTRUDED

u32 CShadowVolumeSceneNode::createEdgesAndCaps(const core::vector3df& light, bool isDirectional,
					SShadowVolume* svp, core::aabbox3d<f32>* bb)
{
	u32 numEdges=0;
	const u32 faceCount = IndexCount / 3;

	if(faceCount >= 1)
		bb->reset(Vertices[Indices[0]]);
	else
		bb->reset(0,0,0);

	// Check every face if it is front or back facing the light.
	core::vector3df lightDir0(light);
	core::vector3df lightDir1(light);
	core::vector3df lightDir2(light);
	for (u32 i=0; i<faceCount; ++i)
	{
		const core::vector3df v0 = Vertices[Indices[3*i+0]];
		const core::vector3df v1 = Vertices[Indices[3*i+1]];
		const core::vector3df v2 = Vertices[Indices[3*i+2]];

		if ( !isDirectional )
		{
			lightDir0 = (v0-light).normalize();
		}
#ifdef IRR_USE_REVERSE_EXTRUDED
		FaceData[i]=core::triangle3df(v2,v1,v0).isFrontFacing(lightDir0);	// actually the back-facing polygons
#else
		FaceData[i]=core::triangle3df(v0,v1,v2).isFrontFacing(lightDir0);
#endif

#if 0	// Useful for internal debugging & testing. Show all the faces in the light.
		if ( FaceData[i] )
		{
			video::SMaterial m;
			m.Lighting = false;
			SceneManager->getVideoDriver()->setMaterial(m);
#ifdef IRR_USE_REVERSE_EXTRUDED
			SceneManager->getVideoDriver()->draw3DTriangle(core::triangle3df(v0+lightDir0,v1+lightDir0,v2+lightDir0), irr::video::SColor(255,255, 0, 0));
#else
			SceneManager->getVideoDriver()->draw3DTriangle(core::triangle3df(v0-lightDir0,v1-lightDir0,v2-lightDir0), irr::video::SColor(255,255, 0, 0));
#endif
		}
#endif

		if (UseZFailMethod && FaceData[i])
		{
#ifdef _DEBUG
			if (svp->size() >= svp->allocated_size()-5)
				os::Printer::log("Allocation too small.", ELL_DEBUG);
#endif
			// add front cap from light-facing faces
			svp->push_back(v2);
			svp->push_back(v1);
			svp->push_back(v0);

			// add back cap
			if ( !isDirectional )
			{
				lightDir1 = (v1-light).normalize();
				lightDir2 = (v2-light).normalize();
			}
			const core::vector3df i0 = v0+lightDir0*Infinity;
			const core::vector3df i1 = v1+lightDir1*Infinity;
			const core::vector3df i2 = v2+lightDir2*Infinity;

			svp->push_back(i0);
			svp->push_back(i1);
			svp->push_back(i2);

			bb->addInternalPoint(i0);
			bb->addInternalPoint(i1);
			bb->addInternalPoint(i2);
		}
	}

	// Create edges
	for (u32 i=0; i<faceCount; ++i)
	{
		// check all front facing faces
		if (FaceData[i] == true)
		{
			const u16 wFace0 = Indices[3*i+0];
			const u16 wFace1 = Indices[3*i+1];
			const u16 wFace2 = Indices[3*i+2];

			if ( Optimization == ESV_NONE )
			{
				// add edge v0-v1
				Edges[2*numEdges+0] = wFace0;
				Edges[2*numEdges+1] = wFace1;
				++numEdges;

				// add edge v1-v2
				Edges[2*numEdges+0] = wFace1;
				Edges[2*numEdges+1] = wFace2;
				++numEdges;

				// add edge v2-v0
				Edges[2*numEdges+0] = wFace2;
				Edges[2*numEdges+1] = wFace0;
				++numEdges;
			}
			else
			{
				const u16 adj0 = Adjacency[3*i+0];
				const u16 adj1 = Adjacency[3*i+1];
				const u16 adj2 = Adjacency[3*i+2];

				// add edges if face is adjacent to back-facing face
				// or if no adjacent face was found
				if (adj0 == i || FaceData[adj0] == false)
				{
					// add edge v0-v1
					Edges[2*numEdges+0] = wFace0;
					Edges[2*numEdges+1] = wFace1;
					++numEdges;
				}

				if (adj1 == i || FaceData[adj1] == false)
				{
					// add edge v1-v2
					Edges[2*numEdges+0] = wFace1;
					Edges[2*numEdges+1] = wFace2;
					++numEdges;
				}

				if (adj2 == i || FaceData[adj2] == false)
				{
					// add edge v2-v0
					Edges[2*numEdges+0] = wFace2;
					Edges[2*numEdges+1] = wFace0;
					++numEdges;
				}
			}
		}
	}
	return numEdges;
}


void CShadowVolumeSceneNode::setShadowMesh(const IMesh* mesh)
{
	if (ShadowMesh == mesh)
		return;
	if (ShadowMesh)
		ShadowMesh->drop();
	ShadowMesh = mesh;
	if (ShadowMesh)
	{
		ShadowMesh->grab();
		Box = ShadowMesh->getBoundingBox();
	}
}


void CShadowVolumeSceneNode::updateShadowVolumes()
{
	const u32 oldIndexCount = IndexCount;
	const u32 oldVertexCount = VertexCount;

	VertexCount = 0;
	IndexCount = 0;
	ShadowVolumesUsed = 0;

	const IMesh* const mesh = ShadowMesh;
	if (!mesh)
		return;

	// create as much shadow volumes as there are lights but
	// do not ignore the max light settings.
	const u32 lightCount = SceneManager->getVideoDriver()->getDynamicLightCount();
	if (!lightCount)
		return;

	// calculate total amount of vertices and indices

	u32 i;
	u32 totalVertices = 0;
	u32 totalIndices = 0;
	const u32 bufcnt = mesh->getMeshBufferCount();

	for (i=0; i<bufcnt; ++i)
	{
		const IMeshBuffer* buf = mesh->getMeshBuffer(i);
		if (	buf->getIndexType() == video::EIT_16BIT 
			&& buf->getPrimitiveType() == scene::EPT_TRIANGLES )
		{
			totalIndices += buf->getIndexCount();
			totalVertices += buf->getVertexCount();
		}
		else
		{
			os::Printer::log("ShadowVolumeSceneNode only supports meshbuffers with 16 bit indices and triangles", ELL_WARNING);
			return;
		}
	}
	if ( totalIndices != (u32)(u16)totalIndices)
	{
		// We could switch to 32-bit indices, not much work and just bit of extra memory (< 192k) per shadow volume.
		// If anyone ever complains and really needs that just switch it. But huge shadows are usually a bad idea as they will be slow.
		os::Printer::log("ShadowVolumeSceneNode does not yet support shadowvolumes which need more than 16 bit indices", ELL_WARNING);
		return;
	}

	// allocate memory if necessary

	Vertices.set_used(totalVertices);
	Indices.set_used(totalIndices);
	FaceData.resize(totalIndices / 3);

	// copy mesh 
	// (could speed this up for static meshes by adding some user flag to prevents copying)
	for (i=0; i<bufcnt; ++i)
	{
		const IMeshBuffer* buf = mesh->getMeshBuffer(i);

		const u16* idxp = buf->getIndices();
		const u16* idxpend = idxp + buf->getIndexCount();
		for (; idxp!=idxpend; ++idxp)
			Indices[IndexCount++] = *idxp + VertexCount;

		const u32 vtxcnt = buf->getVertexCount();
		for (u32 j=0; j<vtxcnt; ++j)
			Vertices[VertexCount++] = buf->getPosition(j);
	}

	// recalculate adjacency if necessary
	if (oldVertexCount != VertexCount || oldIndexCount != IndexCount || AdjacencyDirtyFlag)
		calculateAdjacency();

	core::matrix4 matInv(Parent->getAbsoluteTransformation());
	matInv.makeInverse();
	core::matrix4 matTransp(Parent->getAbsoluteTransformation(), core::matrix4::EM4CONST_TRANSPOSED);
	const core::vector3df parentpos = Parent->getAbsolutePosition();

	for (i=0; i<lightCount; ++i)
	{
		const video::SLight& dl = SceneManager->getVideoDriver()->getDynamicLight(i);

		if ( dl.Type == video::ELT_DIRECTIONAL )
		{
			core::vector3df ldir(dl.Direction);
			matTransp.transformVect(ldir);
			createShadowVolume(ldir, true);
		}
		else
		{
			core::vector3df lpos(dl.Position);
			if (dl.CastShadows &&
				fabs((lpos - parentpos).getLengthSQ()) <= (dl.Radius*dl.Radius*4.0f))
			{
				matInv.transformVect(lpos);
				createShadowVolume(lpos, false);
			}
		}
	}
}

void CShadowVolumeSceneNode::setOptimization(ESHADOWVOLUME_OPTIMIZATION optimization)
{
	if ( Optimization != optimization )
	{
		Optimization = optimization;
		AdjacencyDirtyFlag = true;
	}
}

//! pre render method
void CShadowVolumeSceneNode::OnRegisterSceneNode()
{
	if (IsVisible)
	{
		SceneManager->registerNodeForRendering(this, scene::ESNRP_SHADOW);
		ISceneNode::OnRegisterSceneNode();
	}
}

//! renders the node.
void CShadowVolumeSceneNode::render()
{
	video::IVideoDriver* driver = SceneManager->getVideoDriver();

	if (!ShadowVolumesUsed || !driver)
		return;

	driver->setTransform(video::ETS_WORLD, Parent->getAbsoluteTransformation());

	bool checkFarPlaneClipping = UseZFailMethod && !driver->queryFeature(video::EVDF_DEPTH_CLAMP);

	// get camera frustum converted to local coordinates when we have to check for far plane clipping
	SViewFrustum frust;
	if ( checkFarPlaneClipping )
	{
		const irr::scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
		if ( camera )
		{
			frust = *camera->getViewFrustum();
			core::matrix4 invTrans(Parent->getAbsoluteTransformation(), core::matrix4::EM4CONST_INVERSE);
			frust.transform(invTrans);
		}
		else
			checkFarPlaneClipping = false;
	}

	for (u32 i=0; i<ShadowVolumesUsed; ++i)
	{
		bool drawShadow = true;

		if (checkFarPlaneClipping)
		{
			// Disable shadows drawing, when back cap is behind of ZFar plane.
			// TODO: Using infinite projection matrices instead is said to work better
			//       as then we wouldn't fail when the shadow clip the far plane.
			//       I couldn't get it working (and neither anyone before me it seems).
			//       Anyone who can figure it out is welcome to provide a patch.

			core::vector3df edges[8];
			ShadowBBox[i].getEdges(edges);

			for(int j = 0; j < 8; ++j)
			{
				if (frust.planes[scene::SViewFrustum::VF_FAR_PLANE].classifyPointRelation(edges[j]) == core::ISREL3D_FRONT)
				{
					drawShadow = false;
					break;
				}
			}
		}

		if(drawShadow)
			driver->drawStencilShadowVolume(ShadowVolumes[i], UseZFailMethod, DebugDataVisible);
		else
		{
			// TODO: For some reason (not yet further investigated), Direct3D needs a call to drawStencilShadowVolume
			//       even if we have nothing to draw here to set the renderstate into a StencilShadowMode.
			//       If that's not done it has effect on further render calls.
			core::array<core::vector3df> triangles;
			driver->drawStencilShadowVolume(triangles, UseZFailMethod, DebugDataVisible);
		}
	}
}


//! returns the axis aligned bounding box of this node
const core::aabbox3d<f32>& CShadowVolumeSceneNode::getBoundingBox() const
{
	return Box;
}


//! Generates adjacency information based on mesh indices.
void CShadowVolumeSceneNode::calculateAdjacency()
{
	AdjacencyDirtyFlag = false;

	if ( Optimization == ESV_NONE )
	{
		Adjacency.clear();
	}
	else if ( Optimization == ESV_SILHOUETTE_BY_POS )
	{
		Adjacency.set_used(IndexCount);

		// go through all faces and fetch their three neighbours
		for (u32 f=0; f<IndexCount; f+=3)
		{
			for (u32 edge = 0; edge<3; ++edge)
			{
				const core::vector3df& v1 = Vertices[Indices[f+edge]];
				const core::vector3df& v2 = Vertices[Indices[f+((edge+1)%3)]];

				// now we search an_O_ther _F_ace with these two
				// vertices, which is not the current face.
				u32 of;

				for (of=0; of<IndexCount; of+=3)
				{
					// only other faces
					if (of != f)
					{
						bool cnt1 = false;
						bool cnt2 = false;

						for (s32 e=0; e<3; ++e)
						{
							if (v1.equals(Vertices[Indices[of+e]]))
								cnt1=true;

							if (v2.equals(Vertices[Indices[of+e]]))
								cnt2=true;
						}
						// one match for each vertex, i.e. edge is the same
						if (cnt1 && cnt2)
							break;
					}
				}

				// no adjacent edges -> store face number, else store adjacent face
				if (of >= IndexCount)
					Adjacency[f + edge] = f/3;
				else
					Adjacency[f + edge] = of/3;
			}
		}
	}
}


} // end namespace scene
} // end namespace irr

#endif // _IRR_COMPILE_WITH_SHADOW_VOLUME_SCENENODE_