1 | #include "dxstdafx.h"
|
---|
2 | #include "resource.h"
|
---|
3 | #include "RaytraceRenderer.h"
|
---|
4 | #include "Scene.h"
|
---|
5 | #include "GameManager.h"
|
---|
6 | #include "NodeFilter.h"
|
---|
7 | #include <ctime>
|
---|
8 |
|
---|
9 |
|
---|
10 | #if(0)
|
---|
11 | #define CUBEMAP_SIZE 512 // Size of CubeMaps (MG: 512 works on both current NVidia and ATI driver)
|
---|
12 | #elif(0)
|
---|
13 | #define CUBEMAP_SIZE 1024 // Size of CubeMaps (MG)
|
---|
14 | #elif(1)
|
---|
15 | int CUBEMAP_SIZE = 512;
|
---|
16 | #endif
|
---|
17 |
|
---|
18 |
|
---|
19 | RaytraceRenderer::RaytraceRenderer(void) : Renderer()
|
---|
20 | {
|
---|
21 | srand( (unsigned)time( NULL ) );
|
---|
22 | this->nextUpdate = -1000.0f;
|
---|
23 | g_fFresnelFactor = 1.0f;
|
---|
24 | g_fFresnelFactor = 0.05f;
|
---|
25 | g_fRefractionIndex = 0.95f;
|
---|
26 |
|
---|
27 | g_iNumberOfIteration = 10;
|
---|
28 | g_bUseDistanceImpostors = true;
|
---|
29 | this->counter = 0;
|
---|
30 |
|
---|
31 | this->setRenderPriority(30);//40);
|
---|
32 | this->rendererType |= Renderer::RENDERER_RAYTRACE;
|
---|
33 |
|
---|
34 | g_pEffect = NULL; // D3DX effect interface
|
---|
35 | g_pRenderTargetSurfaceOld = NULL; // Store the original Screen's surface
|
---|
36 | g_pDepthStencilSurfaceOld = NULL; // Store the original Screen's DSS
|
---|
37 |
|
---|
38 | // Cube map textures
|
---|
39 | g_pRoomColorDistCubeMapTexture = NULL; // CubeMap for store: Color + Distance
|
---|
40 | //g_pRoomUVCubeMapTexture = NULL; // CubeMap for store: UV + ObjectID
|
---|
41 |
|
---|
42 | g_pCubeMapDepthStencilSurface = NULL; // Depth-stencil buffer for rendering to cube texture
|
---|
43 | g_pDepthStencilSurface = NULL; // Depth-stencil buffer for Room Texture
|
---|
44 |
|
---|
45 | g_bInitialization = true; // Initialization is in progress
|
---|
46 | g_bUseDistanceImpostors = true; // Use distance impostor method
|
---|
47 |
|
---|
48 | D3DXMatrixPerspectiveFovLH( &this->cubeMapProjectionMat, D3DX_PI * 0.5f, 1.0f, 0.001f, 10000.0f );
|
---|
49 | }
|
---|
50 |
|
---|
51 | RaytraceRenderer::~RaytraceRenderer(void)
|
---|
52 | {
|
---|
53 | SAFE_RELEASE( g_pRoomColorDistCubeMapTexture );
|
---|
54 | //SAFE_RELEASE( g_pRoomUVCubeMapTexture );
|
---|
55 | SAFE_RELEASE( g_pDepthStencilSurface );
|
---|
56 | SAFE_RELEASE( g_pCubeMapDepthStencilSurface );
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | void RaytraceRenderer::setFresnelFactor(float _fresnel)
|
---|
61 | {
|
---|
62 | g_fFresnelFactor = _fresnel;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void RaytraceRenderer::setRefractionIndex(float _refraction)
|
---|
66 | {
|
---|
67 | g_fRefractionIndex = _refraction;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | HRESULT CALLBACK RaytraceRenderer::OnCreateDevice( IDirect3DDevice9* asdf, const D3DSURFACE_DESC* pBackBufferSurfaceDesc )
|
---|
72 | {
|
---|
73 | return S_OK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | IDirect3DTexture9* RaytraceRenderer::CreateTexture( int size, D3DFORMAT Format )
|
---|
77 | {
|
---|
78 | HRESULT hr;
|
---|
79 | IDirect3DTexture9* pTexture;
|
---|
80 | V( this->device->CreateTexture( size, size, 1, D3DUSAGE_RENDERTARGET,
|
---|
81 | Format, D3DPOOL_DEFAULT, &pTexture, NULL ) );
|
---|
82 | return pTexture;
|
---|
83 | }
|
---|
84 |
|
---|
85 | IDirect3DCubeTexture9* RaytraceRenderer::CreateCubeTexture( int size, D3DFORMAT Format )
|
---|
86 | {
|
---|
87 | HRESULT hr;
|
---|
88 | IDirect3DCubeTexture9* pCubeTexture;
|
---|
89 | V( this->device->CreateCubeTexture( size, 1, D3DUSAGE_RENDERTARGET,
|
---|
90 | Format, D3DPOOL_DEFAULT, &pCubeTexture, NULL ) );
|
---|
91 | return pCubeTexture;
|
---|
92 | }
|
---|
93 |
|
---|
94 | IDirect3DSurface9* RaytraceRenderer::CreateDepthStencilSurface( int size )
|
---|
95 | {
|
---|
96 | HRESULT hr;
|
---|
97 | IDirect3DSurface9* pDSSurface;
|
---|
98 | V( this->device->CreateDepthStencilSurface( size, size,
|
---|
99 | DXUTGetDeviceSettings().pp.AutoDepthStencilFormat,
|
---|
100 | D3DMULTISAMPLE_NONE, 0, TRUE, &pDSSurface, NULL ) );
|
---|
101 | return pDSSurface;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | HRESULT RaytraceRenderer::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext ) {
|
---|
106 | this->myScene->manager->printToConsole("on resetdevice in RaytraceRenderer called!");
|
---|
107 |
|
---|
108 | HRESULT hr;
|
---|
109 |
|
---|
110 | if( g_pEffect )
|
---|
111 | V_RETURN( g_pEffect->OnResetDevice() );
|
---|
112 |
|
---|
113 |
|
---|
114 | // Create textures
|
---|
115 | g_pRoomColorDistCubeMapTexture = CreateCubeTexture( CUBEMAP_SIZE, D3DFMT_A16B16G16R16F ); // Because 32 can not use linear filtering.
|
---|
116 | g_pCubeMapDepthStencilSurface = CreateDepthStencilSurface( CUBEMAP_SIZE );
|
---|
117 |
|
---|
118 | g_bInitialization = true;
|
---|
119 | RenderSceneInitialization();
|
---|
120 | return S_OK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void RaytraceRenderer::render()
|
---|
124 | {
|
---|
125 | HRESULT hr;
|
---|
126 | UINT cPass;
|
---|
127 | D3DXMATRIXA16 mScaleLightObjSize, mWorldViewLight, mWorldViewCausticGenerator;
|
---|
128 | switch(this->myScene->getActivePassId()) {
|
---|
129 | case Scene::PASS_NORMAL:
|
---|
130 | case Scene::PASS_RAYTRACE:
|
---|
131 | this->myScene->specialRenderer = this;
|
---|
132 | if ( g_bInitialization )
|
---|
133 | {
|
---|
134 | RenderSceneInitialization();
|
---|
135 | g_bInitialization = false;
|
---|
136 | }
|
---|
137 | this->counter++;
|
---|
138 |
|
---|
139 | if( SUCCEEDED( this->device->BeginScene() ) )
|
---|
140 | {
|
---|
141 | D3DXMATRIX rotMat;
|
---|
142 | D3DXMATRIX invTrans;
|
---|
143 | D3DXMATRIX worldMat;
|
---|
144 | D3DXMATRIX worldViewMat;
|
---|
145 | D3DXMATRIX worldViewProjMat;
|
---|
146 | Vector camPos;
|
---|
147 | Vector nodePos;
|
---|
148 |
|
---|
149 | Node* node = this->myNode.lock().get();
|
---|
150 |
|
---|
151 | //Build world, worldView and worldViewProjection Matrices
|
---|
152 | worldMat = *node->getWorldMatrix();
|
---|
153 | D3DXMatrixMultiply(&worldViewMat, &worldMat, &this->myScene->getViewMatrix());
|
---|
154 | D3DXMatrixMultiply(&worldViewProjMat, &worldViewMat, &this->myScene->getProjectionMatrix());
|
---|
155 |
|
---|
156 | //Build rot and translation Matrix
|
---|
157 | nodePos = node->getAbsolutePosition();
|
---|
158 | D3DXMatrixTranslation(&invTrans, -nodePos.x, -nodePos.y, -nodePos.z);
|
---|
159 | D3DXMatrixMultiply(&rotMat, &worldMat, &invTrans);
|
---|
160 |
|
---|
161 | //Get Camera Postition
|
---|
162 | camPos = this->myScene->getActiveCamera()->getAbsolutePosition();
|
---|
163 | float camPosF[3];
|
---|
164 | camPosF[0] = camPos.x;
|
---|
165 | camPosF[1] = camPos.y;
|
---|
166 | camPosF[2] = camPos.z;
|
---|
167 | //Transfer parameter
|
---|
168 | V( this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW ) );
|
---|
169 | V( g_pEffect->SetTexture( "g_txRoomColorDistanceCubeMap", g_pRoomColorDistCubeMapTexture ) );
|
---|
170 | if(this->myScene->useRaytracer) {
|
---|
171 | V( g_pEffect->SetTechnique( "RefractObject" ) );
|
---|
172 | } else {
|
---|
173 | V( g_pEffect->SetTechnique( "RefractObjectClassic" ) );
|
---|
174 | }
|
---|
175 | V( g_pEffect->SetMatrix( "g_mWorldViewProjection", &worldViewProjMat ) );
|
---|
176 | V( g_pEffect->SetMatrix( "g_mWorldView", &worldViewMat ) );
|
---|
177 |
|
---|
178 | V( g_pEffect->SetMatrix( "g_mWorldCenterObject", &worldMat ) );
|
---|
179 | V( g_pEffect->SetMatrix( "g_mCenterObjectRot", &rotMat) );
|
---|
180 | V( g_pEffect->SetFloatArray( "g_vCameraPos3f", camPosF, 3 ) );
|
---|
181 | V( g_pEffect->SetInt( "g_iNumberOfIteration", g_iNumberOfIteration));
|
---|
182 | V( g_pEffect->SetFloat( "g_fFresnelFactor", g_fFresnelFactor));
|
---|
183 | V( g_pEffect->SetFloat( "g_fRefractionIndex", g_fRefractionIndex ));
|
---|
184 |
|
---|
185 | V( g_pEffect->Begin( &cPass, 0 ) );
|
---|
186 | V( g_pEffect->BeginPass( 0 ) );
|
---|
187 | V( g_pEffect->CommitChanges() );
|
---|
188 | Object3d* obj = (Object3d*) node;
|
---|
189 | for(UINT i = 0; i < obj->Materials.size(); i++) {
|
---|
190 | (*obj->getMesh())->DrawSubset(i);
|
---|
191 | }
|
---|
192 | V( g_pEffect->EndPass() );
|
---|
193 | V( g_pEffect->End() );
|
---|
194 | this->device->EndScene();
|
---|
195 | }
|
---|
196 | this->myScene->specialRenderer = NULL;
|
---|
197 | break;
|
---|
198 | case Scene::PASS_DEPTH:
|
---|
199 | this->renderDepth();
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | void RaytraceRenderer::RenderSceneInitialization()
|
---|
205 | {
|
---|
206 | HRESULT hr;
|
---|
207 |
|
---|
208 | // Bind textures
|
---|
209 | V( g_pEffect->SetTexture( "g_txRoomColorDistanceCubeMap", g_pRoomColorDistCubeMapTexture ) );
|
---|
210 | }
|
---|
211 |
|
---|
212 | void RaytraceRenderer::OnLostDevice(void* pUserContext )
|
---|
213 | {
|
---|
214 | this->myScene->manager->printToConsole("OnLostDevice called from RaytraceRenderer");
|
---|
215 | if( g_pEffect )
|
---|
216 | g_pEffect->OnLostDevice();
|
---|
217 |
|
---|
218 | SAFE_RELEASE( g_pRoomColorDistCubeMapTexture );
|
---|
219 | SAFE_RELEASE( g_pDepthStencilSurface );
|
---|
220 | SAFE_RELEASE( g_pCubeMapDepthStencilSurface );
|
---|
221 | }
|
---|
222 |
|
---|
223 | void RaytraceRenderer::OnDestroyDevice(void* pUserContext )
|
---|
224 | {
|
---|
225 | this->myScene->manager->printToConsole("OnDestroyDevice called from RaytraceRenderer");
|
---|
226 | SAFE_RELEASE( g_pEffect );
|
---|
227 | }
|
---|
228 |
|
---|
229 | void RaytraceRenderer::init() {
|
---|
230 | SPTR<Node> sn = this->myNode.lock();
|
---|
231 | g_pEffect = this->myScene->manager->getEffect(GameManager::EFFECT_RAYTRACE);
|
---|
232 | if(!g_pEffect) {
|
---|
233 | g_pEffect = this->myScene->manager->loadEffect(GameManager::EFFECT_RAYTRACE, L"shaders/RayTraceEffects.obj");
|
---|
234 | }
|
---|
235 |
|
---|
236 | // Create textures
|
---|
237 | g_pRoomColorDistCubeMapTexture = CreateCubeTexture( CUBEMAP_SIZE, D3DFMT_A16B16G16R16F ); // Because 32 can not use linear filtering.
|
---|
238 | g_pCubeMapDepthStencilSurface = CreateDepthStencilSurface( CUBEMAP_SIZE );
|
---|
239 | g_bInitialization = true;
|
---|
240 | RenderSceneInitialization();
|
---|
241 | }
|
---|
242 |
|
---|
243 | void RaytraceRenderer::setCurrentTexture(IDirect3DTexture9* texture) {
|
---|
244 | HRESULT hr;
|
---|
245 | V( g_pEffect->SetTexture( "g_txCurrentTexture", texture) );
|
---|
246 | V( g_pEffect->CommitChanges() );
|
---|
247 | }
|
---|
248 |
|
---|
249 | bool RaytraceRenderer::isLowerThan(Renderer* renderer)
|
---|
250 | {
|
---|
251 | if(this->getRenderPriority() == renderer->getRenderPriority()) {
|
---|
252 | SPTR<Node> node1 = this->myNode.lock();
|
---|
253 | Object3d *myObj = (Object3d *) (node1.get());
|
---|
254 | SPTR<Node> node2 = renderer->myNode.lock();
|
---|
255 | Object3d *otherObj = (Object3d *) (node2.get());
|
---|
256 | return myObj->camDistance>otherObj->camDistance;
|
---|
257 | }
|
---|
258 | return this->getRenderPriority() < renderer->getRenderPriority();
|
---|
259 | }
|
---|
260 |
|
---|
261 | D3DXMATRIX* RaytraceRenderer::getCubeMapProjectionMatrix()
|
---|
262 | {
|
---|
263 | return &this->cubeMapProjectionMat;
|
---|
264 | }
|
---|
265 |
|
---|
266 | D3DXMATRIX RaytraceRenderer::getCubeMapView(int nbView)
|
---|
267 | {
|
---|
268 | D3DXMATRIXA16 invWorldPos;
|
---|
269 | Vector vec = this->myNode.lock()->getAbsolutePosition();
|
---|
270 | D3DXMatrixTranslation(&invWorldPos, -vec.x, -vec.y, -vec.z);
|
---|
271 | return invWorldPos*DXUTGetCubeMapViewMatrix(nbView);
|
---|
272 | }
|
---|
273 |
|
---|
274 | IDirect3DSurface9* RaytraceRenderer::getCubeMapSurface(int nbView)
|
---|
275 | {
|
---|
276 | HRESULT hr;
|
---|
277 | V( g_pRoomColorDistCubeMapTexture->GetCubeMapSurface( (D3DCUBEMAP_FACES)nbView, 0, &this->pCubeMapDistSurf ) );
|
---|
278 | return this->pCubeMapDistSurf;
|
---|
279 | }
|
---|
280 |
|
---|
281 | void RaytraceRenderer::setNumberOfIterations(int _nbIteration)
|
---|
282 | {
|
---|
283 | this->g_iNumberOfIteration = _nbIteration;
|
---|
284 | }
|
---|
285 |
|
---|
286 | bool RaytraceRenderer::needsUpdate()
|
---|
287 | {
|
---|
288 | if(this->nextUpdate==-1000.0f) {
|
---|
289 | //First Time - Render RefractionMap
|
---|
290 | this->renderRefractionMap();
|
---|
291 | this->nextUpdate = 4 + ((((float) rand())/RAND_MAX)-0.5f)*2*2;
|
---|
292 | return true; //First Update
|
---|
293 | } else {
|
---|
294 | this->nextUpdate-= this->myScene->getDeltaTime();
|
---|
295 | if(this->nextUpdate<=0) {
|
---|
296 | this->nextUpdate = 4 + ((((float) rand())/RAND_MAX)-0.5f)*2*2;
|
---|
297 | return true;
|
---|
298 | }
|
---|
299 | return false;
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | void RaytraceRenderer::blurDepthPass(int face) {
|
---|
304 | //In CubeMap steht tiefe
|
---|
305 | /*HRESULT hr;
|
---|
306 | UINT cPass;
|
---|
307 | IDirect3DSurface9* depthMap = this->getCubeMapSurface(face);
|
---|
308 | IDirect3DSurface9* tempDepthMap;
|
---|
309 | V( g_pTempRoomColorDistCubeMapTexture->GetCubeMapSurface( (D3DCUBEMAP_FACES)face, 0, &tempDepthMap ) );
|
---|
310 |
|
---|
311 | //tempTextur mit horizontalem Pass
|
---|
312 | this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
---|
313 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
|
---|
314 | V( this->g_pEffect->SetTexture("g_txTempRoomColorDistanceCubeMap", this->g_pTempRoomColorDistCubeMapTexture) );
|
---|
315 | //V( this->g_pEffect->SetTexture("g_txCurrentTexture", depthMap) );
|
---|
316 | //V( this->g_pEffect->SetInt("face", face) );
|
---|
317 | V( this->g_pEffect->CommitChanges());
|
---|
318 |
|
---|
319 | V( this->device->SetRenderTarget(0, tempDepthMap) );
|
---|
320 | V( this->g_pEffect->SetTechnique("gaussHorizontal") );
|
---|
321 |
|
---|
322 | V( g_pEffect->Begin( &cPass, 0 ) );
|
---|
323 | V( g_pEffect->BeginPass( 0 ) );
|
---|
324 | V( g_pEffect->CommitChanges() );
|
---|
325 | this->myScene->getQuadMesh()->DrawSubset(0);
|
---|
326 | V( g_pEffect->EndPass() );
|
---|
327 | V( g_pEffect->End() );
|
---|
328 |
|
---|
329 | //V( this->g_pEffect->SetTexture("g_txCurrentTexture", tempDepthMap) );
|
---|
330 | V( this->g_pEffect->CommitChanges());
|
---|
331 | V( this->device->SetRenderTarget(0, depthMap) );
|
---|
332 | V( this->g_pEffect->SetTechnique("gaussVertikal") );
|
---|
333 |
|
---|
334 | V( g_pEffect->Begin( &cPass, 0 ) );
|
---|
335 | V( g_pEffect->BeginPass( 0 ) );
|
---|
336 | V( g_pEffect->CommitChanges() );
|
---|
337 | this->myScene->getQuadMesh()->DrawSubset(0);
|
---|
338 | V( g_pEffect->EndPass() );
|
---|
339 | V( g_pEffect->End() );
|
---|
340 |
|
---|
341 | this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
342 | this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
|
---|
343 |
|
---|
344 | SAFE_RELEASE(depthMap);
|
---|
345 | SAFE_RELEASE(tempDepthMap);*/
|
---|
346 | //alte Textur mit vertical pass
|
---|
347 | }
|
---|
348 |
|
---|
349 | void RaytraceRenderer::renderRefractionMap()
|
---|
350 | {
|
---|
351 | //Todo RefractionMap
|
---|
352 | }
|
---|
353 |
|
---|
354 | void RaytraceRenderer::renderDepth() {
|
---|
355 | SPTR<Node> node = this->myNode.lock();
|
---|
356 | Object3d *obj = (Object3d *) (node.get());
|
---|
357 | UINT cPass;
|
---|
358 | HRESULT hr;
|
---|
359 | ID3DXEffect* effect;
|
---|
360 | effect = this->myScene->manager->getEffect(GameManager::EFFECT_DEPTHIMP);
|
---|
361 | if(obj!=NULL && obj->isModelLoaded()) {
|
---|
362 | V( effect->SetMatrix("g_mWorldViewProjection", &this->myScene->activeRenderPass->getWorldViewProjectionMatrix(obj->getWorldMatrix())) );
|
---|
363 | V( effect->SetTechnique("DepthPass") );
|
---|
364 | V( effect->CommitChanges() );
|
---|
365 | this->device->BeginScene();
|
---|
366 | V( effect->Begin( &cPass, 0 ) );
|
---|
367 | V( effect->BeginPass( 0 ) );
|
---|
368 | if (obj->Materials.size() == 0) {
|
---|
369 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
|
---|
370 | this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
|
---|
371 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
|
---|
372 | if(obj->isPMesh()) {
|
---|
373 | (*obj->getProgressiveMesh())->DrawSubset(0);
|
---|
374 | } else {
|
---|
375 | (*obj->getMesh())->DrawSubset(0);
|
---|
376 | }
|
---|
377 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
378 | } else {
|
---|
379 | this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
380 | this->device->SetRenderState( D3DRS_LIGHTING, TRUE );
|
---|
381 | this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
|
---|
382 |
|
---|
383 | // bilinear texture filtering:
|
---|
384 | this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
|
---|
385 | this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
|
---|
386 | this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
|
---|
387 |
|
---|
388 | for(UINT i = 0; i < obj->Materials.size(); i++) {
|
---|
389 | this->device->SetMaterial( &obj->Materials[i] );
|
---|
390 | this->device->SetTexture(0, obj->Textures[i]);
|
---|
391 | if(obj->isPMesh()) {
|
---|
392 | (*obj->getProgressiveMesh())->DrawSubset(i);
|
---|
393 | } else {
|
---|
394 | (*obj->getMesh())->DrawSubset(i);
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | //deactivate textures
|
---|
399 | this->device->SetTexture(0, 0);
|
---|
400 | }
|
---|
401 | V( effect->EndPass() );
|
---|
402 | V( effect->End() );
|
---|
403 | this->device->EndScene();
|
---|
404 | }
|
---|
405 | } |
---|