source: GTP/trunk/App/Demos/Illum/Rain/rainMaterials/gouttesTexCoordsFP.cg @ 2221

Revision 2221, 10.7 KB checked in by plemenos, 17 years ago (diff)
  • Property svn:executable set to *
Line 
1
2///////////////////////////////////////////////////////////////////////////
3//
4//              fragment program in charge of the rendering of the particles
5//              Different versions are available, depending on which extensions are enabled.
6//
7///////////////////////////////////////////////////////////////////////////
8
9
10
11// data comming out from the vertex program
12struct sortie
13{
14        float4 position    : POSITION;
15        float2 texCoords0  : TEXCOORD0;
16        float2 texCoords1  : TEXCOORD1;
17        float4 posworld    : TEXCOORD2;
18        float4 normale     : TEXCOORD3;
19        float3 eyetovert   : TEXCOORD4;
20        float4 www         : WPOS;
21};
22
23
24
25#define PI                3.1415926535
26#define FOVyCam           PI/3.0
27#define FOVyRtt           15.0*PI/18.0
28#define I_REFRACT_EAU     1.33
29
30
31#define ILLUM_FACT       2.0
32
33////////////////////////////////////////////////
34////////////////////////////////////////////////
35// Lighting function
36////////////////////////////////////////////////
37////////////////////////////////////////////////
38float4 lightMe( sortie IN,
39                uniform float4x4 worldview,
40                float4 origColor,
41                float4 lightPos,
42                float4 lightCol,
43                float attenuation)
44{
45      float4 distant = lightPos - IN.posworld;
46      float distFact = 1.0 - min(sqrt(dot(distant.xyz, distant.xyz)) / attenuation, 1.0);
47      distant.xyz = normalize(mul(worldview, distant).xyz);
48      distant.x = 1.0;
49
50      float4 result;
51      float4 a1, a2, a3;
52     
53      a1 = lightCol * distFact * dot(- normalize(IN.eyetovert.xyz), IN.normale.xyz);
54      a2 = max(0, dot(distant.xyz, IN.normale.xyz));
55      a3 = max(0, dot(normalize(distant.xz), -normalize(IN.normale.xz)) * (1.0 - dot(distant.xyz, -IN.normale.xyz)));
56     
57      result = a1 * (a2 + a3);
58     
59      return result;
60}
61
62
63
64
65////////////////////////////////////////////////
66////////////////////////////////////////////////
67// entry point using regular raindrops, and light
68////////////////////////////////////////////////
69////////////////////////////////////////////////
70float4 main_noStreaks_Light(sortie IN,
71                            uniform float nbLights,
72                            uniform float4x4 worldview,
73                            uniform float4 colAmb,
74                            uniform sampler2D image : TEXUNIT0,
75                            uniform sampler2D texPos : TEXUNIT1,
76                            uniform sampler2D masque : TEXUNIT2,
77                            uniform sampler2D lightInfoTexture : TEXUNIT3) : COLOR
78{
79  float4 couleur;
80  float4 retour;
81 
82  float4 colMasque = tex2D(masque, IN.texCoords0);
83
84  if (colMasque.r < 0.2 && colMasque.g < 0.2 && colMasque.b < 0.2)
85      retour.a = 0;
86  else
87  {
88      retour.a = 1;
89      float3 refrac = (colMasque.xyz - 0.5) / 2.0;
90      float2 coords = 0.5 - (0.5 - IN.texCoords1 + refrac.xy / refrac.z) * tan(FOVyCam / 2.0) / tan(FOVyRtt / 2.0) ;
91      couleur.rgb = tex2D(image, coords).rgb;
92           
93      if (coords.x > 1.0 || coords.y > 1.0 || coords.x < 0.0 || coords.y < 0.0)
94      {
95          // we're going out of the texture !
96          couleur.rgb=float3(0, 0, 1);
97      }
98     
99
100      IN.normale.z = 1.0 - sqrt(IN.normale.x * IN.normale.x + IN.normale.y * IN.normale.y);
101      IN.normale.w = 1.0;
102     
103      //lighting
104      retour = couleur * colAmb;
105     
106      for (int l = 0 ; l < nbLights ; l++)
107      {
108                float lightRow = l * (1.0 / nbLights) + (0.5 / nbLights);
109               
110                float4 lightCol = tex2D(lightInfoTexture, float2(1.0 / 8.0, lightRow));
111                float4 lightPos = tex2D(lightInfoTexture, float2(3.0 / 8.0, lightRow));
112                float4 lightDir = tex2D(lightInfoTexture, float2(5.0 / 8.0, lightRow));
113                float lightCut = tex2D(lightInfoTexture, float2(7.0 / 8.0, lightRow)).r;
114                float lightAtt = tex2D(lightInfoTexture, float2(7.0 / 8.0, lightRow)).g;
115               
116                float4 illum = lightMe(IN, worldview, couleur, lightPos, lightCol, lightAtt);
117               
118                // let's check if the particle is within the lighting cone
119                if ( dot( normalize(IN.posworld.xyz - lightPos.xyz) , normalize(lightDir.xyz) ) > cos (lightCut * PI / 180.0) )
120                {
121                    retour += illum * ILLUM_FACT;
122                }
123      }
124  }
125
126  return retour;
127}
128
129
130
131
132
133
134
135////////////////////////////////////////////////
136////////////////////////////////////////////////
137// entry point using snow, and light
138////////////////////////////////////////////////
139////////////////////////////////////////////////
140float4 main_Snow_Light(sortie IN,
141                       uniform float nbLights,
142                       uniform float4x4 worldview,
143                       uniform float4 colAmb,
144                       uniform sampler2D image : TEXUNIT0,
145                       uniform sampler2D texPos : TEXUNIT1,
146                       uniform sampler2D lightInfoTexture : TEXUNIT2) : COLOR
147{
148  float4 couleur;
149  float4 retour;
150 
151  retour = tex2D(image, IN.texCoords0) * colAmb;
152 
153  if (retour.a > 0.01)
154  {
155        for (int l = 0 ; l < nbLights ; l++)
156        {
157                float lightRow = l * (1.0 / nbLights) + (0.5 / nbLights);
158               
159                float4 lightCol = tex2D(lightInfoTexture, float2(1.0 / 8.0, lightRow));
160                float4 lightPos = tex2D(lightInfoTexture, float2(3.0 / 8.0, lightRow));
161                float4 lightDir = tex2D(lightInfoTexture, float2(5.0 / 8.0, lightRow));
162                float lightCut = tex2D(lightInfoTexture, float2(7.0 / 8.0, lightRow)).r;
163                float lightAtt = tex2D(lightInfoTexture, float2(7.0 / 8.0, lightRow)).g;
164               
165                float4 illum = lightMe(IN, worldview, couleur, lightPos, lightCol, lightAtt);
166               
167                // let's check if the snowflake particle is within the lighting cone
168                if ( dot( normalize(IN.posworld.xyz - lightPos.xyz) , normalize(lightDir.xyz) ) > cos (lightCut * PI / 180.0) )
169                {
170                  retour.xyz *= illum.xyz * ILLUM_FACT;
171                }
172        }
173  }
174
175
176    return retour;
177}
178
179
180
181#define DISCRETIZATION          10.0
182
183
184
185////////////////////////////////////////////////
186////////////////////////////////////////////////
187// entry point using streaks, and light
188////////////////////////////////////////////////
189////////////////////////////////////////////////
190float4 main_Streaks_Light(sortie IN,
191                          uniform float nbLights,
192                          uniform float4x4 worldview,
193                          uniform float4 colAmb,
194                          uniform sampler2D image : TEXUNIT0,
195                          uniform sampler2D texPos : TEXUNIT1,
196                          uniform sampler2D masque : TEXUNIT2,
197                          uniform sampler2D shape : TEXUNIT3,
198                          uniform sampler2D lightInfoTexture : TEXUNIT4) : COLOR
199{
200  float4 couleur = float4(0, 0, 0, 0);
201  float4 retour = float4(0, 0, 0, 0);
202  float4 colTexture, colTemp;
203  float4 colMasque;
204  float effective_discretization;
205  int i, l;
206  float3 refrac;
207  float2 coords;
208 
209  float lightRow;
210
211  float4 lightCol;
212  float4 lightPos;
213  float4 lightDir;
214  float lightCut;
215  float lightAtt;
216  float4 illum;
217
218  float4 shapeAlpha = tex2D(shape, 1.0 - IN.texCoords0);
219
220  IN.normale.z = 1.0 - sqrt(IN.normale.x * IN.normale.x + IN.normale.y * IN.normale.y);
221  IN.normale.w = 1.0;
222 
223 
224  if (shapeAlpha.r > 0.2 && shapeAlpha.g > 0.2 && shapeAlpha.b > 0.2 )
225  {
226        effective_discretization = 0;
227               
228        for (i=0 ; i < DISCRETIZATION ; i++)
229        {       
230                colMasque = tex2D(masque, float2(IN.texCoords0.x, (i + 1) / (DISCRETIZATION + 1)));
231               
232                // is this pixel valid to throw a ray ?
233                if (colMasque.r > 0.1 || colMasque.g > 0.1 || colMasque.b > 0.1)
234                {
235                        refrac = (colMasque.xyz - 0.5) / 2.0;
236                        coords = 0.5 - (0.5 - IN.texCoords1 + refrac.xy / refrac.z) * tan(FOVyCam / 2.0) / tan(FOVyRtt / 2.0) ;
237                       
238                        colTexture = tex2D(image, coords);
239                        couleur += colTexture;
240                        effective_discretization++;
241                }
242        }
243
244        colTemp = couleur / (effective_discretization);
245       
246        // lighting
247        retour = colTemp * colAmb;
248       
249        for (l = 0 ; l < nbLights ; l++)
250        {
251                lightRow = l * (1.0 / nbLights) + (0.5 / nbLights);
252               
253                lightCol = tex2D(lightInfoTexture, float2(1.0 / 8.0, lightRow));
254                lightPos = tex2D(lightInfoTexture, float2(3.0 / 8.0, lightRow));
255                lightDir = tex2D(lightInfoTexture, float2(5.0 / 8.0, lightRow));
256                lightCut = tex2D(lightInfoTexture, float2(7.0 / 8.0, lightRow)).r;
257                lightAtt = tex2D(lightInfoTexture, float2(7.0 / 8.0, lightRow)).g;
258                illum = lightMe(IN, worldview, colTemp, lightPos, lightCol, lightAtt);
259               
260                // let's check if the particle is within the lighting cone
261                if ( dot( normalize(IN.posworld.xyz - lightPos.xyz) , normalize(lightDir.xyz) ) > cos (lightCut * PI / 180.0) )
262                {
263                  retour += illum * ILLUM_FACT;
264                }
265        }
266       
267        // alpha blend it a little with the background scene
268        retour.a = 0.6 - abs(IN.texCoords0.x - 0.5) * 1.2;
269  }
270
271  return retour;
272}
273
274
275
276
277
278
279
280////////////////////////////////////////////////
281////////////////////////////////////////////////
282// entry point using regular raindrops, without light
283////////////////////////////////////////////////
284////////////////////////////////////////////////
285float4 main_noStreaks_noLight(sortie IN,
286                              uniform sampler2D image : TEXUNIT0,
287                              uniform sampler2D masque : TEXUNIT2) : COLOR
288{
289  float4 couleur;
290 
291  float4 colMasque = tex2D(masque, IN.texCoords0);
292
293  if (colMasque.r < 0.2 && colMasque.g < 0.2 && colMasque.b < 0.2)
294    couleur.a = 0;
295  else
296    {
297      couleur.a = 1;
298      float3 refrac = (colMasque.xyz - 0.5) / 2.0;
299      float2 coords = 0.5 - (0.5 - IN.texCoords1 + refrac.xy / refrac.z) * tan(FOVyCam / 2.0) / tan(FOVyRtt / 2.0) ;
300      couleur.rgb = tex2D(image, coords).rgb;
301    }
302
303  return couleur;
304}
305
306
307
308
309
310////////////////////////////////////////////////
311////////////////////////////////////////////////
312// entry point using snow, without light
313////////////////////////////////////////////////
314////////////////////////////////////////////////
315float4 main_Snow_noLight(sortie IN,
316                         uniform sampler2D image : TEXUNIT0) : COLOR
317{
318  return tex2D(image, IN.texCoords0);
319}
320
321
322
323
324
325////////////////////////////////////////////////
326////////////////////////////////////////////////
327// entry point using streaks, without light
328////////////////////////////////////////////////
329////////////////////////////////////////////////
330float4 main_Streaks_noLight(sortie IN,
331                            uniform sampler2D image : TEXUNIT0,
332                            uniform sampler2D masque : TEXUNIT2,
333                            uniform sampler2D shape : TEXUNIT3) : COLOR
334{
335  float4 couleur = float4(0, 0, 0, 0);
336  float4 colMasque;
337
338  float4 shapeAlpha = tex2D(shape, IN.texCoords0);
339
340  if (shapeAlpha.r > 0.2 && shapeAlpha.g > 0.2 && shapeAlpha.b > 0.2 )
341    {
342      for (int i=0 ; i <= DISCRETIZATION ; i++)
343        {
344       
345        colMasque = tex2D(masque, float2(IN.texCoords0.x, i / DISCRETIZATION));
346       
347        float3 refrac = (colMasque.xyz - 0.5) / 2.0;
348        float2 coords = 0.5 - (0.5 - IN.texCoords1 + refrac.xy / refrac.z) * tan(FOVyCam / 2.0) / tan(FOVyRtt / 2.0) ;
349        couleur.rgb = couleur.rgb + tex2D(image, coords).rgb;
350        }
351       
352        couleur = couleur / (DISCRETIZATION + 1);
353        couleur.a = 1.0 - abs(IN.texCoords0.x - 0.5) * 2.0;     
354    }
355
356  return couleur;
357}
Note: See TracBrowser for help on using the repository browser.