Particle system using program and texture to draw each particles

Each instance draws a maximum of n_particles. If full, new particles replace the oldest particles. Expired particle are still sent to the CPU but should be discarded by the vertex shader.

Introduced properties

private var _centers: Array[Float]

gamnit :: ParticleSystem :: _centers

Coordinates of each particle effects
private var _clock: Clock

gamnit :: ParticleSystem :: _clock

Clock used to set ots and program::t
private var _n_particles: Int

gamnit :: ParticleSystem :: _n_particles

Maximum number of particles
private var _ots: Array[Float]

gamnit :: ParticleSystem :: _ots

Creation time of each particles
private var _program: ParticleProgram

gamnit :: ParticleSystem :: _program

Program to draw the particles
private var _scales: Array[Float]

gamnit :: ParticleSystem :: _scales

Scale of each particles
private var _texture: nullable Texture

gamnit :: ParticleSystem :: _texture

Texture to apply on particles, if any
private var _ttls: Array[Float]

gamnit :: ParticleSystem :: _ttls

Time-to-live of each particle
fun add(center: Point3d[Float], scale: Float, ttl: Float, time_offset: nullable Float)

gamnit :: ParticleSystem :: add

Add a particle at center with scale, living for ttl from time_offset
private fun centers: Array[Float]

gamnit :: ParticleSystem :: centers

Coordinates of each particle effects
private fun centers=(centers: Array[Float])

gamnit :: ParticleSystem :: centers=

Coordinates of each particle effects
fun clear

gamnit :: ParticleSystem :: clear

Clear all particles
private fun clock: Clock

gamnit :: ParticleSystem :: clock

Clock used to set ots and program::t
private fun clock=(clock: Clock)

gamnit :: ParticleSystem :: clock=

Clock used to set ots and program::t
init defaultinit(n_particles: Int, program: ParticleProgram, texture: nullable Texture)

gamnit :: ParticleSystem :: defaultinit

fun draw

gamnit :: ParticleSystem :: draw

Draw all particles of this emitter
fun n_particles: Int

gamnit :: ParticleSystem :: n_particles

Maximum number of particles
protected fun n_particles=(n_particles: Int)

gamnit :: ParticleSystem :: n_particles=

Maximum number of particles
private fun ots: Array[Float]

gamnit :: ParticleSystem :: ots

Creation time of each particles
private fun ots=(ots: Array[Float])

gamnit :: ParticleSystem :: ots=

Creation time of each particles
fun program: ParticleProgram

gamnit :: ParticleSystem :: program

Program to draw the particles
protected fun program=(program: ParticleProgram)

gamnit :: ParticleSystem :: program=

Program to draw the particles
private fun scales: Array[Float]

gamnit :: ParticleSystem :: scales

Scale of each particles
private fun scales=(scales: Array[Float])

gamnit :: ParticleSystem :: scales=

Scale of each particles
fun texture: nullable Texture

gamnit :: ParticleSystem :: texture

Texture to apply on particles, if any
protected fun texture=(texture: nullable Texture)

gamnit :: ParticleSystem :: texture=

Texture to apply on particles, if any
private fun total_particles=(total_particles: Int)

gamnit :: ParticleSystem :: total_particles=

private fun ttls: Array[Float]

gamnit :: ParticleSystem :: ttls

Time-to-live of each particle
private fun ttls=(ttls: Array[Float])

gamnit :: ParticleSystem :: ttls=

Time-to-live of each particle

Redefined properties

redef type SELF: ParticleSystem

gamnit $ ParticleSystem :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _centers: Array[Float]

gamnit :: ParticleSystem :: _centers

Coordinates of each particle effects
private var _clock: Clock

gamnit :: ParticleSystem :: _clock

Clock used to set ots and program::t
private var _n_particles: Int

gamnit :: ParticleSystem :: _n_particles

Maximum number of particles
private var _ots: Array[Float]

gamnit :: ParticleSystem :: _ots

Creation time of each particles
private var _program: ParticleProgram

gamnit :: ParticleSystem :: _program

Program to draw the particles
private var _scales: Array[Float]

gamnit :: ParticleSystem :: _scales

Scale of each particles
private var _texture: nullable Texture

gamnit :: ParticleSystem :: _texture

Texture to apply on particles, if any
private var _ttls: Array[Float]

gamnit :: ParticleSystem :: _ttls

Time-to-live of each particle
fun add(center: Point3d[Float], scale: Float, ttl: Float, time_offset: nullable Float)

gamnit :: ParticleSystem :: add

Add a particle at center with scale, living for ttl from time_offset
private fun centers: Array[Float]

gamnit :: ParticleSystem :: centers

Coordinates of each particle effects
private fun centers=(centers: Array[Float])

gamnit :: ParticleSystem :: centers=

Coordinates of each particle effects
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun clear

gamnit :: ParticleSystem :: clear

