source: GTP/trunk/App/Games/Jungle_Rumble/src/Scene.cpp @ 1396

Revision 1396, 44.9 KB checked in by giegl, 18 years ago (diff)

GTPD - Jungle Rumble - before sceneFader.fx, .obj fix

Line 
1#include "dxstdafx.h"
2#include ".\scene.h"
3#include "GameManager.h"
4#include "Object3d.h"
5#include "Box.h"
6#include "Sprite.h"
7#include "ParticleGroup.h"
8#include "ParticleEmitter.h"
9#include "SoundNode.h"
10#include "SharedResourceTexture.h"
11#include "fmod.h"
12
13Scene::Scene(void)
14{
15        this->sceneAlpha = 1;
16        this->root.setScene(*this);
17        this->sceneLifeTime = 0;
18        this->usePhysXDebugger = true;
19
20        //Fade Stuff
21        this->visible = true;
22        this->fadeState = NOFADE;
23
24        //Gravity
25        this->pDefaultGravity.x = 0;
26        this->pDefaultGravity.y = -9.8f;
27        this->pDefaultGravity.z = 0;
28
29        //Frustum Stuff
30        this->enableFrustumCulling = true;
31        this->drawBBoxes = true;
32        this->boundingBox = NULL;
33        this->quadTreeBuilt = false;
34
35        //RenderChain Timing usw
36        QueryPerformanceFrequency(&this->frequency);
37        this->firstFrame = true;
38       
39        //PhysicStuff
40        this->pScene = NULL;
41        D3DXMatrixIdentity(&this->worldMatrix);
42        D3DXMatrixIdentity(&this->identMatrix);
43       
44        this->root.myScene = this;
45        this->contactReport = NULL;
46        this->triggerReport = NULL;
47        this->notifyReport = NULL;
48
49        //Get the Device
50        this->device = DXUTGetD3DDevice();
51        this->physxRenderer.setDevice(*this->device);
52
53        //Sun Stuff
54        this->sunDirection.setXYZ(-1.0f, -2.0f, 1.0f);
55        this->specialRenderer = NULL;
56
57        //DefaultRenderPass konfigurieren
58        this->activeRenderPass = NULL;
59
60        //Particle Group Stuff
61        this->particleGroupCounter = 0;
62
63        this->finalImage = NULL;
64        this->preDistortionFinal = NULL;
65        this->distortionMap1 = NULL;
66        this->distortionMap2 = NULL;
67        this->screenDistortionMap = NULL;
68        this->quadMesh = NULL;
69        this->meshCreated = false;
70        this->depthTextureResource = NULL;
71        this->useDepthImposter = true;
72        this->useRaytracer = true;
73
74        this->muteMusic = false;
75}
76
77Scene::~Scene(void)
78{
79        SAFE_RELEASE(this->finalImage);
80        SAFE_RELEASE(this->finalImageSurface);
81        SAFE_RELEASE(this->quadMesh);
82        SAFE_RELEASE(this->depthSurface);
83        SAFE_RELEASE(this->preDistortionFinal);
84        SAFE_RELEASE(this->preDistortionFinalSurface);
85        SAFE_RELEASE(this->screenDistortionMapSurface);
86}
87
88void Scene::initScene(GameManager &_manager)
89{
90        HRESULT hr;
91       
92        this->manager = &_manager;
93       
94        //Textur für endgültiges Bild erzeugen
95        if(!this->finalImage) {
96                //Create Texture
97                V( DXUTGetD3DDevice()->CreateTexture(this->manager->screenWidth, this->manager->screenHeight, 1, D3DUSAGE_RENDERTARGET,
98                //V( DXUTGetD3DDevice()->CreateTexture(this->manager->screenWidth, this->manager->screenHeight+1, 1, D3DUSAGE_RENDERTARGET,
99                D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &this->finalImage, NULL ) );
100
101                //Get Surface for RenderTarget
102                this->finalImage->GetSurfaceLevel(0, &this->finalImageSurface);
103        }
104       
105        this->sceneLifeTime = 0;
106        this->firstFrame = true;
107       
108        this->defaultParticleGroup = (ParticleGroup*) this->createNode(Scene::NODE_PARTICLEGROUP, false);
109        this->initPhysic();
110        this->boundingBox = (Box*) this->createNode(Scene::NODE_BOX);
111        this->boundingBox->setVisible(false);
112        this->boundingBox->setStandBy(true);
113       
114        //Camera Stuff
115        this->defaultCamera = (Camera *) this->createNode(this->NODE_CAMERA);
116        this->activeCamera = this->defaultCamera;
117
118        //Default RenderPass
119        this->defaultRenderPass.setSafeAndRestoreRenderTarget(false);
120        this->defaultRenderPass.setScene(*this);
121        this->defaultRenderPass.setRenderPassId(Scene::PASS_NORMAL);
122        this->defaultRenderPass.setRenderTarget(this->finalImageSurface);
123        this->defaultRenderPass.execute(RenderPass::CMD_CLEARRENDERTARGETANDZBUFFER);
124        this->defaultRenderPass.execute(RenderPass::CMD_RENDERNODES);
125       
126        //Depth RenderPass
127        this->depthTextureResource = (SharedResourceTexture*) this->getSharedResource(Scene::SR_TEXTURE_VIEWDEPTH);
128        if(!this->depthTextureResource) {
129                if(this->depthSurface){
130                        SAFE_RELEASE (this->depthSurface);
131                }
132                this->depthTextureResource = (SharedResourceTexture*) this->createSharedResource(Scene::SR_TEXTURE_VIEWDEPTH);
133                this->depthTextureResource->getTexture()->GetSurfaceLevel(0, &this->depthSurface);
134        }
135       
136        //Load DistortionMaps
137        if(!this->distortionMap1)
138                this->distortionMap1 = this->manager->resManager.loadTexture("./media/textures/heatnoise.tga");
139        if(!this->distortionMap2)
140                this->distortionMap2 = this->manager->resManager.loadTexture("./media/textures/heatnoise2.tga");
141        if(!this->screenDistortionMap) {
142                SharedResourceTexture* distMap = (SharedResourceTexture*) this->createSharedResource(Scene::SR_TEXTURE_DISTORTION);
143                this->screenDistortionMap = distMap->getTexture();
144                this->screenDistortionMap->GetSurfaceLevel(0, &this->screenDistortionMapSurface);
145        }
146
147        if(!this->preDistortionFinal) {
148                //Create Texture
149                V( DXUTGetD3DDevice()->CreateTexture(this->manager->screenWidth, this->manager->screenHeight, 1, D3DUSAGE_RENDERTARGET,
150                D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &this->preDistortionFinal, NULL ) );
151
152                //Get Surface for RenderTarget
153                this->preDistortionFinal->GetSurfaceLevel(0, &this->preDistortionFinalSurface);
154        }
155
156        this->depthRenderPass.setSafeAndRestoreRenderTarget(false);
157        this->depthRenderPass.setScene(*this);
158        this->depthRenderPass.setRenderPassId(Scene::PASS_DEPTH);
159        this->depthRenderPass.setRenderTarget(this->depthSurface);
160        this->depthRenderPass.execute(RenderPass::CMD_CLEARRENDERTARGETANDZBUFFER);
161        this->depthRenderPass.execute(RenderPass::CMD_RENDERNODES);
162        this->depthRenderPass.useThisRendererList(this->defaultRenderPass.usedRendererList);
163
164        //Raytrace RenderPass
165        this->raytraceRenderPass.setSafeAndRestoreRenderTarget(false);
166        this->raytraceRenderPass.setScene(*this);
167        this->raytraceRenderPass.setRenderPassId(Scene::PASS_RAYTRACE);
168        this->raytraceRenderPass.execute(RenderPass::CMD_CLEARRENDERTARGETANDZBUFFER);
169        this->raytraceRenderPass.execute(RenderPass::CMD_DOFRUSTUMCULLING);
170        this->raytraceRenderPass.execute(RenderPass::CMD_RENDERNODES);
171
172}
173
174float Scene::getSceneLiveTime()
175{
176        return this->sceneLifeTime;
177}
178
179void Scene::setVisible(bool _visible)
180{
181        this->visible = _visible;
182}
183
184bool Scene::isVisible()
185{
186        return this->visible;
187}
188void Scene::fadeIn(float _duration)
189{
190        this->fadeDuration = _duration;
191        this->fadeTimer = 0;
192        this->fadeState = FADEIN;
193        this->setVisible(true);
194}
195void Scene::fadeOut(float _duration)
196{
197        this->fadeDuration = _duration;
198        this->fadeTimer = 0;
199        this->fadeState = FADEOUT;
200}
201
202void Scene::setSceneAlpha(float _alpha)
203{
204        if(_alpha>1) _alpha = 1;
205        if(_alpha<0) _alpha = 0;
206        this->sceneAlpha = _alpha;
207}
208
209Node* Scene::getRoot()
210{
211        return &this->root;
212}
213
214void Scene::clearScene() {
215        this->root.removeAllChildren();
216        this->cleanUpScene();
217       
218        this->nodeList.clear();
219        this->particleList.clear();
220        this->rendererList.clear();
221        this->refObject3dList.clear();
222        this->refCameraList.clear();
223        this->refSoundList.clear();
224        this->triggerList.clear();
225        this->particleGroupVector.clear();
226        this->clearSharedResources();
227        this->sharedResourceVector.clear();
228        this->visibleNodeList.clear();
229        this->soundNodeList.clear();
230        //this->usedRendererList.clear();
231        this->zombieNodeList.clear();
232
233        if(this->manager && this->manager->pPhysicsSDK) {
234                this->manager->pPhysicsSDK->releaseScene(*this->pScene);
235                this->pScene = NULL;
236        }
237}
238
239void Scene::setWidth(float _w)
240{
241        this->width = _w;
242}
243
244void Scene::setHeight(float _h)
245{
246        this->height = _h;
247}
248
249void Scene::setDepth(float _d)
250{
251        this->depth = _d;
252}
253
254float Scene::getWidth()
255{
256        return this->width;
257}
258
259float Scene::getHeight()
260{
261        return this->height;
262}
263
264float Scene::getDepth()
265{
266        return this->depth;
267}
268
269Camera* Scene::getActiveCamera()
270{
271        return this->activeCamera;
272}
273
274NxSceneDesc* Scene::getPhysicSceneDescriptor()
275{
276        return &this->sceneDesc;
277}
278
279bool Scene::initPhysic()
280{
281    // Create the scene
282        this->manager->pPhysicsSDK->setParameter(NX_SKIN_WIDTH, (NxReal)0.01);
283    if(this->usePhysXDebugger) {
284                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZATION_SCALE, 5.0f);
285                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1);
286                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_ACTOR_AXES, 1);
287                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_LOCAL_AXES, 1.0f);
288                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_WORLD_AXES, 1);
289                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_ERROR, 1);
290                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_LIMITS, 1);
291                this->manager->pPhysicsSDK->setParameter(NX_VISUALIZE_JOINT_FORCE, 1);
292        }
293        this->sceneDesc.gravity = pDefaultGravity;
294    this->sceneDesc.collisionDetection = true;
295        if(this->pScene!=NULL) {
296                this->manager->pPhysicsSDK->releaseScene(*this->pScene);
297        }
298    this->pScene = this->manager->pPhysicsSDK->createScene(this->sceneDesc);
299        if(this->contactReport) this->pScene->setUserContactReport(this->contactReport);
300        if(this->triggerReport) this->pScene->setUserTriggerReport(this->triggerReport);
301        if(this->notifyReport) this->pScene->setUserNotify(this->notifyReport);
302        this->pScene->setTiming(1.0f/60.0f, 8, NX_TIMESTEP_FIXED);
303       
304        // Create the default material
305    NxMaterial* defaultMaterial = this->pScene->getMaterialFromIndex(0);
306    defaultMaterial->setRestitution(0.5);
307    defaultMaterial->setStaticFriction(0.5);
308    defaultMaterial->setDynamicFriction(0.5);
309
310        return true;
311}
312
313float Scene::getDeltaTime()
314{
315        return this->dt;
316}
317
318void Scene::buildQuadTree()
319{
320        this->fcQuad.setDimension(this->getWidth(), this->getWidth());
321        this->fcQuad.setPosition(this->getWidth()/2, this->getWidth()/2);
322        this->fcQuad.buildQuadTree(QUADLEVEL);
323        this->quadTreeBuilt = true;
324}
325
326RenderPass* Scene::getActiveRenderPass()
327{
328        return this->activeRenderPass;
329}
330
331void Scene::renderScene(float fElapsedTime)
332{
333        if(this->isVisible()) {
334                if(this->device==0) {
335                        this->device = DXUTGetD3DDevice();
336                        this->physxRenderer.setDevice(*this->device);
337                }
338                this->device->SetRenderTarget(0, this->finalImageSurface);
339
340                this->takeTime();
341                //ClearScreen
342                //this->clearRenderTargetAndZBuffer();
343                //Clear SharedResources
344                this->clearSharedResources();
345                //Fetch Physic Results
346                this->fetchPhysicResults();
347                //Fading Stuff
348                this->doFade();
349                //Trigger updates
350                this->updateTrigger();
351                //Update Nodes
352                this->updateNodes();
353                //Calculate Transformations
354                this->calculateTransformations();
355               
356                //startRenderPasses
357                this->startRenderPasses();
358
359                //Cleanup Memory
360                this->cleanUpScene();
361                //Start Physic
362                this->startPhysic();
363
364                this->firstFrame = false;
365        }
366}
367
368void Scene::takeTime()
369{
370        //Take time
371        if(this->firstFrame) {
372                QueryPerformanceCounter(&this->currentFrame);
373                this->lastFrame = this->currentFrame;
374                return;
375        } else {
376                this->lastFrame = this->currentFrame;
377                QueryPerformanceCounter(&this->currentFrame);
378        }
379        double tempdt;
380        tempdt = (double)(this->currentFrame.QuadPart-this->lastFrame.QuadPart)/(double)this->frequency.QuadPart;
381        this->dt = (float) tempdt;     
382        this->dt = min(0.033f, this->dt);
383}
384
385void Scene::fetchPhysicResults()
386{
387        if(!this->firstFrame) {
388                this->pScene->fetchResults(NX_RIGID_BODY_FINISHED, true);
389        }
390}
391
392void Scene::doFade()
393{
394        switch(this->fadeState)
395        {
396                case FADEOUT:
397                        this->fadeTimer+=this->dt;
398                        this->sceneAlpha -= this->dt/this->fadeDuration;
399                        if(this->sceneAlpha<=0) {
400                                this->setVisible(false);
401                                this->fadeState = NOFADE;
402                                this->sceneAlpha = 0;
403                        }
404                        this->updateSoundNodeVolume();
405                        break;
406                case FADEIN:
407                        if(!this->visible)
408                                this->setVisible(true);
409                        this->fadeTimer+=this->dt;
410                        this->sceneAlpha+=this->dt/this->fadeDuration;
411                        if(this->sceneAlpha>=1) {
412                                this->fadeState = NOFADE;
413                                this->sceneAlpha = 1;
414                        }
415                        this->updateSoundNodeVolume();
416                        break;
417                default:
418                        break;
419        }
420}
421
422void Scene::updateTrigger()
423{
424        this->sceneLifeTime+=this->getDeltaTime();
425        std::vector<SPTR<Trigger> >::iterator it;
426        for(it=this->triggerList.begin();it!=this->triggerList.end();it++) {
427                if(this->sceneLifeTime>(*it)->getTime()) {
428                        this->executeTrigger(*it);
429                        this->triggerList.erase(it);
430                        it--;
431                } else {
432                        return;
433                }
434        }
435
436        for(UINT i=0;i<this->triggerList.size();i++) {
437                if(this->sceneLifeTime>this->triggerList.at(i)->getTime()) {
438                        this->executeTrigger(this->triggerList.at(i));
439                        i--;
440                } else {
441                        return;
442                }
443        }
444}
445
446void Scene::updateNodes()
447{
448        this->root.update(this->dt);
449}
450
451
452void Scene::calculateTransformations()
453{
454        //Object transformation
455        this->root.calcWorldMatrix(this->worldMatrix);
456        this->root.updateBBox();
457}
458
459
460
461void Scene::doFrustumCulling()
462{
463        Camera cam;
464        cam.setViewMatrix(*this->activeRenderPass->getViewMatrix());
465        cam.setProjectionMatrix(*this->activeRenderPass->getProjectionMatrix());
466   
467        std::vector<Renderer *>* usedRendererList;
468        usedRendererList = this->activeRenderPass->usedRendererList;
469
470        std::list<SPTR<Node> >::iterator nodeIt;
471        Node* node;
472
473        if(!this->quadTreeBuilt)
474                this->buildQuadTree();
475        usedRendererList->clear();
476
477        Renderer *renderer;
478
479        if(this->enableFrustumCulling) {
480                Plane view[4];
481               
482                view[0] = cam.getNearPlane();
483                view[1] = cam.getFarPlane();
484                view[2] = cam.getLeftPlane();
485                view[3] = cam.getRightPlane();
486                this->fcQuad.clipQuadAgainstCamera(&view[0]);
487
488                for(nodeIt = this->nodeList.begin(); nodeIt!=this->nodeList.end();nodeIt++) {
489                        node = (*nodeIt).get();
490                        if(!node->onStandBy() && node->visible && node->hasRenderer() &&  node->testAgainstFrustum && this->fcQuad.nodeInside(node)) { 
491                                renderer = node->getRenderer().get();
492                                if(this->activeRenderPass == &this->defaultRenderPass) {
493                                        if(renderer->isA(Renderer::RENDERER_PARTICLE)) {
494                                                this->needDepthPass |= renderer->needsExtraPass();
495                                                if(((ParticleRenderer*) renderer)->needHeatHazePass()) {
496                                                        this->needDistortionPass = true;
497                                                        this->heatHazeParticleRendererList.push_back((ParticleRenderer*) renderer);
498                                                }
499                                        } else if(renderer->isA(Renderer::RENDERER_RAYTRACE)) {
500                                                this->needRaytracePass = true;
501                                                this->raytraceRendererList.push_back((RaytraceRenderer*) renderer);
502                                        }
503                                }
504                                usedRendererList->push_back(renderer);
505                        } else if(!node->onStandBy() && node->visible && node->hasRenderer() && !node->testAgainstFrustum) {
506                                renderer = node->getRenderer().get();
507                                if(this->activeRenderPass == &this->defaultRenderPass) {
508                                        if(renderer->isA(Renderer::RENDERER_PARTICLE)) {
509                                                this->needDepthPass |= renderer->needsExtraPass();
510                                                if(((ParticleRenderer*) renderer)->needHeatHazePass()) {
511                                                        this->needDistortionPass = true;
512                                                        this->heatHazeParticleRendererList.push_back((ParticleRenderer*) renderer);
513                                                }
514                                        } else if(renderer->isA(Renderer::RENDERER_RAYTRACE)) {
515                                                this->needRaytracePass = true;
516                                                this->raytraceRendererList.push_back((RaytraceRenderer*) renderer);
517                                        }
518                                }
519                                usedRendererList->push_back(renderer);
520                        }
521                }
522        } else {       
523                for(nodeIt = this->nodeList.begin(); nodeIt!=this->nodeList.end();nodeIt++) {
524                        node = (*nodeIt).get();
525                        if(!node->onStandBy() && node->visible && node->hasRenderer()) {       
526                                renderer = node->getRenderer().get();
527                                if(this->activeRenderPass == &this->defaultRenderPass) {
528                                        if(renderer->isA(Renderer::RENDERER_PARTICLE)) {
529                                                this->needDepthPass |= renderer->needsExtraPass();
530                                                if(((ParticleRenderer*) renderer)->needHeatHazePass()) {
531                                                        this->needDistortionPass = true;
532                                                        this->heatHazeParticleRendererList.push_back((ParticleRenderer*) renderer);
533                                                }
534                                        } else if(renderer->isA(Renderer::RENDERER_RAYTRACE)) {
535                                                this->needRaytracePass = true;
536                                                this->raytraceRendererList.push_back((RaytraceRenderer*) renderer);
537                                        }
538                                }
539                                usedRendererList->push_back(renderer);
540                        }
541                }
542        }
543}
544
545void Scene::doViewingTransformation()
546{
547        this->device->SetTransform(D3DTS_VIEW, this->activeCamera->getViewMatrix() );
548}
549
550
551void Scene::renderNodes()
552{
553        std::vector<Renderer *>* usedRendererList;
554        usedRendererList = this->activeRenderPass->usedRendererList;
555
556        //Sorts the rendering processes by its priority!
557        std::sort(usedRendererList->begin(), usedRendererList->end(), renderSortFunction());
558
559        std::vector<Renderer *>::iterator it;
560        for(it=usedRendererList->begin();it!=usedRendererList->end();it++) {
561                if((*it) == 0) {
562                        this->manager->printToConsole("Renderer in usedRenderList is NULL!!!!");
563                }
564                if((*it)->nodeAdded) {
565                        SPTR<Node> n = (*it)->myNode.lock();
566                        if(!n->onStandBy() && n->visible) {
567                                this->worldMatrix = *n->getWorldMatrix();
568                                (*it)->render();
569                        }
570                }
571        }
572        if(this->usePhysXDebugger && this->activeRenderPass==&this->defaultRenderPass) {
573                D3DXMATRIX worldMat;
574                D3DXMatrixIdentity(&worldMat);
575                this->device->SetTransform( D3DTS_WORLD, &worldMat);
576                this->device->SetTransform( D3DTS_PROJECTION, this->activeCamera->getProjectionMatrix());
577                this->device->SetTransform( D3DTS_VIEW, this->activeCamera->getViewMatrix());
578                const NxDebugRenderable *dbgRenderable = pScene->getDebugRenderable();
579                this->physxRenderer.render(*dbgRenderable);
580                this->device->SetTransform( D3DTS_PROJECTION, &worldMat);
581                this->device->SetTransform( D3DTS_VIEW, &worldMat);
582        }
583        /*D3DXMatrixIdentity(&this->worldMatrix);
584        if(this->drawBBoxes && this->boundingBox!=NULL) {
585                for(it=this->usedRendererList.begin();it!=this->usedRendererList.end();it++) {
586                        SPTR<Node> n = (*it)->myNode.lock();
587                        D3DXMATRIX tMat;
588                        Vector dim = *n->getAbsMaxBBox() - *n->getAbsMinBBox();
589                        Vector absPos;
590                        absPos = *n->getAbsMinBBox()+dim/2;
591                        D3DXMatrixTranslation(&tMat, absPos.x, absPos.y, absPos.z);
592                        this->device->SetTransform( D3DTS_WORLD, &tMat);
593                       
594                        this->boundingBox->setDimension(dim.x,  dim.y, dim.z);
595                        this->boundingBox->getRenderer()->render();
596                }
597        }*/
598}
599
600void Scene::cleanUpScene()
601{
602        std::list<SPTR<Node> >::iterator it;
603        std::list<SPTR<Node> >::iterator nit;
604        std::vector<SPTR<Renderer> >::iterator rit;
605        Node* node;
606        for(it=this->zombieNodeList.begin();it!=this->zombieNodeList.end();it++) {
607                node = (*it).get();
608                node->getFather()->removeChild(*it);
609
610                //Delete Renderer
611                if(node->hasRenderer())
612                {
613                        Renderer *r = node->getRenderer().get();
614                        for(rit=this->rendererList.begin();rit!=this->rendererList.end();rit++) {
615                                if((*rit).get() == r)
616                                {
617                                        this->rendererList.erase(rit);
618                                        break;
619                                }
620                        }
621                }
622
623                //Delete Physic Actor
624                if(node->getActor()!=NULL)
625                {
626                        this->pScene->releaseActor(*node->getActor());
627                }
628
629                //Delete from NodeList
630                this->deleteNodeInList(node, this->nodeList);
631                this->deleteNodeInList(node, this->particleList);
632                this->deleteNodeInList(node, this->soundNodeList);
633        }
634        this->zombieNodeList.clear();
635}
636
637void Scene::deleteNodeInList(Node* node, std::list<SPTR<Node> > &list) {
638        std::list<SPTR<Node> >::iterator it;
639        for(it=list.begin();it!=list.end();it++) {
640                if((*it).get() == node)
641                {
642                        list.erase(it);
643                        break;
644                }
645        }
646}
647
648void Scene::deleteRendererInList(Renderer* renderer) {
649        // remove particle renderer
650        std::vector<SPTR<Renderer> >::iterator it;
651        for(it=rendererList.begin();it!=rendererList.end();it++) {
652                if((*it).get() == renderer) {
653                        rendererList.erase(it);
654                        break;
655                }
656        }
657}
658
659void Scene::startPhysic()
660{
661        this->pScene->simulate(this->dt);
662        this->pScene->flushStream();
663}
664
665void Scene::clearRenderTargetAndZBuffer()
666{
667        DXUTGetD3DDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
668}
669
670//Clear SharedResources
671void Scene::clearSharedResources()
672{
673        for(UINT i=0;i<this->sharedResourceVector.size();i++) {
674                this->sharedResourceVector.at(i)->setResourceAvailable(false);
675        }
676}
677
678void Scene::setKeyPressed(UINT key, bool bKeyPressed)
679{
680        this->keyDown[key] = bKeyPressed;
681}
682
683void Scene::setMouseStatus(bool bLeftButtonDown,  bool bRightButtonDown,bool bMiddleButtonDown,
684                                                bool bSideButton1Down, bool bSideButton2Down, int nMouseWheelDelta,
685                                                int xPos, int yPos)
686{
687        this->mouseButton[MOUSE_LEFT]   = bLeftButtonDown;
688        this->mouseButton[MOUSE_RIGHT]  = bRightButtonDown;
689        this->mouseButton[MOUSE_MIDDLE] = bMiddleButtonDown;
690        this->mouseButton[MOUSE_SIDE1] = bSideButton1Down;
691        this->mouseButton[MOUSE_SIDE2]  = bSideButton2Down;
692        this->mousePos[0] = xPos;
693        this->mousePos[1] = yPos;
694        this->mouseWheel = nMouseWheelDelta;
695}
696
697Node* Scene::createNode(int type)
698{
699        return this->createNode(type, this->root, true, false);
700}
701
702Node* Scene::createNode(int type, Node &father)
703{
704        return this->createNode(type, father, true, false);
705}
706
707Node* Scene::createNode(int type, bool addDefaultRenderer)
708{
709        return this->createNode(type, this->root, addDefaultRenderer, false);
710}
711
712Node* Scene::createNode(int type, Node &father, bool addDefaultRenderer) {
713        return this->createNode(type, father, addDefaultRenderer, false);
714}
715
716Node* Scene::createReferenceNode(int type, bool addDefaultRenderer)
717{
718        Node* n=0;
719        Node* newNode = this->createNode(type, *n, addDefaultRenderer, true);
720        newNode->setStandBy(true);
721        return newNode;
722}
723
724Node* Scene::createNode(int type, Node &father, bool addDefaultRenderer, bool isReference)
725{
726    switch(type)
727        {
728                case NODE_OBJECT3D:
729                        {
730                                SPTR<Node> node(new Object3d);
731                                node->setScene(*this);
732                                if(!isReference) {
733                                        father.addChild(node);
734                                        this->nodeList.push_back(node);
735                                } else {
736                                        this->refObject3dList.push_back(node);
737                                }
738                                if(addDefaultRenderer) {
739                                        SPTR<Renderer> renderer(new SimpleMeshRenderer);
740                                        renderer->setScene(*this);
741                                        WPTR<Node> wn(node);
742                                        renderer->setNode(wn);
743                                        node->setRenderer(renderer);
744                                        renderer->init();
745                                        this->rendererList.push_back(renderer);
746                                }
747                                return node.get();
748                        }
749                        break;
750                case NODE_CAMERA:
751                        {
752                                SPTR<Node> node(new Camera);
753                                if(!isReference) {
754                                        father.addChild(node);
755                                        this->nodeList.push_back(node);
756                                } else {
757                                        this->refCameraList.push_back(node);
758                                }
759                                node->setScene(*this);
760                               
761                                return node.get();
762                        }
763                        break;
764                case NODE_SOUND:
765                        {
766                                SPTR<Node> node(new SoundNode);
767                                if(!isReference) {
768                                        father.addChild(node);
769                                        this->nodeList.push_back(node);
770                                        this->soundNodeList.push_back(node);
771                                } else {
772                                        this->refSoundList.push_back(node);
773                                }
774                                node->setScene(*this);
775                               
776                                return node.get();
777                        }
778                case NODE_BOX:
779                        {
780                                SPTR<Node> node(new Box);
781                                node->setScene(*this);
782                                if(!isReference) {
783                                        father.addChild(node);
784                                        this->nodeList.push_back(node);
785                                }
786                                if(addDefaultRenderer) {
787                                        SPTR<Renderer> renderer(new SimpleMeshRenderer);
788                                        renderer->setScene(*this);
789                                        WPTR<Node> wn(node);
790                                        renderer->setNode(wn);
791                                        node->setRenderer(renderer);
792                                        renderer->init();
793                                        this->rendererList.push_back(renderer);
794                                }
795                                return node.get();
796                        }
797                        break;
798                case NODE_SPRITE:
799                        {
800                                SPTR<Node> node(new Sprite);
801                                node->setScene(*this);
802                                if(!isReference) {
803                                        father.addChild(node);
804                                        this->nodeList.push_back(node);
805                                } else {
806                                        this->refObject3dList.push_back(node);
807                                }
808                                if(addDefaultRenderer) {
809                                        SPTR<Renderer> renderer(new ParticleRenderer);
810                                        renderer->setScene(*this);
811                                        WPTR<Node> wn(node);
812                                        renderer->setNode(wn);
813                                        ((ParticleRenderer*) renderer.get())->setRenderSingleParticle(true);
814                                        node->setRenderer(renderer);
815                                        renderer->init();
816                                        this->rendererList.push_back(renderer);
817                                }
818                                return node.get();
819                        }
820                        break;
821                case NODE_PARTICLEGROUP:
822                        {
823                                SPTR<Node> node(new ParticleGroup);
824                                node->setScene(*this);
825                                if(!isReference) {
826                                        if(&father != this->getRoot()) {
827                                                this->manager->printToConsole("ERROR: Call of createNode for ParticleGroup only allows rootNode as father!!");
828                                        }
829                                        father.addChild(node);
830                                        this->nodeList.push_back(node);
831                                }
832                                if(addDefaultRenderer) {
833                                        //TODO add ParticleRenderer
834                                        SPTR<Renderer> renderer(new ParticleRenderer);
835                                        renderer->setScene(*this);
836                                        WPTR<Node> wn(node);
837                                        renderer->setNode(wn);
838                                        node->setRenderer(renderer);
839                                        renderer->init();
840                                        this->rendererList.push_back(renderer);
841                                }
842                                ((ParticleGroup*) node.get())->setParticleGroup(this->particleGroupCounter);
843                                this->particleGroupVector.push_back(node);
844                                this->particleGroupCounter++;
845                                return node.get();
846                        }
847                        break;
848                case NODE_PARTICLEEMITTER:
849                        {
850                                SPTR<Node> node(new ParticleEmitter);
851                                node->setScene(*this);
852                                if(!isReference) {
853                                        father.addChild(node);
854                                        this->nodeList.push_back(node);
855                                }
856                                return node.get();
857                        }
858                        break;
859        }
860        return 0;
861}
862
863void Scene::connectNodeAndRenderer(Node &node, SPTR<Renderer> &renderer)
864{
865        std::list<SPTR<Node> >::iterator it;
866        SPTR<Node> sN;
867        for(it = this->nodeList.begin(); it!=this->nodeList.end();it++) {
868                if((*it).get() == &node) {
869                        WPTR<Node> wn(*it);
870                        renderer->setScene(*this);
871                        renderer->setNode(wn);
872                        (*it)->setRenderer(renderer);
873                        renderer->init();
874                        this->rendererList.push_back(renderer);
875                        break;
876                }
877        }
878}
879
880void Scene::OnLostDevice( void* pUserContext )
881{
882        SAFE_RELEASE(this->finalImage);
883        SAFE_RELEASE(this->finalImageSurface);
884        SAFE_RELEASE(this->quadMesh);
885        SAFE_RELEASE(this->depthSurface);
886        SAFE_RELEASE(this->preDistortionFinal);
887        SAFE_RELEASE(this->preDistortionFinalSurface);
888        SAFE_RELEASE(this->screenDistortionMapSurface);
889        this->finalImage = NULL;
890        this->finalImageSurface = NULL;
891        this->meshCreated = false;
892        this->quadMesh = NULL;
893        this->preDistortionFinal = NULL;
894        this->preDistortionFinalSurface = NULL;
895        this->screenDistortionMapSurface = NULL;
896
897        std::list<SPTR<Node> >::iterator nit;
898        for(nit=this->nodeList.begin();nit!=this->nodeList.end();nit++) {
899                (*nit)->OnLostDevice(pUserContext);
900        }
901
902        for(UINT i=0;i<this->sharedResourceVector.size();i++) {
903                this->sharedResourceVector.at(i)->OnLostDevice();
904        }
905
906        std::vector<SPTR<Renderer> >::iterator it;
907        for(it=this->rendererList.begin();it!=this->rendererList.end();it++) {
908                (*it)->OnLostDevice(pUserContext);
909        }
910}
911
912void Scene::OnDestroyDevice( void* pUserContext )
913{
914        std::list<SPTR<Node> >::iterator nit;
915        for(nit=this->nodeList.begin();nit!=this->nodeList.end();nit++) {
916                (*nit)->OnDestroyDevice(pUserContext);
917        }
918
919        std::vector<SPTR<Renderer> >::iterator it;
920        for(it=this->rendererList.begin();it!=this->rendererList.end();it++) {
921                (*it)->OnDestroyDevice(pUserContext);
922        }
923
924        this->depthTextureResource = NULL;
925        this->sharedResourceVector.clear();
926        this->distortionMap1 = NULL;
927        this->distortionMap2 = NULL;
928        this->screenDistortionMap = NULL;
929        this->preDistortionFinal = NULL;
930}
931
932HRESULT Scene::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
933{
934        HRESULT hr;
935
936        //Restore that stuff
937        this->device = pd3dDevice;
938
939        SAFE_RELEASE(this->quadMesh);
940        SAFE_RELEASE(this->depthSurface);
941        SAFE_RELEASE(this->preDistortionFinal);
942        SAFE_RELEASE(this->preDistortionFinalSurface);
943        SAFE_RELEASE(this->screenDistortionMapSurface);
944
945
946        //Textur für endgültiges Bild erzeugen
947        if(!this->finalImage) {
948                //Create Texture
949                V( DXUTGetD3DDevice()->CreateTexture(this->manager->screenWidth, this->manager->screenHeight, 1, D3DUSAGE_RENDERTARGET,
950                D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &this->finalImage, NULL ) );
951
952                //Get Surface for RenderTarget
953                this->finalImage->GetSurfaceLevel(0, &this->finalImageSurface);
954        }
955
956        //Depth RenderPass
957        this->depthTextureResource = (SharedResourceTexture*) this->getSharedResource(Scene::SR_TEXTURE_VIEWDEPTH);
958        if(!this->depthTextureResource) {
959                this->depthTextureResource = (SharedResourceTexture*) this->createSharedResource(Scene::SR_TEXTURE_VIEWDEPTH);
960        }
961        this->depthTextureResource->getTexture()->GetSurfaceLevel(0, &this->depthSurface);
962
963        //Load DistortionMaps
964        if(!this->distortionMap1)
965                this->distortionMap1 = this->manager->resManager.loadTexture("./media/textures/heatnoise.tga");
966        if(!this->distortionMap2)
967                this->distortionMap2 = this->manager->resManager.loadTexture("./media/textures/heatnoise2.tga");
968        if(!this->screenDistortionMap) {
969                SharedResourceTexture* distMap = (SharedResourceTexture*) this->createSharedResource(Scene::SR_TEXTURE_DISTORTION);
970                this->screenDistortionMap = distMap->getTexture();
971                this->screenDistortionMap->GetSurfaceLevel(0, &this->screenDistortionMapSurface);
972        }
973
974        if(!this->preDistortionFinal) {
975                //Create Texture
976                V( DXUTGetD3DDevice()->CreateTexture(this->manager->screenWidth, this->manager->screenHeight, 1, D3DUSAGE_RENDERTARGET,
977                D3DFMT_A16B16G16R16F, D3DPOOL_DEFAULT, &this->preDistortionFinal, NULL ) );
978
979                //Get Surface for RenderTarget
980                this->preDistortionFinal->GetSurfaceLevel(0, &this->preDistortionFinalSurface);
981        }
982
983        this->defaultRenderPass.setRenderTarget(this->finalImageSurface);
984        this->depthRenderPass.setRenderTarget(this->depthSurface);
985
986        std::list<SPTR<Node> >::iterator nit;
987        for(nit=this->nodeList.begin();nit!=this->nodeList.end();nit++) {
988                (*nit)->OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
989        }
990
991        std::vector<SPTR<Renderer> >::iterator it;
992        for(it=this->rendererList.begin();it!=this->rendererList.end();it++) {
993                (*it)->OnResetDevice(pd3dDevice, pBackBufferSurfaceDesc, pUserContext);
994        }
995        return S_OK;
996}
997
998Vector Scene::getSunDirection()
999{
1000        return this->sunDirection;
1001}
1002
1003void Scene::setSunDirection(Vector &v)
1004{
1005        this->sunDirection.x = v.x;
1006        this->sunDirection.y = v.y;
1007        this->sunDirection.z = v.z;
1008        this->sunDirection.normalize();
1009}
1010
1011D3DXMATRIX Scene::getIdentityMatrix()
1012{
1013        return this->identMatrix;
1014}
1015
1016D3DXMATRIX Scene::getWorldMatrix()
1017{
1018        return this->worldMatrix;
1019}
1020
1021D3DXMATRIX Scene::getViewMatrix()
1022{
1023        return *this->activeRenderPass->getViewMatrix();
1024}
1025
1026D3DXMATRIX Scene::getProjectionMatrix()
1027{
1028        return *this->activeRenderPass->getProjectionMatrix();
1029}
1030
1031void Scene::setTrigger(int type, float time)
1032{
1033        SPTR<Trigger> t(new Trigger(type, time));
1034        this->triggerList.push_back(t);
1035        std::sort(this->triggerList.begin(), this->triggerList.end(), triggerSortFunction());
1036}
1037
1038void Scene::setTrigger(int type, Node *node, float time)
1039{
1040        SPTR<Node> sN = this->getSmartPointer(node);
1041       
1042        SPTR<Trigger> t(new Trigger(type, sN, time));
1043        this->triggerList.push_back(t);
1044        std::sort(this->triggerList.begin(), this->triggerList.end(), triggerSortFunction());
1045}
1046
1047void Scene::setTrigger(int type, Node *node, Node* secondNode, float time)
1048{
1049        SPTR<Node> sN = this->getSmartPointer(node);
1050        SPTR<Node> secondSN = this->getSmartPointer(secondNode);
1051       
1052        SPTR<Trigger> t(new Trigger(type, sN, secondSN, time));
1053        this->triggerList.push_back(t);
1054        std::sort(this->triggerList.begin(), this->triggerList.end(), triggerSortFunction());
1055}
1056
1057void Scene::setTrigger(int type, Node *node, Node* secondNode, Vector normal, float time)
1058{
1059        SPTR<Node> sN = this->getSmartPointer(node);
1060        SPTR<Node> secondSN = this->getSmartPointer(secondNode);
1061       
1062        SPTR<Trigger> t(new Trigger(type, sN, secondSN, normal, time));
1063        this->triggerList.push_back(t);
1064        std::sort(this->triggerList.begin(), this->triggerList.end(), triggerSortFunction());
1065}
1066
1067SPTR<Node> Scene::getSmartPointer(Node *node)
1068{
1069        std::list<SPTR<Node> >::iterator it;
1070        for(it = this->nodeList.begin(); it!=this->nodeList.end();it++) {
1071                if((*it).get() == node) {
1072                        break;
1073                }
1074        }
1075        if(it==this->nodeList.end()) {
1076                this->manager->printToConsole("NODE NOT FOUND IN LIST!!!");
1077        }
1078        return (*it);
1079}
1080
1081void Scene::executeTrigger(SPTR<Trigger> trigger)
1082{
1083        Trigger *t = trigger.get();
1084        switch(t->getType())
1085        {
1086                case this->TRIGGER_UNSETSTANDBY:
1087                        if(!t->getNode()->isA(Scene::NODE_SOUND)) {
1088                                t->getNode()->setStandBy(false);
1089                        } else {
1090                                t->getNode()->setStandBy(false);
1091                                ((SoundNode*) t->getNode().get())->play();
1092                        }
1093                        break;
1094                case this->TRIGGER_KILLNODE:
1095                        t->getNode()->killMe();
1096                        break;
1097                case this->TRIGGER_SETDETAIL:
1098                        break;
1099        }
1100}
1101
1102void Scene::deleteNode(Node* node)
1103{
1104        std::list<SPTR<Node> >::iterator it;
1105        for(it=this->zombieNodeList.begin();it!=this->zombieNodeList.end();it++) {
1106                if((*it).get() == node) {
1107                        return;
1108                }
1109        }
1110        SPTR<Node> sn = this->getSmartPointer(node);
1111        this->zombieNodeList.push_back(sn);
1112}
1113
1114void Scene::setContactReport(NxUserContactReport *_contactReport)
1115{
1116        this->contactReport = _contactReport;
1117        ((UserContactReport*) this->contactReport)->setScene(this);
1118}
1119
1120void Scene::setTriggerReport(NxUserTriggerReport *_triggerReport)
1121{
1122        this->triggerReport = _triggerReport;
1123        ((UserTriggerReport*) this->triggerReport)->setScene(this);
1124}
1125
1126void Scene::setNotifyReport(NxUserNotify* _notifyReport)
1127{
1128        this->notifyReport = _notifyReport;
1129        ((UserNotify*) this->notifyReport)->setScene(this);
1130}
1131
1132void Scene::setLight(Vector direction) {
1133        //set ambient light:
1134        DXUTGetD3DDevice()->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB(120,120,120) );
1135
1136        // Fill in a light structure defining our light
1137        D3DLIGHT9 light;
1138        ZeroMemory( &light, sizeof(D3DLIGHT9) );
1139        light.Type       = D3DLIGHT_DIRECTIONAL;
1140        light.Diffuse.r  = 1.0f;
1141        light.Diffuse.g  = 1.0f;
1142        light.Diffuse.b  = 1.0f;
1143        light.Diffuse.a  = 1.0f;
1144        light.Range      = 1000.0f;
1145
1146        // Create a direction for our light - it must be normalized 
1147        D3DXVECTOR3 vecDir;
1148        vecDir = D3DXVECTOR3(direction.x,direction.y,direction.z);
1149        D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );
1150
1151        // Tell the device about the light and turn it on
1152        DXUTGetD3DDevice()->SetLight( 0, &light );
1153        DXUTGetD3DDevice()->LightEnable( 0, TRUE );
1154}
1155
1156std::list<SPTR<Node> >* Scene::getParticleList()
1157{
1158        return &this->particleList;
1159}
1160
1161void Scene::addToParticleList(Node *node)
1162{
1163        SPTR<Node> sn = this->getSmartPointer(node);
1164        this->particleList.push_back(sn);
1165}
1166
1167ParticleGroup* Scene::getParticleGroup(int id)
1168{
1169        ParticleGroup* pg;
1170        for(UINT i=0;i<this->particleGroupVector.size();i++) {
1171                pg = (ParticleGroup*) this->particleGroupVector.at(i).get();
1172                if(pg->getParticleGroup()==id) {
1173                        return pg;
1174                }
1175        }
1176        return NULL;
1177}
1178
1179SharedResource* Scene::createSharedResource(int id)
1180{
1181        switch(id) {
1182                case this->SR_TEXTURE_VIEWDEPTH:
1183                        {
1184                                SPTR<SharedResource> srt(new SharedResourceTexture);
1185                                SharedResourceTexture* t = (SharedResourceTexture*) srt.get();
1186                                t->setResourceIdentifier(id);
1187                                t->createTexture(this->manager->screenWidth, this->manager->screenHeight, D3DFMT_A16B16G16R16F, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT);
1188                                this->sharedResourceVector.push_back(srt);
1189                                return t;
1190                        }
1191                        break;
1192                case this->SR_TEXTURE_DISTORTION:
1193                        {
1194                                SPTR<SharedResource> srt(new SharedResourceTexture);
1195                                SharedResourceTexture* t = (SharedResourceTexture*) srt.get();
1196                                t->setResourceIdentifier(id);
1197                                t->createTexture(this->manager->screenWidth, this->manager->screenHeight, D3DFMT_A16B16G16R16F, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT);
1198                                this->sharedResourceVector.push_back(srt);
1199                                return t;
1200                        }
1201                        break;
1202        }
1203        return NULL;
1204}
1205
1206SharedResource* Scene::getSharedResource(int id)
1207{
1208        for(UINT i=0;i<this->sharedResourceVector.size();i++) {
1209                if(this->sharedResourceVector.at(i)->getResourceIdentifier()==id) {
1210                        SharedResource* sr = this->sharedResourceVector.at(i).get();
1211                        return sr;
1212                }
1213        }
1214        return NULL;
1215}
1216
1217int* Scene::getMousePos() {
1218        return this->mousePos;
1219}
1220
1221void Scene::OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext )
1222{
1223}
1224
1225void Scene::createQuadMesh()
1226{       
1227        HRESULT hr;
1228        //Create a quad for rendering to screen
1229        if(SUCCEEDED( D3DXCreateMeshFVF(2, 4, D3DXMESH_MANAGED, this->FVF_Flags, this->device, &this->quadMesh)) ) {
1230                //lock vertex buffer
1231                Vector ecken[4];
1232                Vertex* pVertices;
1233                V( this->quadMesh->LockVertexBuffer( 0, (void**)&pVertices ) );
1234               
1235                ecken[0] = Vector(-1.0f, 1.0f, 0.0f);
1236                ecken[1] = Vector(-1.0f, -1.0f, 0.0f);
1237                ecken[2] = Vector(1.0f, -1.0f, 0.0f);
1238                ecken[3] = Vector(1.0f, 1.0f, 0.0f);
1239
1240                pVertices[0] = Vertex(ecken[0], 0.0f, 0.0f);
1241                pVertices[1] = Vertex(ecken[1], 0.0f, 1.0f);
1242                pVertices[2] = Vertex(ecken[2], 1.0f, 1.0f);
1243                pVertices[3] = Vertex(ecken[3], 1.0f, 0.0f);
1244
1245                //unlock vertex buffer
1246                V( this->quadMesh->UnlockVertexBuffer() );     
1247
1248                // fill the indices:
1249                WORD* pIndices = NULL;
1250                this->quadMesh->LockIndexBuffer( 0, (void**)&pIndices );
1251
1252                pIndices[0] = (WORD) 0; pIndices[1] = (WORD) 2; pIndices[2] = (WORD) 1;
1253                pIndices[3] = (WORD) 0; pIndices[4] = (WORD) 3; pIndices[5] = (WORD) 2;
1254
1255                this->quadMesh->UnlockIndexBuffer();
1256                this->meshCreated=true;
1257        }  else {
1258                this->manager->printToConsole("ERROR creating mesh!");
1259        }
1260       
1261}
1262
1263void Scene::renderFinalImage(ID3DXEffect* fadeEffect)
1264{
1265        HRESULT hr = 0;
1266       
1267       
1268        if(!this->meshCreated) {
1269                this->createQuadMesh();
1270        }
1271
1272        UINT cPass;
1273        V( fadeEffect->SetFloat("alpha", this->sceneAlpha) );
1274        V( fadeEffect->SetTexture("sceneRendering", this->finalImage) );
1275        V( fadeEffect->CommitChanges() );
1276        V( fadeEffect->SetTechnique("fade") );
1277        V( fadeEffect->Begin( &cPass, 0 ) );
1278        V( fadeEffect->BeginPass( 0 ) );
1279        V( this->device->BeginScene() );
1280        V( this->device->SetTexture(0, this->finalImage) );
1281        V( this->quadMesh->DrawSubset(0) );
1282        V( this->device->SetTexture(0, 0) );
1283        V( this->device->EndScene() );
1284        V( fadeEffect->EndPass() );
1285        V( fadeEffect->End() );
1286}
1287
1288int Scene::getActivePassId() {
1289        if(this->activeRenderPass==NULL) {
1290                return this->PASS_NORMAL;
1291        } else {
1292                return this->activeRenderPass->getRenderPassId();
1293        }
1294}
1295
1296void Scene::setActiveRenderPass(RenderPass &_activePass)
1297{
1298        this->activeRenderPass = &_activePass;
1299        this->activeRenderPass->calculateMatrices();
1300}
1301
1302void Scene::startRenderPasses()
1303{
1304        //char temp[100];
1305        HRESULT hr;
1306        bool tempDistortionPass = false;
1307
1308        //Reset values
1309        this->needDepthPass = false;
1310        this->needRaytracePass = false;
1311        this->needDistortionPass = false;
1312        this->heatHazeParticleRendererList.clear();
1313
1314        //DefaultRenderPass aktivieren
1315        this->defaultRenderPass.setViewMatrix(*this->activeCamera->getViewMatrix());
1316        this->defaultRenderPass.setProjectionMatrix(*this->activeCamera->getProjectionMatrix());
1317        this->setActiveRenderPass(this->defaultRenderPass);
1318        //Gegen Frustum Cullen
1319        this->doFrustumCulling();
1320
1321        //Ueberprufen ob heatHaze benötigt wird, defaultRenderPass...renderTarget umstellen!
1322        if(this->needDistortionPass) {
1323                tempDistortionPass = true;
1324                this->defaultRenderPass.setRenderTarget(this->preDistortionFinalSurface);
1325        }
1326
1327        //Ueberpruefen welche Passes gerendert werden
1328        if(this->needDepthPass && this->useDepthImposter) {
1329                //this->depthRenderPass.setRenderTarget(this->finalImageSurface);
1330                //this->manager->printToConsole("DepthPass Needed");
1331                this->depthRenderPass.setViewMatrix(*this->activeCamera->getViewMatrix());
1332                this->depthRenderPass.setProjectionMatrix(*this->activeCamera->getProjectionMatrix());
1333                this->setActiveRenderPass(this->depthRenderPass);
1334                this->activeRenderPass->executePass();
1335                this->depthTextureResource->setResourceAvailable(true);
1336        }
1337
1338        if(this->needRaytracePass) {
1339                //Nächster Raytracerenderer finden
1340                Vector objPos;
1341                Vector camPos;
1342                Vector dist;
1343                float minDistance=9999999;
1344                float distance=0;
1345                RaytraceRenderer* nearestRenderer = NULL;
1346                camPos = this->activeCamera->getAbsolutePosition();
1347
1348                for(UINT i=0;i<this->raytraceRendererList.size();i++) {
1349                        objPos = this->raytraceRendererList.at(i)->myNode.lock().get()->getAbsolutePosition();
1350                        distance = (camPos-objPos).length();
1351                        if(minDistance>distance) {
1352                                minDistance = distance;
1353                                nearestRenderer = this->raytraceRendererList.at(i);
1354                        }
1355                }
1356
1357                //RaytracePass aktivieren
1358                bool oldFrustum = this->enableFrustumCulling;
1359                this->raytraceRenderPass.setProjectionMatrix(*nearestRenderer->getCubeMapProjectionMatrix());
1360                for(int i=0;i<6;i++) {
1361                        this->raytraceRenderPass.setViewMatrix(nearestRenderer->getCubeMapView(i));
1362                        this->raytraceRenderPass.setRenderTarget(nearestRenderer->getCubeMapSurface(i));
1363                        this->setActiveRenderPass(this->raytraceRenderPass);
1364                        if(i==2)
1365                                this->enableFrustumCulling = false ;
1366                        this->activeRenderPass->executePass();
1367                        if(i==2)
1368                                this->enableFrustumCulling = oldFrustum;
1369                        SAFE_RELEASE(this->raytraceRenderPass.renderTarget);
1370
1371                        //BlurThatStuff
1372                        //nearestRenderer->blurDepthPass(i);
1373                }
1374                RaytraceRenderer* tempRenderer;
1375                for(UINT i=0;i<this->raytraceRendererList.size();i++) {
1376                        tempRenderer = this->raytraceRendererList.at(i);
1377                        if(tempRenderer!=nearestRenderer && tempRenderer->needsUpdate()) {
1378                                //this->manager->printToConsole("UPDATING OTHER RAYTRACER!");
1379                                this->raytraceRenderPass.setProjectionMatrix(*tempRenderer->getCubeMapProjectionMatrix());
1380                                for(int i=0;i<6;i++) {
1381                                        this->raytraceRenderPass.setViewMatrix(tempRenderer->getCubeMapView(i));
1382                                        this->raytraceRenderPass.setRenderTarget(tempRenderer->getCubeMapSurface(i));
1383                                        this->setActiveRenderPass(this->raytraceRenderPass);
1384                                        if(i==2)
1385                                                this->enableFrustumCulling = false ;
1386                                        this->activeRenderPass->executePass();
1387                                        if(i==2)
1388                                                this->enableFrustumCulling = oldFrustum;
1389                                        SAFE_RELEASE(this->raytraceRenderPass.renderTarget);
1390                                }
1391                        }
1392                }
1393
1394                //Cullen, Nodes rendern...gegen die 6 flächen....
1395                //Clear Raytracerlist
1396                this->raytraceRendererList.clear();
1397        }
1398
1399        FSOUND_Update();        //Update sount because of jittering
1400
1401        //DefaultRenderPass aktivieren
1402        this->setActiveRenderPass(this->defaultRenderPass);
1403        this->activeRenderPass->executePass();
1404
1405        //if(this->needDistortionPass) {
1406        if(tempDistortionPass) {
1407                //Distortion
1408                if(!this->meshCreated) {
1409                        this->createQuadMesh();
1410                }
1411
1412                ID3DXEffect* particleEffect;
1413                particleEffect = this->manager->getEffect(GameManager::EFFECT_DEPTHIMP);
1414
1415                //Distortion Map rendern
1416                this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
1417                this->device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
1418                this->device->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
1419                this->device->SetRenderState( D3DRS_LIGHTING, FALSE );
1420                this->device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
1421                this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
1422                this->device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1423                this->device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
1424                this->device->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
1425                this->device->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
1426                this->device->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
1427
1428                this->device->SetRenderTarget(0, this->screenDistortionMapSurface);
1429                this->device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 128, 128, 128), 1.0f, 0);
1430
1431                V( particleEffect->SetTexture("distortionMap1", this->distortionMap1) );
1432                V( particleEffect->SetTexture("distortionMap2", this->distortionMap2) );
1433                V( particleEffect->SetTexture("screenDistortionMap", this->screenDistortionMap) );
1434                V( particleEffect->CommitChanges() );
1435
1436                for(UINT i=0;i<this->heatHazeParticleRendererList.size();i++) {
1437                        this->heatHazeParticleRendererList.at(i)->renderHeatHaze();
1438                }
1439
1440                this->device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
1441
1442                //Bild auf finalRenderPass rendern
1443                this->device->SetRenderTarget(0, this->finalImageSurface);
1444                this->device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
1445               
1446               
1447                //Bild in finalImageSurface kopieren
1448                UINT cPass;
1449                V( particleEffect->SetTexture("preDistortion", this->preDistortionFinal) );
1450                V( particleEffect->CommitChanges() );
1451                V( particleEffect->SetTechnique("HeatHazeCopy") );
1452                V( particleEffect->Begin( &cPass, 0 ) );
1453                V( particleEffect->BeginPass( 0 ) );
1454                V( this->device->BeginScene() );
1455                V( this->quadMesh->DrawSubset(0) );
1456                V( this->device->SetTexture(0, 0) );
1457                V( this->device->EndScene() );
1458                V( particleEffect->EndPass() );
1459                V( particleEffect->End() );
1460
1461                this->device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1462                this->device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
1463
1464                //Reset default RenderTarget
1465                this->defaultRenderPass.setRenderTarget(this->finalImageSurface);
1466        }
1467}
1468
1469void Scene::updateSoundNodeVolume()
1470{
1471        std::list<SPTR<Node> >::iterator it;
1472        for(it = this->soundNodeList.begin();it!=this->soundNodeList.end();it++) {
1473                ((SoundNode*) it->get())->setSceneAlpha(this->sceneAlpha);
1474        }
1475        if(this->bgSoundChannel!=-1 && !this->muteMusic) {
1476                FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
1477        }
1478}
1479
1480LPD3DXMESH Scene::getQuadMesh()
1481{
1482        if(!this->meshCreated) {
1483                this->createQuadMesh();
1484        }
1485        return this->quadMesh;
1486}
1487
1488void Scene::setBackgroundSoundMute(bool _muteMusic)
1489{       
1490        this->muteMusic = _muteMusic;
1491        if(!this->muteMusic) {
1492                if(this->bgSoundChannel!=-1) {
1493                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume*this->sceneAlpha));
1494                }
1495        } else {
1496                if(this->bgSoundChannel!=-1) {
1497                        FSOUND_SetVolume(this->bgSoundChannel, 0);
1498                }
1499        }
1500}
1501
1502bool Scene::getBackgroundSoundMute() {
1503        return this->muteMusic;
1504}
1505
1506void Scene::setBackgroundSound(std::string filename) {
1507        this->bgMusicStream = FSOUND_Stream_Open(filename.c_str(), FSOUND_LOOP_NORMAL, 0, 0);
1508}
1509
1510void Scene::setBackgroundSoundVolume(float vol) {
1511        this->bgSoundVolume = vol;
1512        if(this->bgSoundChannel!=-1) {
1513                if(!this->muteMusic) {
1514                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
1515                } else {
1516                        FSOUND_SetVolume(this->bgSoundChannel, 0);
1517                }
1518        }
1519}
1520
1521void Scene::playBackgroundSound() {
1522        if(this->bgMusicStream!=NULL) {
1523                this->bgSoundChannel = FSOUND_Stream_Play(FSOUND_FREE, this->bgMusicStream);
1524                if(!this->muteMusic) {
1525                        FSOUND_SetVolume(this->bgSoundChannel, (int)(255*this->bgSoundVolume));
1526                } else {
1527                        FSOUND_SetVolume(this->bgSoundChannel, 0);
1528                }
1529        }
1530}
Note: See TracBrowser for help on using the repository browser.