Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/src/particles/particles.sml
ViewVC logotype

View of /trunk/sml3d/src/particles/particles.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 292 - (download) (annotate)
Tue Sep 30 07:54:18 2008 UTC (11 years ago) by jhr
File size: 3219 byte(s)
repository restructuring
(* particles.sml
 *
 * COPYRIGHT (c) 2005 John Reppy (http://www.cs.uchicago.edu/~jhr)
 * All rights reserved.
 *)

structure Particles :> PARTICLES =
  struct

    type float = GL.float
    type float3 = (float * float * float)

  (* domains *)
    datatype domain
      = Dpoint of vec3f
      | Dline of {p : vec3f, v : vec3f}
      | Dtriangle of (vec3f * vec3f * vec3f)
      | Dplane of {pt : vec3f, n : vec3f}
      | Drectangle of {}
      | Dbox of {}
      | Dsphere of {}
      | Dcylinder of {}
      | Dcone of {}
      | Dblob of {}
      | Ddisc of {}

    datatype particle = P of {
	pos : vec3f,
	size : vec3f,
	vel : vec3f,
	color : color3f,
	age : float
      }

  (* a particle group is a container for particles that are affected by
   * the same forces, geometry, etc.
   *)
    datatype particle_group = PG of {
	count : int ref,
	maxParticles : int ref,
	particles : particle list ref,
	actions : action list ref,
      (* state used to create new particles *)
	rgb : domain ref,	(* RGB color domain *)
	alpha : float,		(* initial alpha *)
	szD : domain ref,	(* size domain *)
	velD : domain ref,	(* velocity domain *)
	dt : float ref,		(* time step *)
	start : {
	    age : float, stdev : float
	  } ref			(* starting age and std. deviation *)
      }

    withtype action = particle_group -> unit

    fun add (PG{count, maxParticles, particles, ...}) {pos, size, vel, color, age} =
	  not (!count >= !maxParticles)
	  andalso let
	    val p = P{pos=pos, size=size, vel=vel, color=color, age=age}
	    in
	      count := !count+1;
	      particles := p :: !particles
	    end

  (* return true if the point is within the domain *)
    fun within (d, pos) = (case d
	   of Dpoint _ => false
	    | Dline _ => false
	    | Dtriangle _ => false
	    | Dplane of {pt : vec3f, n : vec3f}
	    | Drectangle _ => false
	    | Dbox{??} =>
	    | Dsphere{??} =>
	    | Dcylinder{??} =>
	    | Dcone{??} =>
	    | Dblob{??} =>
	    | Ddisc _ => false
	  (* end case *))

  (* generate a random value uniformly distributed within the domain *)
    fun generate (Dpoint p) = unpackf3 p
      | generate (Dline{p, v}) = unpackf3(V.adds(p, ranf(), v))
      | generate (Dbox{p1, p2}) = let
	  val x = #x p1 + ranf()*(#x p2 - #x p1)
	  val y = #y p1 + ranf()*(#y p2 - #y p1)
	  val z = #z p1 + ranf()*(#z p2 - #z p1)
	  in
	    (x, y, z)
	  end
      | generate (Dtriangle(p1, p2, p3)) =
      | generate (Dplane{pt, n}) = unpackf3 p
      | generate (Drectangle{??}) =
      | generate (Dbox{??}) =
      | generate (Dsphere{??}) =
      | generate (Dcylinder{??}) =
      | generate (Dcone{??}) =
      | generate (Dblob{??}) =
      | generate (Ddisc{??}) =

  (***** Particle group attributes *****)

    fun color (PG{rgbD, alpha, ...}, c) = (
	  rgbD := Dpoint(#r c, #g c, #b c);
	  alpha := #a c)
    fun colorD (PG{rgbD, alpha, ...}, a, d) = (
	  rgbD := d;
	  alpha := a)

    fun size (PG{szD, ...}, v) = (szD := Dpoint v)
    fun sizeD (PG{szD, ...}, d) = (szD := d)

    fun velocity (PG{velD, ...}, v) = (velD = Dpoint v)
    fun velocityD (PG{velD, ...}, d) = (velD := d)

    fun startingAge (PG{start, ...}, age) = (start := age)

    fun timeStep (PG{dt, ...}, delta) = (dt := delta)

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0