Particle drawing program using gl_POINTS

This program should be subclassed to create custom particle effects. Either vertex_shader_source and vertex_shader_core can be refined.

Introduced properties

fun center: AttributeVec4

gamnit :: ParticleProgram :: center

Coordinates of particle effects
protected fun center=(center: AttributeVec4)

gamnit :: ParticleProgram :: center=

Coordinates of particle effects
fun color: AttributeVec4

gamnit :: ParticleProgram :: color

Color tint per vertex
protected fun color=(color: AttributeVec4)

gamnit :: ParticleProgram :: color=

Color tint per vertex
protected fun fragment_shader_source=(fragment_shader_source: Text)

gamnit :: ParticleProgram :: fragment_shader_source=

fun mvp: UniformMat4

gamnit :: ParticleProgram :: mvp

Model view projection matrix
protected fun mvp=(mvp: UniformMat4)

gamnit :: ParticleProgram :: mvp=

Model view projection matrix
fun ot: AttributeFloat

gamnit :: ParticleProgram :: ot

Creation time of each particle
protected fun ot=(ot: AttributeFloat)

gamnit :: ParticleProgram :: ot=

Creation time of each particle
fun scale: AttributeFloat

gamnit :: ParticleProgram :: scale

Scaling per vertex
protected fun scale=(scale: AttributeFloat)

gamnit :: ParticleProgram :: scale=

Scaling per vertex
fun t: UniformFloat

gamnit :: ParticleProgram :: t

Current time
protected fun t=(t: UniformFloat)

gamnit :: ParticleProgram :: t=

Current time
fun texture: UniformSampler2D

gamnit :: ParticleProgram :: texture

Visible texture unit
protected fun texture=(texture: UniformSampler2D)

gamnit :: ParticleProgram :: texture=

Visible texture unit
fun ttl: AttributeFloat

gamnit :: ParticleProgram :: ttl

Time-to-live of each particle
protected fun ttl=(ttl: AttributeFloat)

gamnit :: ParticleProgram :: ttl=

Time-to-live of each particle
fun use_texture: UniformBool

gamnit :: ParticleProgram :: use_texture

Should this program use the texture texture?
protected fun use_texture=(use_texture: UniformBool)

gamnit :: ParticleProgram :: use_texture=

Should this program use the texture texture?
fun vertex_shader_core: String

gamnit :: ParticleProgram :: vertex_shader_core

Core GLSL code for vertex_shader_source
protected fun vertex_shader_source=(vertex_shader_source: Text)

gamnit :: ParticleProgram :: vertex_shader_source=

Redefined properties

redef type SELF: ParticleProgram

gamnit $ ParticleProgram :: SELF

Type of this instance, automatically specialized in every class
redef fun fragment_shader_source: Text

gamnit $ ParticleProgram :: fragment_shader_source

Source code of the fragment shader
redef fun vertex_shader_source: Text

gamnit $ ParticleProgram :: vertex_shader_source

Source code of the vertex shader

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
fun attributes: AttributeMap

gamnit :: GamnitProgram :: attributes

Attributes of this program organized by name
protected fun attributes=(attributes: AttributeMap)

gamnit :: GamnitProgram :: attributes=

Attributes of this program organized by name
fun center: AttributeVec4

gamnit :: ParticleProgram :: center

Coordinates of particle effects
protected fun center=(center: AttributeVec4)

gamnit :: ParticleProgram :: center=

Coordinates of 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 color: AttributeVec4

gamnit :: ParticleProgram :: color

Color tint per vertex
protected fun color=(color: AttributeVec4)

gamnit :: ParticleProgram :: color=

Color tint per vertex
fun delete

gamnit :: GamnitProgram :: delete

Delete this program if it has not already been deleted
fun deleted: Bool

gamnit :: GamnitProgram :: deleted

Has this program been deleted?
protected fun deleted=(deleted: Bool)

gamnit :: GamnitProgram :: deleted=

Has this program been deleted?
fun diagnose

gamnit :: GamnitProgram :: diagnose

Diagnose possible problems with the shaders of the program
fun error: nullable Error

gamnit :: GamnitProgram :: error

