gamnit: use premultiplied alpha
[nit.git] / lib / gamnit / depth / particles.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Particle effects
16 #
17 # Particles are managed by instances of `ParticleSystem` that
18 # are configured for a specific kind of particle.
19 # For instance, a particle system can be created for a max of 100 particles,
20 # with a smoke effect and a precise texture, as in:
21 #
22 # ~~~
23 # var smoke = new ParticleSystem(100, app.smoke_program, new Texture("smoke.png"))
24 # ~~~
25 #
26 # The system must be registered in `app.particle_systems` to be drawn on screen.
27 #
28 # Particles are added to a system with their configuration, as in:
29 #
30 # ~~~
31 # var position = new Point3d[Float](0.0, 0.0, 0.0)
32 # var scale = 2.0
33 # var time_to_live = 1.0 # in seconds
34 # smoke.add(position, scale, time_to_live)
35 # ~~~
36 module particles
37
38 import depth_core
39
40 redef class App
41
42 # Graphics program to display static non-moving particles
43 var static_program = new ParticleProgram
44
45 # Graphics program to display blowing up particles
46 var explosion_program = new ExplosionProgram
47
48 # Graphics program to display particles slowly drifting upwards
49 var smoke_program = new SmokeProgram
50
51 # Enabled particle emitters
52 #
53 # To be populated by the client program.
54 var particle_systems = new Array[ParticleSystem]
55 end
56
57 # Particle system using `program` and `texture` to draw each particles
58 #
59 # Each instance draws a maximum of `n_particles`.
60 # If full, new particles replace the oldest particles.
61 # Expired particle are still sent to the CPU but should be discarded by the vertex shader.
62 class ParticleSystem
63
64 # Maximum number of particles
65 var n_particles: Int
66
67 private var total_particles = 0
68
69 # Program to draw the particles
70 var program: ParticleProgram
71
72 # Texture to apply on particles, if any
73 var texture: nullable Texture
74
75 # Clock used to set `ots` and `program::t`
76 #
77 # TODO control this value from the game logic to allow pausing and slowing time.
78 private var clock = new Clock
79
80 # Coordinates of each particle effects
81 private var centers = new Array[Float]
82
83 # Creation time of each particles
84 private var ots = new Array[Float]
85
86 # Scale of each particles
87 private var scales = new Array[Float]
88
89 # Time-to-live of each particle
90 private var ttls = new Array[Float]
91
92 # Add a particle at `center` with `scale`, living for `ttl` from `time_offset`
93 #
94 # `time_offset` is added to the creation time, it can be used to delay the
95 # apparition of a particle using a positive value.
96 #
97 # See the doc of the precise class of `program`, or the general `ParticleProgram`
98 # for information on the effect of these parameters.
99 fun add(center: Point3d[Float], scale: Float, ttl: Float, time_offset: nullable Float)
100 do
101 var i = total_particles % n_particles
102 total_particles += 1
103
104 centers[i*3 ] = center.x
105 centers[i*3+1] = center.y
106 centers[i*3+2] = center.z
107
108 ttls[i] = ttl
109 scales[i] = scale
110
111 time_offset = time_offset or else 0.0
112 ots[i] = clock.total.to_f + time_offset
113 end
114
115 # Draw all particles of this emitter
116 fun draw
117 do
118 if ots.is_empty then return
119
120 var program = program
121 program.use
122
123 var texture = texture
124 if texture != null then
125 glActiveTexture gl_TEXTURE0
126 glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
127 program.use_texture.uniform true
128 program.texture.uniform 0
129 else
130 program.use_texture.uniform false
131 end
132
133 program.scale.array_enabled = true
134 program.scale.array(scales, 1)
135
136 program.center.array_enabled = true
137 program.center.array(centers, 3)
138
139 program.color.array_enabled = false
140 program.color.uniform(1.0, 1.0, 1.0, 1.0)
141
142 program.ot.array_enabled = true
143 program.ot.array(ots, 1)
144
145 program.ttl.array_enabled = true
146 program.ttl.array(ttls, 1)
147
148 program.t.uniform clock.total.to_f
149 program.mvp.uniform app.world_camera.mvp_matrix
150
151 glDrawArrays(gl_POINTS, 0, ots.length)
152 end
153 end
154
155 # Particle drawing program using `gl_POINTS`
156 #
157 # This program should be subclassed to create custom particle effects.
158 # Either `vertex_shader_source` and `vertex_shader_core` can be refined.
159 class ParticleProgram
160 super GamnitProgramFromSource
161
162 redef var vertex_shader_source = """
163 // Coordinates of particle effects
164 attribute vec4 center;
165
166 // Particles color tint
167 attribute vec4 color;
168 varying vec4 v_color;
169
170 // Per particle scaling
171 attribute float scale;
172
173 // Model view projection matrix
174 uniform mat4 mvp;
175
176 // Time-to-live of each particle
177 attribute float ttl;
178
179 // Creation time of each particle
180 attribute float ot;
181
182 // Current time
183 uniform float t;
184
185 void main()
186 {
187 // Pass varyings to the fragment shader
188 v_color = color;
189
190 float dt = t - ot;
191 float pt = dt/ttl;
192
193 // Discard expired or not yet created particles
194 if (dt > ttl || dt < 0.0) {
195 gl_PointSize = 0.0;
196 return;
197 }
198
199 {{{vertex_shader_core}}}
200 }
201 """
202
203 # Core GLSL code for `vertex_shader_source`
204 #
205 # Refine this function to easily tweak the position, size and color of particles.
206 #
207 # Reminder: Each execution of the vertex shader applies to a single particle.
208 #
209 # ## Input variables:
210 # * `center`: reference coordinates of the particle effect.
211 # This if often the center of the particle itself,
212 # but it can also be reference coordinates for a moving particle.
213 # * `mvp`: model-view-projection matrix.
214 # * `color`: color tint of the particle.
215 #
216 # * `t`: global seconds counter since the creation of this particle emitter.
217 # * `ot`: creation time of the particle, in seconds, in reference to `t`.
218 # * `dt`: seconds since creation of the particle.
219 # * `ttl`: time-to-live of the particle, in seconds.
220 # * `pt`: advancement of this particle in its lifetime, in `[0.0 .. 1.0]`.
221 #
222 # ## Output variables:
223 # * `gl_Position`: position of the particle in camera coordinates.
224 # * `gl_PointSize`: size of the particle in camera coordinates.
225 # Set to `0.0` to discard the particle.
226 # * `v_color`: tint applied to the particle.
227 # Assigned by default to the value of `color`.
228 #
229 # ## Reference implementation
230 #
231 # The default implementation apply the model-view-projection matrix on the position
232 # and scales according to the distance from the camera.
233 # Most particle effects should apply the same base logic as the default implementation.
234 # Here it is for reference:
235 #
236 # ~~~glsl
237 # gl_Position = center * mvp;
238 # gl_PointSize = scale / gl_Position.z;
239 # ~~~
240 fun vertex_shader_core: String do return """
241 gl_Position = center * mvp;
242 gl_PointSize = scale / gl_Position.z;
243 """
244
245 redef var fragment_shader_source = """
246 precision mediump float;
247
248 // Input from the vertex shader
249 varying vec4 v_color;
250
251 // Does this particle use a texture?
252 uniform bool use_texture;
253
254 // Texture to apply on this particle
255 uniform sampler2D texture0;
256
257 void main()
258 {
259 if (use_texture) {
260 gl_FragColor = texture2D(texture0, gl_PointCoord) * v_color;
261 } else {
262 gl_FragColor = v_color;
263 }
264 }
265 """ @ glsl_fragment_shader
266
267 # Coordinates of particle effects
268 var center = attributes["center"].as(AttributeVec4) is lazy
269
270 # Should this program use the texture `texture`?
271 var use_texture = uniforms["use_texture"].as(UniformBool) is lazy
272
273 # Visible texture unit
274 var texture = uniforms["texture0"].as(UniformSampler2D) is lazy
275
276 # Color tint per vertex
277 var color = attributes["color"].as(AttributeVec4) is lazy
278
279 # Scaling per vertex
280 var scale = attributes["scale"].as(AttributeFloat) is lazy
281
282 # Model view projection matrix
283 var mvp = uniforms["mvp"].as(UniformMat4) is lazy
284
285 # Creation time of each particle
286 var ot = attributes["ot"].as(AttributeFloat) is lazy
287
288 # Current time
289 var t = uniforms["t"].as(UniformFloat) is lazy
290
291 # Time-to-live of each particle
292 var ttl = attributes["ttl"].as(AttributeFloat) is lazy
293 end
294
295 # Graphics program to display blowing up particles
296 class ExplosionProgram
297 super ParticleProgram
298
299 redef fun vertex_shader_core do return """
300 gl_Position = center * mvp;
301 gl_PointSize = scale / gl_Position.z * pt;
302
303 if (pt > 0.8) v_color *= (1.0-pt)/0.2;
304 """
305 end
306
307 # Graphics program to display particles slowly drifting upwards
308 class SmokeProgram
309 super ParticleProgram
310
311 redef fun vertex_shader_core do return """
312 vec4 c = center;
313 c.y += dt * 1.0;
314 c.x += dt * 0.1;
315
316 gl_Position = c * mvp;
317 gl_PointSize = scale / gl_Position.z * (pt+0.1);
318
319 if (pt < 0.1)
320 v_color *= pt / 0.1;
321 else
322 v_color *= 1.0 - pt*0.9;
323 """
324 end