/*** Crealysm 3D engine, by Cristiaan Kop - updated 15-08-2013 ***/ /*** ============================================================================== ***/ /*** NAMESPACE: CREALYSM_RENDERER ***/ /*** cpp file with Crealysm scenegraph functions, API independent; CD3dscene src ***/ #include #include #include #include "Crealysm_scenegraph.h" #include "Crealysm_d3dscene.h" #include "Crealysm_d3dlight.h" #include "Crealysm_d3dcam.h" #include "Crealysm_d3dshader.h" #include "Crealysm_math.h" #include "Crealysm_dxmath.h" // for d3dx vector comparisons using namespace Crealysm_d3drenderer; using namespace Crealysm_math; using namespace std; namespace Crealysm_renderer { /**************************************************************************************/ /*** CONSTRUCTOR ***/ /*** ==> usage: when creating a CSceneGraph object ***/ /*** ==> sets all variables in the CSceneGraph object to initial values ***/ /**************************************************************************************/ CSceneGraph::CSceneGraph() { mMaxDirLights = 0; mMaxPointLights = 0; mMeshDataCreated = false; mMeshInstDataCreated = false; mLightDataCreated = false; mEffectDataCreated = false; mEffectMeshDataCreated = false; mEffectMatDataCreated = false; } /**************************************************************************************/ /*** DESTRUCTOR ***/ /*** ==> usage: when CSceneGraph object is not needed anymore ***/ /*** ==> deletes all dynamic arrays with render queue and scene data ***/ /**************************************************************************************/ CSceneGraph::~CSceneGraph() { // no memory freeing, using std::vector now } /**************************************************************************************/ /*** CREATE ***/ /*** ==> usage: when cd3dscene is loaded and scenegraph is needed ***/ /*** ==> creates the scenegraph data and generates all indices ***/ /**************************************************************************************/ bool CSceneGraph::Create(const Crealysm_d3drenderer::CD3dscene &pD3dscene, const int pLightingEffectId) { if(!pD3dscene.IsLoaded()) return false; mMaxDirLights = pD3dscene.mShaders[pLightingEffectId].GetNrDirLights(); mMaxPointLights = pD3dscene.mShaders[pLightingEffectId].GetNrMaxPointLights(); if(!CreateMeshData(pD3dscene)) return false; if(!CreateMeshInstData(pD3dscene)) return false; if(!CreateMaterialData(pD3dscene)) return false; if(!CreateLightData(pD3dscene)) return false; // Effects - shaders if(!CreateEffectMeshData(pD3dscene)) return false; if(!CreateEffectMaterialData()) return false; if(!CreateEffectMatMeshIndex()) return false; mEffectDataCreated = true; return true; } /**************************************************************************************/ /*** UPDATE DIST TO CAM MI BLENDED ***/ /*** ==> usage: during updating the 3d scene graph ***/ /*** ==> updates distance to camera for all blended meshinstances ***/ /**************************************************************************************/ void CSceneGraph::UpdateDistToCamMIBlended(const CD3dscene &pD3dscene, const CD3dcam &pCam) { for(size_t fx=0;fx usage: during updating the 3d scene graph ***/ /*** ==> updates distance to camera for all positional lights (i.e. point lights) ***/ /**************************************************************************************/ void CSceneGraph::UpdateDistToCamLights(const CD3dscene &pD3dscene, const CD3dcam &pCam) { for(size_t lc=0;lc usage: during updating the 3d scene graph ***/ /*** ==> updates meshInstBlended inst vector sorted back to front on cam distance ***/ /**************************************************************************************/ bool CSceneGraph::SortBlendedMeshes() { for(size_t fx=0;fx inst.blendedMeshDistToCam[b]; }); for(size_t mi=0;mi usage: during updating the 3d scene graph ***/ /*** ==> updates active light indices for pos lights, no sorting overall for scene ***/ /**************************************************************************************/ bool CSceneGraph::CullLights(const CD3dscene &pD3dscene) { // find all visible positional lights, now only point lights mVisiblePointLights.resize(0); for(size_t lc=0;lc usage: during scenegraph creation, 1st step ***/ /*** ==> fills the mMesh index with all mesh specific data, no submesh data ***/ /**************************************************************************************/ bool CSceneGraph::CreateMeshData(const CD3dscene &pD3dscene) { bool found; mMesh.resize(pD3dscene.GetNrMeshes()); for(size_t m=0;m usage: during scenegraph creation, after mesh data is created (2nd step) ***/ /*** ==> fills the mMeshInst index with all mesh instance specific data ***/ /**************************************************************************************/ bool CSceneGraph::CreateMeshInstData(const CD3dscene &pD3dscene) { if(!mMeshDataCreated) return false; mMeshInst.resize(pD3dscene.GetNrMeshInst()); mDynamicMeshInstIndex.resize(0); mDynamicMeshInstIndex.reserve(mMeshInst.size()); for(size_t mi=0;mi usage: during scenegraph creation, 3rd step ***/ /*** ==> fills the mMaterials index with submesh and mesh ID's that uses material ***/ /**************************************************************************************/ bool CSceneGraph::CreateMaterialData(const CD3dscene &pD3dscene) { if(!mMeshDataCreated) return false; mMaterials.resize(pD3dscene.GetNrMaterials()); for(size_t mat=0;mat usage: during scenegraph creation, 4th step ***/ /*** ==> fills the light source incides for both directional and point lights ***/ /**************************************************************************************/ bool CSceneGraph::CreateLightData(const CD3dscene &pD3dscene) { mLights.resize(pD3dscene.GetNrLights()); mDirLights.resize(0); mPointLights.resize(0); mVisibleDirLights.reserve(mDirLights.size()); mVisibleDirLights.resize(0); for(size_t lc=0;lc usage: during scenegraph updating, after mesh/light data created ***/ /*** ==> fills the mesh inst affected lights data with lighs affecting the meshinst ***/ /**************************************************************************************/ bool CSceneGraph::UpdateLightAffectMeshData(const CD3dscene &pD3dscene) { if(!mMeshInstDataCreated || !mLightDataCreated) return false; bool max = false; // Loop through all visible mesh instances and visible point lights to match for(size_t mi=0;mi usage: during scenegraph updating, after mesh/light data created ***/ /*** ==> fills the weapon mesh inst affected array with affecting point lights ***/ /**************************************************************************************/ bool CSceneGraph::UpdateLightAffectWeaponMesh(const CD3dscene &pD3dscene, const Crealysm_d3drenderer::CD3dcam &pCam) { if(!pD3dscene.WeaponInScene()) return true; mMeshInstWeapon.affectedPointLights.resize(0); bool max = false; for(size_t lc=0;lc usage: during scenegraph creation, after mesh/inst/material data ***/ /*** ==> fills the mEffect structs with all indices for each effect/shader ***/ /**************************************************************************************/ bool CSceneGraph::CreateEffectMeshData(const Crealysm_d3drenderer::CD3dscene &pD3dscene) { if(!mMeshDataCreated) return false; mEffect.resize(pD3dscene.GetNrShaders()); for(size_t fx=0;fx usage: during scenegraph creation, for each effect ***/ /*** ==> fills the material relevant data for each effect, indices for rendering ***/ /**************************************************************************************/ bool CSceneGraph::CreateEffectMaterialData() { if(!mEffectMeshDataCreated) return false; for(size_t fx=0;fx usage: during scenegraph creation, for each effect ***/ /*** ==> creates indices per material, which meshes use the materials ***/ /**************************************************************************************/ bool CSceneGraph::CreateEffectMatMeshIndex() { if(!mEffectMatDataCreated) return false; bool found; for(size_t fx=0;fx usage: when requesting number of meshes in scenegraph ***/ /*** ==> returns number of meshes ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrMeshes() const { return mMesh.size(); } /**************************************************************************************/ /*** GET NR MESH INST CONST ***/ /*** ==> usage: when requesting number of mesh instances in scenegraph ***/ /*** ==> returns number of mesh instances ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrMeshInst() const { return mMeshInst.size(); } /**************************************************************************************/ /*** GET NR MESH INST DYNAMIC CONST ***/ /*** ==> usage: when requesting number of dynamic mesh instances in scenegraph ***/ /*** ==> returns number of mesh instances ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrMeshInstDynamic() const { return mDynamicMeshInstIndex.size(); } /**************************************************************************************/ /*** GET NR MATERIALS CONST ***/ /*** ==> usage: when requesting number of materials in scenegraph ***/ /*** ==> returns number of materials ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrMaterials() const { return mMaterials.size(); } /**************************************************************************************/ /*** GET NR EFFECTS CONST ***/ /*** ==> usage: when requesting number of effects in scenegraph ***/ /*** ==> returns number of effects ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrEffects() const { return mEffect.size(); } /**************************************************************************************/ /*** GET NR LIGHTS CONST ***/ /*** ==> usage: when requesting total number of lights in scenegraph ***/ /*** ==> returns total number of lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrLights() const { return mLights.size(); } /**************************************************************************************/ /*** GET NR DIR LIGHTS CONST ***/ /*** ==> usage: when requesting number of directional lights in scenegraph ***/ /*** ==> returns number of directional lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrDirLights() const { return mDirLights.size(); } /**************************************************************************************/ /*** GET NR POINT LIGHTS CONST ***/ /*** ==> usage: when requesting number of point lights in scenegraph ***/ /*** ==> returns number of point lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrPointLights() const { return mPointLights.size(); } /**************************************************************************************/ /*** GET MAX DIR LIGHTS CONST ***/ /*** ==> usage: when requesting number of maximum directional lights in scenegraph ***/ /*** ==> returns maximum number of directional lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetMaxDirLights() const { return mMaxDirLights; } /**************************************************************************************/ /*** GET MAX POINT LIGHTS CONST ***/ /*** ==> usage: when requesting number of maximum point lights in scenegraph ***/ /*** ==> returns maximum number of point lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetMaxPointLights() const { return mMaxPointLights; } /**************************************************************************************/ /*** GET NR VISIBLE DIR LIGHTS CONST ***/ /*** ==> usage: when requesting number of visible directional lights in scenegraph ***/ /*** ==> returns number of visible directional lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrVisibleDirLights() const { return mVisibleDirLights.size(); } /**************************************************************************************/ /*** GET NR VISIBLE POINT LIGHTS CONST ***/ /*** ==> usage: when requesting number of visible point lights in scenegraph ***/ /*** ==> returns number of visible point lights ***/ /**************************************************************************************/ size_t CSceneGraph::GetNrVisiblePointLights() const { return mVisiblePointLights.size(); } /**************************************************************************************/ /*** UPDATE MESH VISIBLE INST CONST ***/ /*** ==> usage: during scenegraph update, has a mesh visible instances? ***/ /*** ==> updates the bool vectors if meshes have visible instances ***/ /**************************************************************************************/ bool CSceneGraph::UpdateMeshVisibleInst(const Crealysm_d3drenderer::CD3dscene &pD3dscene) { std::ofstream bla("avisinst.txt"); bla << "Total instances: " << mMeshInst.size() << endl; int visible = 0; for(size_t mi=0;mi