Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Example/GLSL150/ParticleGS_GenerateGS.glsl @ 12083

Last change on this file since 12083 was 12083, checked in by wiesep, 5 years ago

Reorganised shader programs

File size: 6.4 KB
Line 
1#version 150
2
3//
4// Explanation of different particle types
5//
6#define PT_LAUNCHER 0 //Firework Launcher - launches a PT_SHELL every so many seconds
7#define PT_SHELL    1 //Unexploded shell - flies from the origin and explodes into many PT_EMBERXs
8#define PT_EMBER1   2 //basic particle - after it's emitted from the shell, it dies
9#define PT_EMBER2   3 //after it's emitted, it explodes again into many PT_EMBER1s
10#define PT_EMBER3   4 //just a differently colored ember1
11#define P_SHELLLIFE 3.0
12#define P_EMBER1LIFE 2.5
13#define P_EMBER2LIFE 1.5
14#define P_EMBER3LIFE 2.0
15
16//These two were originally shader params, but they caused runtime errors
17#define NUM_EMBER_1S 30
18#define NUM_EMBER_2S 15
19#define NUM_EMBER_3S 10
20//This one was originally a variant, but this also causes runtime errors
21//#define MAX_EMBER_2S 15.0
22
23in block {
24        vec4    Pos;
25        float   Timer;
26    float       Type;
27        vec3    Vel;
28} FireworkData[];
29
30layout(points) in;
31layout(points, max_vertices = 1) out;
32
33uniform sampler1D randomTex;
34uniform vec3 frameGravity;
35uniform float globalTime;
36uniform float elapsedTime;
37uniform float secondsPerFirework;
38out vec4 colour;
39//
40// Generic particle motion handler
41//
42
43void GSGenericHandler( vec3 Pos, vec3 Vel, float Timer, float Type,
44                                                float elapsedTime, 
45                                                vec3 frameGravity)
46{
47    gl_Position.xyz = Pos + (Vel * elapsedTime);
48    Vel += frameGravity;
49    Timer -= elapsedTime;
50        if (Pos.y > -100)
51        {
52                EmitVertex();//Pos : POSITION, Vel : TEXCOORD2, Timer : TEXCOORD0, Type : TEXCOORD1);
53        }
54}
55
56//
57// Sample a random direction from our random texture
58//
59vec3 RandomDir(float fOffset, float globalTime, sampler1D randomTex)
60{
61    float tCoord = (globalTime + fOffset) / 300.0;
62    return texture(randomTex, tCoord).rgb;
63}
64
65//
66// Launcher type particle handler
67//
68void GSLauncherHandler( vec3 Pos, vec3 Vel, float Timer, float Type, 
69                                                float elapsedTime, 
70                                                float globalTime, 
71                                                sampler1D randomTex,
72                                                float secondsPerFirework)
73{
74    if(Timer <= 0)
75    {
76        vec3 vRandom = normalize( RandomDir( Type, globalTime, randomTex) );
77                //Give it more of an up bias
78        vRandom = normalize(vRandom + vec3(0,2.5,0)); 
79               
80                //time to emit a new SHELL
81        gl_Position.xyz = Pos + Vel*elapsedTime;
82        vec3 outputVel = Vel + vRandom*35.0;
83        float  outputTimer = P_SHELLLIFE + vRandom.y*0.5;
84        float outputType = PT_SHELL;
85        EmitVertex();//(outputPos : POSITION, outputVel : TEXCOORD2, outputTimer : TEXCOORD0, outputType : TEXCOORD1);
86       
87        //reset our timer
88        Timer = secondsPerFirework + vRandom.x*0.4;
89    }
90    else
91    {
92        Timer -= elapsedTime;
93    }
94   
95    //emit ourselves to keep us alive
96    EmitVertex();//( Pos : POSITION, Vel : TEXCOORD2, Timer : TEXCOORD0, Type : TEXCOORD1);
97}
98
99//
100// Shell type particle handler
101//     
102void GSShellHandler( vec3 Pos, vec3 Vel, float Timer, float Type,
103                                        float elapsedTime, 
104                                        float globalTime, 
105                                        sampler1D randomTex,
106                                        vec3 frameGravity)
107{
108    if(Timer <= 0)
109    {
110        vec3 outputPos;
111                vec3 outputVel;
112                float outputTimer;
113                float outputType;
114               
115        vec3 vRandom = vec3(0,0,0);
116       
117        //time to emit a series of new Ember1s 
118        for(int i=0; i<NUM_EMBER_1S; i++)
119        {
120            vRandom = normalize( RandomDir( Type+i, globalTime, randomTex ) );
121            gl_Position.xyz = Pos + Vel*elapsedTime;
122            outputVel = Vel + vRandom*15.0;
123            outputTimer = P_EMBER1LIFE;
124            outputType = PT_EMBER1;
125            EmitVertex();//(outputPos : POSITION, outputTimer : TEXCOORD0, outputType : TEXCOORD1, outputVel : TEXCOORD2);
126        }
127       
128        //find out how many Ember2s to emit
129                //Not doing this because it causes a runtime error
130                //int numEmber2s = abs(vRandom.x)*MAX_EMBER_2S;
131        for(int i=0; i<NUM_EMBER_2S; i++)
132        {
133            vRandom = normalize( RandomDir( Type, globalTime, randomTex) );
134            gl_Position.xyz = Pos + Vel*elapsedTime;
135            outputVel = Vel + vRandom*10.0;
136            outputTimer = P_EMBER2LIFE + 0.4*vRandom.x;
137            outputType = PT_EMBER2;
138            EmitVertex();//(outputPos : POSITION, outputVel : TEXCOORD2, outputTimer : TEXCOORD0, outputType : TEXCOORD1);
139        }
140       
141    }
142    else
143    {
144        GSGenericHandler(Pos, Vel, Timer, Type, elapsedTime, frameGravity );
145    }
146}
147
148//
149// Ember1 and Ember3 type particle handler
150//
151void GSEmber1Handler( vec3 Pos, vec3 Vel, float Timer, float Type,
152                                                float elapsedTime, 
153                                                vec3 frameGravity)
154{
155    if(Timer > 0)
156    {
157        GSGenericHandler(Pos, Vel, Timer, Type, elapsedTime, frameGravity );
158    }
159}
160
161//
162// Ember2 type particle handler
163//
164void GSEmber2Handler( vec3 Pos, vec3 Vel, float Timer, float Type,
165                                                float elapsedTime, 
166                                                float globalTime, 
167                                                sampler1D randomTex,
168                                                vec3 frameGravity)
169{
170    if(Timer <= 0)
171    {
172                vec3 outputPos;
173                vec3 outputVel;
174                float outputTimer;
175                float outputType;
176   
177        //time to emit a series of new Ember3s 
178        for(int i=0; i<NUM_EMBER_3S; i++)
179        {
180            gl_Position.xyz = Pos + Vel*elapsedTime;
181            outputVel = Vel + normalize( RandomDir( Type + i, globalTime, randomTex ) )*10.0;
182            outputTimer = P_EMBER3LIFE;
183            outputType = PT_EMBER3;
184            EmitVertex();
185        }
186    }
187    else
188    {
189         GSGenericHandler(Pos, Vel, Timer, Type, elapsedTime, frameGravity );
190    }
191}
192
193void main()
194{
195        if( FireworkData[0].Type == PT_LAUNCHER )
196        GSLauncherHandler( FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type, 
197                                                        elapsedTime, globalTime, randomTex, secondsPerFirework);
198        else if ( FireworkData[0].Type == PT_SHELL )
199        GSShellHandler( FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type, 
200                                                        elapsedTime, globalTime, randomTex, frameGravity);
201        else if ( FireworkData[0].Type == PT_EMBER1 ||
202              FireworkData[0].Type == PT_EMBER3 )
203        GSEmber1Handler( FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type, 
204                                                        elapsedTime, frameGravity);
205    else if( FireworkData[0].Type == PT_EMBER2 )
206        GSEmber2Handler( FireworkData[0].Pos.xyz, FireworkData[0].Vel, FireworkData[0].Timer, FireworkData[0].Type, 
207                                                        elapsedTime, globalTime, randomTex, frameGravity);
208    colour = vec4(1.0,1.0,0.0,1.0);
209        EndPrimitive();
210}
Note: See TracBrowser for help on using the repository browser.