1 | #pragma once
|
---|
2 | #include <My3DGraphRes\ParticleSystem.h>
|
---|
3 | #include <My3DGraphRes\Camera.h>
|
---|
4 | #include <My3DGraphRes\Impostor.h>
|
---|
5 | #include <My3DGraphRes\Texture.h>
|
---|
6 | #include <includes.h>
|
---|
7 | #include <glh/glh_extensions.h>
|
---|
8 | #include <MyCgRes\CgProgram.h>
|
---|
9 | #include <RTTRes\RenderTexture.h>
|
---|
10 |
|
---|
11 | /**
|
---|
12 | @brief This class represents a particle system that uses the Illumination Networks technique.
|
---|
13 |
|
---|
14 | This particle system can be lit with two dinamic directional light sources and a sky light color.
|
---|
15 | The direction and color of the light sources can freely change.
|
---|
16 | */
|
---|
17 | class PreIllumSystem
|
---|
18 | {
|
---|
19 | public:
|
---|
20 | PreIllumSystem(void)
|
---|
21 | {
|
---|
22 | m_IterateCount=0;
|
---|
23 | m_Symmetry=0.0;
|
---|
24 | m_Albedo=0.9;
|
---|
25 | m_Opacity=0.5;
|
---|
26 | m_LightWindowSize=64;
|
---|
27 | m_LRendVisMap=NULL;
|
---|
28 | }
|
---|
29 |
|
---|
30 | ~PreIllumSystem(void);
|
---|
31 |
|
---|
32 | /**
|
---|
33 | @brief a system that stores particle positions and can render them as sprites
|
---|
34 | */
|
---|
35 | ParticleSystem m_System;
|
---|
36 | /**
|
---|
37 | @brief the number of directions the technique should use
|
---|
38 | */
|
---|
39 | int m_DirectionCount;
|
---|
40 | /**
|
---|
41 | @brief the number of particles in the system
|
---|
42 | */
|
---|
43 | int m_ParticleCount;
|
---|
44 | /**
|
---|
45 | @brief the albedo of one particle
|
---|
46 | */
|
---|
47 | float m_Albedo;
|
---|
48 | /**
|
---|
49 | @brief the desirer opacity of the medium
|
---|
50 | */
|
---|
51 | float m_Opacity;
|
---|
52 | /**
|
---|
53 | @brief the symmetry of scattering used in the phase function
|
---|
54 | */
|
---|
55 | float m_Symmetry;
|
---|
56 | /**
|
---|
57 | @brief resolution of the lightsources viewports
|
---|
58 | */
|
---|
59 | int m_LightWindowSize;
|
---|
60 | /**
|
---|
61 | @brief number of iterations in a frame
|
---|
62 |
|
---|
63 | As the result of the last frame is used, this should be set to one.
|
---|
64 | */
|
---|
65 | int m_IterateCount;
|
---|
66 |
|
---|
67 | private:
|
---|
68 |
|
---|
69 | /**
|
---|
70 | @brief color of the sky
|
---|
71 | */
|
---|
72 | Vector m_SkyColor;
|
---|
73 | /**
|
---|
74 | @brief used for fullscreen quad rendering
|
---|
75 | */
|
---|
76 | Impostor m_ScreenQuad;
|
---|
77 | Impostor m_CameraImpostor;
|
---|
78 | Impostor m_LightImpostor;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | @brief view camera
|
---|
82 | */
|
---|
83 | Camera *m_EyeCamera;
|
---|
84 | Camera m_TempCamera;
|
---|
85 | /**
|
---|
86 | @brief position of the first lightsource
|
---|
87 | */
|
---|
88 | Vector m_LightPosition;
|
---|
89 | /**
|
---|
90 | @brief color of the first lightsource
|
---|
91 | */
|
---|
92 | Vector m_LightColor;
|
---|
93 | /**
|
---|
94 | @brief position of the second lightsource
|
---|
95 | */
|
---|
96 | Vector m_LightPosition2;
|
---|
97 | /**
|
---|
98 | @brief color of the second lightsource
|
---|
99 | */
|
---|
100 | Vector m_LightColor2;
|
---|
101 | /**
|
---|
102 | @brief the closest direction from the predefined directions to the light's direction
|
---|
103 | */
|
---|
104 | int m_NearestDir;
|
---|
105 | /**
|
---|
106 | @brief the second closest direction from the predefined directions to the light's direction
|
---|
107 | */
|
---|
108 | int m_NearestDir2;
|
---|
109 | /**
|
---|
110 | @brief weight of m_NearestDir
|
---|
111 |
|
---|
112 | m_NearestDir and m_NearestDir2 will be interpolated
|
---|
113 | */
|
---|
114 | float m_Weight1;
|
---|
115 | /**
|
---|
116 | @brief weight of m_NearestDir2
|
---|
117 |
|
---|
118 | m_NearestDir and m_NearestDir2 will be interpolated
|
---|
119 | */
|
---|
120 | float m_Weight2;
|
---|
121 |
|
---|
122 |
|
---|
123 | // helper members
|
---|
124 | Particle* m_ParticleArray;
|
---|
125 | float* m_PositionArray;
|
---|
126 | float* m_DirectionData;
|
---|
127 | Vector* m_Directions;
|
---|
128 | float* m_DirectionArray;
|
---|
129 | unsigned char* m_LVisMap;
|
---|
130 | unsigned char* m_LRendVisMap;
|
---|
131 | float* m_Pixels;
|
---|
132 |
|
---|
133 | //texture id's
|
---|
134 | /**
|
---|
135 | @brief stores the visibility information of the particles
|
---|
136 |
|
---|
137 | For each particle for each direction the first visible (from that direction) particle's id is stored.
|
---|
138 | */
|
---|
139 | GLuint m_VisibilityTexID;
|
---|
140 | /**
|
---|
141 | @brief stores predefined directions to use
|
---|
142 | */
|
---|
143 | GLuint m_DirectionsTexID;
|
---|
144 | /**
|
---|
145 | @brief a look-up texture to speed up phase function calculation
|
---|
146 | */
|
---|
147 | GLuint m_PhaseTextureID;
|
---|
148 | /**
|
---|
149 | @brief a texture that can be used to determine which particles are visible from the lightsource
|
---|
150 | */
|
---|
151 | GLuint m_LVisMapID;
|
---|
152 | /**
|
---|
153 | @brief used when determining licible particles
|
---|
154 |
|
---|
155 | @see FindVisiblesWithRendering
|
---|
156 | */
|
---|
157 | GLuint m_RenderedVisID;
|
---|
158 | /**
|
---|
159 | @brief stores tau value for each particle
|
---|
160 | */
|
---|
161 | GLuint m_TauTextureID;
|
---|
162 |
|
---|
163 | // Cg programs
|
---|
164 | CgProgram m_TexRectPrograms;
|
---|
165 | CgProgram m_LightIllumPrograms;
|
---|
166 | CgProgram m_IllumIteratePrograms;
|
---|
167 | CgProgram m_EyeRadPrograms;
|
---|
168 | CgProgram m_LVisPrograms;
|
---|
169 | CgProgram m_FinalRenderPrograms;
|
---|
170 |
|
---|
171 | //render textures, textures
|
---|
172 | //direct illumination of the lightsource
|
---|
173 | RenderTexture m_DirectIlumTexture;
|
---|
174 |
|
---|
175 | //two illumtextures are used, must ping-pong between them
|
---|
176 | RenderTexture m_IllumTexture;
|
---|
177 | RenderTexture m_IllumTexture2;
|
---|
178 | RenderTexture* m_IllumColorTex;
|
---|
179 | RenderTexture* m_IllumRenderTex;
|
---|
180 | // radiance to eye
|
---|
181 | RenderTexture m_EyeRadTexture;
|
---|
182 | RenderTexture m_Target;
|
---|
183 | Texture m_BillboardTexture;
|
---|
184 | Texture m_Bbtex;
|
---|
185 | //helper
|
---|
186 | RenderTexture m_ImpostorTexture;
|
---|
187 |
|
---|
188 | /**
|
---|
189 | @brief generates directions equally along the unit sphere
|
---|
190 | */
|
---|
191 | void CreateGivenDirections();
|
---|
192 | /**
|
---|
193 | @brief generates random directions
|
---|
194 | */
|
---|
195 | void CreateRandomDirections(bool fillarray);
|
---|
196 | void Calculate_Up_Right_Vector(Vector viewdir,Vector& UpVector,Vector& RightVector);
|
---|
197 | /**
|
---|
198 | @brief calculates the scattering phase function value for two directions and a symmetry value
|
---|
199 | */
|
---|
200 | float Phase(Vector diri,Vector dirj,float symmetry);
|
---|
201 | /**
|
---|
202 | @brief searches the stored directions and returns the one closest to a given direction
|
---|
203 | */
|
---|
204 | GetNearestDirection(Vector LightPosition);
|
---|
205 | /**
|
---|
206 | @brief Initializator function.
|
---|
207 | */
|
---|
208 | void InitSystem(int particlecount,int directioncount);
|
---|
209 | /**
|
---|
210 | @brief Creates a texture that stores the visibility information of the particles.
|
---|
211 |
|
---|
212 | For each particle for each direction the first visible (from that direction) particle's id is stored.
|
---|
213 | */
|
---|
214 | void CreateVisibilityTexture();
|
---|
215 | /**
|
---|
216 | @brief Creates a texture that stores
|
---|
217 | */
|
---|
218 | void CreateNearestDirectionTexture();
|
---|
219 | /**
|
---|
220 | @brief Creates a look-up texture to speed up phase function calculation.
|
---|
221 | */
|
---|
222 | void CreatePhaseTexture();
|
---|
223 | /**
|
---|
224 | @brief Creates a texture that can be used to determine which particles are visible from the lightsource
|
---|
225 | */
|
---|
226 | void CreateLVisMap();
|
---|
227 | /**
|
---|
228 | @brief Creates a texture tha stores the tau value for each particle.
|
---|
229 |
|
---|
230 | The tau values are calculated from the given desired opacity and the size of the particle.
|
---|
231 | */
|
---|
232 | void CreateTauTexture();
|
---|
233 |
|
---|
234 | /**
|
---|
235 | @brief Refreshes the texture that stores direct illumination information.
|
---|
236 |
|
---|
237 | Direct illumination is the amount of light coming directly from the lightsource.
|
---|
238 | */
|
---|
239 | void RefreshDirectIllumTexture();
|
---|
240 | /**
|
---|
241 | @brief Updates the illumination texture.
|
---|
242 | */
|
---|
243 | void Iterate();
|
---|
244 | /**
|
---|
245 | @brief Updates the eye radiance texture.
|
---|
246 |
|
---|
247 | The eye radiance texture stores the amount of light headig from each particle to the eye.
|
---|
248 | */
|
---|
249 | void CreateEyeRadTexture();
|
---|
250 | /**
|
---|
251 | @brief Finds the visible particles from a point of view.
|
---|
252 |
|
---|
253 | The light visibility texture stores the id of the visible particles (with occlusion) from
|
---|
254 | the light sources. The param "row" means the id of the lightsource ( 0 or 1).
|
---|
255 |
|
---|
256 | The visibility is calculated with rendering the particles from the lightsource.
|
---|
257 | Each particle has a color corresponding it's id. The resulting image is read back, and
|
---|
258 | the pixels are counted. If the number of pixels with a particle's id found is greather than
|
---|
259 | some limit, the particle is visible.
|
---|
260 | */
|
---|
261 | void FindVisiblesWithRendering(Vector LightPosition,int row);
|
---|
262 | /**
|
---|
263 | @brief not used
|
---|
264 | */
|
---|
265 | void RenderToImpostor();
|
---|
266 |
|
---|
267 | public:
|
---|
268 | void SetEyeCamera(Camera* theCam)
|
---|
269 | {
|
---|
270 | m_EyeCamera=theCam;
|
---|
271 | m_CameraImpostor.setViewCamera(theCam);
|
---|
272 | }
|
---|
273 | /**
|
---|
274 | @brief Initializator function.
|
---|
275 | */
|
---|
276 | void Init(int particlecount,int directioncount);
|
---|
277 | /**
|
---|
278 | @brief Render the particle system.
|
---|
279 | */
|
---|
280 | void Display();
|
---|
281 | /**
|
---|
282 | @brief Refreshes the system in a frame.
|
---|
283 |
|
---|
284 | The actual light positions and colors should be passed.
|
---|
285 | */
|
---|
286 | void Refresh(Vector lightpos,Vector lightpos2,Vector lightcolor,Vector lightcolor2);
|
---|
287 | /**
|
---|
288 | @brief Displays one of the textures used by the system.
|
---|
289 |
|
---|
290 | Used for debugging and presentation.
|
---|
291 | */
|
---|
292 | char* DisplayTexture(int tex);
|
---|
293 | void getNearest(Vector* positions)
|
---|
294 | {
|
---|
295 | positions[0]=m_Directions[m_NearestDir]*3;
|
---|
296 | positions[1]=m_Directions[m_NearestDir2]*3;
|
---|
297 |
|
---|
298 | }
|
---|
299 | void IncreaseOpacity()
|
---|
300 | {
|
---|
301 | m_Opacity+=0.02;
|
---|
302 | if(m_Opacity>1)m_Opacity=1;
|
---|
303 | }
|
---|
304 | void DecreaseOpacity()
|
---|
305 | {
|
---|
306 | m_Opacity-=0.02;
|
---|
307 | if(m_Opacity<0)m_Opacity=0;
|
---|
308 | }
|
---|
309 | void IncreaseAlbedo()
|
---|
310 | {
|
---|
311 | m_Albedo+=0.02;
|
---|
312 | if(m_Albedo>1)m_Albedo=1;
|
---|
313 | }
|
---|
314 | void DecreaseAlbedo()
|
---|
315 | {
|
---|
316 | m_Albedo-=0.02;
|
---|
317 | if(m_Albedo<0)m_Albedo=0;
|
---|
318 | }
|
---|
319 | float getAlbedo(){return m_Albedo;}
|
---|
320 | float getOpacity(){return m_Opacity;}
|
---|
321 | void ResetIllumTexture()
|
---|
322 | {
|
---|
323 | m_IllumColorTex->EnablewithColorRelease();
|
---|
324 | glClearColor(0,0,0,1);
|
---|
325 | glClear(GL_COLOR_BUFFER_BIT);
|
---|
326 | m_IllumColorTex->DisablewithColorBind();
|
---|
327 | }
|
---|
328 | void setSkyColor(Vector skyC){m_SkyColor=skyC;}
|
---|
329 | };
|
---|