// Copyright (C) 2002-2011 Nikolaus Gebhardt // This file is part of the "Irrlicht Engine". // For conditions of distribution and use, see copyright notice in irrlicht.h #include "CShadowVolumeSceneNode.h" #include "ISceneManager.h" #include "IMesh.h" #include "IVideoDriver.h" #include "SLight.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), ShadowMesh(0), IndexCount(0), VertexCount(0), ShadowVolumesUsed(0), Infinity(infinity), UseZFailMethod(zfailmethod) { #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) { SShadowVolume* svp = 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); } else { ShadowVolumes.push_back(SShadowVolume()); svp = &ShadowVolumes.getLast(); } svp->reallocate(IndexCount*5); ++ShadowVolumesUsed; const u32 faceCount = IndexCount / 3; if (faceCount * 6 > Edges.size()) Edges.set_used(faceCount*6); u32 numEdges = 0; const core::vector3df ls = light * Infinity; // light scaled //if (UseZFailMethod) // createZFailVolume(faceCount, numEdges, light, svp); //else // createZPassVolume(faceCount, numEdges, light, svp, false); // the createZFailVolume does currently not work 100% correctly, // so we create createZPassVolume with caps if the zfail method // is used createZPassVolume(faceCount, numEdges, light, svp, UseZFailMethod); for (u32 i=0; isize() < svp->allocated_size()-5) { svp->push_back(v1); svp->push_back(v2); svp->push_back(v3); svp->push_back(v2); svp->push_back(v4); svp->push_back(v3); } } } void CShadowVolumeSceneNode::createZFailVolume(s32 faceCount, u32& numEdges, const core::vector3df& light, SShadowVolume* svp) { s32 i; const core::vector3df ls = light * Infinity; // Check every face if it is front or back facing the light. for (i=0; isize() < svp->allocated_size()-5) { // add front cap svp->push_back(v0); svp->push_back(v2); svp->push_back(v1); // add back cap svp->push_back(v0-ls); svp->push_back(v1-ls); svp->push_back(v2-ls); } } } for(i=0; isize() < svp->allocated_size()-5) { svp->push_back(Vertices[wFace0]); svp->push_back(Vertices[wFace2]); svp->push_back(Vertices[wFace1]); svp->push_back(Vertices[wFace0] - light); svp->push_back(Vertices[wFace1] - light); svp->push_back(Vertices[wFace2] - light); } } } } void CShadowVolumeSceneNode::setShadowMesh(const IMesh* mesh) { if ( ShadowMesh == mesh ) return; if (ShadowMesh) ShadowMesh->drop(); ShadowMesh = mesh; if (ShadowMesh) ShadowMesh->grab(); } 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; // calculate total amount of vertices and indices u32 i; u32 totalVertices = 0; u32 totalIndices = 0; const u32 bufcnt = mesh->getMeshBufferCount(); for (i=0; igetMeshBuffer(i); totalIndices += buf->getIndexCount(); totalVertices += buf->getVertexCount(); } // allocate memory if necessary if (totalVertices > Vertices.size()) Vertices.set_used(totalVertices); if (totalIndices > Indices.size()) { Indices.set_used(totalIndices); if (UseZFailMethod) FaceData.set_used(totalIndices / 3); } // copy mesh for (i=0; igetMeshBuffer(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; jgetPosition(j); } // recalculate adjacency if necessary if (oldVertexCount != VertexCount && oldIndexCount != IndexCount && UseZFailMethod) calculateAdjacency(); // create as much shadow volumes as there are lights but // do not ignore the max light settings. const u32 lights = SceneManager->getVideoDriver()->getDynamicLightCount(); core::matrix4 mat = Parent->getAbsoluteTransformation(); mat.makeInverse(); const core::vector3df parentpos = Parent->getAbsolutePosition(); core::vector3df lpos; // TODO: Only correct for point lights. for (i=0; igetVideoDriver()->getDynamicLight(i); lpos = dl.Position; if (dl.CastShadows && fabs((lpos - parentpos).getLengthSQ()) <= (dl.Radius*dl.Radius*4.0f)) { mat.transformVect(lpos); createShadowVolume(lpos); } } } //! 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()); for (u32 i=0; idrawStencilShadowVolume(ShadowVolumes[i].pointer(),ShadowVolumes[i].size(), UseZFailMethod); } } //! returns the axis aligned bounding box of this node const core::aabbox3d& CShadowVolumeSceneNode::getBoundingBox() const { return Box; } //! Generates adjacency information based on mesh indices. void CShadowVolumeSceneNode::calculateAdjacency() { Adjacency.set_used(IndexCount); // go through all faces and fetch their three neighbours for (u32 f=0; f= IndexCount) Adjacency[f + edge] = f/3; else Adjacency[f + edge] = of/3; } } } } // end namespace scene } // end namespace irr