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