Clear all particles
private fun clock: Clock

gamnit :: ParticleSystem :: clock

Clock used to set ots and program::t
private fun clock=(clock: Clock)

gamnit :: ParticleSystem :: clock=

Clock used to set ots and program::t
init defaultinit(n_particles: Int, program: ParticleProgram, texture: nullable Texture)

gamnit :: ParticleSystem :: defaultinit

fun draw

gamnit :: ParticleSystem :: draw

Draw all particles of this emitter
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun n_particles: Int

gamnit :: ParticleSystem :: n_particles

Maximum number of particles
protected fun n_particles=(n_particles: Int)

gamnit :: ParticleSystem :: n_particles=

Maximum number of particles
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
private fun ots: Array[Float]

gamnit :: ParticleSystem :: ots

Creation time of each particles
private fun ots=(ots: Array[Float])

gamnit :: ParticleSystem :: ots=

Creation time of each particles
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun program: ParticleProgram

gamnit :: ParticleSystem :: program

Program to draw the particles
protected fun program=(program: ParticleProgram)

gamnit :: ParticleSystem :: program=

Program to draw the particles
private fun scales: Array[Float]

gamnit :: ParticleSystem :: scales

Scale of each particles
private fun scales=(scales: Array[Float])

gamnit :: ParticleSystem :: scales=

Scale of each particles
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun texture: nullable Texture

gamnit :: ParticleSystem :: texture

Texture to apply on particles, if any
protected fun texture=(texture: nullable Texture)

gamnit :: ParticleSystem :: texture=

Texture to apply on particles, if any
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
private fun total_particles=(total_particles: Int)

gamnit :: ParticleSystem :: total_particles=

private fun ttls: Array[Float]

gamnit :: ParticleSystem :: ttls

Time-to-live of each particle
private fun ttls=(ttls: Array[Float])

gamnit :: ParticleSystem :: ttls=

Time-to-live of each particle
package_diagram gamnit::ParticleSystem ParticleSystem core::Object Object gamnit::ParticleSystem->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

gamnit $ ParticleSystem
# Particle system using `program` and `texture` to draw each particles
#
# Each instance draws a maximum of `n_particles`.
# If full, new particles replace the oldest particles.
# Expired particle are still sent to the CPU but should be discarded by the vertex shader.
class ParticleSystem

	# Maximum number of particles
	var n_particles: Int

	private var total_particles = 0

	# Program to draw the particles
	var program: ParticleProgram

	# Texture to apply on particles, if any
	var texture: nullable Texture

	# Clock used to set `ots` and `program::t`
	#
	# TODO control this value from the game logic to allow pausing and slowing time.
	private var clock = new Clock

	# Coordinates of each particle effects
	private var centers = new Array[Float]

	# Creation time of each particles
	private var ots = new Array[Float]

	# Scale of each particles
	private var scales = new Array[Float]

	# Time-to-live of each particle
	private var ttls = new Array[Float]

	# Clear all particles
	fun clear
	do
		centers.clear
		ots.clear
		scales.clear
		ttls.clear
		total_particles = 0
	end

	# Add a particle at `center` with `scale`, living for `ttl` from `time_offset`
	#
	# `time_offset` is added to the creation time, it can be used to delay the
	# apparition of a particle using a positive value.
	#
	# See the doc of the precise class of `program`, or the general `ParticleProgram`
	# for information on the effect of these parameters.
	fun add(center: Point3d[Float], scale: Float, ttl: Float, time_offset: nullable Float)
	do
		var i = total_particles % n_particles
		total_particles += 1

		centers[i*3  ] = center.x
		centers[i*3+1] = center.y
		centers[i*3+2] = center.z

		ttls[i] = ttl
		scales[i] = scale

		time_offset = time_offset or else 0.0
		ots[i] = clock.total.to_f + time_offset
	end

	# Draw all particles of this emitter
	fun draw
	do
		if ots.is_empty then return

		var program = program
		program.use

		var texture = texture
		if texture != null then
			glActiveTexture gl_TEXTURE0
			glBindTexture(gl_TEXTURE_2D, texture.gl_texture)
			program.use_texture.uniform true
			program.texture.uniform 0
		else
			program.use_texture.uniform false
		end

		program.scale.array_enabled = true
		program.scale.array(scales, 1)

		program.center.array_enabled = true
		program.center.array(centers, 3)

		program.color.array_enabled = false
		program.color.uniform(1.0, 1.0, 1.0, 1.0)

		program.ot.array_enabled = true
		program.ot.array(ots, 1)

		program.ttl.array_enabled = true
		program.ttl.array(ttls, 1)

		program.t.uniform clock.total.to_f
		program.mvp.uniform app.world_camera.mvp_matrix

		glDrawArrays(gl_POINTS, 0, ots.length)
	end
end
lib/gamnit/depth/particles.nit:57,1--163,3