1 | #include "dxstdafx.h"
|
---|
2 | #include "d3ddefs.h"
|
---|
3 | #include <ctime>
|
---|
4 | #include <fstream>
|
---|
5 | #include <cstdio>
|
---|
6 | #include <vector>
|
---|
7 | #include "vector4d.h"
|
---|
8 | #include "color.h"
|
---|
9 | #include "object.h"
|
---|
10 | #include "ObjectVector.h"
|
---|
11 | #include "RadiosityAlgorithm.h"
|
---|
12 |
|
---|
13 |
|
---|
14 | HRESULT RadiosityAlgorithm::moveObjects(float x,float y,float z){
|
---|
15 | HRESULT hr=S_OK;
|
---|
16 | D3DXMATRIX trafomatrix;
|
---|
17 | D3DXMatrixIdentity(&trafomatrix);
|
---|
18 | D3DXMatrixTranslation(&trafomatrix,x,y,z);
|
---|
19 | ObjectModel* box2=objectVector->getObjectModel(1);
|
---|
20 | box2->setTransformMatrix(trafomatrix);
|
---|
21 | return hr;
|
---|
22 | }
|
---|
23 |
|
---|
24 | HRESULT RadiosityAlgorithm::moveCamera(float eyeX,float eyeY,float eyeZ,float lookAtX,float lookAtY,float lookAtZ){
|
---|
25 | HRESULT hr=S_OK;
|
---|
26 | eyePos.x=eyeX;
|
---|
27 | eyePos.y=eyeY;
|
---|
28 | eyePos.z=eyeZ;
|
---|
29 | camLookAt.x=lookAtX;
|
---|
30 | camLookAt.y=lookAtY;
|
---|
31 | camLookAt.z=lookAtZ;
|
---|
32 | return hr;
|
---|
33 | }
|
---|
34 |
|
---|
35 | RadiosityAlgorithm::RadiosityAlgorithm(UINT textureWidth,UINT textureHeight,UINT mipMapLevels,_D3DFORMAT vismapFormat,_D3DFORMAT iterationVismapFormat,_D3DFORMAT renderTextureFormat,_D3DFORMAT mipmapFormat,_D3DFORMAT depthStencilFormat){
|
---|
36 |
|
---|
37 | /*********************/
|
---|
38 | eyePos.x=0;
|
---|
39 | eyePos.y=2.5;
|
---|
40 | eyePos.z=10.0f;
|
---|
41 | /*********************/
|
---|
42 | shootcolors=NULL;
|
---|
43 | pQuadVB=NULL;
|
---|
44 | pPointVB=NULL;
|
---|
45 | emissionSurface=NULL;
|
---|
46 | emissionTexture=NULL;
|
---|
47 | pOrigVisibilityTexture=NULL;
|
---|
48 | pIterationVisibilityTexture=NULL;
|
---|
49 | pRadiosityTexture=NULL;
|
---|
50 | pRadiosityTexture2=NULL;
|
---|
51 | pActualRadianceTexture=NULL;
|
---|
52 | pActualRadianceTexture2=NULL;
|
---|
53 | shootTexture=NULL;
|
---|
54 | radCopyTexture=NULL;
|
---|
55 | visCopyTexture=NULL;
|
---|
56 | tempTexture=NULL;
|
---|
57 | shootColorTexture=NULL;
|
---|
58 | textureDecl=NULL;
|
---|
59 | shootColorSurface=NULL;
|
---|
60 | tempSurface=NULL;
|
---|
61 | visCopySurface=NULL;
|
---|
62 | radCopySurface=NULL;
|
---|
63 | pRenderSurface=NULL;
|
---|
64 | pBackBuffer=NULL;
|
---|
65 | pRadRenderSurface=NULL;
|
---|
66 | pRadRenderSurface2=NULL;
|
---|
67 | pActualRadianceSurface=NULL;
|
---|
68 | pActualRadianceSurface2=NULL;
|
---|
69 |
|
---|
70 | rttDepthSurface=NULL;
|
---|
71 | origDepthSurface=NULL;
|
---|
72 |
|
---|
73 | pIterationVisibilityRenderSurface=NULL;
|
---|
74 | shootSurface=NULL;
|
---|
75 | pBRDFTexture=NULL;
|
---|
76 | pBRDFSurface=NULL;
|
---|
77 |
|
---|
78 | m_pEffect=NULL;
|
---|
79 | nPasses=0;
|
---|
80 | fiRes2=0;
|
---|
81 | fiOld=0;
|
---|
82 | mipmapTexturesArray=NULL;
|
---|
83 | pMipmapSurfaces=NULL;
|
---|
84 |
|
---|
85 |
|
---|
86 | pData=NULL;
|
---|
87 | visMapData=NULL;
|
---|
88 | textureVertices1=NULL;
|
---|
89 | this->textureHeight=textureHeight;
|
---|
90 | this->textureWidth=textureWidth;
|
---|
91 | this->mipMapLevels=mipMapLevels;
|
---|
92 | this->vismapFormat=vismapFormat;
|
---|
93 | this->iterationVismapFormat=iterationVismapFormat;
|
---|
94 | this->renderTextureFormat=renderTextureFormat;
|
---|
95 | this->mipmapFormat=mipmapFormat;
|
---|
96 | this->depthStencilFormat=depthStencilFormat;
|
---|
97 |
|
---|
98 | //errLogger=new ofstream("log.txt");
|
---|
99 | objectVector=new ObjectVector();
|
---|
100 |
|
---|
101 | ObjectModel* box1=new ObjectModel();
|
---|
102 | box1->Load("boxl6.obj", true,false,false, 0.5, 1, 0, 0);
|
---|
103 |
|
---|
104 | ObjectModel* box2=new ObjectModel();
|
---|
105 | box2->Load("kiskocka6.obj", true,true,false, 0.45, 1, 0.51, 0);
|
---|
106 |
|
---|
107 | D3DXMATRIX trafomatrix;
|
---|
108 | D3DXMatrixIdentity(&trafomatrix);
|
---|
109 | D3DXMatrixTranslation(&trafomatrix,0,0.01f,0);
|
---|
110 | box2->setTransformMatrix(trafomatrix);
|
---|
111 | ObjectModel* light=new ObjectModel();
|
---|
112 | light->Load("light.obj",true,true,true,0.2,0.2,0.35,0.4);
|
---|
113 |
|
---|
114 | objectVector->addObjectModel(*box1);
|
---|
115 |
|
---|
116 | objectVector->addObjectModel(*box2);
|
---|
117 |
|
---|
118 | objectVector->addObjectModel(*light);
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | RadiosityAlgorithm::~RadiosityAlgorithm(){
|
---|
123 | SAFE_DELETE(objectVector);
|
---|
124 | SAFE_DELETE_ARRAY(textureVertices1);
|
---|
125 | this->deleteObjects();
|
---|
126 | }
|
---|
127 |
|
---|
128 | HRESULT RadiosityAlgorithm::initObjects(LPDIRECT3DDEVICE9 d3dDevice){
|
---|
129 | shootcolors=new float[4];
|
---|
130 | DWORD effectCompileFlag=0;
|
---|
131 |
|
---|
132 | this->d3dDevice=d3dDevice;
|
---|
133 | #ifdef DEBUG_SHADER
|
---|
134 | effectCompileFlag|=D3DXSHADER_DEBUG;
|
---|
135 | effectCompileFlag |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT;
|
---|
136 | effectCompileFlag |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT;
|
---|
137 |
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | if( FAILED( hr = D3DXCreateEffectFromFile( d3dDevice, L"hlsl.fx", NULL, NULL, effectCompileFlag, NULL, &m_pEffect, NULL))){
|
---|
141 | //*errLogger << "Error reading effect file.\n";
|
---|
142 | return hr;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|
147 | lShoot=D3DXVECTOR4(0.0f,0.0f,0.0f,0.0f);
|
---|
148 | textureVertices1=new D3DVERTEX_1[6];
|
---|
149 |
|
---|
150 | textureVertices1[0]=D3DVERTEX_1(-1.0f,1.0f,0.0f, 0.0f,0.0f);
|
---|
151 | textureVertices1[1]=D3DVERTEX_1(1.0f,1.0f,0.0f, 1.0f,0.0f);
|
---|
152 | textureVertices1[2]=D3DVERTEX_1(-1.0f,-1.0f,0.0f, 0.0f,1.0f);
|
---|
153 | textureVertices1[3]=D3DVERTEX_1(-1.0f,-1.0f,0.0f, 0.0f,1.0f);
|
---|
154 | textureVertices1[4]=D3DVERTEX_1(1.0f,1.0f,0.0f, 1.0f,0.0f);
|
---|
155 | textureVertices1[5]=D3DVERTEX_1(1.0f,-1.0f,0.0f, 1.0f,1.0f);
|
---|
156 |
|
---|
157 | pointVertices=D3DVERTEX_1(0.0f,0.0f,0.0f,0.5f,0.5f);
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 | D3DVERTEXELEMENT9 _texturedecl[]=
|
---|
162 | {
|
---|
163 | {0,0,D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,0 },
|
---|
164 | {0,12,D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,0 },
|
---|
165 | D3DDECL_END()
|
---|
166 | };
|
---|
167 |
|
---|
168 | objectVector->initAllObjects(d3dDevice);
|
---|
169 |
|
---|
170 | hr=d3dDevice->CreateVertexDeclaration(_texturedecl,&textureDecl);
|
---|
171 |
|
---|
172 | hr=d3dDevice->CreateVertexBuffer(sizeof(D3DVERTEX_1)*6,D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pQuadVB,NULL);
|
---|
173 |
|
---|
174 | hr=d3dDevice->CreateVertexBuffer(sizeof(D3DVERTEX_1),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pPointVB,NULL);
|
---|
175 |
|
---|
176 | pQuadVB->Lock(0,sizeof(pData),(void**)&pData,0);
|
---|
177 | memcpy(pData,textureVertices1,sizeof(D3DVERTEX_1)*6);
|
---|
178 | pQuadVB->Unlock();
|
---|
179 |
|
---|
180 | pPointVB->Lock(0,sizeof(pData),(void**)&pData,0);
|
---|
181 | memcpy(pData,&pointVertices,sizeof(pointVertices));
|
---|
182 | pPointVB->Unlock();
|
---|
183 |
|
---|
184 | d3dDevice->GetRenderTarget(0,&pBackBuffer);
|
---|
185 |
|
---|
186 | //create visibility render-to-texture objects
|
---|
187 | d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,vismapFormat,D3DPOOL_DEFAULT,&pOrigVisibilityTexture,NULL);
|
---|
188 | pOrigVisibilityTexture->GetSurfaceLevel(0,&pRenderSurface);
|
---|
189 |
|
---|
190 |
|
---|
191 | //create radiosity render-to-texture objects
|
---|
192 | d3dDevice->CreateTexture(textureWidth/2,textureHeight/2,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pRadiosityTexture,NULL);
|
---|
193 | pRadiosityTexture->GetSurfaceLevel(0,&pRadRenderSurface);
|
---|
194 |
|
---|
195 | //create radiosity render-to-texture objects
|
---|
196 | d3dDevice->CreateTexture(textureWidth/2,textureHeight/2,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pRadiosityTexture2,NULL);
|
---|
197 | pRadiosityTexture2->GetSurfaceLevel(0,&pRadRenderSurface2);
|
---|
198 |
|
---|
199 | //create radiosity render-to-texture objects
|
---|
200 | d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pActualRadianceTexture,NULL);
|
---|
201 | pActualRadianceTexture->GetSurfaceLevel(0,&pActualRadianceSurface);
|
---|
202 |
|
---|
203 | //create radiosity render-to-texture objects
|
---|
204 | d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pActualRadianceTexture2,NULL);
|
---|
205 | pActualRadianceTexture2->GetSurfaceLevel(0,&pActualRadianceSurface2);
|
---|
206 |
|
---|
207 | d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&emissionTexture,NULL);
|
---|
208 | emissionTexture->GetSurfaceLevel(0,&emissionSurface);
|
---|
209 |
|
---|
210 | //create iteration visibility render-to-texture objects
|
---|
211 | d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,iterationVismapFormat,D3DPOOL_DEFAULT,&pIterationVisibilityTexture,NULL);
|
---|
212 | pIterationVisibilityTexture->GetSurfaceLevel(0,&pIterationVisibilityRenderSurface);
|
---|
213 |
|
---|
214 | //create choice visibility render-to-texture objects
|
---|
215 | d3dDevice->CreateTexture(1,1,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&shootTexture,NULL);
|
---|
216 | shootTexture->GetSurfaceLevel(0,&shootSurface);
|
---|
217 |
|
---|
218 | d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pBRDFTexture,NULL);
|
---|
219 | pBRDFTexture->GetSurfaceLevel(0,&pBRDFSurface);
|
---|
220 |
|
---|
221 | //create mipmap render-to-texture objects
|
---|
222 | mipmapTexturesArray=new LPDIRECT3DTEXTURE9[this->mipMapLevels];
|
---|
223 | pMipmapSurfaces=new LPDIRECT3DSURFACE9[this->mipMapLevels];
|
---|
224 | int levels[12];
|
---|
225 | levels[0]=1;
|
---|
226 | for(UINT i=1;i<this->mipMapLevels;i++){
|
---|
227 | levels[i]=levels[i-1]*2;
|
---|
228 | }
|
---|
229 |
|
---|
230 | mipmapTexturesArray[0]=pActualRadianceTexture;
|
---|
231 | mipmapTexturesArray[0]->AddRef();
|
---|
232 | mipmapTexturesArray[0]->GetSurfaceLevel(0,&pMipmapSurfaces[0]);
|
---|
233 | for(int i=1;i<this->mipMapLevels;i++){
|
---|
234 | hr=d3dDevice->CreateTexture(textureWidth/levels[i],textureHeight/levels[i],1,D3DUSAGE_RENDERTARGET,this->mipmapFormat,D3DPOOL_DEFAULT,&mipmapTexturesArray[i],NULL);
|
---|
235 | if(FAILED(hr)){
|
---|
236 | return 0;
|
---|
237 | }
|
---|
238 | hr=mipmapTexturesArray[i]->GetSurfaceLevel(0,&pMipmapSurfaces[i]);
|
---|
239 | if(FAILED(hr)){
|
---|
240 | //*errLogger << "GetSurfaceLevel error.";
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | hr=d3dDevice->CreateDepthStencilSurface(this->textureWidth,this->textureHeight,this->depthStencilFormat,D3DMULTISAMPLE_NONE ,0,true,&rttDepthSurface,NULL);
|
---|
246 |
|
---|
247 | return S_OK;
|
---|
248 | }
|
---|
249 |
|
---|
250 | HRESULT RadiosityAlgorithm::deleteObjects(){
|
---|
251 |
|
---|
252 | SAFE_DELETE_ARRAY(shootcolors);
|
---|
253 |
|
---|
254 | SAFE_RELEASE(rttDepthSurface)
|
---|
255 | SAFE_RELEASE(origDepthSurface)
|
---|
256 |
|
---|
257 | SAFE_RELEASE(pBackBuffer)
|
---|
258 |
|
---|
259 | SAFE_RELEASE(m_pEffect)
|
---|
260 |
|
---|
261 | SAFE_RELEASE(textureDecl)
|
---|
262 |
|
---|
263 | SAFE_RELEASE(visCopyTexture)
|
---|
264 | SAFE_RELEASE(visCopySurface)
|
---|
265 |
|
---|
266 | SAFE_RELEASE(tempTexture)
|
---|
267 | SAFE_RELEASE(tempSurface)
|
---|
268 |
|
---|
269 | SAFE_RELEASE(shootColorTexture)
|
---|
270 | SAFE_RELEASE(shootColorSurface)
|
---|
271 |
|
---|
272 | SAFE_RELEASE(pOrigVisibilityTexture)
|
---|
273 | SAFE_RELEASE(pRenderSurface)
|
---|
274 |
|
---|
275 | SAFE_RELEASE(pIterationVisibilityTexture)
|
---|
276 | SAFE_RELEASE(pIterationVisibilityRenderSurface)
|
---|
277 |
|
---|
278 | SAFE_RELEASE(pRadiosityTexture)
|
---|
279 | SAFE_RELEASE(pRadRenderSurface)
|
---|
280 |
|
---|
281 |
|
---|
282 | SAFE_RELEASE(pBRDFTexture)
|
---|
283 | SAFE_RELEASE(pBRDFSurface)
|
---|
284 |
|
---|
285 |
|
---|
286 | SAFE_RELEASE(pActualRadianceTexture)
|
---|
287 | SAFE_RELEASE(pActualRadianceSurface)
|
---|
288 |
|
---|
289 | SAFE_RELEASE(shootTexture)
|
---|
290 | SAFE_RELEASE(shootSurface)
|
---|
291 |
|
---|
292 |
|
---|
293 | if((mipmapTexturesArray!=NULL)&&(mipmapTexturesArray!=NULL)){
|
---|
294 | for(UINT i=0;i<this->mipMapLevels;i++){
|
---|
295 | SAFE_RELEASE(mipmapTexturesArray[i])
|
---|
296 | SAFE_RELEASE(pMipmapSurfaces[i])
|
---|
297 | }
|
---|
298 | }
|
---|
299 | SAFE_DELETE_ARRAY(mipmapTexturesArray);
|
---|
300 | SAFE_DELETE_ARRAY(pMipmapSurfaces);
|
---|
301 |
|
---|
302 |
|
---|
303 | SAFE_RELEASE(pRadRenderSurface2);
|
---|
304 | SAFE_RELEASE(pRadiosityTexture2)
|
---|
305 |
|
---|
306 | SAFE_RELEASE(pActualRadianceSurface2);
|
---|
307 | SAFE_RELEASE(pActualRadianceTexture2);
|
---|
308 |
|
---|
309 | SAFE_RELEASE(pQuadVB)
|
---|
310 | SAFE_RELEASE(pPointVB)
|
---|
311 |
|
---|
312 | SAFE_RELEASE(emissionSurface)
|
---|
313 | SAFE_RELEASE(emissionTexture)
|
---|
314 |
|
---|
315 | return S_OK;
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | HRESULT RadiosityAlgorithm::doEmissionMapPass(bool doDebugPicture,TCHAR* fileName){
|
---|
320 |
|
---|
321 | //hr=d3dDevice->GetDepthStencilSurface(&origDepthSurface);
|
---|
322 |
|
---|
323 | hr=d3dDevice->SetRenderTarget(0,pRadRenderSurface);
|
---|
324 |
|
---|
325 | hr=d3dDevice->SetDepthStencilSurface(rttDepthSurface);
|
---|
326 |
|
---|
327 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
328 |
|
---|
329 | d3dDevice->BeginScene();
|
---|
330 | if( m_pEffect != NULL ){
|
---|
331 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "EmissionMapTech" );
|
---|
332 | m_pEffect->SetTechnique( hTechnique );
|
---|
333 | m_pEffect->Begin( &nPasses, 0 );
|
---|
334 | for(UINT i=0;i<nPasses;i++){
|
---|
335 | m_pEffect->BeginPass(i);
|
---|
336 |
|
---|
337 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
338 |
|
---|
339 | m_pEffect->EndPass();
|
---|
340 | }
|
---|
341 | m_pEffect->End();
|
---|
342 | }
|
---|
343 | d3dDevice->EndScene();
|
---|
344 |
|
---|
345 |
|
---|
346 | hr=d3dDevice->SetRenderTarget(0,pActualRadianceSurface);
|
---|
347 |
|
---|
348 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
349 |
|
---|
350 | //hr=d3dDevice->SetVertexDeclaration(this->decl);
|
---|
351 |
|
---|
352 | d3dDevice->BeginScene();
|
---|
353 | if( m_pEffect != NULL ){
|
---|
354 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "EmissionMapTech" );
|
---|
355 | m_pEffect->SetTechnique( hTechnique );
|
---|
356 | m_pEffect->Begin( &nPasses, 0 );
|
---|
357 | for(UINT i=0;i<nPasses;i++){
|
---|
358 | m_pEffect->BeginPass(i);
|
---|
359 |
|
---|
360 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
361 |
|
---|
362 | m_pEffect->EndPass();
|
---|
363 | }
|
---|
364 | m_pEffect->End();
|
---|
365 | }
|
---|
366 | d3dDevice->EndScene();
|
---|
367 |
|
---|
368 | hr=d3dDevice->SetRenderTarget(0,emissionSurface);
|
---|
369 |
|
---|
370 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
371 |
|
---|
372 | //hr=d3dDevice->SetVertexDeclaration(decl);
|
---|
373 |
|
---|
374 |
|
---|
375 |
|
---|
376 |
|
---|
377 | d3dDevice->BeginScene();
|
---|
378 | if( m_pEffect != NULL ){
|
---|
379 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "EmissionMapTech" );
|
---|
380 | m_pEffect->SetTechnique( hTechnique );
|
---|
381 | m_pEffect->Begin( &nPasses, 0 );
|
---|
382 | for(UINT i=0;i<nPasses;i++){
|
---|
383 | m_pEffect->BeginPass(i);
|
---|
384 |
|
---|
385 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
386 |
|
---|
387 | m_pEffect->EndPass();
|
---|
388 | }
|
---|
389 | m_pEffect->End();
|
---|
390 | }
|
---|
391 | d3dDevice->EndScene();
|
---|
392 | /********************Save*************/
|
---|
393 | if(doDebugPicture){
|
---|
394 | fileFormat=D3DXIFF_HDR;
|
---|
395 | D3DXSaveTextureToFile(fileName,fileFormat,emissionTexture,NULL);
|
---|
396 | }
|
---|
397 | /*************************************/
|
---|
398 | return S_OK;
|
---|
399 | }
|
---|
400 |
|
---|
401 | HRESULT RadiosityAlgorithm::doOriginalVisibilityMapPass(bool doDebugPicture,TCHAR* fileName){
|
---|
402 | //Render scene with specific HLSL shaders (hemisphere and writethrough)
|
---|
403 |
|
---|
404 | hr=d3dDevice->GetDepthStencilSurface(&origDepthSurface);
|
---|
405 |
|
---|
406 |
|
---|
407 |
|
---|
408 | hr=d3dDevice->SetRenderTarget(0,pRenderSurface);
|
---|
409 | hr=d3dDevice->SetDepthStencilSurface(rttDepthSurface);
|
---|
410 |
|
---|
411 | if(FAILED(hr)){
|
---|
412 | //*errLogger << "setRendertarget error.\n";
|
---|
413 | }
|
---|
414 |
|
---|
415 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
416 |
|
---|
417 | //hr=d3dDevice->SetVertexDeclaration(decl);
|
---|
418 |
|
---|
419 | d3dDevice->BeginScene();
|
---|
420 |
|
---|
421 | if( m_pEffect != NULL ){
|
---|
422 | D3DXHANDLE hTechnique = NULL;
|
---|
423 | hTechnique = m_pEffect->GetTechniqueByName((LPSTR)"OriginalVisibilityMapShader");
|
---|
424 | if(hTechnique==NULL){
|
---|
425 | //*errLogger << "Cannot find technique. Quitting.\n";
|
---|
426 | return -1;
|
---|
427 | }
|
---|
428 | m_pEffect->SetTechnique( hTechnique );
|
---|
429 | m_pEffect->Begin( &nPasses, 0 );
|
---|
430 | for(UINT i=0;i<nPasses;i++){
|
---|
431 | m_pEffect->BeginPass(i);
|
---|
432 |
|
---|
433 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
434 |
|
---|
435 | m_pEffect->EndPass();
|
---|
436 | }
|
---|
437 | m_pEffect->End();
|
---|
438 | }
|
---|
439 |
|
---|
440 | d3dDevice->EndScene();
|
---|
441 |
|
---|
442 |
|
---|
443 | /********************Save*************/
|
---|
444 | if(doDebugPicture){
|
---|
445 | fileFormat=D3DXIFF_HDR;
|
---|
446 | hr=D3DXSaveTextureToFile(fileName,fileFormat,pOrigVisibilityTexture,NULL);
|
---|
447 | }
|
---|
448 | /*************************************/
|
---|
449 |
|
---|
450 | //Vismap surface data extraction.
|
---|
451 |
|
---|
452 |
|
---|
453 |
|
---|
454 | pRenderSurface->GetDesc(&visSurfaceDesc);
|
---|
455 |
|
---|
456 | d3dDevice->CreateTexture(this->textureWidth,textureHeight,1,D3DUSAGE_DYNAMIC,vismapFormat,D3DPOOL_SYSTEMMEM,&visCopyTexture,NULL);
|
---|
457 |
|
---|
458 | visCopyTexture->GetSurfaceLevel(0,&visCopySurface);
|
---|
459 |
|
---|
460 | d3dDevice->GetRenderTargetData(pRenderSurface,visCopySurface);
|
---|
461 |
|
---|
462 |
|
---|
463 | visCopySurface->LockRect(&visRectangleToLock,NULL,0);
|
---|
464 |
|
---|
465 | visMapData=(D3DXFLOAT16*)visRectangleToLock.pBits;
|
---|
466 |
|
---|
467 | visCopySurface->UnlockRect();
|
---|
468 |
|
---|
469 | hr=d3dDevice->SetDepthStencilSurface(origDepthSurface);
|
---|
470 |
|
---|
471 | return S_OK;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | HRESULT RadiosityAlgorithm::initActualRadianceMap(){
|
---|
476 | HRESULT hr;
|
---|
477 | V_RETURN(d3dDevice->SetRenderTarget(0,pActualRadianceSurface))
|
---|
478 | V_RETURN(d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0))
|
---|
479 | return hr;
|
---|
480 | }
|
---|
481 |
|
---|
482 | HRESULT RadiosityAlgorithm::initBRDFAndSpecularMap(){
|
---|
483 | HRESULT hr;
|
---|
484 | LPDIRECT3DTEXTURE9 origBRDFTexture;
|
---|
485 | V_RETURN(d3dDevice->SetRenderTarget(0,pActualRadianceSurface))
|
---|
486 | V_RETURN(d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0))
|
---|
487 | d3dDevice->BeginScene();
|
---|
488 |
|
---|
489 |
|
---|
490 |
|
---|
491 | if( m_pEffect != NULL ){
|
---|
492 | D3DXHANDLE hTechnique = NULL;
|
---|
493 | hTechnique = m_pEffect->GetTechniqueByName((LPSTR)"BRDFShader");
|
---|
494 | if(hTechnique==NULL){
|
---|
495 | //*errLogger << "Cannot find technique. Quitting.\n";
|
---|
496 | return -1;
|
---|
497 | }
|
---|
498 | m_pEffect->SetTechnique( hTechnique );
|
---|
499 | m_pEffect->Begin( &nPasses, 0 );
|
---|
500 | for(UINT i=0;i<nPasses;i++){
|
---|
501 | m_pEffect->BeginPass(i);
|
---|
502 |
|
---|
503 | vector<ObjectModel*>::iterator iter;
|
---|
504 | vector<ObjectModel*> models=objectVector->getAllObjectModels();
|
---|
505 | for(iter=models.begin();iter!=models.end();iter++){
|
---|
506 | LPCTSTR textureName=(*iter)->getTextureName();
|
---|
507 | float shininess=(*iter)->getShininess();
|
---|
508 | hr=D3DXCreateTextureFromFile(d3dDevice,(LPCTSTR)textureName,&origBRDFTexture);
|
---|
509 | if(SUCCEEDED(hr)){
|
---|
510 | m_pEffect->SetTexture("BRDFTexture",origBRDFTexture);
|
---|
511 | m_pEffect->SetFloat("shininess",shininess);
|
---|
512 | V_RETURN((*iter)->RenderWithFX(m_pEffect,true,matWorld,matViewWorldProjection))
|
---|
513 | SAFE_RELEASE(origBRDFTexture)
|
---|
514 | }
|
---|
515 | else{
|
---|
516 | return hr;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | m_pEffect->EndPass();
|
---|
521 | }
|
---|
522 | m_pEffect->End();
|
---|
523 | }
|
---|
524 |
|
---|
525 | d3dDevice->EndScene();
|
---|
526 | return hr;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | HRESULT RadiosityAlgorithm::iterate(UINT niterations,UINT iterationToSave){
|
---|
531 | bool doIterationSave=false;
|
---|
532 | timeBefore=timeGetTime();
|
---|
533 |
|
---|
534 | for(UINT i=1;i<=niterations;i++){
|
---|
535 | if(i==iterationToSave) doIterationSave=true;
|
---|
536 | else doIterationSave=false;
|
---|
537 | //if(i<10||i%10==0){
|
---|
538 | createRadiosityMipMap(i);
|
---|
539 | //}
|
---|
540 |
|
---|
541 |
|
---|
542 | chooseShooter(i);
|
---|
543 | //return E_FAIL;
|
---|
544 | initActualRadianceMap();
|
---|
545 | this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap1.hdr",fromD3D,dirD3D,upD3D,true);
|
---|
546 |
|
---|
547 | this->doRadiosityMapPass(i,doIterationSave,L"itRadMap1.hdr");
|
---|
548 |
|
---|
549 |
|
---|
550 | this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap2.hdr",fromD3D,upD3D,dirD3D,false);
|
---|
551 | this->doRadiosityMapPass(i,doIterationSave,L"itRadMap2.hdr");
|
---|
552 |
|
---|
553 | this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap3.hdr",fromD3D,-upD3D,dirD3D,false);
|
---|
554 | this->doRadiosityMapPass(i,doIterationSave,L"itRadMap3.hdr");
|
---|
555 |
|
---|
556 | D3DXVECTOR3 dirUpCross;
|
---|
557 | D3DXVec3Cross(&dirUpCross,&upD3D,&dirD3D);
|
---|
558 |
|
---|
559 | this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap4.hdr",fromD3D,dirUpCross,dirD3D,false);
|
---|
560 | this->doRadiosityMapPass(i,doIterationSave,L"itRadMap4.hdr");
|
---|
561 |
|
---|
562 | this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap5.hdr",fromD3D,-dirUpCross,dirD3D,false);
|
---|
563 | this->doRadiosityMapPass(i,doIterationSave,L"itRadMap5.hdr");
|
---|
564 | this->doRadiosityActualAveragingPass(i,doIterationSave,L"actavgrad.hdr");
|
---|
565 | }
|
---|
566 | timeAfter==timeGetTime();
|
---|
567 | char buffer1[200];
|
---|
568 | memset(buffer1,0,200);
|
---|
569 | sprintf(buffer1,"%ld",timeAfter-timeBefore);
|
---|
570 | FILE* logger=fopen("log.txt","a+");
|
---|
571 | fprintf(logger,"Time: %s\n",buffer1);
|
---|
572 | fclose(logger);
|
---|
573 | //*errLogger << "Iteration time in seconds: " << buffer1 << "\n";
|
---|
574 | return S_OK;
|
---|
575 | }
|
---|
576 |
|
---|
577 | HRESULT RadiosityAlgorithm::createRadiosityMipMap(UINT niteration){
|
---|
578 | recres2=1.0f/(float)(this->textureWidth);
|
---|
579 | hr=d3dDevice->SetVertexDeclaration(textureDecl);
|
---|
580 | //hr=d3dDevice->SetDepthStencilSurface(rttDepthSurface);
|
---|
581 | if(niteration==1){
|
---|
582 | hr=d3dDevice->SetRenderTarget(0,pMipmapSurfaces[1]);
|
---|
583 | if(FAILED(hr)){
|
---|
584 | //*errLogger << "setRenderTarget error.\n";
|
---|
585 | }
|
---|
586 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
587 | d3dDevice->BeginScene();
|
---|
588 | if( m_pEffect != NULL ){
|
---|
589 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "MipmapBaseCreate" );
|
---|
590 | m_pEffect->SetTechnique( hTechnique );
|
---|
591 | m_pEffect->Begin( &nPasses, 0 );
|
---|
592 | for(UINT j=0;j<nPasses;j++){
|
---|
593 | m_pEffect->BeginPass(j);
|
---|
594 | m_pEffect->SetFloat("recres2",recres2);
|
---|
595 | m_pEffect->SetFloat("niteration",niteration);
|
---|
596 | m_pEffect->SetTexture("radiosity_map",pActualRadianceTexture);
|
---|
597 |
|
---|
598 | hr=d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
|
---|
599 | hr=d3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX_1));
|
---|
600 |
|
---|
601 | if(FAILED(hr)){
|
---|
602 | //*errLogger << "setstreamsource error.\n";
|
---|
603 | }
|
---|
604 | m_pEffect->CommitChanges();
|
---|
605 | hr=d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
|
---|
606 |
|
---|
607 | if(FAILED(hr)){
|
---|
608 | //*errLogger << "drawprimitive error.\n";
|
---|
609 | }
|
---|
610 |
|
---|
611 | m_pEffect->EndPass();
|
---|
612 | }
|
---|
613 | m_pEffect->End();
|
---|
614 | }
|
---|
615 | d3dDevice->EndScene();
|
---|
616 | }
|
---|
617 | recres2*=2.0f;
|
---|
618 |
|
---|
619 |
|
---|
620 |
|
---|
621 | for(UINT i=1;i<this->mipMapLevels-1;i++){
|
---|
622 | hr=d3dDevice->SetRenderTarget(0,pMipmapSurfaces[i+1]);
|
---|
623 | if(FAILED(hr)){
|
---|
624 | //*errLogger << "setRenderTarget error.\n";
|
---|
625 | }
|
---|
626 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
627 | d3dDevice->BeginScene();
|
---|
628 | if( m_pEffect != NULL ){
|
---|
629 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "MipmapLevelCreate" );
|
---|
630 | m_pEffect->SetTechnique( hTechnique );
|
---|
631 | m_pEffect->Begin( &nPasses, 0 );
|
---|
632 | for(UINT j=0;j<nPasses;j++){
|
---|
633 | m_pEffect->BeginPass(j);
|
---|
634 | m_pEffect->SetFloat("recres2",recres2);
|
---|
635 | m_pEffect->SetTexture("luminance_map",mipmapTexturesArray[i]);
|
---|
636 |
|
---|
637 | hr=d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
|
---|
638 | hr=d3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX_1));
|
---|
639 |
|
---|
640 | if(FAILED(hr)){
|
---|
641 | //*errLogger << "setstreamsource error.\n";
|
---|
642 | }
|
---|
643 | m_pEffect->CommitChanges();
|
---|
644 | hr=d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
|
---|
645 |
|
---|
646 | if(FAILED(hr)){
|
---|
647 | //*errLogger << "drawprimitive error.\n";
|
---|
648 | }
|
---|
649 |
|
---|
650 | m_pEffect->EndPass();
|
---|
651 | }
|
---|
652 | m_pEffect->End();
|
---|
653 | }
|
---|
654 | d3dDevice->EndScene();
|
---|
655 | recres2*=2.0f;
|
---|
656 | }
|
---|
657 |
|
---|
658 | return S_OK;
|
---|
659 | }
|
---|
660 |
|
---|
661 | HRESULT RadiosityAlgorithm::doRadiosityAveragingPass(bool doDebugPicture,TCHAR* fileName,UINT niteration){
|
---|
662 | hr=d3dDevice->SetVertexDeclaration(textureDecl);
|
---|
663 | hr=d3dDevice->SetRenderTarget(0,pRadRenderSurface2);
|
---|
664 | if(FAILED(hr)){
|
---|
665 | //*errLogger << "setRenderTarget error.\n";
|
---|
666 | }
|
---|
667 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
668 | d3dDevice->BeginScene();
|
---|
669 | if( m_pEffect != NULL ){
|
---|
670 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "RadAveragingTech" );
|
---|
671 | m_pEffect->SetTechnique( hTechnique );
|
---|
672 | m_pEffect->Begin( &nPasses, 0 );
|
---|
673 | for(UINT j=0;j<nPasses;j++){
|
---|
674 | m_pEffect->BeginPass(j);
|
---|
675 | m_pEffect->SetTexture("radiosity_map",pRadiosityTexture);
|
---|
676 | m_pEffect->SetTexture("emission_map",emissionTexture);
|
---|
677 | m_pEffect->SetFloat("niteration",niteration);
|
---|
678 | hr=d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
|
---|
679 | hr=d3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX_1));
|
---|
680 |
|
---|
681 | if(FAILED(hr)){
|
---|
682 | //*errLogger << "setstreamsource error.\n";
|
---|
683 | }
|
---|
684 | m_pEffect->CommitChanges();
|
---|
685 | hr=d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2);
|
---|
686 |
|
---|
687 | if(FAILED(hr)){
|
---|
688 | //*errLogger << "drawprimitive error.\n";
|
---|
689 | }
|
---|
690 |
|
---|
691 | m_pEffect->EndPass();
|
---|
692 | }
|
---|
693 | m_pEffect->End();
|
---|
694 | }
|
---|
695 | d3dDevice->EndScene();
|
---|
696 | if(doDebugPicture){
|
---|
697 | fileFormat=D3DXIFF_HDR;
|
---|
698 | D3DXSaveTextureToFile(fileName,fileFormat,pRadiosityTexture2,NULL);
|
---|
699 | }
|
---|
700 |
|
---|
701 | return S_OK;
|
---|
702 | }
|
---|
703 |
|
---|
704 |
|
---|
705 | HRESULT RadiosityAlgorithm::chooseShooter(UINT iterations){
|
---|
706 |
|
---|
707 |
|
---|
708 | recres2=1.0f/2.0f;
|
---|
709 |
|
---|
710 | pMipmapSurfaces[this->mipMapLevels-1]->GetDesc(&surfaceDesc);
|
---|
711 |
|
---|
712 | hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->mipmapFormat,D3DPOOL_SYSTEMMEM,&radCopyTexture,NULL);
|
---|
713 | if(FAILED(hr)){
|
---|
714 | //*errLogger << "Create texture.\n";
|
---|
715 | }
|
---|
716 | radCopyTexture->GetSurfaceLevel(0,&radCopySurface);
|
---|
717 |
|
---|
718 | hr=d3dDevice->GetRenderTargetData(pMipmapSurfaces[this->mipMapLevels-1],radCopySurface);
|
---|
719 | if(FAILED(hr)){
|
---|
720 | //*errLogger << "Get render target data.\n";
|
---|
721 | }
|
---|
722 | hr=radCopySurface->LockRect(&rectangleToLock,0,0);
|
---|
723 | if(FAILED(hr)){
|
---|
724 | //*errLogger << "Lock rect failiure.\n";
|
---|
725 | }
|
---|
726 | imageData=(float*)rectangleToLock.pBits;
|
---|
727 |
|
---|
728 | hr=radCopySurface->UnlockRect();
|
---|
729 | if(FAILED(hr)){
|
---|
730 | //*errLogger << "Unlock rect failiure.\n";
|
---|
731 | }
|
---|
732 |
|
---|
733 | fiRes2=imageData[0];
|
---|
734 |
|
---|
735 | if(_isnan(fiRes2)){
|
---|
736 | return(E_FAIL);
|
---|
737 | }
|
---|
738 |
|
---|
739 | SAFE_RELEASE(radCopySurface);
|
---|
740 | SAFE_RELEASE(radCopyTexture);
|
---|
741 |
|
---|
742 |
|
---|
743 |
|
---|
744 | hr=d3dDevice->SetRenderTarget(0,shootSurface);
|
---|
745 | if(FAILED(hr)){
|
---|
746 | //*errLogger << "setRenderTarget error.\n";
|
---|
747 | }
|
---|
748 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
|
---|
749 | hr= d3dDevice->BeginScene();
|
---|
750 | if( m_pEffect != NULL ){
|
---|
751 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "SearchStartTech" );
|
---|
752 | m_pEffect->SetTechnique( hTechnique );
|
---|
753 | m_pEffect->Begin( &nPasses, 0 );
|
---|
754 | for(UINT j=0;j<nPasses;j++){
|
---|
755 | m_pEffect->BeginPass(j);
|
---|
756 | m_pEffect->SetFloat("recres2",recres2);
|
---|
757 | float searchCoords[2]={0,0};
|
---|
758 | m_pEffect->SetFloatArray("searchCoords",searchCoords,2);
|
---|
759 | m_pEffect->SetFloat("firstsum",fiRes2*(float)rand()/(float)RAND_MAX);
|
---|
760 | m_pEffect->SetTexture("sum_map",mipmapTexturesArray[this->mipMapLevels-1]);
|
---|
761 | m_pEffect->SetTexture("mipmap",mipmapTexturesArray[this->mipMapLevels-2]);
|
---|
762 |
|
---|
763 | hr=d3dDevice->SetVertexDeclaration(textureDecl);
|
---|
764 | hr=d3dDevice->SetStreamSource(0,pPointVB,0,sizeof(D3DVERTEX_1));
|
---|
765 |
|
---|
766 | if(FAILED(hr)){
|
---|
767 | //*errLogger << "setstreamsource error.\n";
|
---|
768 | }
|
---|
769 | m_pEffect->CommitChanges();
|
---|
770 | hr=d3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,1);
|
---|
771 |
|
---|
772 | if(FAILED(hr)){
|
---|
773 | //*errLogger << "drawprimitive error.\n";
|
---|
774 | }
|
---|
775 |
|
---|
776 | m_pEffect->EndPass();
|
---|
777 | }
|
---|
778 | m_pEffect->End();
|
---|
779 | }
|
---|
780 | d3dDevice->EndScene();
|
---|
781 | recres2/=2.0f;
|
---|
782 |
|
---|
783 |
|
---|
784 | //shootSurface->GetDesc(&surfaceDesc);
|
---|
785 |
|
---|
786 | hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->renderTextureFormat,D3DPOOL_SYSTEMMEM,&radCopyTexture,NULL);
|
---|
787 | if(FAILED(hr)){
|
---|
788 | //*errLogger << "Create texture.\n";
|
---|
789 | }
|
---|
790 | radCopyTexture->GetSurfaceLevel(0,&radCopySurface);
|
---|
791 |
|
---|
792 | hr=d3dDevice->GetRenderTargetData(shootSurface,radCopySurface);
|
---|
793 | if(FAILED(hr)){
|
---|
794 | //*errLogger << "Get render target data.\n";
|
---|
795 | }
|
---|
796 |
|
---|
797 | hr=radCopySurface->LockRect(&rectangleToLock,0,0);
|
---|
798 | if(FAILED(hr)){
|
---|
799 | //*errLogger << "Lock rect failiure.\n";
|
---|
800 | }
|
---|
801 | //float*
|
---|
802 |
|
---|
803 |
|
---|
804 | imageData=(float*)rectangleToLock.pBits;
|
---|
805 |
|
---|
806 | hr=radCopySurface->UnlockRect();
|
---|
807 | if(FAILED(hr)){
|
---|
808 | //*errLogger << "Unlock rect failiure.\n";
|
---|
809 | }
|
---|
810 |
|
---|
811 | SAFE_RELEASE(radCopySurface);
|
---|
812 | SAFE_RELEASE(radCopyTexture);
|
---|
813 |
|
---|
814 |
|
---|
815 |
|
---|
816 | int levelToGo=iterations>0 ? 1 : 2;
|
---|
817 |
|
---|
818 | //return E_FAIL;
|
---|
819 | d3dDevice->CreateTexture(1,1,1,D3DUSAGE_RENDERTARGET,this->renderTextureFormat,D3DPOOL_DEFAULT,&radCopyTexture,NULL);
|
---|
820 | radCopyTexture->GetSurfaceLevel(0,&radCopySurface);
|
---|
821 |
|
---|
822 |
|
---|
823 | for(int i=this->mipMapLevels-3;i>=levelToGo;i--){
|
---|
824 | hr=d3dDevice->SetRenderTarget(0,radCopySurface);
|
---|
825 | if(FAILED(hr)){
|
---|
826 | //*errLogger << "setRenderTarget error.\n";
|
---|
827 | }
|
---|
828 |
|
---|
829 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
|
---|
830 |
|
---|
831 | d3dDevice->BeginScene();
|
---|
832 | if( m_pEffect != NULL ){
|
---|
833 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "SearchTech" );
|
---|
834 | m_pEffect->SetTechnique( hTechnique );
|
---|
835 | m_pEffect->Begin( &nPasses, 0 );
|
---|
836 | for(UINT j=0;j<nPasses;j++){
|
---|
837 | m_pEffect->BeginPass(j);
|
---|
838 | m_pEffect->SetFloat("recres2",recres2);
|
---|
839 | //m_pEffect->SetFloat("RandomNum",(float)rand()/(float)RAND_MAX);
|
---|
840 | m_pEffect->SetTexture("sum_map",shootTexture);
|
---|
841 | m_pEffect->SetTexture("mipmap",mipmapTexturesArray[i]);
|
---|
842 | hr=d3dDevice->SetStreamSource(0,pPointVB,0,sizeof(D3DVERTEX_1));
|
---|
843 |
|
---|
844 | if(FAILED(hr)){
|
---|
845 | //*errLogger << "setstreamsource error.\n";
|
---|
846 | }
|
---|
847 | m_pEffect->CommitChanges();
|
---|
848 | hr=d3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,1);
|
---|
849 | if(FAILED(hr)){
|
---|
850 | //*errLogger << "drawprimitive error.\n";
|
---|
851 | }
|
---|
852 | m_pEffect->EndPass();
|
---|
853 | }
|
---|
854 | m_pEffect->End();
|
---|
855 | }
|
---|
856 | d3dDevice->EndScene();
|
---|
857 | recres2/=2.0f;
|
---|
858 |
|
---|
859 | tempTexture=radCopyTexture;
|
---|
860 | tempTexture->AddRef();
|
---|
861 | SAFE_RELEASE(radCopyTexture)
|
---|
862 | radCopyTexture=shootTexture;
|
---|
863 | radCopyTexture->AddRef();
|
---|
864 | SAFE_RELEASE(shootTexture)
|
---|
865 | shootTexture=tempTexture;
|
---|
866 | shootTexture->AddRef();
|
---|
867 | SAFE_RELEASE(tempTexture)
|
---|
868 | SAFE_RELEASE(radCopySurface);
|
---|
869 | SAFE_RELEASE(shootSurface);
|
---|
870 | SAFE_RELEASE(tempSurface);
|
---|
871 |
|
---|
872 |
|
---|
873 | shootTexture->GetSurfaceLevel(0,&shootSurface);
|
---|
874 | radCopyTexture->GetSurfaceLevel(0,&radCopySurface);
|
---|
875 |
|
---|
876 | //return E_FAIL;
|
---|
877 | }
|
---|
878 |
|
---|
879 | //SAFE_RELEASE(radCopyTexture);
|
---|
880 | //SAFE_RELEASE(radCopySurface);
|
---|
881 |
|
---|
882 |
|
---|
883 | d3dDevice->CreateTexture(1,1,1,D3DUSAGE_RENDERTARGET,this->renderTextureFormat,D3DPOOL_DEFAULT,&shootColorTexture,NULL);
|
---|
884 | shootColorTexture->GetSurfaceLevel(0,&shootColorSurface);
|
---|
885 | hr=d3dDevice->SetRenderTarget(0,radCopySurface);
|
---|
886 | if(FAILED(hr)){
|
---|
887 | //*errLogger << "setRenderTarget error.\n";
|
---|
888 | }
|
---|
889 | hr=d3dDevice->SetRenderTarget(1,shootColorSurface);
|
---|
890 | if(FAILED(hr)){
|
---|
891 | //*errLogger << "setRenderTarget error.\n";
|
---|
892 | }
|
---|
893 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
|
---|
894 |
|
---|
895 | d3dDevice->BeginScene();
|
---|
896 | if( m_pEffect != NULL ){
|
---|
897 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "SearchEndTech" );
|
---|
898 | hr=m_pEffect->SetTechnique( hTechnique );
|
---|
899 | m_pEffect->Begin( &nPasses, 0 );
|
---|
900 | for(UINT j=0;j<nPasses;j++){
|
---|
901 | m_pEffect->BeginPass(j);
|
---|
902 | m_pEffect->SetFloat("recres2",recres2);
|
---|
903 | m_pEffect->SetTexture("sum_map",shootTexture);
|
---|
904 | if(iterations==1){
|
---|
905 | m_pEffect->SetTexture("mipmap",mipmapTexturesArray[levelToGo-1]);
|
---|
906 | }
|
---|
907 | else{
|
---|
908 | m_pEffect->SetTexture("mipmap",pRadiosityTexture);
|
---|
909 | }
|
---|
910 | hr=d3dDevice->SetStreamSource(0,pPointVB,0,sizeof(D3DVERTEX_1));
|
---|
911 |
|
---|
912 | if(FAILED(hr)){
|
---|
913 | //*errLogger << "setstreamsource error.\n";
|
---|
914 | }
|
---|
915 | m_pEffect->CommitChanges();
|
---|
916 | hr=d3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,1);
|
---|
917 | if(FAILED(hr)){
|
---|
918 | //*errLogger << "drawprimitive error.\n";
|
---|
919 | }
|
---|
920 | m_pEffect->EndPass();
|
---|
921 | }
|
---|
922 | m_pEffect->End();
|
---|
923 | }
|
---|
924 | d3dDevice->EndScene();
|
---|
925 | hr=d3dDevice->SetRenderTarget(1,NULL);
|
---|
926 |
|
---|
927 | recres2/=2.0f;
|
---|
928 |
|
---|
929 |
|
---|
930 | //shootSurface->GetDesc(&surfaceDesc);
|
---|
931 |
|
---|
932 | hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->renderTextureFormat,D3DPOOL_SYSTEMMEM,&tempTexture,NULL);
|
---|
933 | if(FAILED(hr)){
|
---|
934 | //*errLogger << "Create texture.\n";
|
---|
935 | }
|
---|
936 | tempTexture->GetSurfaceLevel(0,&tempSurface);
|
---|
937 |
|
---|
938 | hr=d3dDevice->GetRenderTargetData(radCopySurface,tempSurface);
|
---|
939 | if(FAILED(hr)){
|
---|
940 | //*errLogger << "Get render target data.\n";
|
---|
941 | }
|
---|
942 |
|
---|
943 | hr=tempSurface->LockRect(&rectangleToLock,0,0);
|
---|
944 | if(FAILED(hr)){
|
---|
945 | //*errLogger << "Lock rect failiure.\n";
|
---|
946 | }
|
---|
947 |
|
---|
948 | imageData=(float*)rectangleToLock.pBits;
|
---|
949 |
|
---|
950 | hr=tempSurface->UnlockRect();
|
---|
951 | if(FAILED(hr)){
|
---|
952 | //*errLogger << "Unlock rect failiure.\n";
|
---|
953 | }
|
---|
954 |
|
---|
955 | Vector chosenTexelCoords(imageData[1],imageData[2],0);
|
---|
956 | index2=(int)(imageData[2]*this->textureWidth*(visRectangleToLock.Pitch/4) + imageData[1]*this->textureHeight);
|
---|
957 | index2*=2;
|
---|
958 | shooter = visMapData[index2];
|
---|
959 | //shooter = shooter&0x00ffffff;
|
---|
960 | /*
|
---|
961 | *errLogger << "Sum: " << imageData[0] << "\n";
|
---|
962 |
|
---|
963 | *errLogger << "Coord_x: " << imageData[1]*this->textureWidth<< "\n";
|
---|
964 | *errLogger << "Coord_y: " << imageData[2]*this->textureHeight << "\n";
|
---|
965 | *errLogger << "Shooter: " << index2 << "\n";
|
---|
966 | *errLogger << "Shooter from visMap: " << shooter << "\n";
|
---|
967 | */
|
---|
968 | shootColorSurface->GetDesc(&surfaceDesc);
|
---|
969 |
|
---|
970 | SAFE_RELEASE(tempTexture);
|
---|
971 | SAFE_RELEASE(tempSurface);
|
---|
972 |
|
---|
973 | hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->renderTextureFormat,D3DPOOL_SYSTEMMEM,&tempTexture,NULL);
|
---|
974 | if(FAILED(hr)){
|
---|
975 | //*errLogger << "Create texture.\n";
|
---|
976 | }
|
---|
977 | tempTexture->GetSurfaceLevel(0,&tempSurface);
|
---|
978 |
|
---|
979 | hr=d3dDevice->GetRenderTargetData(shootColorSurface,tempSurface);
|
---|
980 | if(FAILED(hr)){
|
---|
981 | //*errLogger << "Get render target data.\n";
|
---|
982 | return 0;
|
---|
983 | }
|
---|
984 |
|
---|
985 | hr=tempSurface->LockRect(&rectangleToLock,0,0);
|
---|
986 | if(FAILED(hr)){
|
---|
987 | //*errLogger << "Lock rect failiure.\n";
|
---|
988 | }
|
---|
989 |
|
---|
990 | imageData=(float*)rectangleToLock.pBits;
|
---|
991 |
|
---|
992 | hr=tempSurface->UnlockRect();
|
---|
993 | if(FAILED(hr)){
|
---|
994 | //*errLogger << "Unlock rect failiure.\n";
|
---|
995 | }
|
---|
996 |
|
---|
997 | //*errLogger << "fiRes2: " << fiRes2/this->textureWidth/this->textureHeight << "\n";
|
---|
998 | Color pShoot(imageData[0],imageData[1],imageData[2],1.0f);
|
---|
999 | py=pShoot.Lum()/(fiRes2/this->textureWidth/this->textureHeight);
|
---|
1000 |
|
---|
1001 |
|
---|
1002 |
|
---|
1003 | pShoot=pShoot/py;
|
---|
1004 |
|
---|
1005 | SAFE_RELEASE(tempTexture);
|
---|
1006 | SAFE_RELEASE(tempSurface);
|
---|
1007 | SAFE_RELEASE(shootColorSurface);
|
---|
1008 | SAFE_RELEASE(shootColorTexture);
|
---|
1009 | SAFE_RELEASE(radCopySurface)
|
---|
1010 | SAFE_RELEASE(radCopyTexture)
|
---|
1011 |
|
---|
1012 |
|
---|
1013 |
|
---|
1014 |
|
---|
1015 | //hr=d3dDevice->SetVertexDeclaration(decl);
|
---|
1016 |
|
---|
1017 |
|
---|
1018 |
|
---|
1019 | Vector from, dir, up;
|
---|
1020 | Triangle tri;
|
---|
1021 | Vector vertex1,vertex2,vertex3;
|
---|
1022 | Vector texcoord1,texcoord2,texcoord3;
|
---|
1023 | objectVector->getTriangleByIndex((UINT)shooter,tri,vertex1,vertex2,vertex3,texcoord1,texcoord2,texcoord3);
|
---|
1024 |
|
---|
1025 | shooterDisc=tri.area/tri.texarea/M_PI/(float)this->textureWidth/(float)this->textureHeight;
|
---|
1026 | chosenTexelCoords.y=1-chosenTexelCoords.y;
|
---|
1027 | float detbeta=(texcoord2.x-texcoord3.x)*(texcoord3.y-texcoord1.y)-(texcoord2.y-texcoord3.y)*(texcoord3.x-texcoord1.x);
|
---|
1028 | float detalpha=(texcoord1.x-texcoord3.x)*(texcoord3.y-texcoord2.y)-(texcoord1.y-texcoord3.y)*(texcoord3.x-texcoord2.x);
|
---|
1029 | float det1=(texcoord3.y-chosenTexelCoords.y)*(texcoord3.x-texcoord1.x)-(texcoord3.x-chosenTexelCoords.x)*(texcoord3.y-texcoord1.y);
|
---|
1030 | float det2=(texcoord3.y-chosenTexelCoords.y)*(texcoord3.x-texcoord2.x)-(texcoord3.x-chosenTexelCoords.x)*(texcoord3.y-texcoord2.y);
|
---|
1031 | float beta= det1/detbeta;
|
---|
1032 | //float alpha= (beta*(texcoord2.x-texcoord3.x)+texcoord3.x-chosenTexelCoords.x)/(texcoord3.x-texcoord1.x);
|
---|
1033 | float alpha= det2/detalpha;
|
---|
1034 | dir=tri.normal;
|
---|
1035 | up=tri.up;
|
---|
1036 | from=vertex1*alpha+vertex2*beta+vertex3*(1.0f-alpha-beta);
|
---|
1037 |
|
---|
1038 | /*
|
---|
1039 | from=Vector(0,5,0);
|
---|
1040 | dir=Vector(0,-1,0);
|
---|
1041 | up=Vector(1,0,0);
|
---|
1042 | */
|
---|
1043 |
|
---|
1044 | Vector lookat = from + dir;
|
---|
1045 |
|
---|
1046 | dirD3D.x=dir.x;
|
---|
1047 | dirD3D.y=dir.y;
|
---|
1048 | dirD3D.z=dir.z;
|
---|
1049 | fromD3D.x=from.x;
|
---|
1050 | fromD3D.y=from.y;
|
---|
1051 | fromD3D.z=from.z;
|
---|
1052 | lookatD3D.x=lookat.x;
|
---|
1053 | lookatD3D.y=lookat.y;
|
---|
1054 | lookatD3D.z=lookat.z;
|
---|
1055 | upD3D.x=up.x;
|
---|
1056 | upD3D.y=up.y;
|
---|
1057 | upD3D.z=up.z;
|
---|
1058 | /*
|
---|
1059 | *errLogger << "From" << from.x << " " << from.y << " " << from.z << "\n";
|
---|
1060 | *errLogger << "Lookat" << lookat.x << " " << lookat.y << " " << lookat.z << "\n";
|
---|
1061 | *errLogger << "Up" << up.x << " " << up.y << " " << up.z << "\n";
|
---|
1062 | */
|
---|
1063 |
|
---|
1064 | D3DXMatrixIdentity(&matWorld);
|
---|
1065 |
|
---|
1066 |
|
---|
1067 |
|
---|
1068 |
|
---|
1069 |
|
---|
1070 | shootcolors[0]=pShoot.R;
|
---|
1071 | shootcolors[1]=pShoot.G;
|
---|
1072 | shootcolors[2]=pShoot.B;
|
---|
1073 | shootcolors[3]=shooterDisc;
|
---|
1074 |
|
---|
1075 | return S_OK;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | HRESULT RadiosityAlgorithm::doIterationVisibilityMapPass(bool doDebugPicture,TCHAR* fileName,D3DXVECTOR3 center,D3DXVECTOR3 dir,D3DXVECTOR3 up,bool fullScreen){
|
---|
1079 | D3DXVECTOR3 lookat=center+dir;
|
---|
1080 | D3DXMatrixLookAtLH(&matWorld,¢er,&lookat,&up);
|
---|
1081 | D3DXMatrixPerspectiveFovLH(&matProjection,D3DX_PI/2.0,1,0.1,20);
|
---|
1082 | D3DXMatrixMultiply(&matViewWorldProjection,&matWorld,&matProjection);
|
---|
1083 |
|
---|
1084 | hr=d3dDevice->SetRenderTarget(0,pIterationVisibilityRenderSurface);
|
---|
1085 |
|
---|
1086 | if(FAILED(hr)){
|
---|
1087 | //*errLogger << "setRendertarget error.\n";
|
---|
1088 | }
|
---|
1089 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
|
---|
1090 |
|
---|
1091 | //hr=d3dDevice->SetVertexDeclaration(decl);
|
---|
1092 | if(FAILED(hr)){
|
---|
1093 | //*errLogger << "setVertexDecl error.\n";
|
---|
1094 | }
|
---|
1095 |
|
---|
1096 | d3dDevice->BeginScene();
|
---|
1097 |
|
---|
1098 |
|
---|
1099 |
|
---|
1100 | if( m_pEffect != NULL ){
|
---|
1101 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "ITVisShader" );
|
---|
1102 | m_pEffect->SetTechnique( hTechnique );
|
---|
1103 | m_pEffect->Begin( &nPasses, 0 );
|
---|
1104 | for(UINT i=0;i<nPasses;i++){
|
---|
1105 | m_pEffect->BeginPass(i);
|
---|
1106 | m_pEffect->SetFloat("fullScreen",fullScreen);
|
---|
1107 | m_pEffect->SetFloatArray("eyePos",(float*)fromD3D,3);
|
---|
1108 |
|
---|
1109 | m_pEffect->SetMatrix("modelview",&matWorld);
|
---|
1110 | hr=m_pEffect->SetMatrix("modelviewproj",&matViewWorldProjection);
|
---|
1111 |
|
---|
1112 | objectVector->RenderAllWithFX(m_pEffect,false,matWorld,matViewWorldProjection);
|
---|
1113 |
|
---|
1114 | m_pEffect->EndPass();
|
---|
1115 | }
|
---|
1116 | m_pEffect->End();
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | d3dDevice->EndScene();
|
---|
1120 | if(doDebugPicture){
|
---|
1121 | fileFormat=D3DXIFF_HDR;
|
---|
1122 | D3DXSaveTextureToFile(fileName,fileFormat,pIterationVisibilityTexture,NULL);
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | return S_OK;
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | HRESULT RadiosityAlgorithm::doRadiosityMapPass(UINT iterations,bool doDebugPicture,TCHAR* fileName){
|
---|
1129 | D3DXMatrixInverse(&inverseWorldMatrix,NULL,&matWorld);
|
---|
1130 |
|
---|
1131 |
|
---|
1132 | hr=d3dDevice->SetRenderTarget(0,pActualRadianceSurface2);
|
---|
1133 |
|
---|
1134 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
1135 |
|
---|
1136 | d3dDevice->BeginScene();
|
---|
1137 |
|
---|
1138 | if( m_pEffect != NULL ){
|
---|
1139 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "Shoot" );
|
---|
1140 | m_pEffect->SetTechnique( hTechnique );
|
---|
1141 | m_pEffect->Begin( &nPasses, 0 );
|
---|
1142 | for(UINT i=0;i<nPasses;i++){
|
---|
1143 | m_pEffect->BeginPass(i);
|
---|
1144 | //m_pEffect->SetFloat("niteration",(float)iterations);
|
---|
1145 | m_pEffect->SetFloat("py",py);
|
---|
1146 | m_pEffect->SetFloatArray("Lshoot",shootcolors,4);
|
---|
1147 | //m_pEffect->SetFloat("shininess",0.1);
|
---|
1148 | D3DXVECTOR4 eyePos1;
|
---|
1149 | D3DXVec3Transform(&eyePos1,&eyePos,&matViewWorldProjection);
|
---|
1150 | eyePos1.x/=eyePos1.w;
|
---|
1151 | eyePos1.y/=eyePos1.w;
|
---|
1152 | eyePos1.z/=eyePos1.w;
|
---|
1153 | m_pEffect->SetFloatArray("eyePos",eyePos1,3);
|
---|
1154 | m_pEffect->SetMatrix("modelview",&matWorld);
|
---|
1155 | m_pEffect->SetMatrix("modelviewproj",&matViewWorldProjection);
|
---|
1156 | m_pEffect->SetMatrixTranspose("modelviewIT",&inverseWorldMatrix);
|
---|
1157 | m_pEffect->SetTexture("visibility_map",pIterationVisibilityTexture);
|
---|
1158 | m_pEffect->SetTexture("act_radiosity_map",pActualRadianceTexture);
|
---|
1159 | m_pEffect->SetTexture("BRDFMap",pBRDFTexture);
|
---|
1160 |
|
---|
1161 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
1162 |
|
---|
1163 | m_pEffect->EndPass();
|
---|
1164 | }
|
---|
1165 | m_pEffect->End();
|
---|
1166 | }
|
---|
1167 | d3dDevice->EndScene();
|
---|
1168 |
|
---|
1169 |
|
---|
1170 |
|
---|
1171 | pActualRadianceTempTexture=pActualRadianceTexture;
|
---|
1172 | pActualRadianceTempTexture->AddRef();
|
---|
1173 | SAFE_RELEASE(pActualRadianceTexture);
|
---|
1174 | pActualRadianceTexture=pActualRadianceTexture2;
|
---|
1175 | pActualRadianceTexture->AddRef();
|
---|
1176 | SAFE_RELEASE(pActualRadianceTexture2);
|
---|
1177 |
|
---|
1178 | pActualRadianceTexture2=pActualRadianceTempTexture;
|
---|
1179 | pActualRadianceTexture2->AddRef();
|
---|
1180 | SAFE_RELEASE(pActualRadianceTempTexture)
|
---|
1181 | SAFE_RELEASE(pActualRadianceSurface);
|
---|
1182 | SAFE_RELEASE(pActualRadianceSurface2);
|
---|
1183 | pActualRadianceTexture2->GetSurfaceLevel(0,&pActualRadianceSurface2);
|
---|
1184 | pActualRadianceTexture->GetSurfaceLevel(0,&pActualRadianceSurface);
|
---|
1185 |
|
---|
1186 | SAFE_RELEASE(mipmapTexturesArray[0])
|
---|
1187 | SAFE_RELEASE(pMipmapSurfaces[0])
|
---|
1188 |
|
---|
1189 | mipmapTexturesArray[0]=pActualRadianceTexture;
|
---|
1190 | mipmapTexturesArray[0]->AddRef();
|
---|
1191 | mipmapTexturesArray[0]->GetSurfaceLevel(0,&pMipmapSurfaces[0]);
|
---|
1192 |
|
---|
1193 | if(doDebugPicture){
|
---|
1194 | fileFormat=D3DXIFF_HDR;
|
---|
1195 | D3DXSaveTextureToFile(fileName,fileFormat,pActualRadianceTexture,NULL);
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | return S_OK;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | HRESULT RadiosityAlgorithm::doRadiosityActualAveragingPass(UINT iterations,bool doDebugPicture,TCHAR* fileName){
|
---|
1202 |
|
---|
1203 | hr=d3dDevice->SetRenderTarget(0,pMipmapSurfaces[1]);
|
---|
1204 |
|
---|
1205 | hr=d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
1206 |
|
---|
1207 | d3dDevice->BeginScene();
|
---|
1208 |
|
---|
1209 | if( m_pEffect != NULL ){
|
---|
1210 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "ActualAveragingTech" );
|
---|
1211 | m_pEffect->SetTechnique( hTechnique );
|
---|
1212 | m_pEffect->Begin( &nPasses, 0 );
|
---|
1213 | for(UINT i=0;i<nPasses;i++){
|
---|
1214 |
|
---|
1215 | m_pEffect->BeginPass(i);
|
---|
1216 |
|
---|
1217 | m_pEffect->SetTexture("visibility_map",pOrigVisibilityTexture);
|
---|
1218 |
|
---|
1219 | m_pEffect->SetTexture("act_radiosity_map",pActualRadianceTexture);
|
---|
1220 |
|
---|
1221 | m_pEffect->SetTexture("emission_map",emissionTexture);
|
---|
1222 |
|
---|
1223 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
1224 |
|
---|
1225 | m_pEffect->EndPass();
|
---|
1226 | }
|
---|
1227 | m_pEffect->End();
|
---|
1228 | }
|
---|
1229 | d3dDevice->EndScene();
|
---|
1230 | hr=d3dDevice->SetRenderTarget(0,pRadRenderSurface2);
|
---|
1231 |
|
---|
1232 | hr=d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0);
|
---|
1233 |
|
---|
1234 | d3dDevice->BeginScene();
|
---|
1235 |
|
---|
1236 | if( m_pEffect != NULL ){
|
---|
1237 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "ActualAveragingTech2" );
|
---|
1238 | m_pEffect->SetTechnique( hTechnique );
|
---|
1239 | m_pEffect->Begin( &nPasses, 0 );
|
---|
1240 | for(UINT i=0;i<nPasses;i++){
|
---|
1241 |
|
---|
1242 | m_pEffect->BeginPass(i);
|
---|
1243 |
|
---|
1244 | m_pEffect->SetTexture("radiosity_map",this->pRadiosityTexture);
|
---|
1245 | m_pEffect->SetTexture("act_radiosity_map",pActualRadianceTexture);
|
---|
1246 | hr=m_pEffect->SetTexture("visibility_map",pOrigVisibilityTexture);
|
---|
1247 | /*
|
---|
1248 | m_pEffect->SetTexture("emission_map",emissionTexture);
|
---|
1249 | if(iterations==1){
|
---|
1250 | hr=m_pEffect->SetBool("notFirstRun",0);
|
---|
1251 | }
|
---|
1252 | else{
|
---|
1253 | hr=m_pEffect->SetBool("notFirstRun",1);
|
---|
1254 | }
|
---|
1255 | */
|
---|
1256 | objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection);
|
---|
1257 |
|
---|
1258 | m_pEffect->EndPass();
|
---|
1259 | }
|
---|
1260 | m_pEffect->End();
|
---|
1261 | }
|
---|
1262 | d3dDevice->EndScene();
|
---|
1263 |
|
---|
1264 | tempTexture=pRadiosityTexture;
|
---|
1265 | tempTexture->AddRef();
|
---|
1266 | SAFE_RELEASE(pRadiosityTexture)
|
---|
1267 | pRadiosityTexture=pRadiosityTexture2;
|
---|
1268 | pRadiosityTexture->AddRef();
|
---|
1269 | SAFE_RELEASE(pRadiosityTexture2)
|
---|
1270 | pRadiosityTexture2=tempTexture;
|
---|
1271 | pRadiosityTexture2->AddRef();
|
---|
1272 | SAFE_RELEASE(tempTexture)
|
---|
1273 |
|
---|
1274 | SAFE_RELEASE(pRadRenderSurface);
|
---|
1275 | SAFE_RELEASE(pRadRenderSurface2);
|
---|
1276 | pRadiosityTexture->GetSurfaceLevel(0,&pRadRenderSurface);
|
---|
1277 | pRadiosityTexture2->GetSurfaceLevel(0,&pRadRenderSurface2);
|
---|
1278 |
|
---|
1279 | if(doDebugPicture){
|
---|
1280 | fileFormat=D3DXIFF_HDR;
|
---|
1281 | D3DXSaveTextureToFile(L"mipmap1.hdr",fileFormat,this->mipmapTexturesArray[1],NULL);
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | if(doDebugPicture){
|
---|
1285 | fileFormat=D3DXIFF_HDR;
|
---|
1286 | D3DXSaveTextureToFile(fileName,fileFormat,this->pRadiosityTexture,NULL);
|
---|
1287 | }
|
---|
1288 | //hr=d3dDevice->SetRenderTarget(1,NULL);
|
---|
1289 | return hr;
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | HRESULT RadiosityAlgorithm::doFinalPicturePass(bool doDebugPicture,TCHAR* fileName){
|
---|
1293 | /*
|
---|
1294 | eyePos.x=0;
|
---|
1295 | eyePos.y=2.5;
|
---|
1296 | eyePos.z=10.0f;
|
---|
1297 | camLookAt.x=0;
|
---|
1298 | camLookAt.y=2.5f;
|
---|
1299 | camLookAt.z=9.0f;
|
---|
1300 | */
|
---|
1301 | camDir.x=0;
|
---|
1302 | camDir.y=1;
|
---|
1303 | camDir.z=0;
|
---|
1304 |
|
---|
1305 | D3DXMatrixIdentity(&matWorld);
|
---|
1306 | D3DXMatrixLookAtLH(&matWorld,&eyePos,&camLookAt,&camDir);
|
---|
1307 | /*
|
---|
1308 | *errLogger << "World matrix final \n" << matWorld._11;
|
---|
1309 | *errLogger << " " << matWorld._12;
|
---|
1310 | *errLogger << " " << matWorld._13;
|
---|
1311 | *errLogger << " " << matWorld._14 << "\n";
|
---|
1312 | *errLogger << " " << matWorld._21;
|
---|
1313 | *errLogger << " " << matWorld._22;
|
---|
1314 | *errLogger << " " << matWorld._23;
|
---|
1315 | *errLogger << " " << matWorld._24 << "\n";
|
---|
1316 | *errLogger << " " << matWorld._31;
|
---|
1317 | *errLogger << " " << matWorld._32;
|
---|
1318 | *errLogger << " " << matWorld._33;
|
---|
1319 | *errLogger << " " << matWorld._34 << "\n";
|
---|
1320 | *errLogger << " " << matWorld._41;
|
---|
1321 | *errLogger << " " << matWorld._42;
|
---|
1322 | *errLogger << " " << matWorld._43;
|
---|
1323 | *errLogger << " " << matWorld._44 << "\n";
|
---|
1324 | */
|
---|
1325 | //D3DXMatrixInverse(&inverseWorldMatrix,NULL,&matWorld);
|
---|
1326 |
|
---|
1327 | //D3DXMatrixIdentity(&matWorld);
|
---|
1328 | //D3DXMatrixLookAtLH(&matWorld,&fromD3D,&lookatD3D,&upD3D);
|
---|
1329 | D3DXMatrixIdentity(&matProjection);
|
---|
1330 | D3DXMatrixPerspectiveFovLH(&matProjection,D3DX_PI/3.0f,1024.0f/768.0f,0.1f,100);
|
---|
1331 | D3DXMatrixIdentity(&inverseWorldMatrix);
|
---|
1332 | D3DXMatrixMultiply(&inverseWorldMatrix,&matWorld,&matProjection);
|
---|
1333 |
|
---|
1334 | hr=d3dDevice->SetRenderTarget(0,pBackBuffer);
|
---|
1335 | hr=d3dDevice->SetDepthStencilSurface(origDepthSurface);
|
---|
1336 | if(FAILED(hr)){
|
---|
1337 | //*errLogger << "setRendertarget error.\n";
|
---|
1338 | }
|
---|
1339 | d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
|
---|
1340 |
|
---|
1341 | //hr=d3dDevice->SetVertexDeclaration(decl);
|
---|
1342 | if(FAILED(hr)){
|
---|
1343 | //*errLogger << "setVertexDecl error.\n";
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | d3dDevice->BeginScene();
|
---|
1347 |
|
---|
1348 | if( m_pEffect != NULL ){
|
---|
1349 | D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "RenderFinal" );
|
---|
1350 | m_pEffect->SetTechnique( hTechnique );
|
---|
1351 | m_pEffect->Begin( &nPasses, 0 );
|
---|
1352 | for(UINT i=0;i<nPasses;i++){
|
---|
1353 | m_pEffect->BeginPass(i);
|
---|
1354 | m_pEffect->SetMatrix("modelview",&matWorld);
|
---|
1355 | m_pEffect->SetMatrix("modelviewproj",&inverseWorldMatrix);
|
---|
1356 | m_pEffect->SetTexture("final_map",pRadiosityTexture2);
|
---|
1357 | //m_pEffect->SetTexture("emission_map",emissionTexture);
|
---|
1358 |
|
---|
1359 | objectVector->RenderAllWithFX(m_pEffect,false,matWorld,inverseWorldMatrix);
|
---|
1360 |
|
---|
1361 | m_pEffect->EndPass();
|
---|
1362 | }
|
---|
1363 | m_pEffect->End();
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | d3dDevice->EndScene();
|
---|
1367 |
|
---|
1368 | //Save final scene.
|
---|
1369 | if(doDebugPicture){
|
---|
1370 | fileFormat=D3DXIFF_BMP;
|
---|
1371 | D3DXSaveSurfaceToFile(fileName,fileFormat,pBackBuffer,NULL,NULL);
|
---|
1372 | }
|
---|
1373 | //End save final scene.
|
---|
1374 |
|
---|
1375 | return S_OK;
|
---|
1376 | } |
---|