source: GTP/trunk/App/Demos/Illum/pathmap/PathMapEffect.h @ 2212

Revision 2212, 8.5 KB checked in by szirmay, 18 years ago (diff)
Line 
1#pragma once
2#include <vector>
3#include "Parameters.h"
4#include "Vector.hpp"
5#include "Uniform.hpp"
6#include "Radion.hpp"
7#include "KDTree.hpp"
8#include "xmlParser.h"
9
10class Material;
11class TriangleMesh;
12class Transformed;
13class KDTree;
14class RenderStrategy;
15
16/*!
17\brief Main class for the PRM computation and usage application.
18This class encapsulates all resources needed for computing PRMs and
19using them in the final rendering. PRM resources may be generated,
20saved to files, or restored.
21*/
22class PathMapEffect
23{
24        int     NRADIONS;
25        int NCLUSTERS;
26        int DEPTHMAPRES;
27
28        int testClusterId;
29
30        friend class SubEntity;
31        friend class SubMesh;
32        friend class Entity;
33        friend class Mesh;
34        friend class DepthRenderStrategy;
35        friend class FinalCompositionRenderStrategy;
36
37        float clusterSweepCurrentIndex;         //!< test, cycling displayed entry point clusters
38
39        //! pointer to global user-adjustable application parameters object
40        static Parameters*              parameters;
41
42        //! pointer to main DX device
43        LPDIRECT3DDEVICE9               device;
44        //! pointer to main DX effect
45        LPD3DXEFFECT                    effect;
46
47        //! frame color buffer surface
48        //! saved before render-to-texture, and restored as the render target for the final rendering to the screen
49        LPDIRECT3DSURFACE9              frameColorBuffer;
50        //! frame depth buffer surface
51        //! saved before render-to-texture, and restored as the render target for the final rendering to the screen
52        LPDIRECT3DSURFACE9              frameDepthStencilBuffer;
53
54        LPDIRECT3DTEXTURE9              depthMapTexture;                                //!< depth map texture
55        LPDIRECT3DSURFACE9              depthMapDepthStencilBuffer;             //!< depth map texture's surface
56        LPDIRECT3DTEXTURE9              fakeTexture;                                    //!< depth map dummy render texture
57        LPDIRECT3DSURFACE9              fakeSurface;                                    //!< depth map dummy render surface
58
59        LPDIRECT3DVERTEXBUFFER9 starterVertexBuffer;                    //!< vertex buffer with entry point positions, for entry point visualization
60
61        LPDIRECT3DTEXTURE9              weightsTexture;                                 //!< render target texture to which current entry point weights are computed
62        LPDIRECT3DSURFACE9              weightsSurface;                                 //!< weights texture's surface
63    LPDIRECT3DTEXTURE9          sysMemWeightsTexture;                   //!< weights texture copy in system mem
64    LPDIRECT3DSURFACE9          sysMemWeightsSurface;                   //!< surface of weights texture copy in system mem
65        LPDIRECT3DTEXTURE9              radionTexture;                                  //!< texture containing entry point data, input for weight computation
66        LPDIRECT3DSURFACE9              radionSurface;                                  //!< entry point texture's surface
67
68        LPDIRECT3DTEXTURE9              aggrWeightsTexture;
69        LPDIRECT3DSURFACE9              aggrWeightsSurface;
70
71        KDTree* kdtree;         //!< the kd-tree that contains the scene geometry in raytraceable format
72
73        //! clever enum for supported final rendering methods
74        class Method{
75                unsigned int mid;
76                Method(unsigned int mid) {this->mid = mid;}
77                static const wchar_t * methodNames[10];
78        public:
79                Method(const Method& o) {mid = o.mid;}
80                const Method& operator=(const Method& o) {mid = o.mid; return *this;}
81                bool operator==(const Method& o) const {return mid == o.mid;}
82                bool operator!=(const Method& o) const {return mid != o.mid;}
83                const static Method PRM;
84                const static Method SHOWTEX;
85                const static Method LAST;
86                const Method& next() {mid = (mid + 1)%LAST.mid; return *this;}
87                const Method& prev() {mid = (mid + LAST.mid - 1)%LAST.mid; return *this;}
88                const wchar_t* getName() {return methodNames[mid];}
89        }method;
90public:
91        void nextMethod() {method.next();}
92        void prevMethod() {method.prev();}
93        const wchar_t* getCurrentMethodName();
94
95        //! camera
96        CFirstPersonCamera*                     camera;
97        //! primary light source (can be moved just like the real camera, and can be used as the camera)
98        CFirstPersonCamera*                     lightCamera;
99private:
100
101        //! xml data with material name -> texture reference
102        XMLNode xMaterials;
103
104        std::vector<Mesh*>      meshes;
105        //! vector of loaded textures (textures specified in mesh's material/bumpmap will be loaded here)
106        std::vector<LPDIRECT3DTEXTURE9> materialTextures;
107        std::vector<wchar_t*>   materialTextureFileNames;
108        //! vector of ray trace materials
109        std::vector<Material*>  rayTraceMaterials;
110
111        //! for materials with no texture we will render with this texture
112        LPDIRECT3DTEXTURE9              emptyTexture;
113
114        //! private method that loads a mesh and its textures
115        void loadMesh(DWORD fileType, LPCWSTR fileName, LPCWSTR ogreName, int prmAtlasSize, const char* name, int dividePcs, bool generateUV, bool generateTBN);
116
117        //! private method to load a texture
118        LPDIRECT3DTEXTURE9 loadTexture(LPCWSTR fileName, Material** rayTraceMaterial=NULL);
119
120        //! private method make a CPU Material out of a D3D texture
121        Material* createRayTraceMaterial(LPDIRECT3DTEXTURE9 texture);
122
123        //! release material texture resources
124        void releaseTextures();
125        //! release mesh resources
126        void releaseMeshes();
127        //! release entities
128        void releaseEntities();
129
130        //! struct that represents a virtual world object: a mesh and a model-world transformation matrix
131
132        //! renders a full screen quad, invoking the pixel shader for all pixels
133        //! used for rendering the environment map to the background
134        void renderFullScreen(float depth = 0.0f);
135
136        //! vector of virtual world objects
137        std::vector<Entity*> entities;
138public:
139        //! constructor: allocates all resources
140        PathMapEffect(LPDIRECT3DDEVICE9 device,
141                char* prmDirectory,
142                char* meshDirectory,
143                char* mediaDirectory,
144                char* levelFileName,
145                char* materialFileName,
146                bool segmentMeshes,
147                bool computePRM,
148                unsigned int nEntryPoints,
149                unsigned int nClusters,
150                unsigned int depthMapResolution);
151
152        wchar_t prmDirectory[256];
153        wchar_t meshDirectory[256];
154        wchar_t mediaDirectory[256];
155        wchar_t levelFileName[256];
156        wchar_t materialFileName[256];
157        bool SEGMENTMESHES;
158
159        //! destructor: releases all resources
160        ~PathMapEffect(void);
161
162        //! renders the scene using the currently selected method
163        void render();
164
165        //! renders the scene, using the PRMs for indirect illumination
166        void renderWithPRM();
167
168        //! displays a part of a PRM texture
169//      void showPRMTexture();
170
171        //! moves the virtual world objects (camera, light)
172        void move(float fElapsedTime);
173
174        LRESULT handleMessage( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
175
176        //! adds controls for the user-adjustable application parameters
177        static void addUiParameters(Parameters* parameters);
178        static void setUiParameterDefaults(Parameters* parameters);
179
180        LPDIRECT3DDEVICE9 getDevice() {return device;}
181
182        const wchar_t* getWeightsString();
183private:
184        float* weights;                         //!< the array of averaged cluster weights
185        unsigned int* clusterLenghts;   //!< array thet contasins the number of entry points in every cluster (entry points are stored continously [bushStarters, starterVertexBuffer])
186
187        float sumSurfaceArea;                                   //!< summed surface area of all entities
188
189        void createPRMTextures();                               //!< allocates PRM resources for every entity
190
191        //! generates a random direction (cosine distribution) near a normal vector
192        static void sampleShootingDiffuseDirection(int depth, const Vector& normal, Vector& outDir);
193        //! generates a random direction an the unit sphere
194        static void sampleSphereDirection(Vector& outDir);
195       
196        //! finds a random entry point (all entities considered with equal probability)
197        void sampleSurfaceRadion(Radion& starter);
198        //! finds a random entry point (all entities considered with probability proporstional to surface area)
199        void sampleSurfaceRadionUniform(Radion& starter);
200
201        //! shoot virtual light sources from original entry point and add them to the vector
202        void shootRadionBush(Radion& starter, std::vector<Radion>& bushRadions);
203
204        //! sort radions into initial, uniform length clusters
205        int clusterRadions(Radion* partition, int psize, char axis);
206
207        //! use K-means clustering to cluster radions
208        void clusterRadionsKMeans();
209
210        //! perform computaions: generate entry points, render PRMs
211        void precompute();
212
213        int getNBushes() {return NRADIONS; }
214        int getNClusters() {return NCLUSTERS;}
215
216        //! entry points
217        std::vector<Radion> bushStarters;
218
219        int rayId;
220        Ray ray;
221        HitRec hitRec;
222
223        //! fill 'radionsTexture' from 'bushStarters'
224        void uploadRadions();
225        //! fill *pData (will point to locked 'starterVertexBuffer') from 'bushStarters'
226        void fillRadionPosArray(void* pData);
227
228public:
229        //! store all precomputed data for the scene in folder prm
230        void savePathMaps();
231        //! restore all precomputed data for the scene from folder prm
232        void loadPathMaps();
233
234        void exportEntityData();
235
236        void loadScene(const char* sceneFileName);
237        void saveScene(const char* sceneFileName);
238
239        void renderScene(const RenderStrategy& renderStrategy);
240};
Note: See TracBrowser for help on using the repository browser.