#include "dxstdafx.h" #include ".\SkyBoxRenderer.h" #include "GameManager.h" SkyBoxRenderer::SkyBoxRenderer(void) : Renderer() { this->setRenderPriority(0); this->rendererType |= Renderer::RENDERER_SKYBOX; this->rayEffect = NULL; } SkyBoxRenderer::~SkyBoxRenderer(void) { this->myScene->manager->releaseEffect(this->rayEffect); this->rayEffect = NULL; } void SkyBoxRenderer::init() { 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 SkyBoxRenderer::render() { SPTR node = this->myNode.lock(); Object3d *obj = (Object3d *) (node.get()); switch(this->myScene->getActivePassId()) { case Scene::PASS_NORMAL: case Scene::PASS_DEPTH: this->device->BeginScene(); if(obj!=NULL && obj->isModelLoaded()) { this->device->SetTransform(D3DTS_WORLD, obj->getWorldMatrix()); this->device->SetTransform(D3DTS_VIEW, &this->myScene->getViewMatrix()); this->device->SetTransform(D3DTS_PROJECTION, &this->myScene->getProjectionMatrix()); this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW ); 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 ); // Disable writing to the z buffer // achtung: im moment alles standard (true, lessequal, true) this->device->SetRenderState( D3DRS_ZENABLE, FALSE ); this->device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE ); // Clamp texture co-ordinates this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); 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); } } // Wrap texture co-ordinates again this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); // Enable writing to the z buffer again this->device->SetRenderState( D3DRS_ZENABLE, TRUE ); this->device->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); DXUTGetD3DDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0); //deactivate textures this->device->SetTexture(0, 0); } 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(); break; case Scene::PASS_RAYTRACE: this->rayPass(); break; } } void SkyBoxRenderer::rayPass() { UINT cPass; HRESULT hr; this->device->BeginScene(); SPTR node = this->myNode.lock(); Object3d *obj = (Object3d *) (node.get()); 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 ) ); this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW ); 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 ); // Clamp texture co-ordinates this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP ); this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP ); 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); } } // Wrap texture co-ordinates again this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP ); this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP ); // Enable writing to the z buffer again V( DXUTGetD3DDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) ); //deactivate textures this->device->SetTexture(0, 0); } V( this->rayEffect->EndPass() ); V( this->rayEffect->End() ); this->device->EndScene(); } void SkyBoxRenderer::OnLostDevice( void* pUserContext ) { HRESULT hr; this->myScene->manager->printToConsole(" OnLostDevice SkyBoxRenderer"); V( this->rayEffect->OnLostDevice() ); } void SkyBoxRenderer::OnDestroyDevice( void* pUserContext ) { this->myScene->manager->printToConsole(" OnDestroyDevice SkyBoxRenderer"); this->myScene->manager->releaseEffect(this->rayEffect); this->rayEffect = NULL; } HRESULT SkyBoxRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) { this->device = pd3dDevice; 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 S_OK; } bool SkyBoxRenderer::isLowerThan(Renderer* renderer) { return this->getRenderPriority() < renderer->getRenderPriority(); }