1 | #include "dxstdafx.h"
|
---|
2 | #include ".\pathmapeffect.h"
|
---|
3 | #include "Material.hpp"
|
---|
4 | #include "TexturedMaterial.h"
|
---|
5 | #include "WoodMaterial.hpp"
|
---|
6 | #include "TriangleMesh.h"
|
---|
7 | #include "Transformed.h"
|
---|
8 | #include "shlwapi.h"
|
---|
9 | #include <queue>
|
---|
10 | #include <fstream>
|
---|
11 |
|
---|
12 | #define TEST_CLUST 0
|
---|
13 | //#define GENERATE_PATH_MAPS
|
---|
14 |
|
---|
15 | //! D3D vector-matrix multiplication
|
---|
16 | D3DVECTOR operator *(const D3DVECTOR& v, const D3DMATRIX& m)
|
---|
17 | {
|
---|
18 | D3DVECTOR r;
|
---|
19 | r.x = v.x * m._11 + v.y * m._21 + v.z * m._31 + m._41;
|
---|
20 | r.y = v.x * m._12 + v.y * m._22 + v.z * m._32 + m._42;
|
---|
21 | r.z = v.x * m._13 + v.y * m._23 + v.z * m._33 + m._43;
|
---|
22 | float h = v.x * m._14 + v.y * m._24 + v.z * m._34 + m._44;
|
---|
23 | if(h > 0.00001f || h < 0.00001f)
|
---|
24 | {
|
---|
25 | r.x /= h;
|
---|
26 | r.y /= h;
|
---|
27 | r.z /= h;
|
---|
28 | }
|
---|
29 | return r;
|
---|
30 | }
|
---|
31 |
|
---|
32 | //! method name strings to be displayed on screen
|
---|
33 | const wchar_t* PathMapEffect::Method::methodNames[10] =
|
---|
34 | {
|
---|
35 | L"PRM",
|
---|
36 | L"PRM texture",
|
---|
37 | L"_",
|
---|
38 | L"_"
|
---|
39 | , NULL, NULL, NULL, NULL, NULL, NULL
|
---|
40 | };
|
---|
41 |
|
---|
42 | //! method constants
|
---|
43 | const PathMapEffect::Method PathMapEffect::Method::PRM(0);
|
---|
44 | const PathMapEffect::Method PathMapEffect::Method::SHOWTEX(1);
|
---|
45 | const PathMapEffect::Method PathMapEffect::Method::LAST(2);
|
---|
46 |
|
---|
47 | //! constructor
|
---|
48 | PathMapEffect::PathMapEffect(LPDIRECT3DDEVICE9 device)
|
---|
49 | :method(Method::PRM)
|
---|
50 | {
|
---|
51 | rayId = 1;
|
---|
52 | this->device = device;
|
---|
53 |
|
---|
54 | //first person camera allows more freedom of movement
|
---|
55 | camera = new CFirstPersonCamera();
|
---|
56 | lightCamera = new CFirstPersonCamera();
|
---|
57 |
|
---|
58 | HRESULT hr;
|
---|
59 | DWORD effectCompileFlag=0;
|
---|
60 |
|
---|
61 | LPD3DXBUFFER compilationErrors;
|
---|
62 | if(FAILED(
|
---|
63 | hr =
|
---|
64 | D3DXCreateEffectFromFile(
|
---|
65 | device,
|
---|
66 | L"pathMap.fx",
|
---|
67 | NULL,
|
---|
68 | NULL,
|
---|
69 | 0,
|
---|
70 | NULL,
|
---|
71 | &effect,
|
---|
72 | &compilationErrors) )){
|
---|
73 | MessageBoxA( NULL, (LPSTR)compilationErrors->GetBufferPointer(), "Failed to load effect file!", MB_OK);
|
---|
74 | exit(-1);
|
---|
75 | }
|
---|
76 |
|
---|
77 | //store buffers so we can reset them after rendering to texture render targets
|
---|
78 | device->GetRenderTarget(0, &frameColorBuffer);
|
---|
79 | device->GetDepthStencilSurface(&frameDepthStencilBuffer);
|
---|
80 |
|
---|
81 | //load empty texture
|
---|
82 | D3DXCreateTextureFromFile(device, L"media\\empty.bmp", &emptyTexture);
|
---|
83 |
|
---|
84 | //set up a scene
|
---|
85 |
|
---|
86 | loadScene("media\\level.txt");
|
---|
87 |
|
---|
88 | //load hangar mesh
|
---|
89 | loadMesh( L"media\\hangar.x" );
|
---|
90 | // loadMesh( L"media\\fulltexchamber.x" );
|
---|
91 |
|
---|
92 | /* //create hangar entity
|
---|
93 | Entity e1;
|
---|
94 | e1.owner = this;
|
---|
95 | e1.renderMesh = renderMeshes.at(0);
|
---|
96 | D3DXMatrixIdentity(&e1.modelWorldTransform);
|
---|
97 | // D3DXMatrixScaling(&e1.modelWorldTransform, 20.0f, 20.0f, 20.0f);
|
---|
98 | D3DXMatrixScaling(&e1.modelWorldTransform, 0.5f, 1.0f, 0.5f);
|
---|
99 | D3DXMatrixIdentity(&e1.inverseTransposedModelWorldTransform);
|
---|
100 | // D3DXMatrixScaling(&e1.inverseTransposedModelWorldTransform, 1.0f/20.0f, 1.0f/20.0f, 1.0f/20.0f);
|
---|
101 | D3DXMatrixScaling(&e1.inverseTransposedModelWorldTransform, 1.0f/0.5f, 1.0f, 1.0f/0.5f);
|
---|
102 |
|
---|
103 | e1.createRayTraceEntity();
|
---|
104 |
|
---|
105 | entities.push_back(e1);
|
---|
106 |
|
---|
107 |
|
---|
108 | //load 'steps' mesh
|
---|
109 | loadMesh( L"media\\steps.x" );
|
---|
110 |
|
---|
111 | //create step entities
|
---|
112 | for(int eitor=0; eitor < 4; eitor++)
|
---|
113 | {
|
---|
114 | D3DXMATRIX urr;
|
---|
115 | D3DXMATRIX rx; D3DXMatrixRotationY(&rx, Vector::PI * 0.5f * eitor);
|
---|
116 | Entity e2;
|
---|
117 | e2.owner = this;
|
---|
118 | e2.renderMesh = renderMeshes.at(1);
|
---|
119 |
|
---|
120 | D3DXMatrixTranslation(&urr, -15.0f, -14.0f, -7.0f);
|
---|
121 | D3DXMatrixMultiply( &e2.modelWorldTransform, &urr, &rx);
|
---|
122 | D3DXMatrixInverse(&e2.inverseTransposedModelWorldTransform, NULL, &e2.modelWorldTransform);
|
---|
123 |
|
---|
124 | e2.createRayTraceEntity();
|
---|
125 | entities.push_back(e2);
|
---|
126 |
|
---|
127 | D3DXMATRIX trasi, roti;
|
---|
128 | D3DXMatrixTranslation(&trasi, -15.0f + 7.0f, 14.0f, -7.0f -7.0f); //7.25f, 13.5, -7.25); //rot 90 -90 90 trans 7.25 13.5 -7.25
|
---|
129 | D3DXMatrixRotationYawPitchRoll(&roti, -Vector::PI * 0.5, Vector::PI, 0.0);
|
---|
130 | D3DXMatrixMultiply(&urr, &roti, &trasi);
|
---|
131 |
|
---|
132 | D3DXMatrixMultiply( &e2.modelWorldTransform, &urr, &rx);
|
---|
133 |
|
---|
134 | D3DXMatrixInverse(&e2.inverseTransposedModelWorldTransform, NULL, &e2.modelWorldTransform);
|
---|
135 | e2.createRayTraceEntity();
|
---|
136 | entities.push_back(e2);
|
---|
137 | }
|
---|
138 | */
|
---|
139 | /* for(int eitor=0; eitor < 6; eitor++)
|
---|
140 | {
|
---|
141 | Entity e2;
|
---|
142 | e2.owner = this;
|
---|
143 | e2.renderMesh = renderMeshes.at(1);
|
---|
144 | D3DXMatrixIdentity(&e2.modelWorldTransform);
|
---|
145 | D3DXMatrixScaling(&e2.modelWorldTransform, -0.5, 0.5, -0.5);
|
---|
146 | D3DXMATRIX trmx;
|
---|
147 | D3DXMatrixTranslation(&trmx, -8.0 + eitor * 2.8f, -3.2f, -8.6f + eitor * 0.6f);
|
---|
148 | e2.modelWorldTransform *= trmx;
|
---|
149 | D3DXMatrixIdentity(&e2.inverseTransposedModelWorldTransform);
|
---|
150 | D3DXMatrixScaling(&e2.inverseTransposedModelWorldTransform, -2.0, 2.0, -2.0);
|
---|
151 | D3DXMatrixTranslation(&trmx, +8.0 - eitor * 2.8f, 3.2f, 8.6f - eitor * 0.6f);
|
---|
152 | e2.inverseTransposedModelWorldTransform *= trmx;
|
---|
153 | e2.createRayTraceEntity();
|
---|
154 | entities.push_back(e2);
|
---|
155 | }*/
|
---|
156 |
|
---|
157 | loadMesh( L"media\\torch.x" );
|
---|
158 |
|
---|
159 | //compute the surface area of the complete geometry. useful for random sampling.
|
---|
160 | sumSurfaceArea = 0.0;
|
---|
161 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
162 | while(entityIterator != entities.end())
|
---|
163 | {
|
---|
164 | sumSurfaceArea += entityIterator->rayTraceEntity->getSurfaceArea();
|
---|
165 | entityIterator++;
|
---|
166 | }
|
---|
167 |
|
---|
168 | //initialize camera
|
---|
169 | camera->SetViewParams( &D3DXVECTOR3(0.0f, 0.0f, 4.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f));
|
---|
170 | D3DSURFACE_DESC fbdesc;
|
---|
171 | frameColorBuffer->GetDesc(&fbdesc);
|
---|
172 | camera->SetProjParams( D3DX_PI/2, (float)fbdesc.Width / fbdesc.Height, 0.1f, 300.0f );
|
---|
173 |
|
---|
174 | //create resources for entities
|
---|
175 | createPRMTextures();
|
---|
176 |
|
---|
177 | //set up spotlight
|
---|
178 | lightCamera->SetViewParams( &D3DXVECTOR3(5.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f));
|
---|
179 | lightCamera->SetProjParams( D3DX_PI/1.9, 1.0f, 0.1f, 300.0f );
|
---|
180 |
|
---|
181 | //create ray tracing kd-tree containing ray-traceable versions of entities
|
---|
182 | Intersectable** objs = new Intersectable*[entities.size()];
|
---|
183 | for(int u=0; u<entities.size(); u++)
|
---|
184 | objs[u] = entities.at(u).rayTraceEntity;
|
---|
185 | kdtree = new KDTree(objs, entities.size());
|
---|
186 | delete [] objs;
|
---|
187 |
|
---|
188 | //create dummy render target texture for depth map rendering
|
---|
189 | device->CreateTexture(DEPTHMAPRES, DEPTHMAPRES, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &fakeTexture, NULL);
|
---|
190 | fakeTexture->GetSurfaceLevel(0, &fakeSurface);
|
---|
191 |
|
---|
192 | //create a depthstencil texture for depth map rendering
|
---|
193 | device->CreateTexture(DEPTHMAPRES, DEPTHMAPRES, 1,
|
---|
194 | D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24X8, D3DPOOL_DEFAULT, &depthMapTexture, NULL);
|
---|
195 | depthMapTexture->GetSurfaceLevel(0, &depthMapDepthStencilBuffer);
|
---|
196 |
|
---|
197 | //create a stencil surface for PRM rendering (to avoid blend-adding to the same pixel for the same lightsource muliple times)
|
---|
198 | device->CreateDepthStencilSurface(128 * 32, 128 * NCLUSTERSPERENTITY / 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, true, &prmBlendingDepthStencilBuffer, NULL);
|
---|
199 |
|
---|
200 | #ifdef GENERATE_PATH_MAPS
|
---|
201 | precompute();
|
---|
202 | #else
|
---|
203 | loadPathMaps();
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | //create a texture for radion data (will be used for weight computations on the GPU)
|
---|
207 | device->CreateTexture(2 * NRADIONS / 4096, 4096, 1, 0, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &radionTexture, NULL);
|
---|
208 | radionTexture->GetSurfaceLevel(0, &radionSurface);
|
---|
209 |
|
---|
210 | //fill texture with data
|
---|
211 | uploadRadions();
|
---|
212 |
|
---|
213 | //create weights render target
|
---|
214 | device->CreateTexture(4096, NRADIONS / 4096, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &weightsTexture, NULL);
|
---|
215 | weightsTexture->GetSurfaceLevel(0, &weightsSurface);
|
---|
216 |
|
---|
217 | //create a sytem memory duplicate of the weights render target to be able to read back weights data
|
---|
218 | device->CreateTexture(4096, NRADIONS / 4096, 1, 0, D3DFMT_R32F, D3DPOOL_SYSTEMMEM, &sysMemWeightsTexture, NULL);
|
---|
219 | sysMemWeightsTexture->GetSurfaceLevel(0, &sysMemWeightsSurface);
|
---|
220 |
|
---|
221 | //create a vertex buffer to be able to visualize entry radion positions
|
---|
222 | device->CreateVertexBuffer(NRADIONS * sizeof(float) * 6, D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_NORMAL, D3DPOOL_DEFAULT,
|
---|
223 | &starterVertexBuffer, NULL);
|
---|
224 | void* pData;
|
---|
225 | starterVertexBuffer->Lock(0, 0, &pData, 0);
|
---|
226 | fillRadionPosArray(pData);
|
---|
227 | starterVertexBuffer->Unlock();
|
---|
228 |
|
---|
229 |
|
---|
230 | exportEntityData();
|
---|
231 |
|
---|
232 | #ifdef GENERATE_PATH_MAPS
|
---|
233 | savePathMaps();
|
---|
234 | #endif
|
---|
235 | }
|
---|
236 |
|
---|
237 | PathMapEffect::~PathMapEffect(void)
|
---|
238 | {
|
---|
239 | //release all resources allocated in the constructor
|
---|
240 | starterVertexBuffer->Release();
|
---|
241 |
|
---|
242 | depthMapTexture->Release();
|
---|
243 | depthMapDepthStencilBuffer->Release();
|
---|
244 | fakeTexture->Release();
|
---|
245 | fakeSurface->Release();
|
---|
246 |
|
---|
247 | sysMemWeightsTexture->Release();
|
---|
248 | sysMemWeightsSurface->Release();
|
---|
249 | weightsTexture->Release();
|
---|
250 | weightsSurface->Release();
|
---|
251 | radionTexture->Release();
|
---|
252 | radionSurface->Release();
|
---|
253 |
|
---|
254 | prmBlendingDepthStencilBuffer->Release();
|
---|
255 | delete kdtree;
|
---|
256 | emptyTexture->Release();
|
---|
257 | releaseTextures();
|
---|
258 | releaseMeshes();
|
---|
259 | releaseEntities();
|
---|
260 |
|
---|
261 | frameColorBuffer->Release();
|
---|
262 | frameDepthStencilBuffer->Release();
|
---|
263 |
|
---|
264 | effect->Release();
|
---|
265 | delete camera;
|
---|
266 | delete lightCamera;
|
---|
267 | }
|
---|
268 |
|
---|
269 | void PathMapEffect::loadMesh(LPCWSTR fileName)
|
---|
270 | {
|
---|
271 | RenderMesh* renderMesh = new RenderMesh();
|
---|
272 |
|
---|
273 | if(S_OK !=
|
---|
274 | D3DXLoadMeshFromX( fileName,
|
---|
275 | D3DXMESH_MANAGED,
|
---|
276 | device,
|
---|
277 | NULL,
|
---|
278 | &renderMesh->materialBuffer,
|
---|
279 | NULL,
|
---|
280 | (DWORD*)&renderMesh->nSubsets,
|
---|
281 | &renderMesh->mesh))
|
---|
282 | {
|
---|
283 | MessageBox(NULL, fileName, L"Could not load mesh file!", MB_OK);
|
---|
284 | return;
|
---|
285 | }
|
---|
286 | //convert mesh into a vertex format using a position, a normal and a texture register
|
---|
287 | //this is the only data our vertex shaders will require
|
---|
288 | renderMesh->setVertexFormat(D3DFVF_XYZ |
|
---|
289 | D3DFVF_NORMAL |
|
---|
290 | D3DFVF_TEX1, device);
|
---|
291 |
|
---|
292 | //extract the D3DXMATERIAL* pointer from the generic D3DXBUFFER
|
---|
293 | renderMesh->materials = (D3DXMATERIAL*)renderMesh->materialBuffer->GetBufferPointer();
|
---|
294 |
|
---|
295 | //for every material, load the specified texture
|
---|
296 | renderMesh->textures = new LPDIRECT3DTEXTURE9[renderMesh->nSubsets];
|
---|
297 | for(unsigned int i=0; i<renderMesh->nSubsets; i++)
|
---|
298 | {
|
---|
299 | wchar_t* wideTextureFileName = NULL;
|
---|
300 | if(renderMesh->materials[i].pTextureFilename)
|
---|
301 | {
|
---|
302 | wideTextureFileName = new wchar_t[MultiByteToWideChar(CP_ACP, 0, renderMesh->materials[i].pTextureFilename, -1, NULL, 0)];
|
---|
303 | MultiByteToWideChar(CP_ACP, 0, renderMesh->materials[i].pTextureFilename, -1, wideTextureFileName, 511);
|
---|
304 | }
|
---|
305 | renderMesh->textures[i] = loadTexture( wideTextureFileName );
|
---|
306 | if(wideTextureFileName)
|
---|
307 | delete wideTextureFileName;
|
---|
308 | }
|
---|
309 |
|
---|
310 | //lock the buffers and create ray tracing meshes from the data
|
---|
311 | LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
|
---|
312 | renderMesh->mesh->GetVertexBuffer(&vertexBuffer);
|
---|
313 | D3DVERTEX* vertexData;
|
---|
314 | vertexBuffer->Lock(0,renderMesh->mesh->GetNumVertices()*renderMesh->mesh->GetNumBytesPerVertex(),(void**)&vertexData,0);
|
---|
315 | LPDIRECT3DINDEXBUFFER9 indexBuffer;
|
---|
316 | renderMesh->mesh->GetIndexBuffer(&indexBuffer);
|
---|
317 | unsigned short* indexData;
|
---|
318 | indexBuffer->Lock(0,renderMesh->mesh->GetNumFaces()*3*sizeof(unsigned short),(void**)&indexData,0);
|
---|
319 |
|
---|
320 | //create a ray-tracing Material for the texture
|
---|
321 | if(renderMesh->textures[0])
|
---|
322 | {
|
---|
323 | D3DLOCKED_RECT lrect;
|
---|
324 | HRESULT hr = renderMesh->textures[0]->LockRect(0, &lrect, NULL, D3DLOCK_READONLY);
|
---|
325 | LPDIRECT3DSURFACE9 texsurf;
|
---|
326 | renderMesh->textures[0]->GetSurfaceLevel(0, &texsurf);
|
---|
327 | D3DSURFACE_DESC desc;
|
---|
328 | texsurf->GetDesc(&desc);
|
---|
329 | renderMesh->rayTraceMaterial = new TexturedMaterial(lrect.pBits, lrect.Pitch, desc.Width);
|
---|
330 | texsurf->Release();
|
---|
331 | renderMesh->textures[0]->UnlockRect(0);
|
---|
332 | }
|
---|
333 | else
|
---|
334 | renderMesh->rayTraceMaterial = new Material(Vector(1.0f, 1.0f, 1.0f), Vector(0.0f, 0.0f, 0.0f), 1.0);
|
---|
335 |
|
---|
336 | //create a TriangleMesh for ray-tracing
|
---|
337 | renderMesh->rayTraceMesh = new TriangleMesh(renderMesh->rayTraceMaterial, indexData, renderMesh->mesh->GetNumFaces(),
|
---|
338 | vertexData, renderMesh->mesh->GetNumVertices());
|
---|
339 |
|
---|
340 | vertexBuffer->Unlock();
|
---|
341 | indexBuffer->Unlock();
|
---|
342 | vertexBuffer->Release();
|
---|
343 | indexBuffer->Release();
|
---|
344 |
|
---|
345 | //generate edge lines for seamless texture atlas rendering
|
---|
346 | renderMesh->buildEdgeVertexBuffer(device);
|
---|
347 |
|
---|
348 | //store completed mesh
|
---|
349 | renderMeshes.push_back(renderMesh);
|
---|
350 | }
|
---|
351 |
|
---|
352 | LPDIRECT3DTEXTURE9 PathMapEffect::loadTexture(LPCWSTR fileName)
|
---|
353 | {
|
---|
354 | if(fileName == NULL || wcscmp(fileName, L"") == 0)
|
---|
355 | return NULL;
|
---|
356 | wchar_t* mediaFileName = new wchar_t[wcslen(fileName) + 64];
|
---|
357 | //we assume the x file was in folder .\media
|
---|
358 | wcscpy(mediaFileName, L"media\\");
|
---|
359 | wcscat(mediaFileName, fileName);
|
---|
360 | LPDIRECT3DTEXTURE9 tex;
|
---|
361 |
|
---|
362 | D3DXIMAGE_INFO bobo;
|
---|
363 | if(S_OK !=
|
---|
364 | D3DXCreateTextureFromFileEx(device, mediaFileName, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0,
|
---|
365 | D3DFMT_FROM_FILE, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &bobo, NULL, &tex
|
---|
366 | ))
|
---|
367 | {
|
---|
368 | MessageBox(NULL, mediaFileName, L"Could not load texture file!", MB_OK);
|
---|
369 | return NULL;
|
---|
370 | }
|
---|
371 |
|
---|
372 | materialTextures.push_back(tex);
|
---|
373 | return tex;
|
---|
374 | }
|
---|
375 |
|
---|
376 | void PathMapEffect::releaseTextures()
|
---|
377 | {
|
---|
378 | std::vector<LPDIRECT3DTEXTURE9>::iterator i = materialTextures.begin();
|
---|
379 | while(i != materialTextures.end())
|
---|
380 | {
|
---|
381 | (*i)->Release();
|
---|
382 | i++;
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | void PathMapEffect::releaseMeshes()
|
---|
387 | {
|
---|
388 | std::vector<PathMapEffect::RenderMesh*>::iterator i = renderMeshes.begin();
|
---|
389 | while(i != renderMeshes.end())
|
---|
390 | {
|
---|
391 | (*i)->mesh->Release();
|
---|
392 | (*i)->materialBuffer->Release();
|
---|
393 | delete (*i)->rayTraceMesh;
|
---|
394 | delete (*i)->rayTraceMaterial;
|
---|
395 | if((*i)->edgeVertexBuffer)
|
---|
396 | (*i)->edgeVertexBuffer->Release();
|
---|
397 | delete *i;
|
---|
398 | i++;
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | void PathMapEffect::renderWithPRM()
|
---|
403 | {
|
---|
404 | // first pass: render depth
|
---|
405 |
|
---|
406 | device->SetRenderTarget(0, fakeSurface);
|
---|
407 | device->SetDepthStencilSurface(depthMapDepthStencilBuffer);
|
---|
408 | device->Clear( 0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0 );
|
---|
409 | //no color writes
|
---|
410 | device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
|
---|
411 | //render back faces (avoids Z-fighting, no need for comparison bias)
|
---|
412 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
|
---|
413 |
|
---|
414 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
415 | {
|
---|
416 | effect->SetTechnique("Depth");
|
---|
417 | UINT nPasses;
|
---|
418 | effect->Begin(&nPasses, 0);
|
---|
419 | effect->BeginPass(0);
|
---|
420 | //loop through all entities, all mesh subsets, set proper transformations
|
---|
421 |
|
---|
422 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
423 | while(entityIterator != entities.end())
|
---|
424 | {
|
---|
425 | effect->SetMatrix("modelToProjMatrix",
|
---|
426 | &( entityIterator->modelWorldTransform * *lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix() ) );
|
---|
427 | effect->CommitChanges();
|
---|
428 | unsigned int nSubsets = entityIterator->renderMesh->nSubsets;
|
---|
429 | for(int i = 0; i< nSubsets; i++)
|
---|
430 | {
|
---|
431 | entityIterator->renderMesh->mesh->DrawSubset(i);
|
---|
432 | }
|
---|
433 | entityIterator++;
|
---|
434 | }
|
---|
435 |
|
---|
436 | effect->EndPass();
|
---|
437 | effect->End();
|
---|
438 | device->EndScene();
|
---|
439 | }
|
---|
440 |
|
---|
441 | //reset front face rendering, color writes
|
---|
442 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
443 | HRESULT hr = S_OK;
|
---|
444 | hr = device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA| D3DCOLORWRITEENABLE_BLUE| D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
|
---|
445 |
|
---|
446 | // 2. pass: compute weights
|
---|
447 |
|
---|
448 | // simple parallel computation for all pixels: no 3D, depth, shadows, only a full-texture quad
|
---|
449 | hr = device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
---|
450 | hr = device->SetRenderState(D3DRS_STENCILENABLE, false);
|
---|
451 | hr = device->SetRenderTarget(0,weightsSurface);
|
---|
452 | hr = device->SetDepthStencilSurface(NULL);
|
---|
453 | D3DXVECTOR3 lightPos = *lightCamera->GetEyePt();
|
---|
454 | D3DXVECTOR3 lightDir = *lightCamera->GetWorldAhead();
|
---|
455 | lightDir /= D3DXVec3Length( &lightDir );
|
---|
456 | float ffl = parameters->Get(fLightScale) * 100.0f;
|
---|
457 | D3DXVECTOR3 lightPower(ffl, ffl, ffl);
|
---|
458 |
|
---|
459 | device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
|
---|
460 |
|
---|
461 | if( SUCCEEDED( device->BeginScene() ) ){
|
---|
462 | if( effect != NULL ){
|
---|
463 | D3DXHANDLE hTechnique = NULL;
|
---|
464 | hTechnique = effect->GetTechniqueByName((LPSTR)"ComputeWeights");
|
---|
465 | if(hTechnique==NULL){
|
---|
466 | return;
|
---|
467 | }
|
---|
468 | effect->SetTechnique( hTechnique );
|
---|
469 |
|
---|
470 | UINT nPasses = 0;
|
---|
471 | effect->Begin( &nPasses, 0 );
|
---|
472 | for(UINT j=0;j<nPasses;j++){
|
---|
473 | effect->BeginPass(j);
|
---|
474 | hr = effect->SetTexture("radions", radionTexture);
|
---|
475 | static float opttme[9] = {0.5f, 0.0f, 0.0f,
|
---|
476 | 0.0f, -0.5f, 0.0f,
|
---|
477 | 0.5f + (0.5f / DEPTHMAPRES), 0.5f + (0.5f / DEPTHMAPRES), 1.0f};
|
---|
478 |
|
---|
479 | //set global params
|
---|
480 | effect->SetFloatArray("occProjToTexMatrix", opttme, 9);
|
---|
481 | effect->SetMatrix("occWorldToProjMatrix", &(*lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix()));
|
---|
482 |
|
---|
483 | effect->SetTexture("depthMap", depthMapTexture);
|
---|
484 | hr = effect->SetInt("nRadionColumns", NRADIONS / 4096);
|
---|
485 | hr = effect->SetFloatArray("lightPower", (float*)&lightPower, 3);
|
---|
486 | hr = effect->SetFloatArray("lightPos", (float*)&lightPos, 3);
|
---|
487 | hr = effect->SetFloatArray("lightDir", (float*)&lightDir, 3);
|
---|
488 | effect->CommitChanges();
|
---|
489 | drawFullScreenQuad(device);
|
---|
490 | effect->EndPass();
|
---|
491 | }
|
---|
492 | effect->End();
|
---|
493 | }
|
---|
494 | device->EndScene();
|
---|
495 | }
|
---|
496 |
|
---|
497 | // read the weights back
|
---|
498 |
|
---|
499 | D3DLOCKED_RECT rData;
|
---|
500 | hr = device->GetRenderTargetData(weightsSurface, sysMemWeightsSurface);
|
---|
501 | hr = sysMemWeightsSurface->LockRect(&rData, NULL, D3DLOCK_READONLY);
|
---|
502 | float* allEntryWeights = (float*)rData.pBits;
|
---|
503 |
|
---|
504 | // average weights per cluster
|
---|
505 | float sumWeightsAll = 0.0;
|
---|
506 | unsigned int iRadion = 0;
|
---|
507 | for(int iCluster=0; iCluster < NCLUSTERS; iCluster++)
|
---|
508 | {
|
---|
509 | weights[iCluster] = 0.0;
|
---|
510 | float clusterrad = 0.0;
|
---|
511 | for(int clusterSummer=0; clusterSummer < clusterLenghts[iCluster]; clusterSummer++, iRadion++)
|
---|
512 | {
|
---|
513 | float radrad = bushStarters[iRadion].radiance.sum();
|
---|
514 | weights[iCluster] += allEntryWeights[iRadion] * radrad;
|
---|
515 | clusterrad += radrad;
|
---|
516 | }
|
---|
517 | weights[iCluster] /= clusterrad; //(double)clusterLenghts[iCluster];
|
---|
518 | // if(iCluster != TEST_CLUST)
|
---|
519 | // weights[iCluster] = 0.0;
|
---|
520 | // if(weights[iCluster] > 0.005)
|
---|
521 | // weights[iCluster] = 0.005;
|
---|
522 | sumWeightsAll += weights[iCluster];
|
---|
523 | }
|
---|
524 | sysMemWeightsSurface->UnlockRect();
|
---|
525 |
|
---|
526 | // 3. pass: render scene using weights to combine PRM texture atlases
|
---|
527 |
|
---|
528 |
|
---|
529 | device->SetRenderTarget(0, frameColorBuffer);
|
---|
530 | device->SetDepthStencilSurface(frameDepthStencilBuffer);
|
---|
531 | // use backface culling, depth test
|
---|
532 | hr = device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
533 | hr = device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
534 |
|
---|
535 | device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
|
---|
536 |
|
---|
537 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
538 | {
|
---|
539 | effect->SetTechnique("walk");
|
---|
540 | UINT nPasses;
|
---|
541 | effect->Begin(&nPasses, 0);
|
---|
542 | effect->BeginPass(0);
|
---|
543 |
|
---|
544 | static float opttme[9] = {0.5f, 0.0f, 0.0f,
|
---|
545 | 0.0f, -0.5f, 0.0f,
|
---|
546 | 0.5f + (0.5f / DEPTHMAPRES), 0.5f + (0.5f / DEPTHMAPRES), 1.0f};
|
---|
547 |
|
---|
548 | //set global params
|
---|
549 | effect->SetFloatArray("occProjToTexMatrix", opttme, 9);
|
---|
550 | effect->SetMatrix("occWorldToProjMatrix", &(*lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix()));
|
---|
551 |
|
---|
552 | effect->SetTexture("depthMap", depthMapTexture);
|
---|
553 |
|
---|
554 | //loop through entities
|
---|
555 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
556 | while(entityIterator != entities.end())
|
---|
557 | {
|
---|
558 | //set entity params
|
---|
559 | // nearestIdx -> bushId
|
---|
560 | float weightsa[NCLUSTERSPERENTITY];
|
---|
561 | for(unsigned int y=0; y<NCLUSTERSPERENTITY; y++)
|
---|
562 | weightsa[y] = weights[entityIterator->nearClusterIndices[y]];
|
---|
563 | hr = effect->SetFloatArray("weightsa", weightsa, NCLUSTERSPERENTITY);
|
---|
564 | effect->SetMatrix("modelToWorldMatrix", &entityIterator->modelWorldTransform);
|
---|
565 | effect->SetMatrix("inverseTransposedModelToWorldMatrix", &entityIterator->inverseTransposedModelWorldTransform);
|
---|
566 | D3DXMATRIX t = entityIterator->modelWorldTransform;
|
---|
567 | if(parameters->Get(bLookFromLight))
|
---|
568 | effect->SetMatrix("modelToProjMatrix", &(entityIterator->modelWorldTransform * *lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix() ));
|
---|
569 | else
|
---|
570 | effect->SetMatrix("modelToProjMatrix", &(entityIterator->modelWorldTransform * *camera->GetViewMatrix() * *camera->GetProjMatrix() ));
|
---|
571 |
|
---|
572 | effect->SetTexture("filteredAtlas", entityIterator->prmTexture);
|
---|
573 | hr = effect->SetFloatArray("lightPower", (float*)&lightPower, 3);
|
---|
574 | hr = effect->SetFloatArray("lightPos", (float*)&lightPos, 3);
|
---|
575 | hr = effect->SetFloatArray("lightDir", (float*)&lightDir, 3);
|
---|
576 |
|
---|
577 | unsigned int nSubsets = entityIterator->renderMesh->nSubsets;
|
---|
578 | for(int i = 0; i< nSubsets; i++)
|
---|
579 | {
|
---|
580 | //set subset material texture
|
---|
581 | if(entityIterator->renderMesh->textures[i])
|
---|
582 | effect->SetTexture("brdfMap", entityIterator->renderMesh->textures[i]);
|
---|
583 | else
|
---|
584 | effect->SetTexture("brdfMap", emptyTexture);
|
---|
585 | effect->CommitChanges();
|
---|
586 | entityIterator->renderMesh->mesh->DrawSubset(i);
|
---|
587 | }
|
---|
588 | entityIterator++;
|
---|
589 | }
|
---|
590 | effect->EndPass();
|
---|
591 | effect->End();
|
---|
592 | device->EndScene();
|
---|
593 | }
|
---|
594 |
|
---|
595 | if(!parameters->Get(bLookFromLight))
|
---|
596 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
597 | {
|
---|
598 | effect->SetTechnique("torch");
|
---|
599 | UINT nPasses;
|
---|
600 | effect->Begin(&nPasses, 0);
|
---|
601 | effect->BeginPass(0);
|
---|
602 |
|
---|
603 | effect->SetMatrix("modelToProjMatrix", &(*lightCamera->GetWorldMatrix() * *camera->GetViewMatrix() * *camera->GetProjMatrix() ));
|
---|
604 |
|
---|
605 | effect->CommitChanges();
|
---|
606 | renderMeshes[renderMeshes.size()-1]->mesh->DrawSubset(0);
|
---|
607 |
|
---|
608 | effect->EndPass();
|
---|
609 | effect->End();
|
---|
610 | device->EndScene();
|
---|
611 | }
|
---|
612 |
|
---|
613 | }
|
---|
614 |
|
---|
615 | HRESULT PathMapEffect::RenderMesh::setVertexFormat(DWORD fvf, LPDIRECT3DDEVICE9 device)
|
---|
616 | {
|
---|
617 | LPD3DXMESH tempMesh;
|
---|
618 |
|
---|
619 | //clone the mesh to the appropriate format
|
---|
620 | HRESULT hr = mesh->CloneMeshFVF( mesh->GetOptions(),
|
---|
621 | fvf ,
|
---|
622 | device, &tempMesh );
|
---|
623 | if(hr == S_OK)
|
---|
624 | {
|
---|
625 | DWORD* padj = new DWORD[mesh->GetNumFaces() * 3];
|
---|
626 | mesh->GenerateAdjacency(0.00001f, padj);
|
---|
627 | delete padj;
|
---|
628 | //forget he old mesh
|
---|
629 | mesh->Release();
|
---|
630 | //use the cloned mesh
|
---|
631 | mesh = tempMesh;
|
---|
632 | }
|
---|
633 | return hr;
|
---|
634 | }
|
---|
635 |
|
---|
636 | void PathMapEffect::renderFullScreen(float depth)
|
---|
637 | {
|
---|
638 | float fLeftU = 0.0f, fTopV = 0.0f, fRightU = 1.0f, fBottomV = 1.0f;
|
---|
639 |
|
---|
640 | D3DSURFACE_DESC dtdsdRT;
|
---|
641 | PDIRECT3DSURFACE9 pSurfRT;
|
---|
642 |
|
---|
643 | // Acquire render target width and height
|
---|
644 | device->GetRenderTarget(0, &pSurfRT);
|
---|
645 | pSurfRT->GetDesc(&dtdsdRT);
|
---|
646 | pSurfRT->Release();
|
---|
647 |
|
---|
648 | // Ensure that we're directly mapping texels to pixels by offset by 0.5
|
---|
649 | // For more info see the doc page titled "Directly Mapping Texels to Pixels"
|
---|
650 | FLOAT fWidth5 = (FLOAT)dtdsdRT.Width - 0.5f;
|
---|
651 | FLOAT fHeight5 = (FLOAT)dtdsdRT.Height - 0.5f;
|
---|
652 |
|
---|
653 | // Draw the quad
|
---|
654 | struct D3DVERTEX{
|
---|
655 | D3DXVECTOR3 pos;
|
---|
656 | D3DXVECTOR2 tex0;
|
---|
657 | };
|
---|
658 |
|
---|
659 | D3DVERTEX svQuad[4];
|
---|
660 |
|
---|
661 | svQuad[0].pos=D3DXVECTOR3(-1, 1, depth);
|
---|
662 | svQuad[0].tex0 = D3DXVECTOR2(fLeftU, fTopV);
|
---|
663 |
|
---|
664 | svQuad[1].pos = D3DXVECTOR3(1, 1, depth);
|
---|
665 | svQuad[1].tex0 = D3DXVECTOR2(fRightU, fTopV);
|
---|
666 |
|
---|
667 | svQuad[2].pos = D3DXVECTOR3(-1, -1, depth);
|
---|
668 | svQuad[2].tex0 = D3DXVECTOR2(fLeftU, fBottomV);
|
---|
669 |
|
---|
670 | svQuad[3].pos = D3DXVECTOR3(1, -1, depth);
|
---|
671 | svQuad[3].tex0 = D3DXVECTOR2(fRightU, fBottomV);
|
---|
672 |
|
---|
673 | device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
|
---|
674 | device->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0));
|
---|
675 | device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, svQuad, sizeof(D3DVERTEX));
|
---|
676 | }
|
---|
677 |
|
---|
678 | void PathMapEffect::move(float fElapsedTime)
|
---|
679 | {
|
---|
680 | //apply some rotation to one of the entities
|
---|
681 | if(parameters->Get(bTurbo))
|
---|
682 | {
|
---|
683 | camera->FrameMove(fElapsedTime * 10.0f);
|
---|
684 | lightCamera->FrameMove(fElapsedTime * 10.0f);
|
---|
685 | }
|
---|
686 | else
|
---|
687 | {
|
---|
688 | camera->FrameMove(fElapsedTime);
|
---|
689 | lightCamera->FrameMove(fElapsedTime);
|
---|
690 | }
|
---|
691 | /* D3DXMATRIX rot;
|
---|
692 | D3DXMatrixRotationY(&rot, fElapsedTime * 0.1f);
|
---|
693 | D3DXMatrixMultiply( &entities.at(0).modelWorldTransform,
|
---|
694 | &rot,
|
---|
695 | &entities.at(0).modelWorldTransform);
|
---|
696 | D3DXMatrixInverse( &entities.at(0).inverseTransposedModelWorldTransform, NULL,
|
---|
697 | &entities.at(0).modelWorldTransform);
|
---|
698 | D3DXMatrixTranspose( &entities.at(0).inverseTransposedModelWorldTransform, &entities.at(0).inverseTransposedModelWorldTransform);
|
---|
699 |
|
---|
700 | if(entities.size() > 1)
|
---|
701 | {
|
---|
702 | D3DXMatrixTranslation(&entities.at(1).modelWorldTransform, 0.0f, parameters->Get(fSecondModelHeight) * 5.0f , 0.0f);
|
---|
703 | D3DXMatrixTranslation(&entities.at(1).inverseTransposedModelWorldTransform, 0.0f, -parameters->Get(fSecondModelHeight) * 5.0f , 0.0f);
|
---|
704 | }*/
|
---|
705 | }
|
---|
706 |
|
---|
707 | LRESULT PathMapEffect::handleMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
---|
708 |
|
---|
709 | {
|
---|
710 | if(parameters->Get(bMoveLight))
|
---|
711 | return lightCamera->HandleMessages(hWnd, uMsg, wParam, lParam);
|
---|
712 | else
|
---|
713 | return camera->HandleMessages(hWnd, uMsg, wParam, lParam);
|
---|
714 | }
|
---|
715 |
|
---|
716 | void PathMapEffect::addUiParameters(Parameters* parameters)
|
---|
717 | {
|
---|
718 | PathMapEffect::parameters = parameters;
|
---|
719 | parameters->Add( bLookFromLight, "Look from light", 1);
|
---|
720 | parameters->Add( bMoveLight, "Move light", 1);
|
---|
721 | parameters->Add( bDots, "Entry points", 1);
|
---|
722 | parameters->Add( bTurbo, "Turbo", 1);
|
---|
723 | parameters->Add( fLightScale, "Tone scale", 100, 'V', 'B', convert100);
|
---|
724 | }
|
---|
725 |
|
---|
726 | void PathMapEffect::setUiParameterDefaults(Parameters* parameters)
|
---|
727 | {
|
---|
728 | }
|
---|
729 |
|
---|
730 | Parameters* PathMapEffect::parameters = NULL;
|
---|
731 |
|
---|
732 | void PathMapEffect::render()
|
---|
733 | {
|
---|
734 | if(method == Method::PRM)
|
---|
735 | renderWithPRM();
|
---|
736 | else
|
---|
737 | showPRMTexture();
|
---|
738 | if(parameters->Get(bDots))
|
---|
739 | {
|
---|
740 | float psf = 8.0f;
|
---|
741 | HRESULT hr = device->SetRenderState(D3DRS_POINTSIZE, *((DWORD*)&psf));
|
---|
742 | hr = device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
743 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
744 | {
|
---|
745 | D3DXHANDLE hTechnique = NULL;
|
---|
746 | hTechnique = effect->GetTechniqueByName((LPSTR)"Dots");
|
---|
747 | effect->SetTechnique( hTechnique );
|
---|
748 | UINT nPasses = 0;
|
---|
749 | effect->Begin( &nPasses, 0 );
|
---|
750 | for(UINT j=0;j<nPasses;j++){
|
---|
751 | effect->BeginPass(j);
|
---|
752 | hr = effect->SetTexture("depthMap",depthMapTexture);
|
---|
753 | if(parameters->Get(bLookFromLight))
|
---|
754 | effect->SetMatrix("worldToProjMatrix", &(*lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix() ));
|
---|
755 | else
|
---|
756 | effect->SetMatrix("worldToProjMatrix", &(*camera->GetViewMatrix() * *camera->GetProjMatrix() ));
|
---|
757 |
|
---|
758 | static float opttme[9] = {0.5f, 0.0f, 0.0f,
|
---|
759 | 0.0f, -0.5f, 0.0f,
|
---|
760 | 0.5f + (0.5f / DEPTHMAPRES), 0.5f + (0.5f / DEPTHMAPRES), 1.0f};
|
---|
761 |
|
---|
762 | effect->SetFloatArray("occProjToTexMatrix", opttme, 9);
|
---|
763 | D3DXVECTOR3 lightPos = *lightCamera->GetEyePt();
|
---|
764 | hr = effect->SetFloatArray("lightPos", (float*)&lightPos, 3);
|
---|
765 | effect->CommitChanges();
|
---|
766 | hr = device->SetFVF(D3DFVF_XYZ | D3DFVF_NORMAL);
|
---|
767 | hr = device->SetStreamSource(0, starterVertexBuffer, 0, sizeof(float) * 6);
|
---|
768 | UINT offset = 0;
|
---|
769 | for(int iClusterDraw=0; iClusterDraw < NCLUSTERS; iClusterDraw++)
|
---|
770 | {
|
---|
771 | hr = effect->SetFloat("aClusterWeight", weights[iClusterDraw] * 10.0);
|
---|
772 | effect->CommitChanges();
|
---|
773 | // if(iClusterDraw == TEST_CLUST)
|
---|
774 | device->DrawPrimitive(D3DPT_POINTLIST, offset, clusterLenghts[iClusterDraw]);
|
---|
775 | offset += clusterLenghts[iClusterDraw];
|
---|
776 | }
|
---|
777 | effect->EndPass();
|
---|
778 | }
|
---|
779 | effect->End();
|
---|
780 | }
|
---|
781 | device->EndScene();
|
---|
782 | }
|
---|
783 | }
|
---|
784 |
|
---|
785 | void PathMapEffect::Entity::createRayTraceEntity()
|
---|
786 | {
|
---|
787 | rayTraceEntity = new Transformed(renderMesh->rayTraceMesh);
|
---|
788 | rayTraceEntity->setTransforms(modelWorldTransform, inverseTransposedModelWorldTransform);
|
---|
789 | }
|
---|
790 |
|
---|
791 | void PathMapEffect::releaseEntities()
|
---|
792 | {
|
---|
793 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
794 | while(entityIterator != entities.end())
|
---|
795 | {
|
---|
796 | delete entityIterator->rayTraceEntity;
|
---|
797 | entityIterator->prmSurface->Release();
|
---|
798 | entityIterator->prmTexture->Release();
|
---|
799 | entityIterator++;
|
---|
800 | }
|
---|
801 | }
|
---|
802 |
|
---|
803 | void PathMapEffect::sampleSphereDirection(Vector& outDir)
|
---|
804 | {
|
---|
805 | do{
|
---|
806 | outDir = Vector(-1.0 + 2.0 * (float)rand() / RAND_MAX, -1.0 + 2.0 * (float)rand() / RAND_MAX, -1.0 + 2.0 * (float)rand() / RAND_MAX);
|
---|
807 | }while(outDir.norm2() > 1.0);
|
---|
808 | outDir.normalize();
|
---|
809 | }
|
---|
810 |
|
---|
811 | void PathMapEffect::sampleShootingDiffuseDirection(int depth, const Vector& normal, Vector& outDir)
|
---|
812 | {
|
---|
813 | Vector u, v;
|
---|
814 | if(fabsf(normal[0]) < 0.9f)
|
---|
815 | {
|
---|
816 | u.set(0.0f, -normal[2], normal[1]);
|
---|
817 | u *= 1.0f / sqrtf(normal[2] * normal[2] + normal[1] * normal[1]);
|
---|
818 | }
|
---|
819 | else
|
---|
820 | {
|
---|
821 | u.set(normal[2], 0.0f, -normal[0]);
|
---|
822 | u *= 1.0f / sqrtf(normal[2] * normal[2] + normal[0] * normal[0]);
|
---|
823 | }
|
---|
824 | v.setCrossProduct(normal, u);
|
---|
825 |
|
---|
826 | float phi = 2.0f * 3.14159265358979323846f * (float)rand() / RAND_MAX;
|
---|
827 | float xi2 = (float)rand() / RAND_MAX;
|
---|
828 | float cosPhi = cos(phi);
|
---|
829 | float sinPhi = sin(phi);
|
---|
830 | float cosTheta = sqrtf(xi2);
|
---|
831 | float sinTheta = sqrtf(1.0f - xi2);
|
---|
832 |
|
---|
833 | outDir.setScaled(sinTheta * cosPhi, v);
|
---|
834 | outDir.addScaled(sinTheta * sinPhi, u);
|
---|
835 | outDir.addScaled(cosTheta, normal);
|
---|
836 | }
|
---|
837 |
|
---|
838 | void PathMapEffect::createPRMTextures()
|
---|
839 | {
|
---|
840 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
841 | while(entityIterator != entities.end())
|
---|
842 | {
|
---|
843 | device->CreateTexture(128 * 32, 128 * NCLUSTERSPERENTITY / 32, 1, D3DUSAGE_RENDERTARGET,D3DFMT_A16B16G16R16F,D3DPOOL_DEFAULT,&entityIterator->prmTexture,NULL);
|
---|
844 | entityIterator->prmTexture->GetSurfaceLevel(0,&entityIterator->prmSurface);
|
---|
845 | entityIterator++;
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | void PathMapEffect::precompute()
|
---|
850 | {
|
---|
851 | // generate entry points
|
---|
852 | for(unsigned int cRad=0; cRad < NRADIONS; cRad++)
|
---|
853 | {
|
---|
854 | Radion starter;
|
---|
855 | sampleSurfaceRadion(starter);
|
---|
856 | //sampleSurfaceRadionUniform(starter);
|
---|
857 | bushStarters.push_back(starter);
|
---|
858 | }
|
---|
859 |
|
---|
860 | // sort entry radions into clusters
|
---|
861 | Radion* entryArray = (Radion*)&*bushStarters.begin();
|
---|
862 | clusterRadions(entryArray, bushStarters.size(), 0);
|
---|
863 | clusterRadionsKMeans();
|
---|
864 |
|
---|
865 | // for every entity, find the most relevant clusters
|
---|
866 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
867 | while(entityIterator != entities.end())
|
---|
868 | {
|
---|
869 | entityIterator->findNearClusters(bushStarters, NCLUSTERS);
|
---|
870 | entityIterator++;
|
---|
871 | }
|
---|
872 |
|
---|
873 | // for every entry radion, shoot a bush of radions, and add their
|
---|
874 | // contributions to all PRMs of all entities
|
---|
875 | unsigned int iCluster = 0;
|
---|
876 | std::vector<Radion> bushRadions;
|
---|
877 | for(int iStarter = 0; iStarter < NRADIONS; )
|
---|
878 | {
|
---|
879 | bushRadions.clear();
|
---|
880 | for(int iric=0; iric < clusterLenghts[iCluster]; iric++, iStarter++)
|
---|
881 | shootRadionBush(bushStarters[iStarter], bushRadions);
|
---|
882 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
883 | while(entityIterator != entities.end())
|
---|
884 | {
|
---|
885 | entityIterator->renderPRM(device, effect, bushRadions, iCluster, 1.0);
|
---|
886 | entityIterator++;
|
---|
887 | }
|
---|
888 | iCluster++;
|
---|
889 | }
|
---|
890 | }
|
---|
891 |
|
---|
892 | int compareX(const void* a, const void* b)
|
---|
893 | {
|
---|
894 | if( ((const Radion*)a)->position.x < ((const Radion*)b)->position.x )
|
---|
895 | return -1;
|
---|
896 | else
|
---|
897 | return 1;
|
---|
898 | }
|
---|
899 |
|
---|
900 | int compareY(const void* a, const void* b)
|
---|
901 | {
|
---|
902 | if( ((const Radion*)a)->position.y < ((const Radion*)b)->position.y )
|
---|
903 | return -1;
|
---|
904 | else
|
---|
905 | return 1;
|
---|
906 | }
|
---|
907 |
|
---|
908 | int compareZ(const void* a, const void* b)
|
---|
909 | {
|
---|
910 | if( ((const Radion*)a)->position.z < ((const Radion*)b)->position.z )
|
---|
911 | return -1;
|
---|
912 | else
|
---|
913 | return 1;
|
---|
914 | }
|
---|
915 |
|
---|
916 | int compareDirX(const void* a, const void* b)
|
---|
917 | {
|
---|
918 | if( ((const Radion*)a)->normal.x < ((const Radion*)b)->normal.x )
|
---|
919 | return -1;
|
---|
920 | else
|
---|
921 | return 1;
|
---|
922 | }
|
---|
923 |
|
---|
924 | int compareDirY(const void* a, const void* b)
|
---|
925 | {
|
---|
926 | if( ((const Radion*)a)->normal.y < ((const Radion*)b)->normal.y )
|
---|
927 | return -1;
|
---|
928 | else
|
---|
929 | return 1;
|
---|
930 | }
|
---|
931 |
|
---|
932 | int compareDirZ(const void* a, const void* b)
|
---|
933 | {
|
---|
934 | if( ((const Radion*)a)->normal.z < ((const Radion*)b)->normal.z )
|
---|
935 | return -1;
|
---|
936 | else
|
---|
937 | return 1;
|
---|
938 | }
|
---|
939 |
|
---|
940 |
|
---|
941 | void PathMapEffect::clusterRadionsKMeans()
|
---|
942 | {
|
---|
943 | Radion centroids[NCLUSTERS];
|
---|
944 | std::vector<Radion> clusters[NCLUSTERS];
|
---|
945 | //initial clusters: uniform length
|
---|
946 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
947 | clusterLenghts[i] = NRADIONS / NCLUSTERS;
|
---|
948 | for(unsigned int iKMeans = 0; iKMeans < 128; iKMeans++)
|
---|
949 | {
|
---|
950 | //find centroids
|
---|
951 | unsigned int iRadion = 0;
|
---|
952 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
953 | {
|
---|
954 | centroids[i].position = Vector::RGBBLACK;
|
---|
955 | centroids[i].normal = Vector::RGBBLACK;
|
---|
956 | for(unsigned int iRadionInCluster=0; iRadionInCluster < clusterLenghts[i]; iRadionInCluster++, iRadion++)
|
---|
957 | {
|
---|
958 | centroids[i].position += bushStarters.at(iRadion).position;
|
---|
959 | centroids[i].normal += bushStarters.at(iRadion).normal;
|
---|
960 | }
|
---|
961 | centroids[i].position *= 1.0f / (float)clusterLenghts[i];
|
---|
962 | centroids[i].normal.normalize();
|
---|
963 | }
|
---|
964 |
|
---|
965 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
966 | clusters[i].clear();
|
---|
967 |
|
---|
968 | //sort radions to centroids
|
---|
969 | iRadion = 0;
|
---|
970 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
971 | {
|
---|
972 | for(unsigned int iRadionInCluster=0; iRadionInCluster < clusterLenghts[i]; iRadionInCluster++, iRadion++)
|
---|
973 | {
|
---|
974 | unsigned int minimumDistanceClusterIndex = 0;
|
---|
975 | float minimumDistance = FLT_MAX;
|
---|
976 | for(unsigned int u=0; u<NCLUSTERS; u++)
|
---|
977 | {
|
---|
978 | float dirDist = (bushStarters.at(iRadion).normal * centroids[u].normal);
|
---|
979 | float dist = (bushStarters.at(iRadion).position - centroids[u].position).norm() * pow(6.0, 1.0 - (double)dirDist);
|
---|
980 | if(dist < minimumDistance)
|
---|
981 | {
|
---|
982 | minimumDistanceClusterIndex = u;
|
---|
983 | minimumDistance = dist;
|
---|
984 | }
|
---|
985 | }
|
---|
986 | clusters[minimumDistanceClusterIndex].push_back( bushStarters.at(iRadion) );
|
---|
987 | }
|
---|
988 | }
|
---|
989 | //refill bushStarters, set cluster lengths
|
---|
990 | iRadion = 0;
|
---|
991 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
992 | {
|
---|
993 | clusterLenghts[i] = clusters[i].size();
|
---|
994 | for(unsigned int iRadionInCluster=0; iRadionInCluster < clusterLenghts[i]; iRadionInCluster++, iRadion++)
|
---|
995 | {
|
---|
996 | bushStarters.at(iRadion) = clusters[i].at(iRadionInCluster);
|
---|
997 | }
|
---|
998 | }
|
---|
999 | //eliminate empty clusters
|
---|
1000 | for(unsigned int i=1; i<NCLUSTERS; i++)
|
---|
1001 | {
|
---|
1002 | if(clusterLenghts[i] == 0)
|
---|
1003 | {
|
---|
1004 | clusterLenghts[i] = clusterLenghts[i-1] >> 1;
|
---|
1005 | clusterLenghts[i-1] -= clusterLenghts[i-1] >> 1;
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 | for(int i=NCLUSTERS - 1; i >= 0; i--)
|
---|
1009 | {
|
---|
1010 | if(clusterLenghts[i] == 0)
|
---|
1011 | {
|
---|
1012 | clusterLenghts[i] = clusterLenghts[i+1] >> 1;
|
---|
1013 | clusterLenghts[i+1] -= clusterLenghts[i+1] >> 1;
|
---|
1014 | }
|
---|
1015 | }
|
---|
1016 | }
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | int PathMapEffect::clusterRadions(Radion* partition, int psize, char axis)
|
---|
1020 | {
|
---|
1021 | /* if(psize < 2)
|
---|
1022 | return 1;
|
---|
1023 | if(axis == 0)
|
---|
1024 | qsort(partition, psize, sizeof(Radion), compareDirX);
|
---|
1025 | else if(axis == 1)
|
---|
1026 | qsort(partition, psize, sizeof(Radion), compareDirY);
|
---|
1027 | else if(axis == 2)
|
---|
1028 | qsort(partition, psize, sizeof(Radion), compareDirZ);
|
---|
1029 | else if(axis == 3)
|
---|
1030 | qsort(partition, psize, sizeof(Radion), compareX);
|
---|
1031 | else if(axis == 4)
|
---|
1032 | qsort(partition, psize, sizeof(Radion), compareY);
|
---|
1033 | else if(axis == 5)
|
---|
1034 | qsort(partition, psize, sizeof(Radion), compareZ);
|
---|
1035 | clusterRadions(partition, psize >> 1, (axis+1)%6);
|
---|
1036 | clusterRadions(partition + (psize >> 1), psize - (psize >> 1), (axis+1)%6);
|
---|
1037 | return 1;
|
---|
1038 | /*/ if(psize < 2)
|
---|
1039 | return 1;
|
---|
1040 | if(axis == 0)
|
---|
1041 | qsort(partition, psize, sizeof(Radion), compareX);
|
---|
1042 | else if(axis == 1)
|
---|
1043 | qsort(partition, psize, sizeof(Radion), compareY);
|
---|
1044 | else if(axis == 2)
|
---|
1045 | qsort(partition, psize, sizeof(Radion), compareZ);
|
---|
1046 | clusterRadions(partition, psize >> 1, (axis+1)%3);
|
---|
1047 | clusterRadions(partition + (psize >> 1), psize - (psize >> 1), (axis+1)%3);
|
---|
1048 | return 1;//*/
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 |
|
---|
1052 | void PathMapEffect::shootRadionBush(Radion& starter, std::vector<Radion>& bushRadions)
|
---|
1053 | {
|
---|
1054 | bushRadions.push_back(starter);
|
---|
1055 |
|
---|
1056 | int nPhotonsShotForLight = 1; //branching factor
|
---|
1057 | for(int iPhoton = 0; iPhoton < nPhotonsShotForLight; iPhoton++)
|
---|
1058 | {
|
---|
1059 | unsigned int depth = 0;
|
---|
1060 | ray.id = rayId++;
|
---|
1061 | ray.isShadowRay = false;
|
---|
1062 | ray.origin = starter.position;
|
---|
1063 | Vector power = starter.radiance;
|
---|
1064 | float prob = starter.probability;
|
---|
1065 | prob *= nPhotonsShotForLight;
|
---|
1066 | power *= 1.0f / nPhotonsShotForLight;
|
---|
1067 | sampleShootingDiffuseDirection(depth, starter.normal, ray.dir);
|
---|
1068 |
|
---|
1069 | for(;;depth++)
|
---|
1070 | {
|
---|
1071 | kdtree->traverse(ray, hitRec, 0.0f, FLT_MAX);
|
---|
1072 | if(hitRec.isIntersect)
|
---|
1073 | {
|
---|
1074 | Radion nr;
|
---|
1075 | nr.position = hitRec.point;
|
---|
1076 | nr.normal = hitRec.normal;
|
---|
1077 | nr.radiance = power;
|
---|
1078 | nr.radiance %= hitRec.material->getTextureDiffuseBrdf(hitRec.texUV);
|
---|
1079 | nr.probability = prob;
|
---|
1080 | bushRadions.push_back(nr);
|
---|
1081 | }
|
---|
1082 | else
|
---|
1083 | break;
|
---|
1084 | if(depth >= 3)
|
---|
1085 | break;
|
---|
1086 | float rrRandom = (float)rand() / RAND_MAX;
|
---|
1087 | float diffuseAlbedo = hitRec.material->getTextureDiffuseAlbedo(hitRec.texUV);
|
---|
1088 | float idealAlbedo = hitRec.material->getTextureIdealAlbedo(hitRec.texUV);
|
---|
1089 | float refractiveAlbedo = hitRec.material->getRefractiveAlbedo();
|
---|
1090 | if(rrRandom < diffuseAlbedo)
|
---|
1091 | {
|
---|
1092 | ray.id = rayId++;
|
---|
1093 | ray.isShadowRay = false;
|
---|
1094 | ray.origin = hitRec.point;
|
---|
1095 | power %= hitRec.material->getTextureDiffuseBrdf(hitRec.texUV);
|
---|
1096 | power *= 1.0f / diffuseAlbedo;
|
---|
1097 | prob *= diffuseAlbedo;
|
---|
1098 | sampleShootingDiffuseDirection(depth, hitRec.normal, ray.dir);
|
---|
1099 | }
|
---|
1100 | else
|
---|
1101 | {
|
---|
1102 | rrRandom -= diffuseAlbedo;
|
---|
1103 | if(rrRandom < idealAlbedo)
|
---|
1104 | {
|
---|
1105 | ray.dir.setIdealReflectedDirection(ray.dir, hitRec.normal);
|
---|
1106 | ray.id = rayId++;
|
---|
1107 | ray.isShadowRay = false;
|
---|
1108 | ray.origin = hitRec.point;
|
---|
1109 | power %= hitRec.material->getIdealBrdf();
|
---|
1110 | power *= 1.0f / idealAlbedo;
|
---|
1111 | prob *= idealAlbedo;
|
---|
1112 | }
|
---|
1113 | else
|
---|
1114 | break;
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | void PathMapEffect::sampleSurfaceRadion(Radion& starter)
|
---|
1121 | {
|
---|
1122 | float randa = ((double)rand() / RAND_MAX) * entities.size();
|
---|
1123 | // float randa = ((double)rand() / RAND_MAX) * sumSurfaceArea;
|
---|
1124 | float uptonowSurfaceArea = 0.0;
|
---|
1125 | Entity* e = NULL;
|
---|
1126 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
1127 | while(entityIterator != entities.end())
|
---|
1128 | {
|
---|
1129 | uptonowSurfaceArea += 1.0f;
|
---|
1130 | // uptonowSurfaceArea += entityIterator->rayTraceEntity->getSurfaceArea();
|
---|
1131 | if(uptonowSurfaceArea >= randa)
|
---|
1132 | {
|
---|
1133 | e = &*entityIterator;
|
---|
1134 | break;
|
---|
1135 | }
|
---|
1136 | entityIterator++;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | e->rayTraceEntity->sampleSurface(starter);
|
---|
1140 |
|
---|
1141 | float surfa = starter.radiance.z;
|
---|
1142 | starter.radiance = e->rayTraceEntity->getMaterial()->getTextureDiffuseBrdf(starter.radiance);
|
---|
1143 | starter.radiance *= (surfa / NRADIONS) * entities.size();
|
---|
1144 | // starter.radiance *= sumSurfaceArea / NRADIONS;
|
---|
1145 | starter.probability = (float)NRADIONS / (entities.size() * surfa );
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | void PathMapEffect::sampleSurfaceRadionUniform(Radion& starter)
|
---|
1149 | {
|
---|
1150 | float randa = ((double)rand() / RAND_MAX) * sumSurfaceArea;
|
---|
1151 | float uptonowSurfaceArea = 0.0;
|
---|
1152 | Entity* e = NULL;
|
---|
1153 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
1154 | while(entityIterator != entities.end())
|
---|
1155 | {
|
---|
1156 | uptonowSurfaceArea += entityIterator->rayTraceEntity->getSurfaceArea();
|
---|
1157 | if(uptonowSurfaceArea >= randa)
|
---|
1158 | {
|
---|
1159 | e = &*entityIterator;
|
---|
1160 | break;
|
---|
1161 | }
|
---|
1162 | entityIterator++;
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | e->rayTraceEntity->sampleSurface(starter);
|
---|
1166 |
|
---|
1167 | float surfa = starter.radiance.z;
|
---|
1168 | starter.radiance = e->rayTraceEntity->getMaterial()->getTextureDiffuseBrdf(starter.radiance);
|
---|
1169 | starter.radiance *= sumSurfaceArea / NRADIONS;
|
---|
1170 | starter.probability = NRADIONS / sumSurfaceArea;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 |
|
---|
1174 | void PathMapEffect::Entity::renderPRM(LPDIRECT3DDEVICE9 device,
|
---|
1175 | LPD3DXEFFECT effect,
|
---|
1176 | std::vector<Radion>& bushRadions,
|
---|
1177 | unsigned int bushId,
|
---|
1178 | float scaleFactor
|
---|
1179 | )
|
---|
1180 | {
|
---|
1181 | HRESULT hr; UINT nPasses=1;
|
---|
1182 |
|
---|
1183 | //# bushId -> nearestIdx
|
---|
1184 | int radinx = 0xffffff;
|
---|
1185 | for(int i=0; i<NCLUSTERSPERENTITY; i++)
|
---|
1186 | if(nearClusterIndices[i] == bushId)
|
---|
1187 | radinx = i;
|
---|
1188 | if(radinx == 0xffffff)
|
---|
1189 | return;
|
---|
1190 |
|
---|
1191 | hr = device->SetRenderTarget(0, prmSurface);
|
---|
1192 | int tilex = radinx % 32;
|
---|
1193 | int tiley = radinx / 32;
|
---|
1194 |
|
---|
1195 | D3DVIEWPORT9 vp;
|
---|
1196 | vp.X = tilex * 128; vp.Y = tiley * 128;
|
---|
1197 | vp.Width = vp.Height = 128;
|
---|
1198 | vp.MinZ = 0.0; vp.MaxZ = 1.0;
|
---|
1199 |
|
---|
1200 | //clear everything
|
---|
1201 | device->SetRenderTarget(0, prmSurface);
|
---|
1202 | device->SetDepthStencilSurface(owner->prmBlendingDepthStencilBuffer);
|
---|
1203 | device->SetViewport(&vp);
|
---|
1204 |
|
---|
1205 | device->Clear( 0, NULL, D3DCLEAR_TARGET,
|
---|
1206 | D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0 );
|
---|
1207 |
|
---|
1208 | unsigned int nRadions = bushRadions.size();
|
---|
1209 | for(int ir=0; ir < nRadions; ir++)
|
---|
1210 | {
|
---|
1211 | // 1. pass render depth map for virtual light source
|
---|
1212 | device->SetRenderTarget(0, owner->fakeSurface);
|
---|
1213 | device->SetDepthStencilSurface(owner->depthMapDepthStencilBuffer);
|
---|
1214 |
|
---|
1215 | device->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
|
---|
1216 |
|
---|
1217 | device->Clear( 0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0 );
|
---|
1218 |
|
---|
1219 | CFirstPersonCamera radionCamera;
|
---|
1220 | radionCamera.SetViewParams((D3DXVECTOR3*)&bushRadions[ir].position, (D3DXVECTOR3*)&(bushRadions[ir].position + bushRadions[ir].normal));
|
---|
1221 | radionCamera.SetProjParams( D3DX_PI/1.9, 1.0f, 0.1f, 300.0f );
|
---|
1222 |
|
---|
1223 | // render backfaces to avoid z-fighting
|
---|
1224 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
|
---|
1225 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
1226 | {
|
---|
1227 | effect->SetTechnique("Depth");
|
---|
1228 | UINT nPasses;
|
---|
1229 | effect->Begin(&nPasses, 0);
|
---|
1230 | effect->BeginPass(0);
|
---|
1231 | //loop through all entities, all mesh subsets, set proper transformations
|
---|
1232 |
|
---|
1233 | std::vector<PathMapEffect::Entity>::iterator entityIterator = owner->entities.begin();
|
---|
1234 | while(entityIterator != owner->entities.end())
|
---|
1235 | {
|
---|
1236 | effect->SetMatrix("modelToProjMatrix",
|
---|
1237 | &( entityIterator->modelWorldTransform * *radionCamera.GetViewMatrix() * *radionCamera.GetProjMatrix() ) );
|
---|
1238 | effect->CommitChanges();
|
---|
1239 | unsigned int nSubsets = entityIterator->renderMesh->nSubsets;
|
---|
1240 | for(int i = 0; i< nSubsets; i++)
|
---|
1241 | {
|
---|
1242 | entityIterator->renderMesh->mesh->DrawSubset(i);
|
---|
1243 | }
|
---|
1244 | entityIterator++;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | effect->EndPass();
|
---|
1248 | effect->End();
|
---|
1249 | device->EndScene();
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
1253 | device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA);
|
---|
1254 |
|
---|
1255 | // blend-add contribution of virtual light source to PRM
|
---|
1256 | device->SetRenderTarget(0, prmSurface);
|
---|
1257 | device->SetDepthStencilSurface(owner->prmBlendingDepthStencilBuffer);
|
---|
1258 | device->SetViewport(&vp);
|
---|
1259 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
---|
1260 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
|
---|
1261 | device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
|
---|
1262 | device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
|
---|
1263 | device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
|
---|
1264 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
---|
1265 |
|
---|
1266 | // use stencil to avoid adding contribution twice
|
---|
1267 | HRESULT hr = device->Clear( 0, NULL, D3DCLEAR_STENCIL,
|
---|
1268 | D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0 );
|
---|
1269 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
1270 | {
|
---|
1271 | Radion radion = bushRadions[ir];
|
---|
1272 |
|
---|
1273 | if( effect != NULL ){
|
---|
1274 | D3DXHANDLE hTechnique = effect->GetTechniqueByName((LPSTR)"BushToAtlas");
|
---|
1275 | if(hTechnique==NULL){
|
---|
1276 | return;
|
---|
1277 | }
|
---|
1278 | effect->SetTechnique( hTechnique );
|
---|
1279 |
|
---|
1280 | effect->Begin( &nPasses, 0 );
|
---|
1281 | for(UINT j=0;j<nPasses;j++){
|
---|
1282 | effect->BeginPass(j);
|
---|
1283 | Vector fakejake = bushRadions[ir].position;
|
---|
1284 | fakejake = bushRadions[ir].normal;
|
---|
1285 | fakejake = bushRadions[ir].radiance;
|
---|
1286 | hr=effect->SetFloatArray("lightPos", (float*)&bushRadions[ir].position, 3);
|
---|
1287 | hr=effect->SetFloatArray("lightDir", (float*)&bushRadions[ir].normal, 3);
|
---|
1288 | static float opttme[9] = {0.5f, 0.0f, 0.0f,
|
---|
1289 | 0.0f, -0.5f, 0.0f,
|
---|
1290 | 0.5f + (0.5f / DEPTHMAPRES), 0.5f + (0.5f / DEPTHMAPRES), 1.0f};
|
---|
1291 | effect->SetTexture("depthMap", owner->depthMapTexture);
|
---|
1292 |
|
---|
1293 | Vector bb0 = this->rayTraceEntity->bbox.minPoint;
|
---|
1294 | Vector bb1 = this->rayTraceEntity->bbox.maxPoint;
|
---|
1295 | //effect->SetFloat("cutNearness2", (bb1 - bb0).norm2() / 200.0f);
|
---|
1296 | float nne = 5.0f / bushRadions[ir].probability;
|
---|
1297 | float onne = (bb1 - bb0).norm2() / 200.0f;
|
---|
1298 | effect->SetFloat("cutNearness2", nne);
|
---|
1299 | //effect->SetFloat("cutNearness2", 0.0f);
|
---|
1300 |
|
---|
1301 | //set global params
|
---|
1302 | effect->SetFloatArray("occProjToTexMatrix", opttme, 9);
|
---|
1303 | effect->SetMatrix("occWorldToProjMatrix", &(*radionCamera.GetViewMatrix() * *radionCamera.GetProjMatrix()));
|
---|
1304 | effect->SetMatrix("modelToWorldMatrix", &(this->modelWorldTransform));
|
---|
1305 | effect->SetMatrix("inverseTransposedModelToWorldMatrix", &(this->inverseTransposedModelWorldTransform));
|
---|
1306 | Vector scaledPower = bushRadions[ir].radiance * scaleFactor;
|
---|
1307 | hr=effect->SetFloatArray("lightPower", (float*)&scaledPower, 3);
|
---|
1308 |
|
---|
1309 | hr = device->SetRenderState(D3DRS_STENCILENABLE, true);
|
---|
1310 | hr = device->SetRenderState(D3DRS_STENCILREF, 0xffffffff);
|
---|
1311 | hr = device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
|
---|
1312 | hr = device->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
|
---|
1313 |
|
---|
1314 | effect->CommitChanges();
|
---|
1315 |
|
---|
1316 | renderMesh->mesh->DrawSubset(0);
|
---|
1317 |
|
---|
1318 | hr = device->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_NOTEQUAL);
|
---|
1319 | scaledPower = bushRadions[ir].radiance * 1.0f ;
|
---|
1320 | hr=effect->SetFloatArray("lightPower", (float*)&scaledPower, 3);
|
---|
1321 | effect->CommitChanges();
|
---|
1322 |
|
---|
1323 | if(renderMesh->nEdges)
|
---|
1324 | {
|
---|
1325 | device->SetStreamSource(0, renderMesh->edgeVertexBuffer, 0, sizeof(D3DVERTEX));
|
---|
1326 | device->DrawPrimitive(D3DPT_LINELIST, 0, renderMesh->nEdges);
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | effect->EndPass();
|
---|
1330 | }
|
---|
1331 | effect->End();
|
---|
1332 | }
|
---|
1333 | device->EndScene();
|
---|
1334 | device->SetRenderState(D3DRS_STENCILENABLE, false);
|
---|
1335 | device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
1336 | device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
|
---|
1337 | device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
1338 | }
|
---|
1339 | }
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | void PathMapEffect::drawFullScreenQuad(LPDIRECT3DDEVICE9 device, float depth, float fLeftU, float fTopV, float fRightU, float fBottomV)
|
---|
1343 | {
|
---|
1344 | D3DSURFACE_DESC dtdsdRT;
|
---|
1345 | PDIRECT3DSURFACE9 pSurfRT;
|
---|
1346 |
|
---|
1347 | // Acquire render target width and height
|
---|
1348 | device->GetRenderTarget(0, &pSurfRT);
|
---|
1349 | pSurfRT->GetDesc(&dtdsdRT);
|
---|
1350 | pSurfRT->Release();
|
---|
1351 |
|
---|
1352 | // Ensure that we're directly mapping texels to pixels by offset by 0.5
|
---|
1353 | // For more info see the doc page titled "Directly Mapping Texels to Pixels"
|
---|
1354 | FLOAT fWidth5 = (FLOAT)dtdsdRT.Width - 0.5f;
|
---|
1355 | FLOAT fHeight5 = (FLOAT)dtdsdRT.Height - 0.5f;
|
---|
1356 |
|
---|
1357 | // Draw the quad
|
---|
1358 | D3DVERTEX svQuad[4];
|
---|
1359 |
|
---|
1360 | svQuad[0].pos=D3DXVECTOR3(-1, 1, depth);
|
---|
1361 | svQuad[0].normal=D3DXVECTOR3(0,0,0);
|
---|
1362 | svQuad[0].tex0 = D3DXVECTOR2(fLeftU, fTopV);
|
---|
1363 |
|
---|
1364 | svQuad[1].pos = D3DXVECTOR3(1, 1, depth);
|
---|
1365 | svQuad[1].normal = D3DXVECTOR3(0,0,0);
|
---|
1366 | svQuad[1].tex0 = D3DXVECTOR2(fRightU, fTopV);
|
---|
1367 |
|
---|
1368 | svQuad[2].pos = D3DXVECTOR3(-1, -1, depth);
|
---|
1369 | svQuad[2].normal = D3DXVECTOR3(0,0,0);
|
---|
1370 | svQuad[2].tex0 = D3DXVECTOR2(fLeftU, fBottomV);
|
---|
1371 |
|
---|
1372 | svQuad[3].pos = D3DXVECTOR3(1, -1, depth);
|
---|
1373 | svQuad[3].normal = D3DXVECTOR3(0,0,0);
|
---|
1374 | svQuad[3].tex0 = D3DXVECTOR2(fRightU, fBottomV);
|
---|
1375 |
|
---|
1376 | device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
|
---|
1377 | device->SetFVF(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0));
|
---|
1378 | device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, svQuad, sizeof(D3DVERTEX));
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | void PathMapEffect::showPRMTexture()
|
---|
1382 | {
|
---|
1383 | device->SetRenderTarget(0, frameColorBuffer);
|
---|
1384 | device->SetDepthStencilSurface(frameDepthStencilBuffer);
|
---|
1385 |
|
---|
1386 | device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
|
---|
1387 |
|
---|
1388 | if( SUCCEEDED( device->BeginScene() ) ){
|
---|
1389 | if( effect != NULL ){
|
---|
1390 | D3DXHANDLE hTechnique = effect->GetTechniqueByName((LPSTR)"ShowTex");
|
---|
1391 | if(hTechnique==NULL){
|
---|
1392 | return;
|
---|
1393 | }
|
---|
1394 | effect->SetTechnique( hTechnique );
|
---|
1395 | UINT nPasses = 0;
|
---|
1396 | effect->Begin( &nPasses, 0 );
|
---|
1397 | for(UINT j=0;j<nPasses;j++){
|
---|
1398 | effect->BeginPass(j);
|
---|
1399 |
|
---|
1400 | //loop through entities
|
---|
1401 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
1402 | //while(entityIterator != entities.end())
|
---|
1403 | {
|
---|
1404 | //set entity params
|
---|
1405 | effect->SetMatrix("modelToWorldMatrix", &entityIterator->modelWorldTransform);
|
---|
1406 | effect->SetMatrix("inverseTransposedModelToWorldMatrix", &entityIterator->inverseTransposedModelWorldTransform);
|
---|
1407 | D3DXMATRIX t = entityIterator->modelWorldTransform;
|
---|
1408 | if(parameters->Get(bLookFromLight))
|
---|
1409 | effect->SetMatrix("modelToProjMatrix", &(entityIterator->modelWorldTransform * *lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix() ));
|
---|
1410 | else
|
---|
1411 | effect->SetMatrix("modelToProjMatrix", &(entityIterator->modelWorldTransform * *camera->GetViewMatrix() * *camera->GetProjMatrix() ));
|
---|
1412 |
|
---|
1413 | effect->SetTexture("filteredAtlas", entityIterator->prmTexture);
|
---|
1414 |
|
---|
1415 | unsigned int nSubsets = entityIterator->renderMesh->nSubsets;
|
---|
1416 | for(int i = 0; i< nSubsets; i++)
|
---|
1417 | {
|
---|
1418 | //set subset material texture
|
---|
1419 | effect->SetTexture("texToShow", entityIterator->prmTexture);
|
---|
1420 | effect->CommitChanges();
|
---|
1421 | drawFullScreenQuad(device);
|
---|
1422 | // effect->CommitChanges();
|
---|
1423 | // entityIterator->renderMesh->mesh->DrawSubset(i);
|
---|
1424 | }
|
---|
1425 | entityIterator++;
|
---|
1426 | }
|
---|
1427 | effect->EndPass();
|
---|
1428 | }
|
---|
1429 | effect->End();
|
---|
1430 | }
|
---|
1431 | device->EndScene();
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 | void PathMapEffect::RenderMesh::buildEdgeVertexBuffer(LPDIRECT3DDEVICE9 device)
|
---|
1437 | {
|
---|
1438 | LPD3DXMESH pMesh = this->mesh;
|
---|
1439 | DWORD* pAdj = new DWORD[3 * pMesh->GetNumFaces()];
|
---|
1440 | for(int r=0; r<pMesh->GetNumFaces()*3; r++)
|
---|
1441 | pAdj[r] = 0xffff;
|
---|
1442 | pMesh->ConvertPointRepsToAdjacency(NULL, pAdj);
|
---|
1443 |
|
---|
1444 | LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
|
---|
1445 | pMesh->GetVertexBuffer(&vertexBuffer);
|
---|
1446 | D3DVERTEX* vertexData;
|
---|
1447 | vertexBuffer->Lock(0,pMesh->GetNumVertices()*pMesh->GetNumBytesPerVertex(),(void**)&vertexData,0);
|
---|
1448 | LPDIRECT3DINDEXBUFFER9 indexBuffer;
|
---|
1449 | pMesh->GetIndexBuffer(&indexBuffer);
|
---|
1450 | unsigned short* indexData;
|
---|
1451 | indexBuffer->Lock(0,pMesh->GetNumFaces()*3*sizeof(unsigned short),(void**)&indexData,0);
|
---|
1452 |
|
---|
1453 |
|
---|
1454 | int countEdges = 0;
|
---|
1455 | for(int u=0; u<pMesh->GetNumFaces(); u++)
|
---|
1456 | for(int t=0; t<3; t++)
|
---|
1457 | if(pAdj[u * 3 + t] >= pMesh->GetNumFaces())
|
---|
1458 | countEdges++;
|
---|
1459 |
|
---|
1460 | if(countEdges == 0)
|
---|
1461 | {
|
---|
1462 | edgeVertexBuffer = NULL;
|
---|
1463 | nEdges = 0;
|
---|
1464 | vertexBuffer->Unlock();
|
---|
1465 | indexBuffer->Unlock();
|
---|
1466 | SAFE_RELEASE(vertexBuffer);
|
---|
1467 | SAFE_RELEASE(indexBuffer);
|
---|
1468 | return;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | device->CreateVertexBuffer(2 * countEdges * pMesh->GetNumBytesPerVertex(),
|
---|
1472 | D3DUSAGE_WRITEONLY, pMesh->GetFVF(), D3DPOOL_DEFAULT,
|
---|
1473 | &edgeVertexBuffer, NULL);
|
---|
1474 | D3DVERTEX* pevData;
|
---|
1475 | edgeVertexBuffer->Lock(0, 0, (void**)&pevData, 0);
|
---|
1476 |
|
---|
1477 | int iEdge = 0;
|
---|
1478 | for(int u=0; u<pMesh->GetNumFaces(); u++)
|
---|
1479 | {
|
---|
1480 | for(int t=0; t<3; t++)
|
---|
1481 | {
|
---|
1482 | if(pAdj[u * 3 + t] >= pMesh->GetNumFaces())
|
---|
1483 | {
|
---|
1484 | unsigned short edge0 = indexData[u * 3 + t];
|
---|
1485 | unsigned short edge1 = indexData[u * 3 + (t+1)%3];
|
---|
1486 | unsigned short inner = indexData[u * 3 + (t+2)%3];
|
---|
1487 |
|
---|
1488 | //push vertex edge0, edge1
|
---|
1489 | pevData[iEdge * 2] = vertexData[edge0];
|
---|
1490 | pevData[iEdge * 2 + 1] = vertexData[edge1];
|
---|
1491 |
|
---|
1492 | D3DXVECTOR2 ediff = vertexData[edge1].tex0 - vertexData[edge0].tex0;
|
---|
1493 | if(fabsf(ediff.x) > fabsf(ediff.y))
|
---|
1494 | {
|
---|
1495 | if(vertexData[inner].tex0.y <= vertexData[edge0].tex0.y +
|
---|
1496 | (vertexData[inner].tex0.x - vertexData[edge0].tex0.x) *
|
---|
1497 | (vertexData[edge1].tex0.y - vertexData[edge0].tex0.y) /
|
---|
1498 | (vertexData[edge1].tex0.x - vertexData[edge0].tex0.x))
|
---|
1499 | {
|
---|
1500 | pevData[iEdge * 2].tex0.y += 1.0/DBLATLASSIZE;
|
---|
1501 | pevData[iEdge * 2 + 1].tex0.y += 1.0/DBLATLASSIZE;
|
---|
1502 | }
|
---|
1503 | else
|
---|
1504 | {
|
---|
1505 | pevData[iEdge * 2].tex0.y -= 1.0/DBLATLASSIZE;
|
---|
1506 | pevData[iEdge * 2 + 1].tex0.y -= 1.0/DBLATLASSIZE;
|
---|
1507 | }
|
---|
1508 | D3DXVECTOR2 edgeDirUnitExt(1.0f, (vertexData[edge1].tex0.y - vertexData[edge0].tex0.y) /
|
---|
1509 | (vertexData[edge1].tex0.x - vertexData[edge0].tex0.x));
|
---|
1510 | edgeDirUnitExt *= 1.0/ATLASSIZE;
|
---|
1511 | if(vertexData[edge1].tex0.x > vertexData[edge0].tex0.x)
|
---|
1512 | {
|
---|
1513 | pevData[iEdge * 2].tex0 -= edgeDirUnitExt;
|
---|
1514 | pevData[iEdge * 2 + 1].tex0 += edgeDirUnitExt;
|
---|
1515 | }
|
---|
1516 | else
|
---|
1517 | {
|
---|
1518 | pevData[iEdge * 2].tex0 += edgeDirUnitExt;
|
---|
1519 | pevData[iEdge * 2 + 1].tex0 -= edgeDirUnitExt;
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 | else
|
---|
1523 | {
|
---|
1524 | if(vertexData[inner].tex0.x <= vertexData[edge0].tex0.x +
|
---|
1525 | (vertexData[inner].tex0.y - vertexData[edge0].tex0.y) *
|
---|
1526 | (vertexData[edge1].tex0.x - vertexData[edge0].tex0.x) /
|
---|
1527 | (vertexData[edge1].tex0.y - vertexData[edge0].tex0.y))
|
---|
1528 | {
|
---|
1529 | pevData[iEdge * 2].tex0.x += 1.0/DBLATLASSIZE;
|
---|
1530 | pevData[iEdge * 2 + 1].tex0.x += 1.0/DBLATLASSIZE;
|
---|
1531 | }
|
---|
1532 | else
|
---|
1533 | {
|
---|
1534 | pevData[iEdge * 2].tex0.x -= 1.0/DBLATLASSIZE;
|
---|
1535 | pevData[iEdge * 2 + 1].tex0.x -= 1.0/DBLATLASSIZE;
|
---|
1536 | }
|
---|
1537 | D3DXVECTOR2 edgeDirUnitExt((vertexData[edge1].tex0.x - vertexData[edge0].tex0.x) /
|
---|
1538 | (vertexData[edge1].tex0.y - vertexData[edge0].tex0.y), 1.0f);
|
---|
1539 | edgeDirUnitExt *= 1.0/ATLASSIZE;
|
---|
1540 | if(vertexData[edge1].tex0.y > vertexData[edge0].tex0.y)
|
---|
1541 | {
|
---|
1542 | pevData[iEdge * 2].tex0 -= edgeDirUnitExt;
|
---|
1543 | pevData[iEdge * 2 + 1].tex0 += edgeDirUnitExt;
|
---|
1544 | }
|
---|
1545 | else
|
---|
1546 | {
|
---|
1547 | pevData[iEdge * 2].tex0 += edgeDirUnitExt;
|
---|
1548 | pevData[iEdge * 2 + 1].tex0 -= edgeDirUnitExt;
|
---|
1549 | }
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | iEdge++;
|
---|
1553 | }
|
---|
1554 | }
|
---|
1555 | }
|
---|
1556 | nEdges = iEdge;
|
---|
1557 |
|
---|
1558 | edgeVertexBuffer->Unlock();
|
---|
1559 | vertexBuffer->Unlock();
|
---|
1560 | indexBuffer->Unlock();
|
---|
1561 | SAFE_RELEASE(vertexBuffer);
|
---|
1562 | SAFE_RELEASE(indexBuffer);
|
---|
1563 |
|
---|
1564 | delete pAdj;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | void PathMapEffect::uploadRadions()
|
---|
1568 | {
|
---|
1569 | int nRadions = bushStarters.size();
|
---|
1570 | struct TMR{
|
---|
1571 | float pos[4];
|
---|
1572 | float dir[4];
|
---|
1573 | // float pow[4];
|
---|
1574 | };
|
---|
1575 | TMR* tm = new TMR[nRadions];
|
---|
1576 | for(int i=0; i<nRadions; i++)
|
---|
1577 | {
|
---|
1578 | //*
|
---|
1579 | tm[i].pos[0] = bushStarters[i].position[0];
|
---|
1580 | tm[i].pos[1] = bushStarters[i].position[1];
|
---|
1581 | tm[i].pos[2] = bushStarters[i].position[2];
|
---|
1582 | tm[i].pos[3] = 1.0;
|
---|
1583 | tm[i].dir[0] = bushStarters[i].normal[0];
|
---|
1584 | tm[i].dir[1] = bushStarters[i].normal[1];
|
---|
1585 | tm[i].dir[2] = bushStarters[i].normal[2];
|
---|
1586 | tm[i].dir[3] = 1.0;
|
---|
1587 | // tm[i].pow[0] = bushStarters[i].radiance[0];
|
---|
1588 | // tm[i].pow[1] = bushStarters[i].radiance[1];
|
---|
1589 | // tm[i].pow[2] = bushStarters[i].radiance[2];
|
---|
1590 | // tm[i].pow[3] = 1.0;
|
---|
1591 | /*/
|
---|
1592 | tm[i].pos[0] = 0.9;
|
---|
1593 | tm[i].pos[1] = 0.2;
|
---|
1594 | tm[i].pos[2] = 0.2;
|
---|
1595 | tm[i].pos[3] = 1.0;
|
---|
1596 | tm[i].dir[0] = 0.2;
|
---|
1597 | tm[i].dir[1] = 0.9;
|
---|
1598 | tm[i].dir[2] = 0.2;
|
---|
1599 | tm[i].dir[3] = 1.0;
|
---|
1600 | tm[i].pow[0] = 0.2;
|
---|
1601 | tm[i].pow[1] = 0.2;
|
---|
1602 | tm[i].pow[2] = 0.9; //blue
|
---|
1603 | tm[i].pow[3] = 0.99;
|
---|
1604 | //*/
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | RECT imgSize;
|
---|
1608 | imgSize.top = imgSize.left = 0;
|
---|
1609 | imgSize.bottom = nRadions;
|
---|
1610 | imgSize.right = 2 * nRadions / 4096;
|
---|
1611 | D3DXLoadSurfaceFromMemory(radionSurface, NULL, NULL, tm, D3DFMT_A32B32G32R32F,
|
---|
1612 | imgSize.right * 4 * sizeof(float), NULL, &imgSize, D3DX_FILTER_NONE, 0);
|
---|
1613 |
|
---|
1614 | delete [] tm;
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | void PathMapEffect::fillRadionPosArray(void* pData)
|
---|
1618 | {
|
---|
1619 | int nRadions = NRADIONS;
|
---|
1620 | struct TMR{
|
---|
1621 | float pos[3];
|
---|
1622 | float rad[3];
|
---|
1623 | };
|
---|
1624 | TMR* tm = (TMR*)pData;
|
---|
1625 | for(int i=0; i<nRadions; i++)
|
---|
1626 | {
|
---|
1627 | tm[i].pos[0] = bushStarters[i].position[0];
|
---|
1628 | tm[i].pos[1] = bushStarters[i].position[1];
|
---|
1629 | tm[i].pos[2] = bushStarters[i].position[2];
|
---|
1630 | tm[i].rad[0] = bushStarters[i].radiance[0];
|
---|
1631 | tm[i].rad[1] = bushStarters[i].radiance[1];
|
---|
1632 | tm[i].rad[2] = bushStarters[i].radiance[2];
|
---|
1633 | }
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | const wchar_t* PathMapEffect::getWeightsString()
|
---|
1637 | {
|
---|
1638 | static wchar_t ws[512];
|
---|
1639 | ws[0] = 0;
|
---|
1640 | char wan[64];
|
---|
1641 | float sumw = 0.0f;
|
---|
1642 | for(int i=0; i<33; i++)
|
---|
1643 | {
|
---|
1644 | // StrCpy(wan, L"%f ");
|
---|
1645 | if(i == 32)
|
---|
1646 | sprintf(wan, "%.3f ", (double)sumw);
|
---|
1647 | else
|
---|
1648 | {
|
---|
1649 | sumw += weights[i];
|
---|
1650 | sprintf(wan, "%.3f ", (double)weights[i]);
|
---|
1651 | }
|
---|
1652 | wchar_t* wideValue = new wchar_t[MultiByteToWideChar(CP_ACP, 0, wan, -1, NULL, 0)];
|
---|
1653 | MultiByteToWideChar(CP_ACP, 0, wan, -1, wideValue, 511);
|
---|
1654 | StrCatW(ws, wideValue);
|
---|
1655 | delete wideValue;
|
---|
1656 | }
|
---|
1657 | return ws;
|
---|
1658 | }
|
---|
1659 |
|
---|
1660 | struct ClusterDist2{
|
---|
1661 | unsigned int index;
|
---|
1662 | double dist2;
|
---|
1663 | ClusterDist2(unsigned int index, double dist2) {this->index = index; this->dist2 = dist2;}
|
---|
1664 | bool operator<(const ClusterDist2& o) const {return dist2 > o.dist2;}
|
---|
1665 | bool operator>(const ClusterDist2& o) const {return dist2 < o.dist2;}
|
---|
1666 | bool operator<=(const ClusterDist2& o) const {return dist2 >= o.dist2;}
|
---|
1667 | bool operator>=(const ClusterDist2& o) const {return dist2 <= o.dist2;}
|
---|
1668 | };
|
---|
1669 |
|
---|
1670 | void PathMapEffect::Entity::findNearClusters(const std::vector<Radion>& starters, unsigned int nClusters)
|
---|
1671 | {
|
---|
1672 | D3DXVECTOR3 cc = (D3DXVECTOR3&)rayTraceEntity->bbox.getCentre();
|
---|
1673 |
|
---|
1674 | std::priority_queue<ClusterDist2> nearestClusterDists;
|
---|
1675 |
|
---|
1676 | for(unsigned int iCluster = 0; iCluster < NCLUSTERS; iCluster++)
|
---|
1677 | {
|
---|
1678 | unsigned int iRadion = 0;
|
---|
1679 | unsigned int nRadionsPerCluster = owner->clusterLenghts[iCluster];
|
---|
1680 | double aClusterDist2 = 0.0;
|
---|
1681 | for(unsigned int iric=0; iric < nRadionsPerCluster; iric++, iRadion++)
|
---|
1682 | {
|
---|
1683 | aClusterDist2 += (starters[iRadion++].position - cc).norm2();
|
---|
1684 | }
|
---|
1685 | nearestClusterDists.push( ClusterDist2(iCluster, aClusterDist2) );
|
---|
1686 | }
|
---|
1687 | for(unsigned int i=0; i<32; i++)
|
---|
1688 | {
|
---|
1689 | nearClusterIndices[i] = 31 - i;// nearestClusterDists.top().index;
|
---|
1690 | nearestClusterDists.pop();
|
---|
1691 | }
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 |
|
---|
1695 |
|
---|
1696 | void PathMapEffect::savePathMaps()
|
---|
1697 | {
|
---|
1698 | std::fstream entryPointFile("prm\\prmEntryPoints.dat", std::ios::out | std::ios::binary);
|
---|
1699 |
|
---|
1700 | for(unsigned int u=0; u < NRADIONS; u++)
|
---|
1701 | {
|
---|
1702 | entryPointFile << bushStarters.at(u);
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | for(unsigned int c=0; c < NCLUSTERS; c++)
|
---|
1706 | {
|
---|
1707 | entryPointFile.write((char*)&clusterLenghts[c], sizeof(unsigned int));
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | entryPointFile.flush();
|
---|
1711 | entryPointFile.close();
|
---|
1712 |
|
---|
1713 | unsigned int iEntity = 0;
|
---|
1714 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
1715 | while(entityIterator != entities.end())
|
---|
1716 | {
|
---|
1717 | wchar_t prmFileName[256];
|
---|
1718 | // wsprintf(prmFileName, L"prm\\prm_%08d.hdr", iEntity);
|
---|
1719 | MultiByteToWideChar(CP_ACP, 0, entityIterator->prmfilename, -1, prmFileName, 255);
|
---|
1720 |
|
---|
1721 | D3DXSaveSurfaceToFile(prmFileName, D3DXIFF_HDR, entityIterator->prmSurface, NULL, NULL);
|
---|
1722 | entityIterator++;
|
---|
1723 | iEntity++;
|
---|
1724 | }
|
---|
1725 | }
|
---|
1726 |
|
---|
1727 | void PathMapEffect::loadPathMaps()
|
---|
1728 | {
|
---|
1729 | std::fstream entryPointFile("prm\\prmEntryPoints.dat", std::ios::in | std::ios::binary);
|
---|
1730 |
|
---|
1731 | bushStarters.clear();
|
---|
1732 |
|
---|
1733 | for(unsigned int u=0; u < NRADIONS; u++)
|
---|
1734 | {
|
---|
1735 | Radion ri;
|
---|
1736 | entryPointFile >> ri;
|
---|
1737 | bushStarters.push_back(ri);
|
---|
1738 | }
|
---|
1739 |
|
---|
1740 | for(unsigned int c=0; c < NCLUSTERS; c++)
|
---|
1741 | {
|
---|
1742 | entryPointFile.read((char*)&clusterLenghts[c], sizeof(unsigned int));
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | entryPointFile.close();
|
---|
1746 |
|
---|
1747 | unsigned int iEntity = 0;
|
---|
1748 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
1749 | while(entityIterator != entities.end())
|
---|
1750 | {
|
---|
1751 | entityIterator->findNearClusters(bushStarters, NCLUSTERS);
|
---|
1752 |
|
---|
1753 | wchar_t prmFileName[256];
|
---|
1754 | // wsprintf(prmFileName, L"prm\\prm_%08d.hdr", iEntity);
|
---|
1755 | MultiByteToWideChar(CP_ACP, 0, entityIterator->prmfilename, -1, prmFileName, 255);
|
---|
1756 | D3DXLoadSurfaceFromFile(entityIterator->prmSurface, NULL, NULL, prmFileName, NULL, D3DX_DEFAULT, 0, NULL);
|
---|
1757 | entityIterator++;
|
---|
1758 | iEntity++;
|
---|
1759 | }
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | void PathMapEffect::exportEntityData()
|
---|
1763 | {
|
---|
1764 | std::fstream entryPointFile("media\\entityPathMapData.txt", std::ios::out);
|
---|
1765 |
|
---|
1766 | unsigned int iEntity = 0;
|
---|
1767 | std::vector<PathMapEffect::Entity>::iterator entityIterator = entities.begin();
|
---|
1768 | while(entityIterator != entities.end())
|
---|
1769 | {
|
---|
1770 | entryPointFile << "entity " << entityIterator->name << '\n';
|
---|
1771 | entryPointFile << "{" << '\n';
|
---|
1772 | entryPointFile << " pathmapfile " << entityIterator->prmfilename << '\n';
|
---|
1773 | entryPointFile << " clusters ";
|
---|
1774 | for(int i=0; i<NCLUSTERSPERENTITY; i++)
|
---|
1775 | entryPointFile << entityIterator->nearClusterIndices[i] << " ";
|
---|
1776 | entryPointFile << '\n';
|
---|
1777 | entryPointFile << "}" << '\n';
|
---|
1778 | entryPointFile << '\n';
|
---|
1779 |
|
---|
1780 | // for(int i=0; i<16; i++)
|
---|
1781 | // entryPointFile << entityIterator->modelWorldTransform.m[i%4][i/4] << " ";
|
---|
1782 | entryPointFile << '\n';
|
---|
1783 | entityIterator++;
|
---|
1784 | iEntity++;
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | entryPointFile.flush();
|
---|
1788 | entryPointFile.close();
|
---|
1789 |
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | void PathMapEffect::loadScene(const char* sceneFileName)
|
---|
1793 | {
|
---|
1794 | std::fstream sceneFile(sceneFileName, std::ios::in);
|
---|
1795 |
|
---|
1796 | char keyword[256];
|
---|
1797 | while(!sceneFile.eof())
|
---|
1798 | {
|
---|
1799 | sceneFile >> keyword;
|
---|
1800 | if(strcmp(keyword, "mesh") == 0)
|
---|
1801 | {
|
---|
1802 | char meshname[256];
|
---|
1803 | sceneFile >> meshname;
|
---|
1804 | if(strcmp(meshname, "{") == 0)
|
---|
1805 | strcpy(meshname, "mesh");
|
---|
1806 | while(strcmp(keyword, "}") != 0 && !sceneFile.eof())
|
---|
1807 | {
|
---|
1808 | sceneFile >> keyword;
|
---|
1809 | if(strcmp(keyword, "xfile") == 0)
|
---|
1810 | {
|
---|
1811 | sceneFile >> keyword;
|
---|
1812 | wchar_t* wide = NULL;
|
---|
1813 | if(keyword)
|
---|
1814 | {
|
---|
1815 | wide = new wchar_t[MultiByteToWideChar(CP_ACP, 0, keyword, -1, NULL, 0)];
|
---|
1816 | MultiByteToWideChar(CP_ACP, 0, keyword, -1, wide, 511);
|
---|
1817 | }
|
---|
1818 | loadMesh(wide);
|
---|
1819 | strcpy(renderMeshes[renderMeshes.size()-1]->name, meshname);
|
---|
1820 | delete wide;
|
---|
1821 | }
|
---|
1822 | }
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | if(strcmp(keyword, "entity") == 0)
|
---|
1826 | {
|
---|
1827 | Entity e;
|
---|
1828 | e.owner = this;
|
---|
1829 |
|
---|
1830 | char entityname[256];
|
---|
1831 | sceneFile >> entityname;
|
---|
1832 | if(strcmp(entityname, "{") == 0)
|
---|
1833 | strcpy(entityname, "noname");
|
---|
1834 |
|
---|
1835 | strcpy(e.name, entityname);
|
---|
1836 |
|
---|
1837 | while(strcmp(keyword, "}") != 0 && !sceneFile.eof())
|
---|
1838 | {
|
---|
1839 | sceneFile >> keyword;
|
---|
1840 | if(strcmp(keyword, "pathmapfile") == 0)
|
---|
1841 | {
|
---|
1842 | sceneFile >> keyword;
|
---|
1843 | strcpy(e.prmfilename, keyword);
|
---|
1844 | }
|
---|
1845 | if(strcmp(keyword, "mesh") == 0)
|
---|
1846 | {
|
---|
1847 | sceneFile >> keyword;
|
---|
1848 |
|
---|
1849 | std::vector<PathMapEffect::RenderMesh*>::iterator i = renderMeshes.begin();
|
---|
1850 | while(i != renderMeshes.end())
|
---|
1851 | {
|
---|
1852 | if(strcmp((*i)->name, keyword) == 0)
|
---|
1853 | {
|
---|
1854 | e.renderMesh = *i;
|
---|
1855 | break;
|
---|
1856 | }
|
---|
1857 | i++;
|
---|
1858 | }
|
---|
1859 | }
|
---|
1860 | if(strcmp(keyword, "transformation") == 0)
|
---|
1861 | {
|
---|
1862 | for(int i=0; i<16; i++)
|
---|
1863 | sceneFile >> e.modelWorldTransform.m[i%4][i/4];
|
---|
1864 | D3DXMatrixInverse(&e.inverseTransposedModelWorldTransform, NULL, &e.modelWorldTransform);
|
---|
1865 | }
|
---|
1866 | }
|
---|
1867 |
|
---|
1868 | e.createRayTraceEntity();
|
---|
1869 | entities.push_back(e);
|
---|
1870 | }
|
---|
1871 | }
|
---|
1872 | sceneFile.close();
|
---|
1873 | } |
---|