#include "dxstdafx.h" #include ".\simplemeshrenderer.h" #include "GameManager.h" #include "Sprite.h" SimpleMeshRenderer::SimpleMeshRenderer(void) : Renderer() { this->effect = NULL; this->rayEffect = NULL; this->alphaBlending = false; this->setRenderPriority(30); this->rendererType |= Renderer::RENDERER_SIMPLEMESH; } SimpleMeshRenderer::~SimpleMeshRenderer(void) { this->myScene->manager->releaseEffect(this->effect); this->myScene->manager->releaseEffect(this->rayEffect); this->effect = NULL; this->rayEffect = NULL; } void SimpleMeshRenderer::init() { this->effect = this->myScene->manager->getEffect(GameManager::EFFECT_DEPTHIMP); if(!this->effect) { this->effect = this->myScene->manager->loadEffect(GameManager::EFFECT_DEPTHIMP, L"shaders/depthImp.obj"); } this->rayEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE); if(!this->rayEffect) { this->rayEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj"); } } void SimpleMeshRenderer::render() { switch(this->myScene->getActivePassId()) { case Scene::PASS_NORMAL: this->defaultPass(); break; case Scene::PASS_DEPTH: this->depthPass(); break; case Scene::PASS_RAYTRACE: this->rayPass(); break; } } void SimpleMeshRenderer::defaultPass() { HRESULT hr = 0; SPTR node = this->myNode.lock(); if(node->isA(Scene::NODE_SPRITE)) { return; } Object3d *obj = (Object3d *) (node.get()); if(obj!=NULL && obj->isModelLoaded()) { ID3DXEffect* fadeEffect; UINT cPass; this->device->BeginScene(); fadeEffect = this->myScene->manager->getEffect(GameManager::EFFECT_FADE); V( fadeEffect->SetFloat("alpha", obj->softKillAlpha) ); V( fadeEffect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) ); D3DXMATRIX modelViewIT; D3DXMATRIX invModelView; modelViewIT = *obj->getWorldMatrix(); D3DXMatrixInverse(&invModelView, NULL, &modelViewIT); D3DXMATRIX trans; Vector absPos = obj->getAbsolutePosition(); D3DXMatrixTranslation(&trans, -absPos.x, -absPos.y, -absPos.z); D3DXMatrixMultiply(&modelViewIT, &modelViewIT, &trans); D3DXMatrixTranspose(&modelViewIT, &modelViewIT); Vector lightDir; Vector eyeDir; Vector halfVector; lightDir = ((this->myScene->getSunDirection()) * -1); eyeDir = this->myScene->getActiveCamera()->getAbsolutePosition() - obj->getAbsolutePosition(); halfVector = lightDir + eyeDir; halfVector.normalize(); lightDir.normalize(); V( fadeEffect->SetMatrix("g_mWorldViewIT", &modelViewIT) ); V( fadeEffect->SetVector("lightDir", &lightDir)); V( fadeEffect->SetVector("halfVector", &halfVector) ); V( fadeEffect->CommitChanges() ); V( fadeEffect->SetTechnique("fadeObject") ); V( fadeEffect->Begin( &cPass, 0 ) ); V( fadeEffect->BeginPass( 0 ) ); if(obj->doingSoftKill) { this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); } if (obj->Materials.size() == 0) { this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); this->device->SetRenderState( D3DRS_LIGHTING, FALSE ); this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); if(obj->isPMesh()) { (*obj->getProgressiveMesh())->DrawSubset(0); } else { (*obj->getMesh())->DrawSubset(0); } this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } else { this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW); this->device->SetRenderState( D3DRS_LIGHTING, FALSE ); this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); // bilinear texture filtering: this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); for(UINT i = 0; i < obj->Materials.size(); i++) { V( fadeEffect->SetFloatArray("ambient", (float*) (&obj->Materials[i].Ambient), 4) ); V( fadeEffect->SetFloatArray("diffuse", (float*) (&obj->Materials[i].Diffuse), 4) ); V( fadeEffect->SetFloatArray("specular", (float*) (&obj->Materials[i].Specular), 4) ); V( fadeEffect->SetFloat("power", obj->Materials[i].Power) ); V( fadeEffect->SetTexture("objTexture", obj->Textures[i]) ); V( fadeEffect->CommitChanges() ); if(obj->isPMesh()) { (*obj->getProgressiveMesh())->DrawSubset(i); } else { (*obj->getMesh())->DrawSubset(i); } } //deactivate textures this->device->SetTexture(0, 0); V( fadeEffect->EndPass() ); V( fadeEffect->End() ); if(obj->doingSoftKill) { this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); } } this->device->SetTransform(D3DTS_WORLD, &this->myScene->getIdentityMatrix()); this->device->SetTransform(D3DTS_VIEW, &this->myScene->getIdentityMatrix()); this->device->SetTransform(D3DTS_PROJECTION, &this->myScene->getIdentityMatrix()); this->device->EndScene(); } } void SimpleMeshRenderer::depthPass() { SPTR node = this->myNode.lock(); Object3d *obj = (Object3d *) (node.get()); UINT cPass; HRESULT hr; if(obj!=NULL && obj->isModelLoaded()) { V( this->effect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) ); V( this->effect->SetTechnique("DepthPass") ); V( this->effect->CommitChanges() ); this->device->BeginScene(); V( this->effect->Begin( &cPass, 0 ) ); V( this->effect->BeginPass( 0 ) ); if (obj->Materials.size() == 0) { this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); this->device->SetRenderState( D3DRS_LIGHTING, FALSE ); this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); if(obj->isPMesh()) { (*obj->getProgressiveMesh())->DrawSubset(0); } else { (*obj->getMesh())->DrawSubset(0); } this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } else { this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW); this->device->SetRenderState( D3DRS_LIGHTING, TRUE ); this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); if(this->alphaBlending) { this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); } // bilinear texture filtering: this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); for(UINT i = 0; i < obj->Materials.size(); i++) { this->device->SetMaterial( &obj->Materials[i] ); this->device->SetTexture(0, obj->Textures[i]); if(obj->isPMesh()) { (*obj->getProgressiveMesh())->DrawSubset(i); } else { (*obj->getMesh())->DrawSubset(i); } } if(this->alphaBlending) { this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); } //deactivate textures this->device->SetTexture(0, 0); } V( this->effect->EndPass() ); V( this->effect->End() ); this->device->EndScene(); } } void SimpleMeshRenderer::rayPass() { SPTR node = this->myNode.lock(); Object3d *obj = (Object3d *) (node.get()); UINT cPass; HRESULT hr; this->device->BeginScene(); if(obj!=NULL && obj->isModelLoaded()) { V( this->rayEffect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) ); V( this->rayEffect->SetMatrix("g_mWorldView", &this->myScene->activeRenderPass->getWorldViewMatrix(obj->getWorldMatrix())) ); V( this->rayEffect->SetTechnique("ColorDistance") ); V( this->rayEffect->CommitChanges() ); V( this->rayEffect->Begin( &cPass, 0 ) ); V( this->rayEffect->BeginPass( 0 ) ); if (obj->Materials.size() == 0) { this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ); this->device->SetRenderState( D3DRS_LIGHTING, FALSE ); this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); if(obj->isPMesh()) { (*obj->getProgressiveMesh())->DrawSubset(0); } else { (*obj->getMesh())->DrawSubset(0); } this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); } else { this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW); this->device->SetRenderState( D3DRS_LIGHTING, TRUE ); this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); if(this->alphaBlending) { this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); } // bilinear texture filtering: this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); for(UINT i = 0; i < obj->Materials.size(); i++) { this->device->SetMaterial( &obj->Materials[i] ); this->device->SetTexture(0, obj->Textures[i]); V( this->rayEffect->SetTexture( "g_txCurrentTexture", obj->Textures[i]) ); V( this->rayEffect->CommitChanges() ); if(obj->isPMesh()) { (*obj->getProgressiveMesh())->DrawSubset(i); } else { (*obj->getMesh())->DrawSubset(i); } } if(this->alphaBlending) { this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); } //deactivate textures this->device->SetTexture(0, 0); } V( this->rayEffect->EndPass() ); V( this->rayEffect->End() ); this->device->EndScene(); } } void SimpleMeshRenderer::enableAlphaBlending(bool _alpha) { this->alphaBlending = _alpha; } void SimpleMeshRenderer::OnLostDevice( void* pUserContext ) { HRESULT hr; this->myScene->manager->printToConsole(" OnLostDevice SimpleMeshRenderer"); V( this->effect->OnLostDevice() ); V( this->rayEffect->OnLostDevice() ); } void SimpleMeshRenderer::OnDestroyDevice( void* pUserContext ) { this->myScene->manager->printToConsole(" OnDestroyDevice SimpleMeshRenderer"); this->myScene->manager->releaseEffect(this->effect); this->myScene->manager->releaseEffect(this->rayEffect); this->effect = NULL; this->rayEffect = NULL; } HRESULT SimpleMeshRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { this->device = pd3dDevice; this->effect = this->myScene->manager->getEffect(GameManager::EFFECT_DEPTHIMP); if(!this->effect) { this->effect = this->myScene->manager->loadEffect(GameManager::EFFECT_DEPTHIMP, L"shaders/depthImp.obj"); } this->rayEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE); if(!this->rayEffect) { this->rayEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj"); } return NULL; } bool SimpleMeshRenderer::isLowerThan(Renderer* renderer) { SPTR node1 = this->myNode.lock(); Object3d *myObj = (Object3d *) (node1.get()); SPTR node2 = renderer->myNode.lock(); Object3d *otherObj = (Object3d *) (node2.get()); if(this->getRenderPriority()getRenderPriority()) { return true; } else { if(this->getRenderPriority()==renderer->getRenderPriority()) { if(myObj->doingSoftKill && !otherObj->doingSoftKill) { return false; } if(!myObj->doingSoftKill && otherObj->doingSoftKill) { return true; } return myObj->camDistance>otherObj->camDistance; } else { return false; } } }