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

SCM Repository

[sml3d] View of /trunk/sml3d/examples/bounce/ball.sml
ViewVC logotype

View of /trunk/sml3d/examples/bounce/ball.sml

Parent Directory Parent Directory | Revision Log Revision Log


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

structure Ball : sig

    datatype ball = BALL of{
	initT : GL.double,	(* time of position/direction info *)
	pos : GL.vec3f,		(* position *)
	dir : GL.vec3f,		(* unit direction vector *)
	color : GL.color4ub
      }

    val radius : GL.double

    val new : GL.double -> ball

    val draw : GL.double -> ball -> unit

  end = struct

    structure U = SML3dTypeUtil

    val epsilon = 1.0e~6

    datatype ball = BALL of {
	initT : GL.double,	(* time of position/direction info *)
	pos : GL.vec3f,		(* position *)
	dir : GL.vec3f,		(* unit direction vector *)
	color : GL.color4ub
      }

  (* the size of a ball *)
    val radius : GL.double = 0.05

  (* generate a random float *)
    val ranf = FP.dtof o Rand48.drand

  (* generate a random unit vector *)
    fun randVec () = let
	  fun randCoord () = (2.0 * ranf()) - 1.0
	  fun lp () = let
		val v = U.packv3 (randCoord(), randCoord(), randCoord())
		val lenSq = Vec3f.lengthSq v
		in
		  if (Vec3f.lengthSq v <= epsilon)
		    then lp()
		    else Vec3f.scale(1.0 / lenSq, v)
		end
	  in
	    lp()
	  end

  (* ball colors *)
    val colors = Vector.fromList [
	    U.packc4 (0w204, 0w255, 0w102, 0w255),	(* honeydew *)
	    U.packc4 (0w255,   0w0, 0w128, 0w255),	(* strawberry *)
	    U.packc4 (0w102, 0w255, 0w102, 0w255),	(* flora *)
	    U.packc4 ( 0w0,  0w255, 0w255, 0w255),	(* turqoise *)
	    U.packc4 (0w255, 0w102, 0w102, 0w255),	(* salmon *)
	    U.packc4 (0w255, 0w111, 0w207, 0w255),	(* carnation *)
	    U.packc4 (  0w0, 0w128, 0w255, 0w255),	(* aqua *)
	    U.packc4 (0w102, 0w255, 0w204, 0w255)	(* spindrift *)
	  ]

    fun new t = BALL{
	    initT = t,
	    pos = let
		fun randCoord () = (1.8 * ranf()) - 0.9
		in
		  U.packv3 (randCoord(), randCoord(), randCoord())
		end,
	    dir = randVec(),
	    color = Vector.sub(colors, Rand48.lrand() mod Vector.length colors)
	  }

    fun draw t (BALL{initT, pos, dir, color, ...}) = (
	  GL.pushMatrix();
	  GL.translatefv (Vec3f.adds(pos, FP.dtof(t - initT), dir));
	  GL.color4ubv color;
	  GLUT.solidSphere (radius, 12, 8);
	  GL.popMatrix())

  end

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