#include "dxstdafx.h" #include "d3ddefs.h" #include #include #include #include #include "vector4d.h" #include "color.h" #include "object.h" #include "ObjectVector.h" #include "RadiosityAlgorithm.h" HRESULT RadiosityAlgorithm::moveObjects(float x,float y,float z){ HRESULT hr=S_OK; D3DXMATRIX trafomatrix; D3DXMatrixIdentity(&trafomatrix); D3DXMatrixTranslation(&trafomatrix,x,y,z); ObjectModel* box2=objectVector->getObjectModel(1); box2->setTransformMatrix(trafomatrix); return hr; } HRESULT RadiosityAlgorithm::moveCamera(float eyeX,float eyeY,float eyeZ,float lookAtX,float lookAtY,float lookAtZ){ HRESULT hr=S_OK; eyePos.x=eyeX; eyePos.y=eyeY; eyePos.z=eyeZ; camLookAt.x=lookAtX; camLookAt.y=lookAtY; camLookAt.z=lookAtZ; return hr; } RadiosityAlgorithm::RadiosityAlgorithm(UINT textureWidth,UINT textureHeight,UINT mipMapLevels,_D3DFORMAT vismapFormat,_D3DFORMAT iterationVismapFormat,_D3DFORMAT renderTextureFormat,_D3DFORMAT mipmapFormat,_D3DFORMAT depthStencilFormat){ /*********************/ eyePos.x=0; eyePos.y=2.5; eyePos.z=10.0f; /*********************/ shootcolors=NULL; pQuadVB=NULL; pPointVB=NULL; emissionSurface=NULL; emissionTexture=NULL; pOrigVisibilityTexture=NULL; pIterationVisibilityTexture=NULL; pRadiosityTexture=NULL; pRadiosityTexture2=NULL; pActualRadianceTexture=NULL; pActualRadianceTexture2=NULL; shootTexture=NULL; radCopyTexture=NULL; visCopyTexture=NULL; tempTexture=NULL; shootColorTexture=NULL; textureDecl=NULL; shootColorSurface=NULL; tempSurface=NULL; visCopySurface=NULL; radCopySurface=NULL; pRenderSurface=NULL; pBackBuffer=NULL; pRadRenderSurface=NULL; pRadRenderSurface2=NULL; pActualRadianceSurface=NULL; pActualRadianceSurface2=NULL; rttDepthSurface=NULL; origDepthSurface=NULL; pIterationVisibilityRenderSurface=NULL; shootSurface=NULL; pBRDFTexture=NULL; pBRDFSurface=NULL; m_pEffect=NULL; nPasses=0; fiRes2=0; fiOld=0; mipmapTexturesArray=NULL; pMipmapSurfaces=NULL; pData=NULL; visMapData=NULL; textureVertices1=NULL; this->textureHeight=textureHeight; this->textureWidth=textureWidth; this->mipMapLevels=mipMapLevels; this->vismapFormat=vismapFormat; this->iterationVismapFormat=iterationVismapFormat; this->renderTextureFormat=renderTextureFormat; this->mipmapFormat=mipmapFormat; this->depthStencilFormat=depthStencilFormat; //errLogger=new ofstream("log.txt"); objectVector=new ObjectVector(); ObjectModel* box1=new ObjectModel(); box1->Load("boxl6.obj", true,false,false, 0.5, 1, 0, 0); ObjectModel* box2=new ObjectModel(); box2->Load("kiskocka6.obj", true,true,false, 0.45, 1, 0.51, 0); D3DXMATRIX trafomatrix; D3DXMatrixIdentity(&trafomatrix); D3DXMatrixTranslation(&trafomatrix,0,0.01f,0); box2->setTransformMatrix(trafomatrix); ObjectModel* light=new ObjectModel(); light->Load("light.obj",true,true,true,0.2,0.2,0.35,0.4); objectVector->addObjectModel(*box1); objectVector->addObjectModel(*box2); objectVector->addObjectModel(*light); } RadiosityAlgorithm::~RadiosityAlgorithm(){ SAFE_DELETE(objectVector); SAFE_DELETE_ARRAY(textureVertices1); this->deleteObjects(); } HRESULT RadiosityAlgorithm::initObjects(LPDIRECT3DDEVICE9 d3dDevice){ shootcolors=new float[4]; DWORD effectCompileFlag=0; this->d3dDevice=d3dDevice; #ifdef DEBUG_SHADER effectCompileFlag|=D3DXSHADER_DEBUG; effectCompileFlag |= D3DXSHADER_FORCE_PS_SOFTWARE_NOOPT; effectCompileFlag |= D3DXSHADER_FORCE_VS_SOFTWARE_NOOPT; #endif if( FAILED( hr = D3DXCreateEffectFromFile( d3dDevice, L"hlsl.fx", NULL, NULL, effectCompileFlag, NULL, &m_pEffect, NULL))){ //*errLogger << "Error reading effect file.\n"; return hr; } lShoot=D3DXVECTOR4(0.0f,0.0f,0.0f,0.0f); textureVertices1=new D3DVERTEX_1[6]; textureVertices1[0]=D3DVERTEX_1(-1.0f,1.0f,0.0f, 0.0f,0.0f); textureVertices1[1]=D3DVERTEX_1(1.0f,1.0f,0.0f, 1.0f,0.0f); textureVertices1[2]=D3DVERTEX_1(-1.0f,-1.0f,0.0f, 0.0f,1.0f); textureVertices1[3]=D3DVERTEX_1(-1.0f,-1.0f,0.0f, 0.0f,1.0f); textureVertices1[4]=D3DVERTEX_1(1.0f,1.0f,0.0f, 1.0f,0.0f); textureVertices1[5]=D3DVERTEX_1(1.0f,-1.0f,0.0f, 1.0f,1.0f); pointVertices=D3DVERTEX_1(0.0f,0.0f,0.0f,0.5f,0.5f); D3DVERTEXELEMENT9 _texturedecl[]= { {0,0,D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,0 }, {0,12,D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,0 }, D3DDECL_END() }; objectVector->initAllObjects(d3dDevice); hr=d3dDevice->CreateVertexDeclaration(_texturedecl,&textureDecl); hr=d3dDevice->CreateVertexBuffer(sizeof(D3DVERTEX_1)*6,D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pQuadVB,NULL); hr=d3dDevice->CreateVertexBuffer(sizeof(D3DVERTEX_1),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_MANAGED,&pPointVB,NULL); pQuadVB->Lock(0,sizeof(pData),(void**)&pData,0); memcpy(pData,textureVertices1,sizeof(D3DVERTEX_1)*6); pQuadVB->Unlock(); pPointVB->Lock(0,sizeof(pData),(void**)&pData,0); memcpy(pData,&pointVertices,sizeof(pointVertices)); pPointVB->Unlock(); d3dDevice->GetRenderTarget(0,&pBackBuffer); //create visibility render-to-texture objects d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,vismapFormat,D3DPOOL_DEFAULT,&pOrigVisibilityTexture,NULL); pOrigVisibilityTexture->GetSurfaceLevel(0,&pRenderSurface); //create radiosity render-to-texture objects d3dDevice->CreateTexture(textureWidth/2,textureHeight/2,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pRadiosityTexture,NULL); pRadiosityTexture->GetSurfaceLevel(0,&pRadRenderSurface); //create radiosity render-to-texture objects d3dDevice->CreateTexture(textureWidth/2,textureHeight/2,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pRadiosityTexture2,NULL); pRadiosityTexture2->GetSurfaceLevel(0,&pRadRenderSurface2); //create radiosity render-to-texture objects d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pActualRadianceTexture,NULL); pActualRadianceTexture->GetSurfaceLevel(0,&pActualRadianceSurface); //create radiosity render-to-texture objects d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pActualRadianceTexture2,NULL); pActualRadianceTexture2->GetSurfaceLevel(0,&pActualRadianceSurface2); d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&emissionTexture,NULL); emissionTexture->GetSurfaceLevel(0,&emissionSurface); //create iteration visibility render-to-texture objects d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,iterationVismapFormat,D3DPOOL_DEFAULT,&pIterationVisibilityTexture,NULL); pIterationVisibilityTexture->GetSurfaceLevel(0,&pIterationVisibilityRenderSurface); //create choice visibility render-to-texture objects d3dDevice->CreateTexture(1,1,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&shootTexture,NULL); shootTexture->GetSurfaceLevel(0,&shootSurface); d3dDevice->CreateTexture(textureWidth,textureHeight,1,D3DUSAGE_RENDERTARGET,renderTextureFormat,D3DPOOL_DEFAULT,&pBRDFTexture,NULL); pBRDFTexture->GetSurfaceLevel(0,&pBRDFSurface); //create mipmap render-to-texture objects mipmapTexturesArray=new LPDIRECT3DTEXTURE9[this->mipMapLevels]; pMipmapSurfaces=new LPDIRECT3DSURFACE9[this->mipMapLevels]; int levels[12]; levels[0]=1; for(UINT i=1;imipMapLevels;i++){ levels[i]=levels[i-1]*2; } mipmapTexturesArray[0]=pActualRadianceTexture; mipmapTexturesArray[0]->AddRef(); mipmapTexturesArray[0]->GetSurfaceLevel(0,&pMipmapSurfaces[0]); for(int i=1;imipMapLevels;i++){ hr=d3dDevice->CreateTexture(textureWidth/levels[i],textureHeight/levels[i],1,D3DUSAGE_RENDERTARGET,this->mipmapFormat,D3DPOOL_DEFAULT,&mipmapTexturesArray[i],NULL); if(FAILED(hr)){ return 0; } hr=mipmapTexturesArray[i]->GetSurfaceLevel(0,&pMipmapSurfaces[i]); if(FAILED(hr)){ //*errLogger << "GetSurfaceLevel error."; return 0; } } hr=d3dDevice->CreateDepthStencilSurface(this->textureWidth,this->textureHeight,this->depthStencilFormat,D3DMULTISAMPLE_NONE ,0,true,&rttDepthSurface,NULL); return S_OK; } HRESULT RadiosityAlgorithm::deleteObjects(){ SAFE_DELETE_ARRAY(shootcolors); SAFE_RELEASE(rttDepthSurface) SAFE_RELEASE(origDepthSurface) SAFE_RELEASE(pBackBuffer) SAFE_RELEASE(m_pEffect) SAFE_RELEASE(textureDecl) SAFE_RELEASE(visCopyTexture) SAFE_RELEASE(visCopySurface) SAFE_RELEASE(tempTexture) SAFE_RELEASE(tempSurface) SAFE_RELEASE(shootColorTexture) SAFE_RELEASE(shootColorSurface) SAFE_RELEASE(pOrigVisibilityTexture) SAFE_RELEASE(pRenderSurface) SAFE_RELEASE(pIterationVisibilityTexture) SAFE_RELEASE(pIterationVisibilityRenderSurface) SAFE_RELEASE(pRadiosityTexture) SAFE_RELEASE(pRadRenderSurface) SAFE_RELEASE(pBRDFTexture) SAFE_RELEASE(pBRDFSurface) SAFE_RELEASE(pActualRadianceTexture) SAFE_RELEASE(pActualRadianceSurface) SAFE_RELEASE(shootTexture) SAFE_RELEASE(shootSurface) if((mipmapTexturesArray!=NULL)&&(mipmapTexturesArray!=NULL)){ for(UINT i=0;imipMapLevels;i++){ SAFE_RELEASE(mipmapTexturesArray[i]) SAFE_RELEASE(pMipmapSurfaces[i]) } } SAFE_DELETE_ARRAY(mipmapTexturesArray); SAFE_DELETE_ARRAY(pMipmapSurfaces); SAFE_RELEASE(pRadRenderSurface2); SAFE_RELEASE(pRadiosityTexture2) SAFE_RELEASE(pActualRadianceSurface2); SAFE_RELEASE(pActualRadianceTexture2); SAFE_RELEASE(pQuadVB) SAFE_RELEASE(pPointVB) SAFE_RELEASE(emissionSurface) SAFE_RELEASE(emissionTexture) return S_OK; } HRESULT RadiosityAlgorithm::doEmissionMapPass(bool doDebugPicture,TCHAR* fileName){ //hr=d3dDevice->GetDepthStencilSurface(&origDepthSurface); hr=d3dDevice->SetRenderTarget(0,pRadRenderSurface); hr=d3dDevice->SetDepthStencilSurface(rttDepthSurface); d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "EmissionMapTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); hr=d3dDevice->SetRenderTarget(0,pActualRadianceSurface); d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); //hr=d3dDevice->SetVertexDeclaration(this->decl); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "EmissionMapTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); hr=d3dDevice->SetRenderTarget(0,emissionSurface); d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); //hr=d3dDevice->SetVertexDeclaration(decl); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "EmissionMapTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); /********************Save*************/ if(doDebugPicture){ fileFormat=D3DXIFF_HDR; D3DXSaveTextureToFile(fileName,fileFormat,emissionTexture,NULL); } /*************************************/ return S_OK; } HRESULT RadiosityAlgorithm::doOriginalVisibilityMapPass(bool doDebugPicture,TCHAR* fileName){ //Render scene with specific HLSL shaders (hemisphere and writethrough) hr=d3dDevice->GetDepthStencilSurface(&origDepthSurface); hr=d3dDevice->SetRenderTarget(0,pRenderSurface); hr=d3dDevice->SetDepthStencilSurface(rttDepthSurface); if(FAILED(hr)){ //*errLogger << "setRendertarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); //hr=d3dDevice->SetVertexDeclaration(decl); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = NULL; hTechnique = m_pEffect->GetTechniqueByName((LPSTR)"OriginalVisibilityMapShader"); if(hTechnique==NULL){ //*errLogger << "Cannot find technique. Quitting.\n"; return -1; } m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); /********************Save*************/ if(doDebugPicture){ fileFormat=D3DXIFF_HDR; hr=D3DXSaveTextureToFile(fileName,fileFormat,pOrigVisibilityTexture,NULL); } /*************************************/ //Vismap surface data extraction. pRenderSurface->GetDesc(&visSurfaceDesc); d3dDevice->CreateTexture(this->textureWidth,textureHeight,1,D3DUSAGE_DYNAMIC,vismapFormat,D3DPOOL_SYSTEMMEM,&visCopyTexture,NULL); visCopyTexture->GetSurfaceLevel(0,&visCopySurface); d3dDevice->GetRenderTargetData(pRenderSurface,visCopySurface); visCopySurface->LockRect(&visRectangleToLock,NULL,0); visMapData=(D3DXFLOAT16*)visRectangleToLock.pBits; visCopySurface->UnlockRect(); hr=d3dDevice->SetDepthStencilSurface(origDepthSurface); return S_OK; } HRESULT RadiosityAlgorithm::initActualRadianceMap(){ HRESULT hr; V_RETURN(d3dDevice->SetRenderTarget(0,pActualRadianceSurface)) V_RETURN(d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0)) return hr; } HRESULT RadiosityAlgorithm::initBRDFAndSpecularMap(){ HRESULT hr; LPDIRECT3DTEXTURE9 origBRDFTexture; V_RETURN(d3dDevice->SetRenderTarget(0,pActualRadianceSurface)) V_RETURN(d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0)) d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = NULL; hTechnique = m_pEffect->GetTechniqueByName((LPSTR)"BRDFShader"); if(hTechnique==NULL){ //*errLogger << "Cannot find technique. Quitting.\n"; return -1; } m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); vector::iterator iter; vector models=objectVector->getAllObjectModels(); for(iter=models.begin();iter!=models.end();iter++){ LPCTSTR textureName=(*iter)->getTextureName(); float shininess=(*iter)->getShininess(); hr=D3DXCreateTextureFromFile(d3dDevice,(LPCTSTR)textureName,&origBRDFTexture); if(SUCCEEDED(hr)){ m_pEffect->SetTexture("BRDFTexture",origBRDFTexture); m_pEffect->SetFloat("shininess",shininess); V_RETURN((*iter)->RenderWithFX(m_pEffect,true,matWorld,matViewWorldProjection)) SAFE_RELEASE(origBRDFTexture) } else{ return hr; } } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); return hr; } HRESULT RadiosityAlgorithm::iterate(UINT niterations,UINT iterationToSave){ bool doIterationSave=false; timeBefore=timeGetTime(); for(UINT i=1;i<=niterations;i++){ if(i==iterationToSave) doIterationSave=true; else doIterationSave=false; //if(i<10||i%10==0){ createRadiosityMipMap(i); //} chooseShooter(i); //return E_FAIL; initActualRadianceMap(); this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap1.hdr",fromD3D,dirD3D,upD3D,true); this->doRadiosityMapPass(i,doIterationSave,L"itRadMap1.hdr"); this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap2.hdr",fromD3D,upD3D,dirD3D,false); this->doRadiosityMapPass(i,doIterationSave,L"itRadMap2.hdr"); this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap3.hdr",fromD3D,-upD3D,dirD3D,false); this->doRadiosityMapPass(i,doIterationSave,L"itRadMap3.hdr"); D3DXVECTOR3 dirUpCross; D3DXVec3Cross(&dirUpCross,&upD3D,&dirD3D); this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap4.hdr",fromD3D,dirUpCross,dirD3D,false); this->doRadiosityMapPass(i,doIterationSave,L"itRadMap4.hdr"); this->doIterationVisibilityMapPass(doIterationSave,L"itVisMap5.hdr",fromD3D,-dirUpCross,dirD3D,false); this->doRadiosityMapPass(i,doIterationSave,L"itRadMap5.hdr"); this->doRadiosityActualAveragingPass(i,doIterationSave,L"actavgrad.hdr"); } timeAfter==timeGetTime(); char buffer1[200]; memset(buffer1,0,200); sprintf(buffer1,"%ld",timeAfter-timeBefore); FILE* logger=fopen("log.txt","a+"); fprintf(logger,"Time: %s\n",buffer1); fclose(logger); //*errLogger << "Iteration time in seconds: " << buffer1 << "\n"; return S_OK; } HRESULT RadiosityAlgorithm::createRadiosityMipMap(UINT niteration){ recres2=1.0f/(float)(this->textureWidth); hr=d3dDevice->SetVertexDeclaration(textureDecl); //hr=d3dDevice->SetDepthStencilSurface(rttDepthSurface); if(niteration==1){ hr=d3dDevice->SetRenderTarget(0,pMipmapSurfaces[1]); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "MipmapBaseCreate" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT j=0;jBeginPass(j); m_pEffect->SetFloat("recres2",recres2); m_pEffect->SetFloat("niteration",niteration); m_pEffect->SetTexture("radiosity_map",pActualRadianceTexture); hr=d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); hr=d3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX_1)); if(FAILED(hr)){ //*errLogger << "setstreamsource error.\n"; } m_pEffect->CommitChanges(); hr=d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2); if(FAILED(hr)){ //*errLogger << "drawprimitive error.\n"; } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); } recres2*=2.0f; for(UINT i=1;imipMapLevels-1;i++){ hr=d3dDevice->SetRenderTarget(0,pMipmapSurfaces[i+1]); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "MipmapLevelCreate" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT j=0;jBeginPass(j); m_pEffect->SetFloat("recres2",recres2); m_pEffect->SetTexture("luminance_map",mipmapTexturesArray[i]); hr=d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); hr=d3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX_1)); if(FAILED(hr)){ //*errLogger << "setstreamsource error.\n"; } m_pEffect->CommitChanges(); hr=d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2); if(FAILED(hr)){ //*errLogger << "drawprimitive error.\n"; } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); recres2*=2.0f; } return S_OK; } HRESULT RadiosityAlgorithm::doRadiosityAveragingPass(bool doDebugPicture,TCHAR* fileName,UINT niteration){ hr=d3dDevice->SetVertexDeclaration(textureDecl); hr=d3dDevice->SetRenderTarget(0,pRadRenderSurface2); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "RadAveragingTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT j=0;jBeginPass(j); m_pEffect->SetTexture("radiosity_map",pRadiosityTexture); m_pEffect->SetTexture("emission_map",emissionTexture); m_pEffect->SetFloat("niteration",niteration); hr=d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE); hr=d3dDevice->SetStreamSource(0,pQuadVB,0,sizeof(D3DVERTEX_1)); if(FAILED(hr)){ //*errLogger << "setstreamsource error.\n"; } m_pEffect->CommitChanges(); hr=d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2); if(FAILED(hr)){ //*errLogger << "drawprimitive error.\n"; } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); if(doDebugPicture){ fileFormat=D3DXIFF_HDR; D3DXSaveTextureToFile(fileName,fileFormat,pRadiosityTexture2,NULL); } return S_OK; } HRESULT RadiosityAlgorithm::chooseShooter(UINT iterations){ recres2=1.0f/2.0f; pMipmapSurfaces[this->mipMapLevels-1]->GetDesc(&surfaceDesc); hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->mipmapFormat,D3DPOOL_SYSTEMMEM,&radCopyTexture,NULL); if(FAILED(hr)){ //*errLogger << "Create texture.\n"; } radCopyTexture->GetSurfaceLevel(0,&radCopySurface); hr=d3dDevice->GetRenderTargetData(pMipmapSurfaces[this->mipMapLevels-1],radCopySurface); if(FAILED(hr)){ //*errLogger << "Get render target data.\n"; } hr=radCopySurface->LockRect(&rectangleToLock,0,0); if(FAILED(hr)){ //*errLogger << "Lock rect failiure.\n"; } imageData=(float*)rectangleToLock.pBits; hr=radCopySurface->UnlockRect(); if(FAILED(hr)){ //*errLogger << "Unlock rect failiure.\n"; } fiRes2=imageData[0]; if(_isnan(fiRes2)){ return(E_FAIL); } SAFE_RELEASE(radCopySurface); SAFE_RELEASE(radCopyTexture); hr=d3dDevice->SetRenderTarget(0,shootSurface); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); hr= d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "SearchStartTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT j=0;jBeginPass(j); m_pEffect->SetFloat("recres2",recres2); float searchCoords[2]={0,0}; m_pEffect->SetFloatArray("searchCoords",searchCoords,2); m_pEffect->SetFloat("firstsum",fiRes2*(float)rand()/(float)RAND_MAX); m_pEffect->SetTexture("sum_map",mipmapTexturesArray[this->mipMapLevels-1]); m_pEffect->SetTexture("mipmap",mipmapTexturesArray[this->mipMapLevels-2]); hr=d3dDevice->SetVertexDeclaration(textureDecl); hr=d3dDevice->SetStreamSource(0,pPointVB,0,sizeof(D3DVERTEX_1)); if(FAILED(hr)){ //*errLogger << "setstreamsource error.\n"; } m_pEffect->CommitChanges(); hr=d3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,1); if(FAILED(hr)){ //*errLogger << "drawprimitive error.\n"; } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); recres2/=2.0f; //shootSurface->GetDesc(&surfaceDesc); hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->renderTextureFormat,D3DPOOL_SYSTEMMEM,&radCopyTexture,NULL); if(FAILED(hr)){ //*errLogger << "Create texture.\n"; } radCopyTexture->GetSurfaceLevel(0,&radCopySurface); hr=d3dDevice->GetRenderTargetData(shootSurface,radCopySurface); if(FAILED(hr)){ //*errLogger << "Get render target data.\n"; } hr=radCopySurface->LockRect(&rectangleToLock,0,0); if(FAILED(hr)){ //*errLogger << "Lock rect failiure.\n"; } //float* imageData=(float*)rectangleToLock.pBits; hr=radCopySurface->UnlockRect(); if(FAILED(hr)){ //*errLogger << "Unlock rect failiure.\n"; } SAFE_RELEASE(radCopySurface); SAFE_RELEASE(radCopyTexture); int levelToGo=iterations>0 ? 1 : 2; //return E_FAIL; d3dDevice->CreateTexture(1,1,1,D3DUSAGE_RENDERTARGET,this->renderTextureFormat,D3DPOOL_DEFAULT,&radCopyTexture,NULL); radCopyTexture->GetSurfaceLevel(0,&radCopySurface); for(int i=this->mipMapLevels-3;i>=levelToGo;i--){ hr=d3dDevice->SetRenderTarget(0,radCopySurface); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "SearchTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT j=0;jBeginPass(j); m_pEffect->SetFloat("recres2",recres2); //m_pEffect->SetFloat("RandomNum",(float)rand()/(float)RAND_MAX); m_pEffect->SetTexture("sum_map",shootTexture); m_pEffect->SetTexture("mipmap",mipmapTexturesArray[i]); hr=d3dDevice->SetStreamSource(0,pPointVB,0,sizeof(D3DVERTEX_1)); if(FAILED(hr)){ //*errLogger << "setstreamsource error.\n"; } m_pEffect->CommitChanges(); hr=d3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,1); if(FAILED(hr)){ //*errLogger << "drawprimitive error.\n"; } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); recres2/=2.0f; tempTexture=radCopyTexture; tempTexture->AddRef(); SAFE_RELEASE(radCopyTexture) radCopyTexture=shootTexture; radCopyTexture->AddRef(); SAFE_RELEASE(shootTexture) shootTexture=tempTexture; shootTexture->AddRef(); SAFE_RELEASE(tempTexture) SAFE_RELEASE(radCopySurface); SAFE_RELEASE(shootSurface); SAFE_RELEASE(tempSurface); shootTexture->GetSurfaceLevel(0,&shootSurface); radCopyTexture->GetSurfaceLevel(0,&radCopySurface); //return E_FAIL; } //SAFE_RELEASE(radCopyTexture); //SAFE_RELEASE(radCopySurface); d3dDevice->CreateTexture(1,1,1,D3DUSAGE_RENDERTARGET,this->renderTextureFormat,D3DPOOL_DEFAULT,&shootColorTexture,NULL); shootColorTexture->GetSurfaceLevel(0,&shootColorSurface); hr=d3dDevice->SetRenderTarget(0,radCopySurface); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } hr=d3dDevice->SetRenderTarget(1,shootColorSurface); if(FAILED(hr)){ //*errLogger << "setRenderTarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "SearchEndTech" ); hr=m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT j=0;jBeginPass(j); m_pEffect->SetFloat("recres2",recres2); m_pEffect->SetTexture("sum_map",shootTexture); if(iterations==1){ m_pEffect->SetTexture("mipmap",mipmapTexturesArray[levelToGo-1]); } else{ m_pEffect->SetTexture("mipmap",pRadiosityTexture); } hr=d3dDevice->SetStreamSource(0,pPointVB,0,sizeof(D3DVERTEX_1)); if(FAILED(hr)){ //*errLogger << "setstreamsource error.\n"; } m_pEffect->CommitChanges(); hr=d3dDevice->DrawPrimitive(D3DPT_POINTLIST,0,1); if(FAILED(hr)){ //*errLogger << "drawprimitive error.\n"; } m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); hr=d3dDevice->SetRenderTarget(1,NULL); recres2/=2.0f; //shootSurface->GetDesc(&surfaceDesc); hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->renderTextureFormat,D3DPOOL_SYSTEMMEM,&tempTexture,NULL); if(FAILED(hr)){ //*errLogger << "Create texture.\n"; } tempTexture->GetSurfaceLevel(0,&tempSurface); hr=d3dDevice->GetRenderTargetData(radCopySurface,tempSurface); if(FAILED(hr)){ //*errLogger << "Get render target data.\n"; } hr=tempSurface->LockRect(&rectangleToLock,0,0); if(FAILED(hr)){ //*errLogger << "Lock rect failiure.\n"; } imageData=(float*)rectangleToLock.pBits; hr=tempSurface->UnlockRect(); if(FAILED(hr)){ //*errLogger << "Unlock rect failiure.\n"; } Vector chosenTexelCoords(imageData[1],imageData[2],0); index2=(int)(imageData[2]*this->textureWidth*(visRectangleToLock.Pitch/4) + imageData[1]*this->textureHeight); index2*=2; shooter = visMapData[index2]; //shooter = shooter&0x00ffffff; /* *errLogger << "Sum: " << imageData[0] << "\n"; *errLogger << "Coord_x: " << imageData[1]*this->textureWidth<< "\n"; *errLogger << "Coord_y: " << imageData[2]*this->textureHeight << "\n"; *errLogger << "Shooter: " << index2 << "\n"; *errLogger << "Shooter from visMap: " << shooter << "\n"; */ shootColorSurface->GetDesc(&surfaceDesc); SAFE_RELEASE(tempTexture); SAFE_RELEASE(tempSurface); hr=d3dDevice->CreateTexture(1,1,1,D3DUSAGE_DYNAMIC,this->renderTextureFormat,D3DPOOL_SYSTEMMEM,&tempTexture,NULL); if(FAILED(hr)){ //*errLogger << "Create texture.\n"; } tempTexture->GetSurfaceLevel(0,&tempSurface); hr=d3dDevice->GetRenderTargetData(shootColorSurface,tempSurface); if(FAILED(hr)){ //*errLogger << "Get render target data.\n"; return 0; } hr=tempSurface->LockRect(&rectangleToLock,0,0); if(FAILED(hr)){ //*errLogger << "Lock rect failiure.\n"; } imageData=(float*)rectangleToLock.pBits; hr=tempSurface->UnlockRect(); if(FAILED(hr)){ //*errLogger << "Unlock rect failiure.\n"; } //*errLogger << "fiRes2: " << fiRes2/this->textureWidth/this->textureHeight << "\n"; Color pShoot(imageData[0],imageData[1],imageData[2],1.0f); py=pShoot.Lum()/(fiRes2/this->textureWidth/this->textureHeight); pShoot=pShoot/py; SAFE_RELEASE(tempTexture); SAFE_RELEASE(tempSurface); SAFE_RELEASE(shootColorSurface); SAFE_RELEASE(shootColorTexture); SAFE_RELEASE(radCopySurface) SAFE_RELEASE(radCopyTexture) //hr=d3dDevice->SetVertexDeclaration(decl); Vector from, dir, up; Triangle tri; Vector vertex1,vertex2,vertex3; Vector texcoord1,texcoord2,texcoord3; objectVector->getTriangleByIndex((UINT)shooter,tri,vertex1,vertex2,vertex3,texcoord1,texcoord2,texcoord3); shooterDisc=tri.area/tri.texarea/M_PI/(float)this->textureWidth/(float)this->textureHeight; chosenTexelCoords.y=1-chosenTexelCoords.y; float detbeta=(texcoord2.x-texcoord3.x)*(texcoord3.y-texcoord1.y)-(texcoord2.y-texcoord3.y)*(texcoord3.x-texcoord1.x); float detalpha=(texcoord1.x-texcoord3.x)*(texcoord3.y-texcoord2.y)-(texcoord1.y-texcoord3.y)*(texcoord3.x-texcoord2.x); float det1=(texcoord3.y-chosenTexelCoords.y)*(texcoord3.x-texcoord1.x)-(texcoord3.x-chosenTexelCoords.x)*(texcoord3.y-texcoord1.y); float det2=(texcoord3.y-chosenTexelCoords.y)*(texcoord3.x-texcoord2.x)-(texcoord3.x-chosenTexelCoords.x)*(texcoord3.y-texcoord2.y); float beta= det1/detbeta; //float alpha= (beta*(texcoord2.x-texcoord3.x)+texcoord3.x-chosenTexelCoords.x)/(texcoord3.x-texcoord1.x); float alpha= det2/detalpha; dir=tri.normal; up=tri.up; from=vertex1*alpha+vertex2*beta+vertex3*(1.0f-alpha-beta); /* from=Vector(0,5,0); dir=Vector(0,-1,0); up=Vector(1,0,0); */ Vector lookat = from + dir; dirD3D.x=dir.x; dirD3D.y=dir.y; dirD3D.z=dir.z; fromD3D.x=from.x; fromD3D.y=from.y; fromD3D.z=from.z; lookatD3D.x=lookat.x; lookatD3D.y=lookat.y; lookatD3D.z=lookat.z; upD3D.x=up.x; upD3D.y=up.y; upD3D.z=up.z; /* *errLogger << "From" << from.x << " " << from.y << " " << from.z << "\n"; *errLogger << "Lookat" << lookat.x << " " << lookat.y << " " << lookat.z << "\n"; *errLogger << "Up" << up.x << " " << up.y << " " << up.z << "\n"; */ D3DXMatrixIdentity(&matWorld); shootcolors[0]=pShoot.R; shootcolors[1]=pShoot.G; shootcolors[2]=pShoot.B; shootcolors[3]=shooterDisc; return S_OK; } HRESULT RadiosityAlgorithm::doIterationVisibilityMapPass(bool doDebugPicture,TCHAR* fileName,D3DXVECTOR3 center,D3DXVECTOR3 dir,D3DXVECTOR3 up,bool fullScreen){ D3DXVECTOR3 lookat=center+dir; D3DXMatrixLookAtLH(&matWorld,¢er,&lookat,&up); D3DXMatrixPerspectiveFovLH(&matProjection,D3DX_PI/2.0,1,0.1,20); D3DXMatrixMultiply(&matViewWorldProjection,&matWorld,&matProjection); hr=d3dDevice->SetRenderTarget(0,pIterationVisibilityRenderSurface); if(FAILED(hr)){ //*errLogger << "setRendertarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); //hr=d3dDevice->SetVertexDeclaration(decl); if(FAILED(hr)){ //*errLogger << "setVertexDecl error.\n"; } d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "ITVisShader" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); m_pEffect->SetFloat("fullScreen",fullScreen); m_pEffect->SetFloatArray("eyePos",(float*)fromD3D,3); m_pEffect->SetMatrix("modelview",&matWorld); hr=m_pEffect->SetMatrix("modelviewproj",&matViewWorldProjection); objectVector->RenderAllWithFX(m_pEffect,false,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); if(doDebugPicture){ fileFormat=D3DXIFF_HDR; D3DXSaveTextureToFile(fileName,fileFormat,pIterationVisibilityTexture,NULL); } return S_OK; } HRESULT RadiosityAlgorithm::doRadiosityMapPass(UINT iterations,bool doDebugPicture,TCHAR* fileName){ D3DXMatrixInverse(&inverseWorldMatrix,NULL,&matWorld); hr=d3dDevice->SetRenderTarget(0,pActualRadianceSurface2); d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "Shoot" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); //m_pEffect->SetFloat("niteration",(float)iterations); m_pEffect->SetFloat("py",py); m_pEffect->SetFloatArray("Lshoot",shootcolors,4); //m_pEffect->SetFloat("shininess",0.1); D3DXVECTOR4 eyePos1; D3DXVec3Transform(&eyePos1,&eyePos,&matViewWorldProjection); eyePos1.x/=eyePos1.w; eyePos1.y/=eyePos1.w; eyePos1.z/=eyePos1.w; m_pEffect->SetFloatArray("eyePos",eyePos1,3); m_pEffect->SetMatrix("modelview",&matWorld); m_pEffect->SetMatrix("modelviewproj",&matViewWorldProjection); m_pEffect->SetMatrixTranspose("modelviewIT",&inverseWorldMatrix); m_pEffect->SetTexture("visibility_map",pIterationVisibilityTexture); m_pEffect->SetTexture("act_radiosity_map",pActualRadianceTexture); m_pEffect->SetTexture("BRDFMap",pBRDFTexture); objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); pActualRadianceTempTexture=pActualRadianceTexture; pActualRadianceTempTexture->AddRef(); SAFE_RELEASE(pActualRadianceTexture); pActualRadianceTexture=pActualRadianceTexture2; pActualRadianceTexture->AddRef(); SAFE_RELEASE(pActualRadianceTexture2); pActualRadianceTexture2=pActualRadianceTempTexture; pActualRadianceTexture2->AddRef(); SAFE_RELEASE(pActualRadianceTempTexture) SAFE_RELEASE(pActualRadianceSurface); SAFE_RELEASE(pActualRadianceSurface2); pActualRadianceTexture2->GetSurfaceLevel(0,&pActualRadianceSurface2); pActualRadianceTexture->GetSurfaceLevel(0,&pActualRadianceSurface); SAFE_RELEASE(mipmapTexturesArray[0]) SAFE_RELEASE(pMipmapSurfaces[0]) mipmapTexturesArray[0]=pActualRadianceTexture; mipmapTexturesArray[0]->AddRef(); mipmapTexturesArray[0]->GetSurfaceLevel(0,&pMipmapSurfaces[0]); if(doDebugPicture){ fileFormat=D3DXIFF_HDR; D3DXSaveTextureToFile(fileName,fileFormat,pActualRadianceTexture,NULL); } return S_OK; } HRESULT RadiosityAlgorithm::doRadiosityActualAveragingPass(UINT iterations,bool doDebugPicture,TCHAR* fileName){ hr=d3dDevice->SetRenderTarget(0,pMipmapSurfaces[1]); hr=d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "ActualAveragingTech" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); m_pEffect->SetTexture("visibility_map",pOrigVisibilityTexture); m_pEffect->SetTexture("act_radiosity_map",pActualRadianceTexture); m_pEffect->SetTexture("emission_map",emissionTexture); objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); hr=d3dDevice->SetRenderTarget(0,pRadRenderSurface2); hr=d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0x00,0,0,0),1.0f,0); d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "ActualAveragingTech2" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); m_pEffect->SetTexture("radiosity_map",this->pRadiosityTexture); m_pEffect->SetTexture("act_radiosity_map",pActualRadianceTexture); hr=m_pEffect->SetTexture("visibility_map",pOrigVisibilityTexture); /* m_pEffect->SetTexture("emission_map",emissionTexture); if(iterations==1){ hr=m_pEffect->SetBool("notFirstRun",0); } else{ hr=m_pEffect->SetBool("notFirstRun",1); } */ objectVector->RenderAllWithFX(m_pEffect,true,matWorld,matViewWorldProjection); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); tempTexture=pRadiosityTexture; tempTexture->AddRef(); SAFE_RELEASE(pRadiosityTexture) pRadiosityTexture=pRadiosityTexture2; pRadiosityTexture->AddRef(); SAFE_RELEASE(pRadiosityTexture2) pRadiosityTexture2=tempTexture; pRadiosityTexture2->AddRef(); SAFE_RELEASE(tempTexture) SAFE_RELEASE(pRadRenderSurface); SAFE_RELEASE(pRadRenderSurface2); pRadiosityTexture->GetSurfaceLevel(0,&pRadRenderSurface); pRadiosityTexture2->GetSurfaceLevel(0,&pRadRenderSurface2); if(doDebugPicture){ fileFormat=D3DXIFF_HDR; D3DXSaveTextureToFile(L"mipmap1.hdr",fileFormat,this->mipmapTexturesArray[1],NULL); } if(doDebugPicture){ fileFormat=D3DXIFF_HDR; D3DXSaveTextureToFile(fileName,fileFormat,this->pRadiosityTexture,NULL); } //hr=d3dDevice->SetRenderTarget(1,NULL); return hr; } HRESULT RadiosityAlgorithm::doFinalPicturePass(bool doDebugPicture,TCHAR* fileName){ /* eyePos.x=0; eyePos.y=2.5; eyePos.z=10.0f; camLookAt.x=0; camLookAt.y=2.5f; camLookAt.z=9.0f; */ camDir.x=0; camDir.y=1; camDir.z=0; D3DXMatrixIdentity(&matWorld); D3DXMatrixLookAtLH(&matWorld,&eyePos,&camLookAt,&camDir); /* *errLogger << "World matrix final \n" << matWorld._11; *errLogger << " " << matWorld._12; *errLogger << " " << matWorld._13; *errLogger << " " << matWorld._14 << "\n"; *errLogger << " " << matWorld._21; *errLogger << " " << matWorld._22; *errLogger << " " << matWorld._23; *errLogger << " " << matWorld._24 << "\n"; *errLogger << " " << matWorld._31; *errLogger << " " << matWorld._32; *errLogger << " " << matWorld._33; *errLogger << " " << matWorld._34 << "\n"; *errLogger << " " << matWorld._41; *errLogger << " " << matWorld._42; *errLogger << " " << matWorld._43; *errLogger << " " << matWorld._44 << "\n"; */ //D3DXMatrixInverse(&inverseWorldMatrix,NULL,&matWorld); //D3DXMatrixIdentity(&matWorld); //D3DXMatrixLookAtLH(&matWorld,&fromD3D,&lookatD3D,&upD3D); D3DXMatrixIdentity(&matProjection); D3DXMatrixPerspectiveFovLH(&matProjection,D3DX_PI/3.0f,1024.0f/768.0f,0.1f,100); D3DXMatrixIdentity(&inverseWorldMatrix); D3DXMatrixMultiply(&inverseWorldMatrix,&matWorld,&matProjection); hr=d3dDevice->SetRenderTarget(0,pBackBuffer); hr=d3dDevice->SetDepthStencilSurface(origDepthSurface); if(FAILED(hr)){ //*errLogger << "setRendertarget error.\n"; } d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); //hr=d3dDevice->SetVertexDeclaration(decl); if(FAILED(hr)){ //*errLogger << "setVertexDecl error.\n"; } d3dDevice->BeginScene(); if( m_pEffect != NULL ){ D3DXHANDLE hTechnique = m_pEffect->GetTechniqueByName( "RenderFinal" ); m_pEffect->SetTechnique( hTechnique ); m_pEffect->Begin( &nPasses, 0 ); for(UINT i=0;iBeginPass(i); m_pEffect->SetMatrix("modelview",&matWorld); m_pEffect->SetMatrix("modelviewproj",&inverseWorldMatrix); m_pEffect->SetTexture("final_map",pRadiosityTexture2); //m_pEffect->SetTexture("emission_map",emissionTexture); objectVector->RenderAllWithFX(m_pEffect,false,matWorld,inverseWorldMatrix); m_pEffect->EndPass(); } m_pEffect->End(); } d3dDevice->EndScene(); //Save final scene. if(doDebugPicture){ fileFormat=D3DXIFF_BMP; D3DXSaveSurfaceToFile(fileName,fileFormat,pBackBuffer,NULL,NULL); } //End save final scene. return S_OK; }