Merge branch 'explain-assert' into master
[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 # Clear all particles
93 fun clear
94 do
95 centers.clear
96 ots.clear
97 scales.clear
98 ttls.clear
99 total_particles = 0
100 end
101
102 # Add a particle at `center` with `scale`, living for `ttl` from `time_offset`
103 #
104 # `time_offset` is added to the creation time, it can be used to delay the
105 # apparition of a particle using a positive value.
106 #
107 # See the doc of the precise class of `program`, or the general `ParticleProgram`
108 # for information on the effect of these parameters.
109 fun add(center: Point3d[Float], scale: Float, ttl: Float, time_offset: nullable Float)
110 do
111 var i = total_particles % n_particles
112 total_particles += 1
113
114 centers[i*3 ] = center.x
115 centers[i*3+1] = center.y
116 centers[i*3+2] = center.z
117
118 ttls[i] = ttl
119 scales[i] = scale
120
121 time_offset = time_offset or else 0.0
122 ots[i] = clock.total.to_f + time_offset
123 end
124
125 # Draw all particles of this emitter
126 fun draw
127 do
128 if ots.is_empty then return
129
130 var program = program
131 program.use
132
133 var texture = texture
134 if texture != null then
135 glActiveTexture gl_TEXTURE0
136 glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
137 program.use_texture.uniform true
138 program.texture.uniform 0
139 else
140 program.use_texture.uniform false
141 end
142
143 program.scale.array_enabled = true
144 program.scale.array(scales, 1)
145
146 program.center.array_enabled = true
147 program.center.array(centers, 3)
148
149 program.color.array_enabled = false
150 program.color.uniform(1.0, 1.0, 1.0, 1.0)
151
152 program.ot.array_enabled = true
153 program.ot.array(ots, 1)
154
155 program.ttl.array_enabled = true
156 program.ttl.array(ttls, 1)
157
158 program.t.uniform clock.total.to_f
159 program.mvp.uniform app.world_camera.mvp_matrix
160
161 glDrawArrays(gl_POINTS, 0, ots.length)
162 end
163 end
164
165 # Particle drawing program using `gl_POINTS`
166 #
167 # This program should be subclassed to create custom particle effects.
168 # Either `vertex_shader_source` and `vertex_shader_core` can be refined.
169 class ParticleProgram
170 super GamnitProgramFromSource
171
172 redef var vertex_shader_source = """
173 // Coordinates of particle effects
174 attribute vec4 center;
175
176 // Particles color tint
177 attribute vec4 color;
178 varying vec4 v_color;
179
180 // Per particle scaling
181 attribute float scale;
182
183 // Model view projection matrix
184 uniform mat4 mvp;
185
186 // Time-to-live of each particle
187 attribute float ttl;
188
189 // Creation time of each particle
190 attribute float ot;
191
192 // Current time
193 uniform float t;
194
195 void main()
196 {
197 // Pass varyings to the fragment shader
198 v_color = color;
199
200 float dt = t - ot;
201 float pt = dt/ttl;
202
203 // Discard expired or not yet created particles
204 if (dt > ttl || dt < 0.0) {
205 gl_PointSize = 0.0;
206 return;
207 }
208
209 {{{vertex_shader_core}}}
210 }
211 """
212
213 # Core GLSL code for `vertex_shader_source`
214 #
215 # Refine this function to easily tweak the position, size and color of particles.
216 #
217 # Reminder: Each execution of the vertex shader applies to a single particle.
218 #
219 # ## Input variables:
220 # * `center`: reference coordinates of the particle effect.
221 # This if often the center of the particle itself,
222 # but it can also be reference coordinates for a moving particle.
223 # * `mvp`: model-view-projection matrix.
224 # * `color`: color tint of the particle.
225 #
226 # * `t`: global seconds counter since the creation of this particle emitter.
227 # * `ot`: creation time of the particle, in seconds, in reference to `t`.
228 # * `dt`: seconds since creation of the particle.
229 # * `ttl`: time-to-live of the particle, in seconds.
230 # * `pt`: advancement of this particle in its lifetime, in `[0.0 .. 1.0]`.
231 #
232 # ## Output variables:
233 # * `gl_Position`: position of the particle in camera coordinates.
234 # * `gl_PointSize`: size of the particle in camera coordinates.
235 # Set to `0.0` to discard the particle.
236 # * `v_color`: tint applied to the particle.
237 # Assigned by default to the value of `color`.
238 #
239 # ## Reference implementation
240 #
241 # The default implementation apply the model-view-projection matrix on the position
242 # and scales according to the distance from the camera.
243 # Most particle effects should apply the same base logic as the default implementation.
244 # Here it is for reference:
245 #
246 # ~~~glsl
247 # gl_Position = center * mvp;
248 # gl_PointSize = scale / gl_Position.z;
249 # ~~~
250 fun vertex_shader_core: String do return """
251 gl_Position = center * mvp;
252 gl_PointSize = scale / gl_Position.z;
253 """
254
255 redef var fragment_shader_source = """
256 precision mediump float;
257
258 // Input from the vertex shader
259 varying vec4 v_color;
260
261 // Does this particle use a texture?
262 uniform bool use_texture;
263
264 // Texture to apply on this particle
265 uniform sampler2D texture0;
266
267 void main()
268 {
269 if (use_texture) {
270 gl_FragColor = texture2D(texture0, gl_PointCoord) * v_color;
271 } else {
272 gl_FragColor = v_color;
273 }
274 }
275 """ @ glsl_fragment_shader
276
277 # Coordinates of particle effects
278 var center = attributes["center"].as(AttributeVec4) is lazy
279
280 # Should this program use the texture `texture`?
281 var use_texture = uniforms["use_texture"].as(UniformBool) is lazy
282
283 # Visible texture unit
284 var texture = uniforms["texture0"].as(UniformSampler2D) is lazy
285
286 # Color tint per vertex
287 var color = attributes["color"].as(AttributeVec4) is lazy
288
289 # Scaling per vertex
290 var scale = attributes["scale"].as(AttributeFloat) is lazy
291
292 # Model view projection matrix
293 var mvp = uniforms["mvp"].as(UniformMat4) is lazy
294
295 # Creation time of each particle
296 var ot = attributes["ot"].as(AttributeFloat) is lazy
297
298 # Current time
299 var t = uniforms["t"].as(UniformFloat) is lazy
300
301 # Time-to-live of each particle
302 var ttl = attributes["ttl"].as(AttributeFloat) is lazy
303 end
304
305 # Graphics program to display blowing up particles
306 class ExplosionProgram
307 super ParticleProgram
308
309 redef fun vertex_shader_core do return """
310 gl_Position = center * mvp;
311 gl_PointSize = scale / gl_Position.z * pt;
312
313 if (pt > 0.8) v_color *= (1.0-pt)/0.2;
314 """
315 end
316
317 # Graphics program to display particles slowly drifting upwards
318 class SmokeProgram
319 super ParticleProgram
320
321 redef fun vertex_shader_core do return """
322 vec4 c = center;
323 c.y += dt * 1.0;
324 c.x += dt * 0.1;
325
326 gl_Position = c * mvp;
327 gl_PointSize = scale / gl_Position.z * (pt+0.1);
328
329 if (pt < 0.1)
330 v_color *= pt / 0.1;
331 else
332 v_color *= 1.0 - pt*0.9;
333 """
334 end