Last error raised by compile_and_link
protected fun error=(error: nullable Error)

gamnit :: GamnitProgram :: error=

Last error raised by compile_and_link
abstract fun fragment_shader: FragmentShader

gamnit :: GamnitProgram :: fragment_shader

Fragment shader to attach to this program
abstract fun fragment_shader_source: Text

gamnit :: GamnitProgramFromSource :: fragment_shader_source

Source code of the fragment shader
protected fun fragment_shader_source=(fragment_shader_source: Text)

gamnit :: ParticleProgram :: fragment_shader_source=

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 mvp: UniformMat4

gamnit :: ParticleProgram :: mvp

Model view projection matrix
protected fun mvp=(mvp: UniformMat4)

gamnit :: ParticleProgram :: mvp=

Model view projection matrix
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun ot: AttributeFloat

gamnit :: ParticleProgram :: ot

Creation time of each particle
protected fun ot=(ot: AttributeFloat)

gamnit :: ParticleProgram :: ot=

Creation time of each particle
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 scale: AttributeFloat

gamnit :: ParticleProgram :: scale

Scaling per vertex
protected fun scale=(scale: AttributeFloat)

gamnit :: ParticleProgram :: scale=

Scaling per vertex
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 t: UniformFloat

gamnit :: ParticleProgram :: t

Current time
protected fun t=(t: UniformFloat)

gamnit :: ParticleProgram :: t=

Current time
fun texture: UniformSampler2D

gamnit :: ParticleProgram :: texture

Visible texture unit
protected fun texture=(texture: UniformSampler2D)

gamnit :: ParticleProgram :: texture=

Visible texture unit
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun ttl: AttributeFloat

gamnit :: ParticleProgram :: ttl

Time-to-live of each particle
protected fun ttl=(ttl: AttributeFloat)

gamnit :: ParticleProgram :: ttl=

Time-to-live of each particle
fun uniforms: UniformMap

gamnit :: GamnitProgram :: uniforms

Uniforms of this program organized by name
protected fun uniforms=(uniforms: UniformMap)

gamnit :: GamnitProgram :: uniforms=

Uniforms of this program organized by name
fun use

gamnit :: GamnitProgram :: use

Notify the GPU to use this program
fun use_texture: UniformBool

gamnit :: ParticleProgram :: use_texture

Should this program use the texture texture?
protected fun use_texture=(use_texture: UniformBool)

gamnit :: ParticleProgram :: use_texture=

Should this program use the texture texture?
abstract fun vertex_shader: VertexShader

gamnit :: GamnitProgram :: vertex_shader

Vertex shader to attach to this program
fun vertex_shader_core: String

gamnit :: ParticleProgram :: vertex_shader_core

Core GLSL code for vertex_shader_source
abstract fun vertex_shader_source: Text

gamnit :: GamnitProgramFromSource :: vertex_shader_source

Source code of the vertex shader
protected fun vertex_shader_source=(vertex_shader_source: Text)

gamnit :: ParticleProgram :: vertex_shader_source=

package_diagram gamnit::ParticleProgram ParticleProgram gamnit::GamnitProgramFromSource GamnitProgramFromSource gamnit::ParticleProgram->gamnit::GamnitProgramFromSource gamnit::GamnitProgram GamnitProgram gamnit::GamnitProgramFromSource->gamnit::GamnitProgram ...gamnit::GamnitProgram ... ...gamnit::GamnitProgram->gamnit::GamnitProgram gamnit::ExplosionProgram ExplosionProgram gamnit::ExplosionProgram->gamnit::ParticleProgram gamnit::SmokeProgram SmokeProgram gamnit::SmokeProgram->gamnit::ParticleProgram

Ancestors

abstract class GamnitProgram

gamnit :: GamnitProgram

Gamnit graphical program
interface Object

core :: Object

The root of the class hierarchy.

Parents

class GamnitProgramFromSource

gamnit :: GamnitProgramFromSource

Gamnit graphical program from the shaders source code

Children

class ExplosionProgram

gamnit :: ExplosionProgram

Graphics program to display blowing up particles
class SmokeProgram

gamnit :: SmokeProgram

Graphics program to display particles slowly drifting upwards

