source: GTP/trunk/App/Demos/Illum/explosion_DX/Explosion.cpp @ 1484

Revision 1484, 8.5 KB checked in by szirmay, 18 years ago (diff)
Line 
1#include "dxstdafx.h"
2#include "media.h"
3#include ".\Explosion.h"
4
5Explosion::Explosion(void)
6{
7        pSys.addSystem(PARTICLE_SYSTEM_1, true,  D3DCOLOR_COLORVALUE(1.0f, 0.0f, 0.0f, 1.0f), 1.8, 2.2);
8        pSys.addSystem(PARTICLE_SYSTEM_2, false, D3DCOLOR_COLORVALUE( 0.0f, 1.0f, 0.0f, 0.2f), 2.8, 3.5);
9        pSys.addSystem(PARTICLE_SYSTEM_3, false, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 1.0f, 0.015f), 9.0, 10.2);     
10}
11
12Explosion::~Explosion(void)
13{
14}
15
16void Explosion::OnCreateDevice( IDirect3DDevice9* pd3dDevice)
17{
18       
19        this->pd3dDevice = pd3dDevice; 
20
21        mesh = new Mesh(SCENE_MESH, 1, D3DXVECTOR3(0,0,0));
22               
23        ID3DXBuffer* errBuff = NULL;
24        if( FAILED(D3DXCreateEffectFromFile( pd3dDevice, L"Media//Shaders/Scene.fx", NULL, NULL, NULL,
25                NULL, &g_pEffect, &errBuff )))
26        {
27                int BufSize = errBuff->GetBufferSize();
28
29                // displaying error message of arbitrary length
30                wchar_t* wbuf = new wchar_t[BufSize];
31                mbstowcs( wbuf, (const char*)errBuff->GetBufferPointer(), BufSize );
32                MessageBox(NULL, wbuf, L".fx Compilation Error", MB_ICONERROR);         // show error message
33
34                delete wbuf;
35                exit(-1);
36        }
37
38        pSys.OnCreateDevice(pd3dDevice);
39       
40}
41
42void Explosion::OnDestroyDevice()
43{
44        delete mesh;
45        SAFE_RELEASE( g_pEffect );
46
47        pSys.OnDestroyDevice();
48}
49
50void Explosion::OnResetDevice(const D3DSURFACE_DESC* pBackBufferDesc )
51{
52        m_ScreenWidth = pBackBufferDesc->Width;
53        m_ScreenHeight = pBackBufferDesc->Height;
54
55
56        if( g_pEffect )
57        g_pEffect->OnResetDevice();
58       
59        D3DXCreateTextureFromFile( pd3dDevice, COLOR_TEXTURE_PATH, &ColorTexture );
60
61        FrameBufferTexture = new CDXTexture();
62        if (!FrameBufferTexture->InitTex(pd3dDevice, "framebuffer", m_ScreenWidth, m_ScreenHeight, D3DFMT_A16B16G16R16F, false))
63        {
64                MessageBox(NULL, L"FrameBufferTexture creation failed!",L"", MB_OK);
65        }
66        ParticleTexture = new CDXTexture();
67       
68        createParticleTex();
69
70        PhaseTexture = new CDXTexture();
71        if (!PhaseTexture->InitTex(pd3dDevice, "phaseTexture", 256, 256, D3DFMT_A16B16G16R16F, false))
72        {
73                MessageBox(NULL, L"PhaseTexture creation failed!",L"", MB_OK);
74        }
75        phaseCreated = false;
76
77        pSys.setScreenDimensions(m_ScreenWidth, m_ScreenHeight);
78
79        // create vertex buffer for full-screen quad
80        pd3dDevice->CreateVertexBuffer(sizeof(float) * 18, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_DEFAULT, &FullScreenQuad, 0);
81        float* snipet;
82        FullScreenQuad->Lock(0, 0, (void**)&snipet, D3DLOCK_DISCARD);
83        int index = 0;
84        snipet[index++] = -1; snipet[index++] = -1; snipet[index++] = 0;
85        snipet[index++] =  1; snipet[index++] =  1; snipet[index++] = 0; 
86        snipet[index++] =  1; snipet[index++] = -1; snipet[index++] = 0; 
87
88        snipet[index++] = -1; snipet[index++] = -1; snipet[index++] = 0; 
89        snipet[index++] = -1; snipet[index++] =  1; snipet[index++] = 0; 
90        snipet[index++] =  1; snipet[index++] =  1; snipet[index++] = 0; 
91
92        FullScreenQuad->Unlock();
93
94
95        pSys.OnResetDevice();
96}
97
98void Explosion::OnLostDevice()
99{
100        if( g_pEffect )
101        g_pEffect->OnLostDevice();
102
103        SAFE_RELEASE(ColorTexture);
104        SAFE_RELEASE(FullScreenQuad);
105
106        delete FrameBufferTexture;
107        destroyParticleTex();
108        delete PhaseTexture;
109
110        pSys.OnLostDevice();
111}
112
113void Explosion::destroyParticleTex()
114{
115        delete ParticleTexture;
116        delete HeatTexture;
117}
118
119void Explosion::createParticleTex()
120{
121        int width, height;
122        int ratio = params->GetInt(fTexRatio);
123        lastRatio = ratio;
124        float r = pow(2.0, ratio);
125        r = 1.0 / r;
126        width = m_ScreenWidth * r;
127        height = m_ScreenHeight * r;
128        ParticleTexture = new CDXTexture();
129        if (!ParticleTexture->InitTex(pd3dDevice, "particleColor", width, height, D3DFMT_A16B16G16R16F, false))
130        {
131                MessageBox(NULL, L"ParticleColorTexture creation failed!",L"", MB_OK);
132        }
133        HeatTexture = new CDXTexture();
134        if (!HeatTexture->InitTex(pd3dDevice, "particleHeat", width, height, D3DFMT_A16B16G16R16F, false))
135        {
136                MessageBox(NULL, L"ParticleHeatTexture creation failed!",L"", MB_OK);
137        }
138}
139
140void Explosion::slidersChanged()
141{
142        int ratio = params->GetInt(fTexRatio);
143        if(ratio != lastRatio)
144        {
145                destroyParticleTex();
146                createParticleTex();
147        }
148}
149
150void Explosion::renderPhaseTexture()
151{
152        PhaseTexture->StartRender();
153
154                D3DCOLOR backgroundColor = D3DCOLOR_ARGB(0, 0, 0, 0);
155                V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, backgroundColor, 1.0f, 0) );
156               
157       
158                UINT p;
159                g_pEffect->SetTechnique("Phase");
160                g_pEffect->Begin(&p, 0 );
161                g_pEffect->BeginPass( 0 );     
162       
163                pd3dDevice->SetStreamSource(0, FullScreenQuad, 0, sizeof(float) * 3);
164                pd3dDevice->SetFVF(D3DFVF_XYZ);
165                pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
166
167                g_pEffect->EndPass();
168                g_pEffect->End();
169
170        PhaseTexture->EndRender();
171
172        phaseCreated = true;
173}
174
175void Explosion::OnFrameRender( D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj )
176{       
177        if(!phaseCreated)
178                renderPhaseTexture();
179
180        FrameBufferTexture->StartRender(true, 0);
181       
182                D3DCOLOR backgroundColor = D3DCOLOR_ARGB(0, 0, 0, 0);
183                V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, backgroundColor, 1.0f, 0) );
184               
185                D3DXMATRIXA16 mWorld;
186               
187               
188                D3DXMatrixIdentity(&mWorld);
189               
190                SetWorldViewProj(mWorld,mView,mProj);
191                pSys.SetWorldViewProj(mWorld,mView,mProj);
192               
193                 
194                g_pEffect->SetTexture("ColorMap",ColorTexture);
195                float lp[]={LightPos->x,LightPos->y,LightPos->z};
196                g_pEffect->SetFloatArray("mLightPos",lp,3);
197                float cp[]={camera->GetEyePt()->x,camera->GetEyePt()->y,camera->GetEyePt()->z};
198                g_pEffect->SetFloatArray("mCameraPos",cp,3);
199                g_pEffect->CommitChanges();
200       
201
202        //rendertarget = scenetarget
203
204                UINT p;
205                g_pEffect->SetTechnique("NOSHADING");
206                g_pEffect->Begin(&p, 0 );
207                g_pEffect->BeginPass( 0 );     
208       
209                if(params->Get(bObjectOn))
210                        mesh->Draw();
211               
212                g_pEffect->EndPass();
213                g_pEffect->End();
214
215        FrameBufferTexture->EndRender();
216
217        ParticleTexture->StartRender(false, 0);
218        HeatTexture->StartRender(false, 1);
219       
220                backgroundColor = D3DCOLOR_ARGB(0, 0, 0, 0);
221                        V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, backgroundColor, 1.0f, 0) );
222               
223        pSys.setCameraPosition(*camera->GetEyePt());
224                pSys.draw(FrameBufferTexture->m_Tex, PhaseTexture->m_Tex);
225                if(!params->Get(bPaused))
226                        pSys.stepFrame();
227
228        ParticleTexture->EndRender();
229        HeatTexture->EndRender();
230
231        g_pEffect->SetFloat( "fireTemperature", pSys.getFireTemperature());
232        g_pEffect->SetTexture( "PlanckColors", pSys.getPlanckTexture());
233        g_pEffect->SetTexture("SceneTexture", FrameBufferTexture->m_Tex);
234        g_pEffect->SetTexture("ParticleTexture", ParticleTexture->m_Tex);
235        g_pEffect->SetTexture("HeatTexture", HeatTexture->m_Tex);       
236        //g_pEffect->SetTexture("HeatTexture", PhaseTexture->m_Tex);   
237        float shimmerOffset = params->Get(fShimmerAmount) / 20.0;
238        g_pEffect->SetFloat("shimmerOffset", shimmerOffset);
239        float bluramount = 1.0 - params->Get(fMotionBlur);
240        g_pEffect->SetFloat("motionBlur", bluramount);
241        g_pEffect->CommitChanges();
242
243        g_pEffect->SetTechnique("FullScreen");
244    g_pEffect->Begin(&p, 0 );
245        g_pEffect->BeginPass( 0 );
246
247        pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
248        pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
249        pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
250        pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
251       
252        pd3dDevice->SetStreamSource(0, FullScreenQuad, 0, sizeof(float) * 3);
253        pd3dDevice->SetFVF(D3DFVF_XYZ);
254        pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
255
256        pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
257        pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
258        g_pEffect->EndPass();
259        g_pEffect->End();       
260}
261
262void Explosion::addUiParams(Parameters& p)
263{
264        pSys.setParams(p);
265        params=&p;
266        p.Add( fFireColor, "Fire temperature", 100);
267        p.Add( fFireIntensity, "Fire density", 100);
268        p.Add( fSmokeIntensity, "Smoke density", 100);
269        p.Add( fDustIntensity, "Dust density", 100);
270        p.Add( fShimmerAmount, "ShimmerAmount", 100);
271        p.Add( fTexRatio, "Rendertexture ratio", 4);
272        p.Add( fMotionBlur, "Motion blur amount", 100);
273
274       
275       
276}
277
278void Explosion::SetWorldViewProj(D3DXMATRIXA16& mWorld, D3DXMATRIXA16& mView, D3DXMATRIXA16& mProj )
279{
280        D3DXMATRIXA16 mWorldView = mWorld * mView;
281        D3DXMATRIXA16 mWorldViewProjection = mWorldView * mProj;
282
283        D3DXMATRIXA16 mWorldViewI, mWorldViewIT;
284        D3DXMatrixInverse(&mWorldViewI, NULL, &mWorldView);
285        D3DXMatrixTranspose(&mWorldViewIT, &mWorldViewI);
286
287        V( g_pEffect->SetMatrix( "WorldView", &mWorldView ) );
288        V( g_pEffect->SetMatrix( "WorldViewIT", &mWorldViewIT ) );
289        V( g_pEffect->SetMatrix( "WorldViewProj", &mWorldViewProjection ) );
290        V( g_pEffect->SetMatrix( "Proj", &mWorldViewProjection ) );
291        V( g_pEffect->CommitChanges() );
292}
293
Note: See TracBrowser for help on using the repository browser.