source: GTP/trunk/App/Games/Jungle_Rumble/src/SkyBoxRenderer.cpp @ 1378

Revision 1378, 6.3 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - integrate into GTP SVN structure

Line 
1#include "dxstdafx.h"
2#include ".\SkyBoxRenderer.h"
3#include "GameManager.h"
4
5SkyBoxRenderer::SkyBoxRenderer(void) : Renderer()
6{
7        this->setRenderPriority(0);
8        this->rendererType |= Renderer::RENDERER_SKYBOX;
9        this->rayEffect = NULL;
10}
11
12SkyBoxRenderer::~SkyBoxRenderer(void)
13{
14        this->myScene->manager->releaseEffect(this->rayEffect);
15        this->rayEffect = NULL;
16}
17
18void SkyBoxRenderer::init() {
19        this->rayEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE);
20        if(!this->rayEffect) {
21                this->rayEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj");
22        }
23}
24
25void SkyBoxRenderer::render()
26{
27        SPTR<Node> node = this->myNode.lock();
28        Object3d *obj = (Object3d *) (node.get());
29        switch(this->myScene->getActivePassId()) {
30                case Scene::PASS_NORMAL:
31                case Scene::PASS_DEPTH:
32                        this->device->BeginScene();
33                        if(obj!=NULL && obj->isModelLoaded()) {
34                                this->device->SetTransform(D3DTS_WORLD, obj->getWorldMatrix());
35                                this->device->SetTransform(D3DTS_VIEW, &this->myScene->getViewMatrix());
36                                this->device->SetTransform(D3DTS_PROJECTION, &this->myScene->getProjectionMatrix());
37                                this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
38                                this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
39                                this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
40                               
41                                // bilinear texture filtering:
42                                this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
43                                this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
44                                this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
45
46                                // Disable writing to the z buffer
47                                // achtung: im moment alles standard (true, lessequal, true)
48                                this->device->SetRenderState( D3DRS_ZENABLE, FALSE );
49                                this->device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );
50
51                                // Clamp texture co-ordinates
52                                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU,  D3DTADDRESS_CLAMP );
53                                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV,  D3DTADDRESS_CLAMP );
54
55                                for(UINT i = 0; i < obj->Materials.size(); i++) {
56                                        this->device->SetMaterial( &obj->Materials[i] );
57                                        this->device->SetTexture(0, obj->Textures[i]);
58                                        if(obj->isPMesh()) {
59                                                (*obj->getProgressiveMesh())->DrawSubset(i);   
60                                        } else {
61                                                (*obj->getMesh())->DrawSubset(i);
62                                        }
63                                }
64                               
65                                // Wrap texture co-ordinates again
66                                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU,  D3DTADDRESS_WRAP );
67                                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV,  D3DTADDRESS_WRAP );
68
69                                // Enable writing to the z buffer again
70                                this->device->SetRenderState( D3DRS_ZENABLE, TRUE );
71                                this->device->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
72                                DXUTGetD3DDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
73
74                                //deactivate textures
75                                this->device->SetTexture(0, 0);
76                        }
77                        this->device->SetTransform(D3DTS_WORLD, &this->myScene->getIdentityMatrix());
78                        this->device->SetTransform(D3DTS_VIEW, &this->myScene->getIdentityMatrix());
79                        this->device->SetTransform(D3DTS_PROJECTION, &this->myScene->getIdentityMatrix());
80                        this->device->EndScene();
81                        break;
82                case Scene::PASS_RAYTRACE:
83                        this->rayPass();
84                        break;
85        }
86}
87
88void SkyBoxRenderer::rayPass()
89{
90        UINT cPass;
91        HRESULT hr;
92        this->device->BeginScene();
93        SPTR<Node> node = this->myNode.lock();
94        Object3d *obj = (Object3d *) (node.get());
95        if(obj!=NULL && obj->isModelLoaded()) {
96                V( this->rayEffect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) );
97                V( this->rayEffect->SetMatrix("g_mWorldView", &this->myScene->activeRenderPass->getWorldViewMatrix(obj->getWorldMatrix())) );
98                V( this->rayEffect->SetTechnique("ColorDistance") );
99                V( this->rayEffect->CommitChanges() );
100                V( this->rayEffect->Begin( &cPass, 0 ) );
101                V( this->rayEffect->BeginPass( 0 ) );
102                this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CW );
103                this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
104                this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
105               
106                // bilinear texture filtering:
107                this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
108                this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
109                this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
110
111                // Clamp texture co-ordinates
112                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU,  D3DTADDRESS_CLAMP );
113                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV,  D3DTADDRESS_CLAMP );
114
115                for(UINT i = 0; i < obj->Materials.size(); i++) {
116                        this->device->SetMaterial( &obj->Materials[i] );
117                        this->device->SetTexture(0, obj->Textures[i]);
118                        V( this->rayEffect->SetTexture( "g_txCurrentTexture",   obj->Textures[i]) );
119                        V( this->rayEffect->CommitChanges() );
120                        if(obj->isPMesh()) {
121                                (*obj->getProgressiveMesh())->DrawSubset(i);   
122                        } else {
123                                (*obj->getMesh())->DrawSubset(i);
124                        }
125                }
126               
127                // Wrap texture co-ordinates again
128                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSU,  D3DTADDRESS_WRAP );
129                this->device->SetSamplerState( 0, D3DSAMP_ADDRESSV,  D3DTADDRESS_WRAP );
130
131                // Enable writing to the z buffer again
132                V( DXUTGetD3DDevice()->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );
133
134                //deactivate textures
135                this->device->SetTexture(0, 0);
136        }
137        V( this->rayEffect->EndPass() );
138        V( this->rayEffect->End() );
139        this->device->EndScene();       
140}
141
142void SkyBoxRenderer::OnLostDevice( void* pUserContext )
143{
144        HRESULT hr;
145        this->myScene->manager->printToConsole(" OnLostDevice SkyBoxRenderer");
146        V( this->rayEffect->OnLostDevice() );
147}
148
149void SkyBoxRenderer::OnDestroyDevice( void* pUserContext )
150{
151        this->myScene->manager->printToConsole(" OnDestroyDevice SkyBoxRenderer");
152        this->myScene->manager->releaseEffect(this->rayEffect);
153        this->rayEffect = NULL;
154}
155
156HRESULT SkyBoxRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
157{
158        this->device = pd3dDevice;
159        this->rayEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE);
160        if(!this->rayEffect) {
161                this->rayEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj");
162        }
163        return S_OK;
164}
165
166bool SkyBoxRenderer::isLowerThan(Renderer* renderer)
167{
168        return this->getRenderPriority() < renderer->getRenderPriority();
169}
Note: See TracBrowser for help on using the repository browser.