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

SCM Repository

[sml3d] View of /trunk/sml3d/examples/md3-viewer/animation.sml
ViewVC logotype

View of /trunk/sml3d/examples/md3-viewer/animation.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 400 - (download) (annotate)
Wed Jan 14 17:17:14 2009 UTC (10 years, 7 months ago) by jhr
File size: 5331 byte(s)
  Loader for animation.cfg file
(* animation.sml
 *
 * COPYRIGHT (c) 2009 John Reppy (http://cs.uchicago.edu/~jhr)
 * All rights reserved.
 *
 * The animation.cfg file controls how the frames of animation in the MD3 files are grouped
 * together.  Each figure has 
 *
 * See docs/model_manual.html for more information.
 *)

structure Animation =
  struct

    val animations = [
			      (* Full-body animations *)
	    "BOTH_DEATH1",	(* The first twirling death animation *)
	    "BOTH_DEAD1",	(* The end of the first twirling death animation *)
	    "BOTH_DEATH2",	(* The second twirling death animation *)
	    "BOTH_DEAD2",	(* The end of the second twirling death animation *)
	    "BOTH_DEATH3",	(* The back flip death animation *)
	    "BOTH_DEAD3",	(* The end of the back flip death animation *)
			      (* Upper-body only animations *)
	    "TORSO_GESTURE",	(* The torso's gesturing animation *)
	    "TORSO_ATTACK",	(* The torso's attack1 animation *)
	    "TORSO_ATTACK2",	(* The torso's attack2 animation *)
	    "TORSO_DROP",	(* The torso's weapon drop animation *)
	    "TORSO_RAISE",	(* The torso's weapon pickup animation *)
	    "TORSO_STAND",	(* The torso's idle stand animation *)
	    "TORSO_STAND2",	(* The torso's idle stand2 animation *)
			      (* Lower-body only animations *)
	    "LEGS_WALKCR",	(* The legs's crouching walk animation *)
	    "LEGS_WALK",	(* The legs's walk animation *)
	    "LEGS_RUN",		(* The legs's run animation *)
	    "LEGS_BACK",	(* The legs's running backwards animation *)
	    "LEGS_SWIM",	(* The legs's swimming animation *)
	    "LEGS_JUMP",	(* The legs's jumping animation *)
	    "LEGS_LAND",	(* The legs's landing animation *)
	    "LEGS_JUMPB",	(* The legs's jumping back animation *)
	    "LEGS_LANDB",	(* The legs's landing back animation *)
	    "LEGS_IDLE",	(* The legs's idle stand animation *)
	    "LEGS_IDLECR",	(* The legs's idle crouching animation *)
	    "LEGS_TURN"		(* The legs's turn in place animation *)
	  ]

    datatype kind = BOTH | UPPER | LOWER

  (* animation IDs and kinds *)
    val death1 = (0, BOTH)
    val dead1 = (1, BOTH)
    val death2 = (2, BOTH)
    val dead2 = (3, BOTH)
    val death3 = (4, BOTH)
    val dead3 = (5, BOTH)
    val gesture = (6, UPPER)
    val attack1 = (7, UPPER)
    val attack2 = (8, UPPER)
    val dropWeap = (9, UPPER)
    val raiseWeap = (10, UPPER)
    val stand = (11, UPPER)
    val stand2 = (12, UPPER)
    val walkcr = (6, LOWER)
    val walk = (7, LOWER)
    val run = (8, LOWER)
    val back = (9, LOWER)
    val swim = (10, LOWER)
    val jump = (11, LOWER)
    val land = (12, LOWER)
    val jumpb = (13, LOWER)
    val landb = (14, LOWER)
    val idleLegs = (15, LOWER)
    val idlecrLegs = (16, LOWER)
    val turn = (17, LOWER)

    type animation_info = {
	id : int,		(* animation ID (from above list) *)
	kind : kind,		(* kind of animation *)
	first : int,		(* first frame of animation *)
	numFrames : int,	(* number of frames of animation *)
	looping : int,		(* looping frames (==0 for non-looping animation) *)
	period : Float.float	(* time between frames *)
      }

    fun readAnimation dir = let
	  val file = OS.joinDirFile {dir=dir, file="animation.cfg"}
	  val inS = TexIO.openIn file
	  fun readFile () = let
		fun readLn (id, kind) = (case TextIO.inputLine inS
		       of NONE => raise Fail "unexpected EOF in animation.cfg"
			| SOME ln => (case String.tokens Char.isSpace ln
			     of ("//" :: _) => readLn ()
(* TODO: record the extra info from the file *)
			      | ("sex" :: mf :: _) => readLn ()
			      | ("headoffset" :: x :: y :: z :: _) => readLn ()
			      | ("footsteps" :: token :: _) => readLn ()
			      | (fst :: num :: loop :: fps :: _) => let
				  val SOME first = Int.fromString fst
				  val SOME numFrames = Int.fromString num
				  val SOME looping = Int.fromString loop
				  val SOME fps = Float.fromString fps
				  in {
				    id = di, kind = kind,
				    first = first, numFrames = numFrames, looping = looping,
				    period = 1.0 / fps
				  } end
			      | _ => readLn ()
			    (* end case *))
		      (* end case *))
		val bothAnimations = [
			readLn death1,
			readLn dead1,
			readLn death2,
			readLn dead2,
			readLn death3,
			readLn dead3
		      ]
		val upperAnimations = [
			readLn gesture,
			readLn attack1,
			readLn attack2,
			readLn dropWeap,
			readLn raiseWeap,
			readLn stand,
			readLn stand2
		      ]
		val lowerAnimations = [
			readLn walkcr,
			readLn walk,
			readLn run,
			readLn back,
			readLn swim,
			readLn jump,
			readLn land,
			readLn jumpb,
			readLn landb,
			readLn idleLegs,
			readLn idlecrLegs,
			readLn turn
		      ]
	      (* adjust the starting frames for the lower animations *)
		val {first=firstUpper, ...} = List.hd upperAnimations
		val {first=firstLower, ...} = List.hd lowerAnimations
		val offset = firstLower - firstUpper
		val lowerAnimations =
		      List.map
			(fn {id, kind, first, numFrames, looping, period} => {
			  id = id, kind = kind,
			  first = first - offset,
			  numFrames = numFrames, looping = looping, period = period
			})
			  lowerAnimations
		in {
		  upper = Vector.fromList(bothAnimations @ upperAnimations),
		  lower = Vector.fromList(bothAnimations @ upperAnimations)
		} end
	  in
	    (readFile () before TextIO.closeIn inS) handle ex => (TextIO.closeIn inS; raise ex)
	  end

  end

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