Class definitions

gamnit $ ParticleProgram
# Particle drawing program using `gl_POINTS`
#
# This program should be subclassed to create custom particle effects.
# Either `vertex_shader_source` and `vertex_shader_core` can be refined.
class ParticleProgram
	super GamnitProgramFromSource

	redef var vertex_shader_source = """
		// Coordinates of particle effects
		attribute vec4 center;

		// Particles color tint
		attribute vec4 color;
		varying vec4 v_color;

		// Per particle scaling
		attribute float scale;

		// Model view projection matrix
		uniform mat4 mvp;

		// Time-to-live of each particle
		attribute float ttl;

		// Creation time of each particle
		attribute float ot;

		// Current time
		uniform float t;

		void main()
		{
			// Pass varyings to the fragment shader
			v_color = color;

			float dt = t - ot;
			float pt = dt/ttl;

			// Discard expired or not yet created particles
			if (dt > ttl || dt < 0.0) {
				gl_PointSize = 0.0;
				return;
			}

			{{{vertex_shader_core}}}
		}
		"""

	# Core GLSL code for `vertex_shader_source`
	#
	# Refine this function to easily tweak the position, size and color of particles.
	#
	# Reminder: Each execution of the vertex shader applies to a single particle.
	#
	# ## Input variables:
	# * `center`: reference coordinates of the particle effect.
	#   This if often the center of the particle itself,
	#   but it can also be reference coordinates for a moving particle.
	# * `mvp`: model-view-projection matrix.
	# * `color`: color tint of the particle.
	#
	# * `t`: global seconds counter since the creation of this particle emitter.
	# * `ot`: creation time of the particle, in seconds, in reference to `t`.
	# * `dt`: seconds since creation of the particle.
	# * `ttl`: time-to-live of the particle, in seconds.
	# * `pt`: advancement of this particle in its lifetime, in `[0.0 .. 1.0]`.
	#
	# ## Output variables:
	# * `gl_Position`: position of the particle in camera coordinates.
	# * `gl_PointSize`: size of the particle in camera coordinates.
	#   Set to `0.0` to discard the particle.
	# * `v_color`: tint applied to the particle.
	#   Assigned by default to the value of `color`.
	#
	# ## Reference implementation
	#
	# The default implementation apply the model-view-projection matrix on the position
	# and scales according to the distance from the camera.
	# Most particle effects should apply the same base logic as the default implementation.
	# Here it is for reference:
	#
	# ~~~glsl
	# gl_Position = center * mvp;
	# gl_PointSize = scale / gl_Position.z;
	# ~~~
	fun vertex_shader_core: String do return """
			gl_Position = center * mvp;
			gl_PointSize = scale / gl_Position.z;
	"""

	redef var fragment_shader_source = """
		precision mediump float;

		// Input from the vertex shader
		varying vec4 v_color;

		// Does this particle use a texture?
		uniform bool use_texture;

		// Texture to apply on this particle
		uniform sampler2D texture0;

		void main()
		{
			if (use_texture) {
				gl_FragColor = texture2D(texture0, gl_PointCoord) * v_color;
			} else {
				gl_FragColor = v_color;
			}
		}
		""" @ glsl_fragment_shader

	# Coordinates of particle effects
	var center = attributes["center"].as(AttributeVec4) is lazy

	# Should this program use the texture `texture`?
	var use_texture = uniforms["use_texture"].as(UniformBool) is lazy

	# Visible texture unit
	var texture = uniforms["texture0"].as(UniformSampler2D) is lazy

	# Color tint per vertex
	var color = attributes["color"].as(AttributeVec4) is lazy

	# Scaling per vertex
	var scale = attributes["scale"].as(AttributeFloat) is lazy

	# Model view projection matrix
	var mvp = uniforms["mvp"].as(UniformMat4) is lazy

	# Creation time of each particle
	var ot = attributes["ot"].as(AttributeFloat) is lazy

	# Current time
	var t = uniforms["t"].as(UniformFloat) is lazy

	# Time-to-live of each particle
	var ttl = attributes["ttl"].as(AttributeFloat) is lazy
end
lib/gamnit/depth/particles.nit:165,1--303,3