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 "Entity.h"
|
---|
9 | #include "DepthRenderStrategy.h"
|
---|
10 | #include "FinalCompositionRenderStrategy.h"
|
---|
11 | #include "SubEntity.h"
|
---|
12 | #include "Entity.h"
|
---|
13 | #include "Mesh.h"
|
---|
14 | #include "L.h"
|
---|
15 | #include "shlwapi.h"
|
---|
16 | #include <queue>
|
---|
17 | #include <fstream>
|
---|
18 |
|
---|
19 | #define MAXNCLUSTERS 512
|
---|
20 | //#define GENERATE_PATH_MAPS
|
---|
21 |
|
---|
22 | //! D3D vector-matrix multiplication
|
---|
23 | D3DVECTOR operator *(const D3DVECTOR& v, const D3DMATRIX& m)
|
---|
24 | {
|
---|
25 | D3DVECTOR r;
|
---|
26 | r.x = v.x * m._11 + v.y * m._21 + v.z * m._31 + m._41;
|
---|
27 | r.y = v.x * m._12 + v.y * m._22 + v.z * m._32 + m._42;
|
---|
28 | r.z = v.x * m._13 + v.y * m._23 + v.z * m._33 + m._43;
|
---|
29 | float h = v.x * m._14 + v.y * m._24 + v.z * m._34 + m._44;
|
---|
30 | if(h > 0.00001f || h < 0.00001f)
|
---|
31 | {
|
---|
32 | r.x /= h;
|
---|
33 | r.y /= h;
|
---|
34 | r.z /= h;
|
---|
35 | }
|
---|
36 | return r;
|
---|
37 | }
|
---|
38 |
|
---|
39 | D3DXVECTOR3 operator *(const D3DXVECTOR3& v, const D3DXMATRIX& m)
|
---|
40 | {
|
---|
41 | D3DXVECTOR3 r;
|
---|
42 | r.x = v.x * m._11 + v.y * m._21 + v.z * m._31 + m._41;
|
---|
43 | r.y = v.x * m._12 + v.y * m._22 + v.z * m._32 + m._42;
|
---|
44 | r.z = v.x * m._13 + v.y * m._23 + v.z * m._33 + m._43;
|
---|
45 | float h = v.x * m._14 + v.y * m._24 + v.z * m._34 + m._44;
|
---|
46 | if(h > 0.00001f || h < 0.00001f)
|
---|
47 | {
|
---|
48 | r.x /= h;
|
---|
49 | r.y /= h;
|
---|
50 | r.z /= h;
|
---|
51 | }
|
---|
52 | return r;
|
---|
53 | }
|
---|
54 |
|
---|
55 | //! method name strings to be displayed on screen
|
---|
56 | const wchar_t* PathMapEffect::Method::methodNames[10] =
|
---|
57 | {
|
---|
58 | L"PRM",
|
---|
59 | L"PRM texture",
|
---|
60 | L"_",
|
---|
61 | L"_"
|
---|
62 | , NULL, NULL, NULL, NULL, NULL, NULL
|
---|
63 | };
|
---|
64 |
|
---|
65 | //! method constants
|
---|
66 | const PathMapEffect::Method PathMapEffect::Method::PRM(0);
|
---|
67 | const PathMapEffect::Method PathMapEffect::Method::SHOWTEX(1);
|
---|
68 | const PathMapEffect::Method PathMapEffect::Method::LAST(2);
|
---|
69 |
|
---|
70 | //! constructor
|
---|
71 | PathMapEffect::PathMapEffect(LPDIRECT3DDEVICE9 device,
|
---|
72 | char* prmDirectory,
|
---|
73 | char* meshDirectory,
|
---|
74 | char* mediaDirectory,
|
---|
75 | char* levelFileName,
|
---|
76 | char* materialFileName,
|
---|
77 | bool segmentMeshes,
|
---|
78 | bool computePRM,
|
---|
79 | bool uniformSampling,
|
---|
80 | bool atlasGen,
|
---|
81 | unsigned int nEntryPoints,
|
---|
82 | unsigned int nClusters,
|
---|
83 | unsigned int depthMapResolution
|
---|
84 | )
|
---|
85 | :method(Method::PRM)
|
---|
86 | {
|
---|
87 | cruiseLoop = 0;
|
---|
88 | loopPos = 0.0;
|
---|
89 | D3DXMatrixIdentity(&shipPos);
|
---|
90 |
|
---|
91 | testClusterId = 0;
|
---|
92 |
|
---|
93 | NRADIONS = 4096 * (nEntryPoints / 4096);
|
---|
94 | NCLUSTERS = nClusters;
|
---|
95 | DEPTHMAPRES = depthMapResolution;
|
---|
96 | this->SEGMENTMESHES = segmentMeshes;
|
---|
97 |
|
---|
98 | this->UNIFORMSAMPLING = uniformSampling;
|
---|
99 |
|
---|
100 | weights = new float[NCLUSTERS];
|
---|
101 | clusterLenghts = new unsigned int[NCLUSTERS];
|
---|
102 |
|
---|
103 | clusterSweepCurrentIndex = 0;
|
---|
104 |
|
---|
105 | rayId = 1;
|
---|
106 | this->device = device;
|
---|
107 |
|
---|
108 | //first person camera allows more freedom of movement
|
---|
109 | camera = new CFirstPersonCamera();
|
---|
110 | lightCamera = new CFirstPersonCamera();
|
---|
111 |
|
---|
112 | HRESULT hr;
|
---|
113 | DWORD effectCompileFlag=0;
|
---|
114 |
|
---|
115 | LPD3DXBUFFER compilationErrors;
|
---|
116 | if(FAILED(
|
---|
117 | hr =
|
---|
118 | D3DXCreateEffectFromFile(
|
---|
119 | device,
|
---|
120 | L"pathMap.fx",
|
---|
121 | NULL,
|
---|
122 | NULL,
|
---|
123 | 0,
|
---|
124 | NULL,
|
---|
125 | &effect,
|
---|
126 | &compilationErrors) )){
|
---|
127 | MessageBoxA( NULL, (LPSTR)compilationErrors->GetBufferPointer(), "Failed to load effect file!", MB_OK);
|
---|
128 | exit(-1);
|
---|
129 | }
|
---|
130 |
|
---|
131 | //store buffers so we can reset them after rendering to texture render targets
|
---|
132 | device->GetRenderTarget(0, &frameColorBuffer);
|
---|
133 | device->GetDepthStencilSurface(&frameDepthStencilBuffer);
|
---|
134 |
|
---|
135 | wcscpy(this->mediaDirectory, L::l+mediaDirectory);
|
---|
136 | wcscpy(this->meshDirectory, L::l+meshDirectory);
|
---|
137 | wcscpy(this->prmDirectory, L::l+prmDirectory);
|
---|
138 |
|
---|
139 | //load empty texture
|
---|
140 | wcscpy(this->levelFileName, L::l+mediaDirectory);
|
---|
141 | wcscat(this->levelFileName, L"\\");
|
---|
142 | wcscat(this->levelFileName, L::l+levelFileName);
|
---|
143 |
|
---|
144 | wcscpy(this->materialFileName, L::l+mediaDirectory);
|
---|
145 | wcscat(this->materialFileName, L"\\");
|
---|
146 | wcscat(this->materialFileName, L::l+materialFileName);
|
---|
147 |
|
---|
148 | wchar_t emptyTextureName[256];
|
---|
149 | wcscpy(emptyTextureName, L::l+mediaDirectory);
|
---|
150 | wcscat(emptyTextureName, L"\\");
|
---|
151 | wcscat(emptyTextureName, L"empty.bmp");
|
---|
152 |
|
---|
153 | D3DXCreateTextureFromFile(device, emptyTextureName, &emptyTexture);
|
---|
154 |
|
---|
155 | D3DXCreateTextureFromFile(device, L"media\\fighter.jpg", &shipBrdfTexture);
|
---|
156 | D3DXLoadMeshFromX(L"media\\fighter.x",
|
---|
157 | D3DXMESH_MANAGED,
|
---|
158 | device,
|
---|
159 | NULL, //adjacency
|
---|
160 | NULL, //material
|
---|
161 | NULL, //shader
|
---|
162 | NULL,
|
---|
163 | &shipMesh);
|
---|
164 |
|
---|
165 | //set up a scene
|
---|
166 |
|
---|
167 | xMaterials = XMLNode::openFileHelper(this->materialFileName, L"materials");
|
---|
168 | // loadScene("media\\space.txt");
|
---|
169 | loadScene(LC::c-this->levelFileName);
|
---|
170 |
|
---|
171 | // loadMesh( L"media\\fighter.x", 0, "", 1);
|
---|
172 |
|
---|
173 | //compute the surface area of the complete geometry. useful for random sampling.
|
---|
174 | sumSurfaceArea = 0.0;
|
---|
175 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
176 | while(entityIterator != entities.end())
|
---|
177 | {
|
---|
178 | sumSurfaceArea += (*entityIterator)->getSurfaceArea();
|
---|
179 | entityIterator++;
|
---|
180 | }
|
---|
181 |
|
---|
182 | //initialize camera
|
---|
183 | // camera->SetViewParams( &D3DXVECTOR3(0.0f, 0.0f, 4.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f));
|
---|
184 | camera->SetViewParams( &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 0.0f, 1.0f));
|
---|
185 | D3DSURFACE_DESC fbdesc;
|
---|
186 | frameColorBuffer->GetDesc(&fbdesc);
|
---|
187 | camera->SetProjParams( D3DX_PI/2, (float)fbdesc.Width / fbdesc.Height, 0.1f, 300.0f );
|
---|
188 |
|
---|
189 | //set up spotlight
|
---|
190 | lightCamera->SetViewParams( &D3DXVECTOR3(5.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f));
|
---|
191 | lightCamera->SetProjParams( D3DX_PI/1.9, 1.0f, 0.1f, 300.0f );
|
---|
192 |
|
---|
193 | //create ray tracing kd-tree containing ray-traceable versions of entities
|
---|
194 | {
|
---|
195 | std::vector<Intersectable*> rayTraceEntities;
|
---|
196 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
197 | while(entityIterator != entities.end())
|
---|
198 | {
|
---|
199 | (*entityIterator)->gatherRayTraceEntities(rayTraceEntities);
|
---|
200 | entityIterator++;
|
---|
201 | }
|
---|
202 | Intersectable** objs = (Intersectable**)&*rayTraceEntities.begin();
|
---|
203 | kdtree = new KDTree(objs, rayTraceEntities.size());
|
---|
204 | }
|
---|
205 |
|
---|
206 | //create dummy render target texture for depth map rendering
|
---|
207 | device->CreateTexture(DEPTHMAPRES, DEPTHMAPRES, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &fakeTexture, NULL);
|
---|
208 | fakeTexture->GetSurfaceLevel(0, &fakeSurface);
|
---|
209 |
|
---|
210 | //create a depthstencil texture for depth map rendering
|
---|
211 | device->CreateTexture(DEPTHMAPRES, DEPTHMAPRES, 1,
|
---|
212 | D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24X8, D3DPOOL_DEFAULT, &depthMapTexture, NULL);
|
---|
213 | depthMapTexture->GetSurfaceLevel(0, &depthMapDepthStencilBuffer);
|
---|
214 |
|
---|
215 | if(computePRM)
|
---|
216 | precompute();
|
---|
217 | else
|
---|
218 | {
|
---|
219 | loadPathMaps();
|
---|
220 | }
|
---|
221 |
|
---|
222 | if(computePRM || segmentMeshes)
|
---|
223 | saveScene("processedMeshes\\processed.level");
|
---|
224 |
|
---|
225 | //create a texture for radion data (will be used for weight computations on the GPU)
|
---|
226 | device->CreateTexture(2 * NRADIONS / 4096, 4096, 1, 0, D3DFMT_A32B32G32R32F, D3DPOOL_DEFAULT, &radionTexture, NULL);
|
---|
227 | radionTexture->GetSurfaceLevel(0, &radionSurface);
|
---|
228 |
|
---|
229 | //fill texture with data
|
---|
230 | uploadRadions();
|
---|
231 |
|
---|
232 | //create weights render target
|
---|
233 | device->CreateTexture(4096, NRADIONS / 4096, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &weightsTexture, NULL);
|
---|
234 | weightsTexture->GetSurfaceLevel(0, &weightsSurface);
|
---|
235 |
|
---|
236 | //create a sytem memory duplicate of the weights render target to be able to read back weights data
|
---|
237 | device->CreateTexture(4096, NRADIONS / 4096, 1, 0, D3DFMT_R32F, D3DPOOL_SYSTEMMEM, &sysMemWeightsTexture, NULL);
|
---|
238 | sysMemWeightsTexture->GetSurfaceLevel(0, &sysMemWeightsSurface);
|
---|
239 |
|
---|
240 | //create aggr weights render target
|
---|
241 | device->CreateTexture(MAXNCLUSTERS, 1, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &aggrWeightsTexture, NULL);
|
---|
242 | aggrWeightsTexture->GetSurfaceLevel(0, &aggrWeightsSurface);
|
---|
243 |
|
---|
244 | //create a vertex buffer to be able to visualize entry radion positions
|
---|
245 | device->CreateVertexBuffer(NRADIONS * sizeof(float) * 6, D3DUSAGE_WRITEONLY, D3DFVF_XYZ | D3DFVF_NORMAL, D3DPOOL_DEFAULT,
|
---|
246 | &starterVertexBuffer, NULL);
|
---|
247 | void* pData;
|
---|
248 | starterVertexBuffer->Lock(0, 0, &pData, 0);
|
---|
249 | fillRadionPosArray(pData);
|
---|
250 | starterVertexBuffer->Unlock();
|
---|
251 |
|
---|
252 | if(computePRM)
|
---|
253 | savePathMaps();
|
---|
254 | }
|
---|
255 |
|
---|
256 | PathMapEffect::~PathMapEffect(void)
|
---|
257 | {
|
---|
258 | shipMesh->Release();
|
---|
259 | shipBrdfTexture->Release();
|
---|
260 |
|
---|
261 | delete weights;
|
---|
262 | delete clusterLenghts;
|
---|
263 |
|
---|
264 | //release all resources allocated in the constructor
|
---|
265 | starterVertexBuffer->Release();
|
---|
266 |
|
---|
267 | depthMapTexture->Release();
|
---|
268 | depthMapDepthStencilBuffer->Release();
|
---|
269 | fakeTexture->Release();
|
---|
270 | fakeSurface->Release();
|
---|
271 |
|
---|
272 | sysMemWeightsTexture->Release();
|
---|
273 | sysMemWeightsSurface->Release();
|
---|
274 | weightsTexture->Release();
|
---|
275 | weightsSurface->Release();
|
---|
276 | aggrWeightsTexture->Release();
|
---|
277 | aggrWeightsSurface->Release();
|
---|
278 | radionTexture->Release();
|
---|
279 | radionSurface->Release();
|
---|
280 |
|
---|
281 | delete kdtree;
|
---|
282 | emptyTexture->Release();
|
---|
283 | releaseTextures();
|
---|
284 | releaseMeshes();
|
---|
285 | releaseEntities();
|
---|
286 |
|
---|
287 | frameColorBuffer->Release();
|
---|
288 | frameDepthStencilBuffer->Release();
|
---|
289 |
|
---|
290 | effect->Release();
|
---|
291 | delete camera;
|
---|
292 | delete lightCamera;
|
---|
293 | }
|
---|
294 |
|
---|
295 | void PathMapEffect::loadMesh(DWORD fileType, LPCWSTR fileName, LPCWSTR ogreName, int prmAtlasSize, const char* name, int dividePcs, bool generateUV, bool generateTBN, unsigned int originalAtlasTexCoordIndex)
|
---|
296 | {
|
---|
297 | wchar_t mFileName[512];
|
---|
298 | wcscpy(mFileName, this->mediaDirectory);
|
---|
299 | wcscat(mFileName, L"\\");
|
---|
300 | wcscat(mFileName, fileName);
|
---|
301 | Mesh* mesh = new Mesh(this, fileType, mFileName, ogreName, prmAtlasSize, name, dividePcs, generateUV, generateTBN, originalAtlasTexCoordIndex);
|
---|
302 | meshes.push_back(mesh);
|
---|
303 | }
|
---|
304 |
|
---|
305 | LPDIRECT3DTEXTURE9 PathMapEffect::loadTexture(LPCWSTR fileName, Material** rayTraceMaterial)
|
---|
306 | {
|
---|
307 | if(fileName == NULL || wcscmp(fileName, L"") == 0)
|
---|
308 | return NULL;
|
---|
309 | int texIndex = 0;
|
---|
310 | std::vector<wchar_t*>::iterator nameIterator = materialTextureFileNames.begin();
|
---|
311 | while(nameIterator != materialTextureFileNames.end())
|
---|
312 | {
|
---|
313 | if(wcscmp( *nameIterator, fileName) == 0)
|
---|
314 | {
|
---|
315 | *rayTraceMaterial = rayTraceMaterials.at(texIndex);
|
---|
316 | return materialTextures.at(texIndex);
|
---|
317 | }
|
---|
318 | nameIterator++;
|
---|
319 | texIndex++;
|
---|
320 | }
|
---|
321 | wchar_t* mediaFileName = new wchar_t[wcslen(fileName) + 64];
|
---|
322 | //we assume the x file is the media folder
|
---|
323 | wcscpy(mediaFileName, this->mediaDirectory);
|
---|
324 | wcscat(mediaFileName, L"\\");
|
---|
325 | wcscat(mediaFileName, fileName);
|
---|
326 | LPDIRECT3DTEXTURE9 tex;
|
---|
327 |
|
---|
328 | D3DXIMAGE_INFO bobo;
|
---|
329 | if(S_OK !=
|
---|
330 | D3DXCreateTextureFromFileEx(device, mediaFileName, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0,
|
---|
331 | D3DFMT_FROM_FILE, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &bobo, NULL, &tex
|
---|
332 | ))
|
---|
333 | {
|
---|
334 | MessageBox(NULL, mediaFileName, L"Could not load texture file!", MB_OK);
|
---|
335 | return NULL;
|
---|
336 | }
|
---|
337 |
|
---|
338 | delete mediaFileName;
|
---|
339 |
|
---|
340 | materialTextures.push_back(tex);
|
---|
341 | wchar_t* storedName = new wchar_t[wcslen(fileName)+1];
|
---|
342 | wcscpy(storedName, fileName);
|
---|
343 | materialTextureFileNames.push_back(storedName);
|
---|
344 |
|
---|
345 | if(rayTraceMaterial != NULL)
|
---|
346 | {
|
---|
347 | D3DLOCKED_RECT lrect;
|
---|
348 | HRESULT hr = tex->LockRect(0, &lrect, NULL, D3DLOCK_READONLY);
|
---|
349 | LPDIRECT3DSURFACE9 texsurf;
|
---|
350 | tex->GetSurfaceLevel(0, &texsurf);
|
---|
351 | D3DSURFACE_DESC desc;
|
---|
352 | texsurf->GetDesc(&desc);
|
---|
353 | *rayTraceMaterial = new TexturedMaterial(lrect.pBits, lrect.Pitch, desc.Width, desc.Height);
|
---|
354 | texsurf->Release();
|
---|
355 | tex->UnlockRect(0);
|
---|
356 | }
|
---|
357 | rayTraceMaterials.push_back(*rayTraceMaterial);
|
---|
358 |
|
---|
359 | return tex;
|
---|
360 | }
|
---|
361 |
|
---|
362 | void PathMapEffect::releaseTextures()
|
---|
363 | {
|
---|
364 | std::vector<LPDIRECT3DTEXTURE9>::iterator i = materialTextures.begin();
|
---|
365 | while(i != materialTextures.end())
|
---|
366 | {
|
---|
367 | (*i)->Release();
|
---|
368 | i++;
|
---|
369 | }
|
---|
370 |
|
---|
371 | std::vector<Material*>::iterator z = rayTraceMaterials.begin();
|
---|
372 | while(z != rayTraceMaterials.end())
|
---|
373 | {
|
---|
374 | delete (*z);
|
---|
375 | z++;
|
---|
376 | }
|
---|
377 |
|
---|
378 | std::vector<wchar_t*>::iterator c = materialTextureFileNames.begin();
|
---|
379 | while(c != materialTextureFileNames.end())
|
---|
380 | {
|
---|
381 | delete [] (*c);
|
---|
382 | c++;
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | void PathMapEffect::releaseMeshes()
|
---|
387 | {
|
---|
388 | std::vector<Mesh*>::iterator i = meshes.begin();
|
---|
389 | while(i != meshes.end())
|
---|
390 | {
|
---|
391 | delete *i;
|
---|
392 | i++;
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | void PathMapEffect::renderWithPRM()
|
---|
397 | {
|
---|
398 | // 1. pass: render depth
|
---|
399 | DepthRenderStrategy depthRenderStrategy(this);
|
---|
400 |
|
---|
401 | depthRenderStrategy.camera = *lightCamera;
|
---|
402 | renderScene(depthRenderStrategy);
|
---|
403 |
|
---|
404 | // 2. pass: compute weights
|
---|
405 |
|
---|
406 | // simple parallel computation for all pixels: no 3D, depth, shadows, only a full-texture quad
|
---|
407 | HRESULT hr = device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
|
---|
408 | hr = device->SetRenderState(D3DRS_STENCILENABLE, false);
|
---|
409 | hr = device->SetRenderTarget(0,weightsSurface);
|
---|
410 | hr = device->SetDepthStencilSurface(NULL);
|
---|
411 | D3DXVECTOR3 lightPos = *lightCamera->GetEyePt();
|
---|
412 | D3DXVECTOR3 lightDir = *lightCamera->GetWorldAhead();
|
---|
413 | lightDir /= D3DXVec3Length( &lightDir );
|
---|
414 | float ffl = parameters->Get(fLightScale) * 100.0f;
|
---|
415 | D3DXVECTOR3 lightPower(ffl, ffl, ffl);
|
---|
416 |
|
---|
417 | device->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
|
---|
418 |
|
---|
419 | if( SUCCEEDED( device->BeginScene() ) ){
|
---|
420 | if( effect != NULL ){
|
---|
421 | D3DXHANDLE hTechnique = NULL;
|
---|
422 | hTechnique = effect->GetTechniqueByName((LPSTR)"ComputeWeights");
|
---|
423 | if(hTechnique==NULL){
|
---|
424 | return;
|
---|
425 | }
|
---|
426 | effect->SetTechnique( hTechnique );
|
---|
427 |
|
---|
428 | UINT nPasses = 0;
|
---|
429 | effect->Begin( &nPasses, 0 );
|
---|
430 | for(UINT j=0;j<nPasses;j++){
|
---|
431 | effect->BeginPass(j);
|
---|
432 | hr = effect->SetTexture("radions", radionTexture);
|
---|
433 | static float opttme[9] = {0.5f, 0.0f, 0.0f,
|
---|
434 | 0.0f, -0.5f, 0.0f,
|
---|
435 | 0.5f + (0.5f / DEPTHMAPRES), 0.5f + (0.5f / DEPTHMAPRES), 1.0f};
|
---|
436 |
|
---|
437 | //set global params
|
---|
438 | effect->SetFloatArray("occProjToTexMatrix", opttme, 9);
|
---|
439 | if(parameters->Get(bCruise))
|
---|
440 | effect->SetMatrix("occWorldToProjMatrix", &(shipPosInverse * *lightCamera->GetProjMatrix()));
|
---|
441 | else
|
---|
442 | effect->SetMatrix("occWorldToProjMatrix", &(*lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix()));
|
---|
443 |
|
---|
444 | effect->SetTexture("depthMap", depthMapTexture);
|
---|
445 | hr = effect->SetInt("nRadionColumns", NRADIONS / 4096);
|
---|
446 | hr = effect->SetFloatArray("lightPower", (float*)&lightPower, 3);
|
---|
447 | hr = effect->SetFloatArray("lightPos", (float*)&lightPos, 3);
|
---|
448 | hr = effect->SetFloatArray("lightDir", (float*)&lightDir, 3);
|
---|
449 | effect->CommitChanges();
|
---|
450 | renderFullScreen();
|
---|
451 | effect->EndPass();
|
---|
452 | }
|
---|
453 | effect->End();
|
---|
454 | }
|
---|
455 | device->EndScene();
|
---|
456 | }
|
---|
457 |
|
---|
458 | // read the weights back
|
---|
459 |
|
---|
460 | D3DLOCKED_RECT rData;
|
---|
461 | hr = device->GetRenderTargetData(weightsSurface, sysMemWeightsSurface);
|
---|
462 | hr = sysMemWeightsSurface->LockRect(&rData, NULL, D3DLOCK_READONLY);
|
---|
463 | float* allEntryWeights = (float*)rData.pBits;
|
---|
464 |
|
---|
465 | // average weights per cluster
|
---|
466 | float sumWeightsAll = 0.0;
|
---|
467 | unsigned int iRadion = 0;
|
---|
468 | for(int iCluster=0; iCluster < NCLUSTERS; iCluster++)
|
---|
469 | {
|
---|
470 | weights[iCluster] = 0.0;
|
---|
471 | float clusterrad = 0.0;
|
---|
472 | for(int clusterSummer=0; clusterSummer < clusterLenghts[iCluster]; clusterSummer++, iRadion++)
|
---|
473 | {
|
---|
474 | float radrad = bushStarters[iRadion].radiance.sum();
|
---|
475 | weights[iCluster] += allEntryWeights[iRadion] * radrad;
|
---|
476 | clusterrad += radrad;
|
---|
477 | }
|
---|
478 | if(clusterrad > 0.01)
|
---|
479 | weights[iCluster] /= clusterrad; //(double)clusterLenghts[iCluster];
|
---|
480 | else
|
---|
481 | weights[iCluster] = 0.0f;
|
---|
482 | // if(iCluster != TEST_CLUST)
|
---|
483 | // weights[iCluster] = 0.0;
|
---|
484 | // if(weights[iCluster] > 0.005)
|
---|
485 | // weights[iCluster] = 0.005;
|
---|
486 | sumWeightsAll += weights[iCluster];
|
---|
487 | }
|
---|
488 | sysMemWeightsSurface->UnlockRect();
|
---|
489 |
|
---|
490 | // use backface culling, depth test
|
---|
491 | hr = device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
|
---|
492 | hr = device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
493 |
|
---|
494 | /* for(int testi=0; testi < NCLUSTERS; testi++)
|
---|
495 | weights[testi] = 0.000;
|
---|
496 | weights[testClusterId] = 0.05;*/
|
---|
497 |
|
---|
498 | // 3. pass: render scene using weights to combine PRM texture atlases
|
---|
499 |
|
---|
500 | FinalCompositionRenderStrategy finalCompositionRenderStrategy(this);
|
---|
501 | finalCompositionRenderStrategy.lightDir = lightDir;
|
---|
502 | finalCompositionRenderStrategy.lightPos = lightPos;
|
---|
503 | finalCompositionRenderStrategy.lightPower = lightPower;
|
---|
504 |
|
---|
505 | renderScene(finalCompositionRenderStrategy);
|
---|
506 |
|
---|
507 |
|
---|
508 | if(!parameters->Get(bLookFromLight))
|
---|
509 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
510 | {
|
---|
511 | effect->SetTechnique("torch");
|
---|
512 | UINT nPasses;
|
---|
513 | effect->Begin(&nPasses, 0);
|
---|
514 | effect->BeginPass(0);
|
---|
515 |
|
---|
516 | D3DXMATRIX scaleShip;
|
---|
517 | D3DXMatrixScaling(&scaleShip, 0.02f, 0.02f, 0.02f);
|
---|
518 |
|
---|
519 | effect->SetTexture("brdfMap", shipBrdfTexture);
|
---|
520 | effect->SetMatrix("modelToProjMatrix", &(scaleShip * shipPos * *camera->GetViewMatrix() * *camera->GetProjMatrix() ));
|
---|
521 |
|
---|
522 | effect->CommitChanges();
|
---|
523 | shipMesh->DrawSubset(0);
|
---|
524 |
|
---|
525 | effect->EndPass();
|
---|
526 | effect->End();
|
---|
527 | device->EndScene();
|
---|
528 | }
|
---|
529 |
|
---|
530 | }
|
---|
531 |
|
---|
532 | void PathMapEffect::renderFullScreen(float depth)
|
---|
533 | {
|
---|
534 | float fLeftU = 0.0f, fTopV = 0.0f, fRightU = 1.0f, fBottomV = 1.0f;
|
---|
535 |
|
---|
536 | D3DSURFACE_DESC dtdsdRT;
|
---|
537 | PDIRECT3DSURFACE9 pSurfRT;
|
---|
538 |
|
---|
539 | // Acquire render target width and height
|
---|
540 | device->GetRenderTarget(0, &pSurfRT);
|
---|
541 | pSurfRT->GetDesc(&dtdsdRT);
|
---|
542 | pSurfRT->Release();
|
---|
543 |
|
---|
544 | // Ensure that we're directly mapping texels to pixels by offset by 0.5
|
---|
545 | // For more info see the doc page titled "Directly Mapping Texels to Pixels"
|
---|
546 | FLOAT fWidth5 = (FLOAT)dtdsdRT.Width - 0.5f;
|
---|
547 | FLOAT fHeight5 = (FLOAT)dtdsdRT.Height - 0.5f;
|
---|
548 |
|
---|
549 | // Draw the quad
|
---|
550 | struct D3DVERTEXQUAD{
|
---|
551 | D3DXVECTOR3 pos;
|
---|
552 | D3DXVECTOR2 tex0;
|
---|
553 | };
|
---|
554 |
|
---|
555 | D3DVERTEXQUAD svQuad[4];
|
---|
556 |
|
---|
557 | svQuad[0].pos=D3DXVECTOR3(-1, 1, depth);
|
---|
558 | svQuad[0].tex0 = D3DXVECTOR2(fLeftU, fTopV);
|
---|
559 |
|
---|
560 | svQuad[1].pos = D3DXVECTOR3(1, 1, depth);
|
---|
561 | svQuad[1].tex0 = D3DXVECTOR2(fRightU, fTopV);
|
---|
562 |
|
---|
563 | svQuad[2].pos = D3DXVECTOR3(-1, -1, depth);
|
---|
564 | svQuad[2].tex0 = D3DXVECTOR2(fLeftU, fBottomV);
|
---|
565 |
|
---|
566 | svQuad[3].pos = D3DXVECTOR3(1, -1, depth);
|
---|
567 | svQuad[3].tex0 = D3DXVECTOR2(fRightU, fBottomV);
|
---|
568 |
|
---|
569 | device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
|
---|
570 | device->SetFVF(D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0));
|
---|
571 | device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, svQuad, sizeof(D3DVERTEXQUAD));
|
---|
572 | }
|
---|
573 |
|
---|
574 | void PathMapEffect::move(float fElapsedTime)
|
---|
575 | {
|
---|
576 | clusterSweepCurrentIndex += fElapsedTime * 2.0f;
|
---|
577 | if(clusterSweepCurrentIndex > NCLUSTERS)
|
---|
578 | clusterSweepCurrentIndex -= NCLUSTERS;
|
---|
579 |
|
---|
580 | loopPos += fElapsedTime * 0.3;
|
---|
581 | if(loopPos > Vector::PI)
|
---|
582 | {
|
---|
583 | loopPos -= Vector::PI;
|
---|
584 | if(cruiseLoop == 1)
|
---|
585 | cruiseLoop = 0;
|
---|
586 | else
|
---|
587 | cruiseLoop = 1;
|
---|
588 | /* if(cruiseLoop == 1 || cruiseLoop == 3 || cruiseLoop == 5)
|
---|
589 | {
|
---|
590 | cruiseLoop = (rand() % 3) * 2;
|
---|
591 | }
|
---|
592 | else if(cruiseLoop == 0 || cruiseLoop == 8)
|
---|
593 | {
|
---|
594 | cruiseLoop = 1 + (rand() % 2) * 5;
|
---|
595 | }
|
---|
596 | else if(cruiseLoop == 4 || cruiseLoop == 6)
|
---|
597 | {
|
---|
598 | cruiseLoop = 5 + (rand() % 2) * 2;
|
---|
599 | }
|
---|
600 | else if(cruiseLoop == 2 || cruiseLoop == 7)
|
---|
601 | {
|
---|
602 | cruiseLoop = 3 + (rand() % 2) * 5;
|
---|
603 | }*/
|
---|
604 | }
|
---|
605 |
|
---|
606 | D3DXMATRIX mm, markerPos;
|
---|
607 | D3DXMatrixIdentity(&markerPos);
|
---|
608 | /* if(cruiseLoop < 6)
|
---|
609 | {
|
---|
610 | //to loop
|
---|
611 | D3DXMatrixRotationAxis(&mm, &D3DXVECTOR3(0, 1, 0), (cruiseLoop / 2) * Vector::PI * 0.66666666666);
|
---|
612 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
613 |
|
---|
614 | //pull in
|
---|
615 | D3DXMatrixTranslation(&mm, 17.0, 0, 0);
|
---|
616 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
617 |
|
---|
618 | //orbit
|
---|
619 | D3DXMatrixRotationAxis(&mm, &D3DXVECTOR3(0, 0, 1), (cruiseLoop%2 != 0)?(loopPos + Vector::PI):loopPos);
|
---|
620 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
621 |
|
---|
622 | //push out
|
---|
623 | D3DXMatrixTranslation(&mm, -17.0, 0, 0);
|
---|
624 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
625 |
|
---|
626 | //raise
|
---|
627 | D3DXMatrixTranslation(&mm, 4.0, 0, 0);
|
---|
628 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
629 |
|
---|
630 | //turn nose
|
---|
631 | D3DXMatrixRotationAxis(&mm, &D3DXVECTOR3(1, 0, 0), Vector::PI / 2.0);
|
---|
632 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
633 | }
|
---|
634 | else
|
---|
635 | {
|
---|
636 | //orbit
|
---|
637 | D3DXMatrixRotationAxis(&mm, &D3DXVECTOR3(0, 1, 0), -(cruiseLoop - 6) %3 * Vector::PI * 0.6666666666 -
|
---|
638 | loopPos * 0.666666666666666);
|
---|
639 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
640 |
|
---|
641 | //push out
|
---|
642 | D3DXMatrixTranslation(&mm, 30.0, 0, 0);
|
---|
643 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
644 |
|
---|
645 | //turn nose
|
---|
646 | // D3DXMatrixRotationAxis(&mm, &D3DXVECTOR3(0, 1, 0), Vector::PI / 2.0);
|
---|
647 | // D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
648 | }
|
---|
649 | */
|
---|
650 | D3DXMatrixTranslation(&markerPos, loopPos * 30.0 - 20.0, 5.0, 0);
|
---|
651 | //turn nose
|
---|
652 | D3DXMatrixRotationAxis(&mm, &D3DXVECTOR3(0, 1, 0), Vector::PI / 2.0);
|
---|
653 | D3DXMatrixMultiply(&markerPos, &mm, &markerPos);
|
---|
654 |
|
---|
655 | shipPos = markerPos;// * 0.05 + shipPos * 0.95;
|
---|
656 |
|
---|
657 | D3DXMatrixLookAtLH(&shipPosInverse,
|
---|
658 | &(D3DXVECTOR3(0, 0, 0) * shipPos),
|
---|
659 | &(D3DXVECTOR3(0, 0, 1) * shipPos),
|
---|
660 | &(D3DXVECTOR3(0, 1, 0) * shipPos - D3DXVECTOR3(0, 0, 0) * shipPos));
|
---|
661 |
|
---|
662 | D3DXMatrixInverse(&shipPos, NULL, &shipPosInverse);
|
---|
663 |
|
---|
664 | if(parameters->Get(bCruise))
|
---|
665 | {
|
---|
666 | lightCamera->SetViewParams(
|
---|
667 | &(D3DXVECTOR3(0, 0, 0) * shipPos),
|
---|
668 | &(D3DXVECTOR3(0, 0, 1) * shipPos)
|
---|
669 | );
|
---|
670 | }
|
---|
671 |
|
---|
672 | if(parameters->Get(bTurbo))
|
---|
673 | {
|
---|
674 | camera->FrameMove(fElapsedTime * 10.0f);
|
---|
675 | lightCamera->FrameMove(fElapsedTime * 10.0f);
|
---|
676 | }
|
---|
677 | else
|
---|
678 | {
|
---|
679 | camera->FrameMove(fElapsedTime);
|
---|
680 | lightCamera->FrameMove(fElapsedTime);
|
---|
681 | }
|
---|
682 | }
|
---|
683 |
|
---|
684 | LRESULT PathMapEffect::handleMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
---|
685 | {
|
---|
686 | if(uMsg == WM_KEYDOWN)
|
---|
687 | {
|
---|
688 | switch(wParam)
|
---|
689 | {
|
---|
690 | case 'C':
|
---|
691 | testClusterId = (testClusterId + NCLUSTERS - 1) % NCLUSTERS;
|
---|
692 | break;
|
---|
693 | case 'Z':
|
---|
694 | testClusterId = (testClusterId + 1) % NCLUSTERS;
|
---|
695 | break;
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | if(parameters->Get(bMoveLight))
|
---|
700 | return lightCamera->HandleMessages(hWnd, uMsg, wParam, lParam);
|
---|
701 | else
|
---|
702 | return camera->HandleMessages(hWnd, uMsg, wParam, lParam);
|
---|
703 |
|
---|
704 | }
|
---|
705 |
|
---|
706 | void PathMapEffect::addUiParameters(Parameters* parameters)
|
---|
707 | {
|
---|
708 | PathMapEffect::parameters = parameters;
|
---|
709 | parameters->Add( bLookFromLight, "Look from light", 1);
|
---|
710 | parameters->Add( bMoveLight, "Move light", 1);
|
---|
711 | parameters->Add( bDots, "Entry points", 1);
|
---|
712 | parameters->Add( bTurbo, "Turbo", 1);
|
---|
713 | parameters->Add( bCruise, "Cruise", 1);
|
---|
714 | parameters->Add( fLightScale, "Tone scale", 100, '1', '2', convertLightScale);
|
---|
715 | parameters->Add( fIndirectLightingMode, "Lighting mode", 3, '3', '4', convertInt4);
|
---|
716 | parameters->Add( fTorchDistance, "Torch distance", 100, '5', '6', convertTorchDistance);
|
---|
717 | }
|
---|
718 |
|
---|
719 | void PathMapEffect::setUiParameterDefaults(Parameters* parameters)
|
---|
720 | {
|
---|
721 | }
|
---|
722 |
|
---|
723 | Parameters* PathMapEffect::parameters = NULL;
|
---|
724 |
|
---|
725 | void PathMapEffect::render()
|
---|
726 | {
|
---|
727 | renderWithPRM();
|
---|
728 | if(parameters->Get(bDots))
|
---|
729 | {
|
---|
730 | float psf = 8.0f;
|
---|
731 | HRESULT hr = device->SetRenderState(D3DRS_POINTSIZE, *((DWORD*)&psf));
|
---|
732 | hr = device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
|
---|
733 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
734 | {
|
---|
735 | D3DXHANDLE hTechnique = NULL;
|
---|
736 | hTechnique = effect->GetTechniqueByName((LPSTR)"Dots");
|
---|
737 | effect->SetTechnique( hTechnique );
|
---|
738 | UINT nPasses = 0;
|
---|
739 | effect->Begin( &nPasses, 0 );
|
---|
740 | for(UINT j=0;j<nPasses;j++){
|
---|
741 | effect->BeginPass(j);
|
---|
742 | hr = effect->SetTexture("depthMap",depthMapTexture);
|
---|
743 | if(parameters->Get(bLookFromLight))
|
---|
744 | {
|
---|
745 | if(parameters->Get(bCruise))
|
---|
746 | effect->SetMatrix("worldToProjMatrix", &(shipPosInverse * *lightCamera->GetProjMatrix() ));
|
---|
747 | else
|
---|
748 | effect->SetMatrix("worldToProjMatrix", &(*lightCamera->GetViewMatrix() * *lightCamera->GetProjMatrix() ));
|
---|
749 | }
|
---|
750 | else
|
---|
751 | effect->SetMatrix("worldToProjMatrix", &(*camera->GetViewMatrix() * *camera->GetProjMatrix() ));
|
---|
752 |
|
---|
753 | static float opttme[9] = {0.5f, 0.0f, 0.0f,
|
---|
754 | 0.0f, -0.5f, 0.0f,
|
---|
755 | 0.5f + (0.5f / DEPTHMAPRES), 0.5f + (0.5f / DEPTHMAPRES), 1.0f};
|
---|
756 |
|
---|
757 | effect->SetFloatArray("occProjToTexMatrix", opttme, 9);
|
---|
758 | D3DXVECTOR3 lightPos = *lightCamera->GetEyePt();
|
---|
759 | hr = effect->SetFloatArray("lightPos", (float*)&lightPos, 3);
|
---|
760 | effect->CommitChanges();
|
---|
761 | hr = device->SetFVF(D3DFVF_XYZ | D3DFVF_NORMAL);
|
---|
762 | hr = device->SetStreamSource(0, starterVertexBuffer, 0, sizeof(float) * 6);
|
---|
763 | UINT offset = 0;
|
---|
764 | for(int iClusterDraw=0; iClusterDraw < NCLUSTERS; iClusterDraw++)
|
---|
765 | {
|
---|
766 | // hr = effect->SetFloat("aClusterWeight", weights[iClusterDraw] * 10.0);
|
---|
767 | hr = effect->SetFloat("aClusterWeight", (float)iClusterDraw);
|
---|
768 | effect->CommitChanges();
|
---|
769 | // if(iClusterDraw - clusterSweepCurrentIndex > -1.0 &&
|
---|
770 | // iClusterDraw - clusterSweepCurrentIndex < 0.0 )
|
---|
771 | // if(iClusterDraw == testClusterId)
|
---|
772 | device->DrawPrimitive(D3DPT_POINTLIST, offset, clusterLenghts[iClusterDraw]);
|
---|
773 | offset += clusterLenghts[iClusterDraw];
|
---|
774 | }
|
---|
775 | effect->EndPass();
|
---|
776 | }
|
---|
777 | effect->End();
|
---|
778 | }
|
---|
779 | device->EndScene();
|
---|
780 | }
|
---|
781 | }
|
---|
782 |
|
---|
783 | void PathMapEffect::releaseEntities()
|
---|
784 | {
|
---|
785 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
786 | while(entityIterator != entities.end())
|
---|
787 | {
|
---|
788 | delete *entityIterator;
|
---|
789 | entityIterator++;
|
---|
790 | }
|
---|
791 | }
|
---|
792 |
|
---|
793 | void PathMapEffect::sampleSphereDirection(Vector& outDir)
|
---|
794 | {
|
---|
795 | do{
|
---|
796 | 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);
|
---|
797 | }while(outDir.norm2() > 1.0);
|
---|
798 | outDir.normalize();
|
---|
799 | }
|
---|
800 |
|
---|
801 | void PathMapEffect::sampleShootingDiffuseDirection(int depth, const Vector& normal, Vector& outDir)
|
---|
802 | {
|
---|
803 | Vector u, v;
|
---|
804 | if(fabsf(normal[0]) < 0.9f)
|
---|
805 | {
|
---|
806 | u.set(0.0f, -normal[2], normal[1]);
|
---|
807 | u *= 1.0f / sqrtf(normal[2] * normal[2] + normal[1] * normal[1]);
|
---|
808 | }
|
---|
809 | else
|
---|
810 | {
|
---|
811 | u.set(normal[2], 0.0f, -normal[0]);
|
---|
812 | u *= 1.0f / sqrtf(normal[2] * normal[2] + normal[0] * normal[0]);
|
---|
813 | }
|
---|
814 | v.setCrossProduct(normal, u);
|
---|
815 |
|
---|
816 | float phi = 2.0f * 3.14159265358979323846f * (float)rand() / RAND_MAX;
|
---|
817 | float xi2 = (float)rand() / RAND_MAX;
|
---|
818 | float cosPhi = cos(phi);
|
---|
819 | float sinPhi = sin(phi);
|
---|
820 | float cosTheta = sqrtf(xi2);
|
---|
821 | float sinTheta = sqrtf(1.0f - xi2);
|
---|
822 |
|
---|
823 | outDir.setScaled(sinTheta * cosPhi, v);
|
---|
824 | outDir.addScaled(sinTheta * sinPhi, u);
|
---|
825 | outDir.addScaled(cosTheta, normal);
|
---|
826 | }
|
---|
827 |
|
---|
828 | void PathMapEffect::precompute()
|
---|
829 | {
|
---|
830 | // generate entry points
|
---|
831 | for(unsigned int cRad=0; cRad < NRADIONS; cRad++)
|
---|
832 | {
|
---|
833 | Radion starter;
|
---|
834 | if(UNIFORMSAMPLING)
|
---|
835 | sampleSurfaceRadionUniform(starter);
|
---|
836 | else
|
---|
837 | sampleSurfaceRadion(starter);
|
---|
838 |
|
---|
839 | bushStarters.push_back(starter);
|
---|
840 | }
|
---|
841 |
|
---|
842 | // sort entry radions into clusters
|
---|
843 | Radion* entryArray = (Radion*)&*bushStarters.begin();
|
---|
844 | clusterRadions(entryArray, bushStarters.size(), 0);
|
---|
845 | clusterRadionsKMeans();
|
---|
846 |
|
---|
847 | // for every entity, find the most relevant clusters
|
---|
848 | srand(0xef23ab32);
|
---|
849 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
850 | while(entityIterator != entities.end())
|
---|
851 | {
|
---|
852 | (*entityIterator)->findNearClusters(bushStarters, NCLUSTERS);
|
---|
853 | (*entityIterator)->clearPRM();
|
---|
854 | entityIterator++;
|
---|
855 | }
|
---|
856 |
|
---|
857 | // for every entry radion, shoot a bush of radions, and add their
|
---|
858 | // contributions to all PRMs of all entities
|
---|
859 | unsigned int iCluster = 0;
|
---|
860 | std::vector<Radion> bushRadions;
|
---|
861 | for(int iStarter = 0; iStarter < NRADIONS; )
|
---|
862 | {
|
---|
863 | bushRadions.clear();
|
---|
864 | for(int iric=0; iric < clusterLenghts[iCluster]; iric++, iStarter++)
|
---|
865 | shootRadionBush(bushStarters[iStarter], bushRadions);
|
---|
866 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
867 | while(entityIterator != entities.end())
|
---|
868 | {
|
---|
869 | (*entityIterator)->renderPRM(bushRadions, iCluster);
|
---|
870 | entityIterator++;
|
---|
871 | }
|
---|
872 | iCluster++;
|
---|
873 | }
|
---|
874 |
|
---|
875 | //scale down multiple pixel writes
|
---|
876 | entityIterator = entities.begin();
|
---|
877 | while(entityIterator != entities.end())
|
---|
878 | {
|
---|
879 | (*entityIterator)->normalizePRM();
|
---|
880 | entityIterator++;
|
---|
881 | }
|
---|
882 | }
|
---|
883 |
|
---|
884 | int compareX(const void* a, const void* b)
|
---|
885 | {
|
---|
886 | if( ((const Radion*)a)->position.x < ((const Radion*)b)->position.x )
|
---|
887 | return -1;
|
---|
888 | else
|
---|
889 | return 1;
|
---|
890 | }
|
---|
891 |
|
---|
892 | int compareY(const void* a, const void* b)
|
---|
893 | {
|
---|
894 | if( ((const Radion*)a)->position.y < ((const Radion*)b)->position.y )
|
---|
895 | return -1;
|
---|
896 | else
|
---|
897 | return 1;
|
---|
898 | }
|
---|
899 |
|
---|
900 | int compareZ(const void* a, const void* b)
|
---|
901 | {
|
---|
902 | if( ((const Radion*)a)->position.z < ((const Radion*)b)->position.z )
|
---|
903 | return -1;
|
---|
904 | else
|
---|
905 | return 1;
|
---|
906 | }
|
---|
907 |
|
---|
908 | int compareDirX(const void* a, const void* b)
|
---|
909 | {
|
---|
910 | if( ((const Radion*)a)->normal.x < ((const Radion*)b)->normal.x )
|
---|
911 | return -1;
|
---|
912 | else
|
---|
913 | return 1;
|
---|
914 | }
|
---|
915 |
|
---|
916 | int compareDirY(const void* a, const void* b)
|
---|
917 | {
|
---|
918 | if( ((const Radion*)a)->normal.y < ((const Radion*)b)->normal.y )
|
---|
919 | return -1;
|
---|
920 | else
|
---|
921 | return 1;
|
---|
922 | }
|
---|
923 |
|
---|
924 | int compareDirZ(const void* a, const void* b)
|
---|
925 | {
|
---|
926 | if( ((const Radion*)a)->normal.z < ((const Radion*)b)->normal.z )
|
---|
927 | return -1;
|
---|
928 | else
|
---|
929 | return 1;
|
---|
930 | }
|
---|
931 |
|
---|
932 |
|
---|
933 | void PathMapEffect::clusterRadionsKMeans()
|
---|
934 | {
|
---|
935 | Radion centroids[MAXNCLUSTERS];
|
---|
936 | std::vector<Radion> clusters[MAXNCLUSTERS];
|
---|
937 | //initial clusters: uniform length
|
---|
938 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
939 | clusterLenghts[i] = NRADIONS / NCLUSTERS;
|
---|
940 | for(unsigned int iKMeans = 0; iKMeans < 128; iKMeans++)
|
---|
941 | {
|
---|
942 | //find centroids
|
---|
943 | unsigned int iRadion = 0;
|
---|
944 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
945 | {
|
---|
946 | centroids[i].position = Vector::RGBBLACK;
|
---|
947 | centroids[i].normal = Vector::RGBBLACK;
|
---|
948 | for(unsigned int iRadionInCluster=0; iRadionInCluster < clusterLenghts[i]; iRadionInCluster++, iRadion++)
|
---|
949 | {
|
---|
950 | centroids[i].position += bushStarters.at(iRadion).position;
|
---|
951 | centroids[i].normal += bushStarters.at(iRadion).normal;
|
---|
952 | }
|
---|
953 | centroids[i].position *= 1.0f / (float)clusterLenghts[i];
|
---|
954 | centroids[i].normal.normalize();
|
---|
955 | }
|
---|
956 |
|
---|
957 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
958 | clusters[i].clear();
|
---|
959 |
|
---|
960 | //sort radions to centroids
|
---|
961 | iRadion = 0;
|
---|
962 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
963 | {
|
---|
964 | for(unsigned int iRadionInCluster=0; iRadionInCluster < clusterLenghts[i]; iRadionInCluster++, iRadion++)
|
---|
965 | {
|
---|
966 | unsigned int minimumDistanceClusterIndex = 0;
|
---|
967 | float minimumDistance = FLT_MAX;
|
---|
968 | for(unsigned int u=0; u<NCLUSTERS; u++)
|
---|
969 | {
|
---|
970 | float dirDist = (bushStarters.at(iRadion).normal * centroids[u].normal);
|
---|
971 | float dist = (bushStarters.at(iRadion).position - centroids[u].position).norm() * pow(6.0, 1.0 - (double)dirDist);
|
---|
972 | if(dist < minimumDistance)
|
---|
973 | {
|
---|
974 | minimumDistanceClusterIndex = u;
|
---|
975 | minimumDistance = dist;
|
---|
976 | }
|
---|
977 | }
|
---|
978 | clusters[minimumDistanceClusterIndex].push_back( bushStarters.at(iRadion) );
|
---|
979 | }
|
---|
980 | }
|
---|
981 | //refill bushStarters, set cluster lengths
|
---|
982 | iRadion = 0;
|
---|
983 | for(unsigned int i=0; i<NCLUSTERS; i++)
|
---|
984 | {
|
---|
985 | clusterLenghts[i] = clusters[i].size();
|
---|
986 | for(unsigned int iRadionInCluster=0; iRadionInCluster < clusterLenghts[i]; iRadionInCluster++, iRadion++)
|
---|
987 | {
|
---|
988 | bushStarters.at(iRadion) = clusters[i].at(iRadionInCluster);
|
---|
989 | }
|
---|
990 | }
|
---|
991 | //eliminate empty clusters
|
---|
992 | for(unsigned int i=1; i<NCLUSTERS; i++)
|
---|
993 | {
|
---|
994 | if(clusterLenghts[i] == 0)
|
---|
995 | {
|
---|
996 | clusterLenghts[i] = clusterLenghts[i-1] >> 1;
|
---|
997 | clusterLenghts[i-1] -= clusterLenghts[i-1] >> 1;
|
---|
998 | }
|
---|
999 | }
|
---|
1000 | for(int i=NCLUSTERS - 1; i >= 0; 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 | }
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | int PathMapEffect::clusterRadions(Radion* partition, int psize, char axis)
|
---|
1012 | {
|
---|
1013 | /* if(psize < 2)
|
---|
1014 | return 1;
|
---|
1015 | if(axis == 0)
|
---|
1016 | qsort(partition, psize, sizeof(Radion), compareDirX);
|
---|
1017 | else if(axis == 1)
|
---|
1018 | qsort(partition, psize, sizeof(Radion), compareDirY);
|
---|
1019 | else if(axis == 2)
|
---|
1020 | qsort(partition, psize, sizeof(Radion), compareDirZ);
|
---|
1021 | else if(axis == 3)
|
---|
1022 | qsort(partition, psize, sizeof(Radion), compareX);
|
---|
1023 | else if(axis == 4)
|
---|
1024 | qsort(partition, psize, sizeof(Radion), compareY);
|
---|
1025 | else if(axis == 5)
|
---|
1026 | qsort(partition, psize, sizeof(Radion), compareZ);
|
---|
1027 | clusterRadions(partition, psize >> 1, (axis+1)%6);
|
---|
1028 | clusterRadions(partition + (psize >> 1), psize - (psize >> 1), (axis+1)%6);
|
---|
1029 | return 1;
|
---|
1030 | /*/ if(psize < 2)
|
---|
1031 | return 1;
|
---|
1032 | if(axis == 0)
|
---|
1033 | qsort(partition, psize, sizeof(Radion), compareX);
|
---|
1034 | else if(axis == 1)
|
---|
1035 | qsort(partition, psize, sizeof(Radion), compareY);
|
---|
1036 | else if(axis == 2)
|
---|
1037 | qsort(partition, psize, sizeof(Radion), compareZ);
|
---|
1038 | clusterRadions(partition, psize >> 1, (axis+1)%3);
|
---|
1039 | clusterRadions(partition + (psize >> 1), psize - (psize >> 1), (axis+1)%3);
|
---|
1040 | return 1;//*/
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 |
|
---|
1044 | void PathMapEffect::shootRadionBush(Radion& starter, std::vector<Radion>& bushRadions)
|
---|
1045 | {
|
---|
1046 | bushRadions.push_back(starter);
|
---|
1047 |
|
---|
1048 | int nPhotonsShotForLight = 1; //branching factor
|
---|
1049 | for(int iPhoton = 0; iPhoton < nPhotonsShotForLight; iPhoton++)
|
---|
1050 | {
|
---|
1051 | unsigned int depth = 0;
|
---|
1052 | ray.id = rayId++;
|
---|
1053 | ray.isShadowRay = false;
|
---|
1054 | ray.origin = starter.position;
|
---|
1055 | Vector power = starter.radiance;
|
---|
1056 | float prob = starter.probability;
|
---|
1057 | prob *= nPhotonsShotForLight;
|
---|
1058 | power *= 1.0f / nPhotonsShotForLight;
|
---|
1059 | sampleShootingDiffuseDirection(depth, starter.normal, ray.dir);
|
---|
1060 |
|
---|
1061 | for(;;depth++)
|
---|
1062 | {
|
---|
1063 | kdtree->traverse(ray, hitRec, 0.0f, FLT_MAX);
|
---|
1064 | if(hitRec.isIntersect)
|
---|
1065 | {
|
---|
1066 | Radion nr;
|
---|
1067 | nr.position = hitRec.point;
|
---|
1068 | nr.normal = hitRec.normal;
|
---|
1069 | nr.radiance = power;
|
---|
1070 | nr.radiance %= hitRec.material->getTextureDiffuseBrdf(hitRec.texUV);
|
---|
1071 | nr.probability = prob;
|
---|
1072 | bushRadions.push_back(nr);
|
---|
1073 | }
|
---|
1074 | else
|
---|
1075 | break;
|
---|
1076 | if(depth >= 3)
|
---|
1077 | break;
|
---|
1078 | float rrRandom = (float)rand() / RAND_MAX;
|
---|
1079 | float diffuseAlbedo = hitRec.material->getTextureDiffuseAlbedo(hitRec.texUV);
|
---|
1080 | float idealAlbedo = hitRec.material->getTextureIdealAlbedo(hitRec.texUV);
|
---|
1081 | float refractiveAlbedo = hitRec.material->getRefractiveAlbedo();
|
---|
1082 | if(rrRandom < diffuseAlbedo)
|
---|
1083 | {
|
---|
1084 | ray.id = rayId++;
|
---|
1085 | ray.isShadowRay = false;
|
---|
1086 | ray.origin = hitRec.point;
|
---|
1087 | power %= hitRec.material->getTextureDiffuseBrdf(hitRec.texUV);
|
---|
1088 | power *= 1.0f / diffuseAlbedo;
|
---|
1089 | prob *= diffuseAlbedo;
|
---|
1090 | sampleShootingDiffuseDirection(depth, hitRec.normal, ray.dir);
|
---|
1091 | }
|
---|
1092 | else
|
---|
1093 | {
|
---|
1094 | rrRandom -= diffuseAlbedo;
|
---|
1095 | if(rrRandom < idealAlbedo)
|
---|
1096 | {
|
---|
1097 | ray.dir.setIdealReflectedDirection(ray.dir, hitRec.normal);
|
---|
1098 | ray.id = rayId++;
|
---|
1099 | ray.isShadowRay = false;
|
---|
1100 | ray.origin = hitRec.point;
|
---|
1101 | power %= hitRec.material->getIdealBrdf();
|
---|
1102 | power *= 1.0f / idealAlbedo;
|
---|
1103 | prob *= idealAlbedo;
|
---|
1104 | }
|
---|
1105 | else
|
---|
1106 | break;
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | void PathMapEffect::sampleSurfaceRadion(Radion& starter)
|
---|
1113 | {
|
---|
1114 | float randa = ((double)rand() / RAND_MAX) * entities.size();
|
---|
1115 | float uptonowSurfaceArea = 0.0;
|
---|
1116 | Entity* e = NULL;
|
---|
1117 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
1118 | while(entityIterator != entities.end())
|
---|
1119 | {
|
---|
1120 | uptonowSurfaceArea += 1.0f;
|
---|
1121 | if(uptonowSurfaceArea >= randa)
|
---|
1122 | {
|
---|
1123 | e = *entityIterator;
|
---|
1124 | break;
|
---|
1125 | }
|
---|
1126 | entityIterator++;
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | const Material* m = e->sampleSurface(starter);
|
---|
1130 |
|
---|
1131 | float surfa = starter.radiance.z;
|
---|
1132 |
|
---|
1133 | Vector rtexc = starter.radiance;
|
---|
1134 | starter.radiance = m->getTextureDiffuseBrdf(rtexc);
|
---|
1135 |
|
---|
1136 | starter.radiance *= sumSurfaceArea / NRADIONS;
|
---|
1137 | starter.probability = (float)NRADIONS / (surfa * entities.size());
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | void PathMapEffect::sampleSurfaceRadionUniform(Radion& starter)
|
---|
1141 | {
|
---|
1142 | float randa = ((double)rand() / RAND_MAX) * sumSurfaceArea;
|
---|
1143 | float uptonowSurfaceArea = 0.0;
|
---|
1144 | Entity* e = NULL;
|
---|
1145 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
1146 | while(entityIterator != entities.end())
|
---|
1147 | {
|
---|
1148 | uptonowSurfaceArea += (*entityIterator)->getSurfaceArea();
|
---|
1149 | if(uptonowSurfaceArea >= randa)
|
---|
1150 | {
|
---|
1151 | e = *entityIterator;
|
---|
1152 | break;
|
---|
1153 | }
|
---|
1154 | entityIterator++;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | const Material* m = e->sampleSurface(starter);
|
---|
1158 |
|
---|
1159 | float surfa = starter.radiance.z;
|
---|
1160 |
|
---|
1161 | Vector rtexc = starter.radiance;
|
---|
1162 | starter.radiance = m->getTextureDiffuseBrdf(rtexc);
|
---|
1163 | starter.radiance *= sumSurfaceArea / NRADIONS;
|
---|
1164 | starter.probability = (float)NRADIONS / sumSurfaceArea;
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | void PathMapEffect::uploadRadions()
|
---|
1168 | {
|
---|
1169 | int nRadions = bushStarters.size();
|
---|
1170 | struct TMR{
|
---|
1171 | float pos[4];
|
---|
1172 | float dir[4];
|
---|
1173 | // float pow[4];
|
---|
1174 | };
|
---|
1175 | TMR* tm = new TMR[nRadions];
|
---|
1176 | for(int i=0; i<nRadions; i++)
|
---|
1177 | {
|
---|
1178 | //*
|
---|
1179 | tm[i].pos[0] = bushStarters[i].position[0];
|
---|
1180 | tm[i].pos[1] = bushStarters[i].position[1];
|
---|
1181 | tm[i].pos[2] = bushStarters[i].position[2];
|
---|
1182 | tm[i].pos[3] = 1.0;
|
---|
1183 | tm[i].dir[0] = bushStarters[i].normal[0];
|
---|
1184 | tm[i].dir[1] = bushStarters[i].normal[1];
|
---|
1185 | tm[i].dir[2] = bushStarters[i].normal[2];
|
---|
1186 | tm[i].dir[3] = 1.0;
|
---|
1187 | // tm[i].pow[0] = bushStarters[i].radiance[0];
|
---|
1188 | // tm[i].pow[1] = bushStarters[i].radiance[1];
|
---|
1189 | // tm[i].pow[2] = bushStarters[i].radiance[2];
|
---|
1190 | // tm[i].pow[3] = 1.0;
|
---|
1191 | /*/
|
---|
1192 | tm[i].pos[0] = 0.9;
|
---|
1193 | tm[i].pos[1] = 0.2;
|
---|
1194 | tm[i].pos[2] = 0.2;
|
---|
1195 | tm[i].pos[3] = 1.0;
|
---|
1196 | tm[i].dir[0] = 0.2;
|
---|
1197 | tm[i].dir[1] = 0.9;
|
---|
1198 | tm[i].dir[2] = 0.2;
|
---|
1199 | tm[i].dir[3] = 1.0;
|
---|
1200 | tm[i].pow[0] = 0.2;
|
---|
1201 | tm[i].pow[1] = 0.2;
|
---|
1202 | tm[i].pow[2] = 0.9; //blue
|
---|
1203 | tm[i].pow[3] = 0.99;
|
---|
1204 | //*/
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | RECT imgSize;
|
---|
1208 | imgSize.top = imgSize.left = 0;
|
---|
1209 | imgSize.bottom = nRadions;
|
---|
1210 | imgSize.right = 2 * nRadions / 4096;
|
---|
1211 | D3DXLoadSurfaceFromMemory(radionSurface, NULL, NULL, tm, D3DFMT_A32B32G32R32F,
|
---|
1212 | imgSize.right * 4 * sizeof(float), NULL, &imgSize, D3DX_FILTER_NONE, 0);
|
---|
1213 |
|
---|
1214 | delete [] tm;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | void PathMapEffect::fillRadionPosArray(void* pData)
|
---|
1218 | {
|
---|
1219 | int nRadions = NRADIONS;
|
---|
1220 | struct TMR{
|
---|
1221 | float pos[3];
|
---|
1222 | float rad[3];
|
---|
1223 | };
|
---|
1224 | TMR* tm = (TMR*)pData;
|
---|
1225 | for(int i=0; i<nRadions; i++)
|
---|
1226 | {
|
---|
1227 | tm[i].pos[0] = bushStarters[i].position[0];
|
---|
1228 | tm[i].pos[1] = bushStarters[i].position[1];
|
---|
1229 | tm[i].pos[2] = bushStarters[i].position[2];
|
---|
1230 | tm[i].rad[0] = bushStarters[i].radiance[0];
|
---|
1231 | tm[i].rad[1] = bushStarters[i].radiance[1];
|
---|
1232 | tm[i].rad[2] = bushStarters[i].radiance[2];
|
---|
1233 | }
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | const wchar_t* PathMapEffect::getWeightsString()
|
---|
1237 | {
|
---|
1238 | static wchar_t ws[512];
|
---|
1239 | ws[0] = 0;
|
---|
1240 | char wan[64];
|
---|
1241 | float sumw = 0.0f;
|
---|
1242 | for(int i=0; i<33; i++)
|
---|
1243 | {
|
---|
1244 | // StrCpy(wan, L"%f ");
|
---|
1245 | if(i == 32)
|
---|
1246 | sprintf(wan, "%.3f ", (double)sumw);
|
---|
1247 | else
|
---|
1248 | {
|
---|
1249 | sumw += weights[i];
|
---|
1250 | sprintf(wan, "%.3f ", (double)weights[i]);
|
---|
1251 | }
|
---|
1252 | wchar_t* wideValue = new wchar_t[MultiByteToWideChar(CP_ACP, 0, wan, -1, NULL, 0)];
|
---|
1253 | MultiByteToWideChar(CP_ACP, 0, wan, -1, wideValue, 511);
|
---|
1254 | StrCatW(ws, wideValue);
|
---|
1255 | delete wideValue;
|
---|
1256 | }
|
---|
1257 | return ws;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 |
|
---|
1261 | void PathMapEffect::savePathMaps()
|
---|
1262 | {
|
---|
1263 | // std::fstream entryPointFile("prm\\prmEntryPoints.dat", std::ios::out | std::ios::binary);
|
---|
1264 | char eFileName[512];
|
---|
1265 | sprintf(eFileName, "%s\\prmEntryPoints.text", LC::c-this->prmDirectory);
|
---|
1266 | std::fstream entryPointFile(eFileName, std::ios::out);
|
---|
1267 |
|
---|
1268 | entryPointFile << "nEntryPoints ";
|
---|
1269 | entryPointFile << NRADIONS;
|
---|
1270 | entryPointFile << "\n";
|
---|
1271 |
|
---|
1272 | for(unsigned int u=0; u < NRADIONS; u++)
|
---|
1273 | {
|
---|
1274 | entryPointFile << bushStarters.at(u);
|
---|
1275 | entryPointFile << "\n";
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | entryPointFile << "Clusters ";
|
---|
1279 | entryPointFile << NCLUSTERS;
|
---|
1280 | entryPointFile << "\n";
|
---|
1281 |
|
---|
1282 | for(unsigned int c=0; c < NCLUSTERS; c++)
|
---|
1283 | {
|
---|
1284 | // entryPointFile.write((char*)&clusterLenghts[c], sizeof(unsigned int));
|
---|
1285 | entryPointFile << clusterLenghts[c] << '\n';
|
---|
1286 | }
|
---|
1287 | entryPointFile << "\n";
|
---|
1288 |
|
---|
1289 | entryPointFile.flush();
|
---|
1290 | entryPointFile.close();
|
---|
1291 |
|
---|
1292 | unsigned int iEntity = 0;
|
---|
1293 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
1294 | while(entityIterator != entities.end())
|
---|
1295 | {
|
---|
1296 | (*entityIterator)->savePRM();
|
---|
1297 |
|
---|
1298 | entityIterator++;
|
---|
1299 | iEntity++;
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | void PathMapEffect::loadPathMaps()
|
---|
1304 | {
|
---|
1305 | /* std::fstream entryPointFile("prm\\prmEntryPoints.dat", std::ios::in | std::ios::binary);
|
---|
1306 |
|
---|
1307 | bushStarters.clear();
|
---|
1308 |
|
---|
1309 | for(unsigned int u=0; u < NRADIONS; u++)
|
---|
1310 | {
|
---|
1311 | Radion ri;
|
---|
1312 | entryPointFile >> ri;
|
---|
1313 | bushStarters.push_back(ri);
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | for(unsigned int c=0; c < NCLUSTERS; c++)
|
---|
1317 | {
|
---|
1318 | entryPointFile.read((char*)&clusterLenghts[c], sizeof(unsigned int));
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | entryPointFile.close();*/
|
---|
1322 |
|
---|
1323 | char eFileName[512];
|
---|
1324 | sprintf(eFileName, "%s\\prmEntryPoints.text", LC::c-this->prmDirectory);
|
---|
1325 |
|
---|
1326 | std::fstream entryPointFile(eFileName, std::ios::in);
|
---|
1327 |
|
---|
1328 | char keyword[256];
|
---|
1329 |
|
---|
1330 | unsigned int redrad;
|
---|
1331 | entryPointFile >> keyword; //nradions
|
---|
1332 | entryPointFile >> redrad;
|
---|
1333 |
|
---|
1334 | bushStarters.clear();
|
---|
1335 |
|
---|
1336 | for(unsigned int u=0; u < NRADIONS; u++)
|
---|
1337 | {
|
---|
1338 | Radion ri;
|
---|
1339 | entryPointFile >> ri;
|
---|
1340 | bushStarters.push_back(ri);
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | entryPointFile >> keyword; //ncluster
|
---|
1344 | entryPointFile >> redrad;
|
---|
1345 |
|
---|
1346 | for(unsigned int c=0; c < NCLUSTERS; c++)
|
---|
1347 | {
|
---|
1348 | entryPointFile >> clusterLenghts[c];
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | entryPointFile.close();
|
---|
1352 |
|
---|
1353 | srand(0xef23ab32);
|
---|
1354 | unsigned int iEntity = 0;
|
---|
1355 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
1356 | while(entityIterator != entities.end())
|
---|
1357 | {
|
---|
1358 | (*entityIterator)->findNearClusters(bushStarters, NCLUSTERS);
|
---|
1359 | (*entityIterator)->loadPRM();
|
---|
1360 |
|
---|
1361 | entityIterator++;
|
---|
1362 | iEntity++;
|
---|
1363 | }
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | void PathMapEffect::loadScene(const char* sceneFileName)
|
---|
1367 | {
|
---|
1368 | std::fstream sceneFile(sceneFileName, std::ios::in);
|
---|
1369 |
|
---|
1370 | char keyword[256];
|
---|
1371 | while(!sceneFile.eof())
|
---|
1372 | {
|
---|
1373 | sceneFile >> keyword;
|
---|
1374 | if(strcmp(keyword, "mesh") == 0)
|
---|
1375 | {
|
---|
1376 | int meshAtlasSize = 128;
|
---|
1377 | bool needsUV = true;
|
---|
1378 | unsigned int originalAtlasTexCoordIndex = 0;
|
---|
1379 | bool needsTBN = false;
|
---|
1380 | int dividePcs = 1;
|
---|
1381 | char meshname[256];
|
---|
1382 | wchar_t* wide = NULL;
|
---|
1383 | wchar_t* wideXML = NULL;
|
---|
1384 | wchar_t* wideOgre = NULL;
|
---|
1385 | Vector color = Vector::RGBWHITE;
|
---|
1386 |
|
---|
1387 | sceneFile >> meshname;
|
---|
1388 | if(strcmp(meshname, "{") == 0)
|
---|
1389 | strcpy(meshname, "mesh");
|
---|
1390 | while(strcmp(keyword, "}") != 0 && !sceneFile.eof())
|
---|
1391 | {
|
---|
1392 | sceneFile >> keyword;
|
---|
1393 | if(strcmp(keyword, "xfile") == 0)
|
---|
1394 | {
|
---|
1395 | sceneFile >> keyword;
|
---|
1396 | if(keyword)
|
---|
1397 | {
|
---|
1398 | wide = L::clone(keyword);
|
---|
1399 | }
|
---|
1400 | }
|
---|
1401 | if(strcmp(keyword, "ogreXMLfile") == 0)
|
---|
1402 | {
|
---|
1403 | sceneFile >> keyword;
|
---|
1404 | if(keyword)
|
---|
1405 | {
|
---|
1406 | wideXML = L::clone(keyword);
|
---|
1407 | }
|
---|
1408 | }
|
---|
1409 | if(strcmp(keyword, "ogrefile") == 0)
|
---|
1410 | {
|
---|
1411 | sceneFile >> keyword;
|
---|
1412 | if(keyword)
|
---|
1413 | {
|
---|
1414 | wideOgre = L::clone(keyword);
|
---|
1415 | }
|
---|
1416 | }
|
---|
1417 | if(strcmp(keyword, "pathmapresolution") == 0)
|
---|
1418 | {
|
---|
1419 | sceneFile >> meshAtlasSize;
|
---|
1420 | }
|
---|
1421 | if(strcmp(keyword, "divide") == 0)
|
---|
1422 | {
|
---|
1423 | sceneFile >> dividePcs;
|
---|
1424 | }
|
---|
1425 | if(strcmp(keyword, "atlascoord") == 0)
|
---|
1426 | {
|
---|
1427 | needsUV = false;
|
---|
1428 | sceneFile >> originalAtlasTexCoordIndex;
|
---|
1429 | }
|
---|
1430 | }
|
---|
1431 | if(wideXML)
|
---|
1432 | {
|
---|
1433 | loadMesh(Mesh::OgreXMLMesh, wideXML, wideOgre, meshAtlasSize, meshname, dividePcs, needsUV, needsTBN, originalAtlasTexCoordIndex);
|
---|
1434 | delete [] wideXML;
|
---|
1435 | }
|
---|
1436 | else if(wide)
|
---|
1437 | {
|
---|
1438 | loadMesh(Mesh::DirectXMesh, wide, wideOgre, meshAtlasSize, meshname, dividePcs, needsUV, needsTBN, originalAtlasTexCoordIndex);
|
---|
1439 | delete [] wide;
|
---|
1440 | }
|
---|
1441 | else
|
---|
1442 | if(wideOgre)
|
---|
1443 | delete [] wideOgre;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | if(strcmp(keyword, "entity") == 0)
|
---|
1447 | {
|
---|
1448 | char entityName[256];
|
---|
1449 | char prmFileName[256];
|
---|
1450 | Mesh* baseMesh = NULL;
|
---|
1451 | D3DXMATRIX trafo;
|
---|
1452 | D3DXMatrixIdentity(&trafo);
|
---|
1453 | int nNearClusters = 16;
|
---|
1454 |
|
---|
1455 | sceneFile >> entityName;
|
---|
1456 | if(strcmp(entityName, "{") == 0)
|
---|
1457 | strcpy(entityName, "noname");
|
---|
1458 |
|
---|
1459 | while(strcmp(keyword, "}") != 0 && !sceneFile.eof())
|
---|
1460 | {
|
---|
1461 | sceneFile >> keyword;
|
---|
1462 | if(strcmp(keyword, "pathmapfile") == 0)
|
---|
1463 | {
|
---|
1464 | sceneFile >> keyword;
|
---|
1465 | strcpy(prmFileName, keyword);
|
---|
1466 | }
|
---|
1467 | if(strcmp(keyword, "mesh") == 0)
|
---|
1468 | {
|
---|
1469 | sceneFile >> keyword;
|
---|
1470 |
|
---|
1471 | std::vector<Mesh*>::iterator i = meshes.begin();
|
---|
1472 | while(i != meshes.end())
|
---|
1473 | {
|
---|
1474 | if(strcmp((*i)->getName(), keyword) == 0)
|
---|
1475 | {
|
---|
1476 | baseMesh = *i;
|
---|
1477 | break;
|
---|
1478 | }
|
---|
1479 | i++;
|
---|
1480 | }
|
---|
1481 | }
|
---|
1482 | if(strcmp(keyword, "transformation") == 0)
|
---|
1483 | {
|
---|
1484 | for(int i=0; i<16; i++)
|
---|
1485 | sceneFile >> trafo.m[i%4][i/4];
|
---|
1486 | }
|
---|
1487 | if(strcmp(keyword, "pathmapclusters") == 0)
|
---|
1488 | {
|
---|
1489 | sceneFile >> nNearClusters;
|
---|
1490 | }
|
---|
1491 | }
|
---|
1492 | if(baseMesh)
|
---|
1493 | {
|
---|
1494 | Entity* e = new Entity(baseMesh, entityName, prmFileName, trafo, nNearClusters);
|
---|
1495 | entities.push_back(e);
|
---|
1496 | }
|
---|
1497 | }
|
---|
1498 | }
|
---|
1499 | sceneFile.close();
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 |
|
---|
1503 | void PathMapEffect::renderScene(const RenderStrategy& renderStrategy)
|
---|
1504 | {
|
---|
1505 | renderStrategy.applyTargets();
|
---|
1506 |
|
---|
1507 | renderStrategy.applyRenderState();
|
---|
1508 |
|
---|
1509 | if( SUCCEEDED( device->BeginScene() ) )
|
---|
1510 | {
|
---|
1511 | renderStrategy.applyTechnique();
|
---|
1512 | UINT nPasses;
|
---|
1513 | effect->Begin( &nPasses, 0 );
|
---|
1514 | for(UINT j=0;j<nPasses;j++)
|
---|
1515 | {
|
---|
1516 | effect->BeginPass(j);
|
---|
1517 |
|
---|
1518 | //loop through all entities
|
---|
1519 | std::vector<Entity*>::iterator entityIterator = entities.begin();
|
---|
1520 | while(entityIterator != entities.end())
|
---|
1521 | {
|
---|
1522 |
|
---|
1523 | (*entityIterator)->drawAllSubEntities(renderStrategy);
|
---|
1524 | entityIterator++;
|
---|
1525 | }
|
---|
1526 |
|
---|
1527 | effect->EndPass();
|
---|
1528 | }
|
---|
1529 | effect->End();
|
---|
1530 | device->EndScene();
|
---|
1531 | }
|
---|
1532 |
|
---|
1533 | renderStrategy.resetRenderState();
|
---|
1534 |
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 | void PathMapEffect::saveScene(const char* sceneFileName)
|
---|
1538 | {
|
---|
1539 | std::ofstream psf(sceneFileName);
|
---|
1540 |
|
---|
1541 | std::vector<Mesh*>::iterator iMesh = meshes.begin();
|
---|
1542 | while(iMesh != meshes.end())
|
---|
1543 | {
|
---|
1544 | (*iMesh)->saveSceneInfo(psf);
|
---|
1545 | iMesh++;
|
---|
1546 | }
|
---|
1547 |
|
---|
1548 | std::vector<Entity*>::iterator iEntity = entities.begin();
|
---|
1549 | while(iEntity != entities.end())
|
---|
1550 | {
|
---|
1551 | (*iEntity)->saveSceneInfo(psf);
|
---|
1552 | iEntity++;
|
---|
1553 | }
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 | const wchar_t* PathMapEffect::getCurrentMethodName()
|
---|
1557 | {
|
---|
1558 | int lmode = parameters->GetInt(fIndirectLightingMode);
|
---|
1559 | if(lmode == 3)
|
---|
1560 | return L"PRM only";
|
---|
1561 | if(lmode == 2)
|
---|
1562 | return L"direct + PRM";
|
---|
1563 | if(lmode == 1)
|
---|
1564 | return L"direct + ambient";
|
---|
1565 | if(lmode == 0)
|
---|
1566 | return L"direct only";
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | wchar_t* PathMapEffect::getCruiseLoop()
|
---|
1570 | {
|
---|
1571 | static wchar_t s[4];
|
---|
1572 | s[1] = 0;
|
---|
1573 |
|
---|
1574 | s[0] = L'0' + cruiseLoop;
|
---|
1575 |
|
---|
1576 | return s;
|
---|
1577 | } |
---|