Mismatched skeleton/animation safety nets
[dormin.git] / rend.ml
blobb15fd88395953dd82178007d160061a74f39003e
1 class type draw = object
2 method draw: unit
3 method char: char -> draw
4 method help: (string * string * string) list
5 end;;
7 let try_vbo = ref true
8 let nmo_name = ref None
9 let anb_names = ref []
10 let skb_name = ref None
11 let mipmaps = ref false
12 let slerp_step = ref 1.0
14 type view =
15 { mutable w : int
16 ; mutable h : int
17 ; mutable rotx : float
18 ; mutable roty : float
19 ; mutable rotz : float
20 ; mutable zoom : float
21 ; mutable center : (float * float * float)
22 ; mutable radial_scale : float
23 ; mutable objs : draw list
24 ; mutable persp : bool
25 ; mutable last_time : float
26 ; mutable animated : bool
27 ; mutable dumpchan : out_channel Lazy.t
28 ; mutable dodump : bool
29 ; mutable aincr : float
30 ; mutable roteye: bool
31 ; mutable sphere : bool
32 ; mutable help : bool
33 ; mutable x : int
34 ; mutable y : int
35 ; mutable mtype : [`none|`zoom|`rotate|`move]
36 ; mutable transl : (float * float * float)
37 ; mutable alpha : float
38 ; mutable ambient : float
39 ; mutable diffuse : float
42 let view =
43 { w = 0; h = 0
44 ; rotx = 4.0; roty = 16.0; rotz = 0.0
45 ; center = (0.0, 0.0, 0.0)
46 ; radial_scale = 0.0
47 ; zoom = 1.2
48 ; objs = []
49 ; persp = true
50 ; last_time = 0.0
51 ; animated = false
52 ; dumpchan = lazy (open_out_bin "dump.rgb")
53 ; dodump = false
54 ; aincr = 3.0
55 ; roteye = true
56 ; sphere = false
57 ; help = false
58 ; x = 0
59 ; y = 0
60 ; mtype = `none
61 ; transl = (0.0, 0.0, 0.0)
62 ; alpha = 0.04
63 ; ambient = 1.3
64 ; diffuse = 0.5
68 let mapchar c = view.objs <- List.map (fun draw -> draw#char c) view.objs;;
69 let appdraw () = List.iter (fun draw -> draw#draw) view.objs;;
70 let deg2rad deg = deg /. 180.0 *. acos ~-.1.;;
72 let center_and_radial_scale (minx, maxx, miny, maxy, minz, maxz) =
73 let xc = (maxx +. minx) /. 2.0 in
74 let yc = (maxy +. miny) /. 2.0 in
75 let zc = (maxz +. minz) /. 2.0 in
76 let rs =
77 let rs = maxx -. minx in
78 let rs = max rs (maxy -. miny) in
79 let rs = max rs (maxz -. minz) in
82 if false
83 then (
84 Format.eprintf "x (% f, % f)@." minx maxx;
85 Format.eprintf "y (% f, % f)@." miny maxy;
86 Format.eprintf "z (% f, % f)@." minz maxz;
87 Format.eprintf "c (% f, % f, % f)@." xc yc zc;
88 Format.eprintf "rs %f@." rs;
90 ((xc, yc, zc), rs)
93 let help () =
94 let font = Glut.BITMAP_HELVETICA_18 in
95 let draw_string x y s =
96 GlPix.raster_pos ~x ~y ();
97 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
99 GlMat.mode `projection;
100 GlMat.push ();
101 GlMat.load_identity ();
102 GlMat.mode `modelview;
103 GlMat.push ();
104 GlMat.load_identity ();
105 GlMat.ortho
106 ~x:(0.0, float view.w)
107 ~y:(0.0, float view.h)
108 ~z:(-1., 1.)
111 Gl.disable `depth_test;
112 Gl.disable `alpha_test;
114 GlDraw.polygon_mode `both `fill;
115 Gl.enable `blend;
116 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
117 GlDraw.color (0., 0., 0.) ~alpha:0.3;
118 GlDraw.rect (0., 0.) (float view.w, float view.h);
119 Gl.disable `blend;
121 GlDraw.color (1., 1., 1.);
122 let rec loop row = function
123 | [] -> ()
124 | (s, s2, s3) :: rest ->
125 let y = view.h - row * 18 - 2 in
126 let x = if row = 1 then 0.0 else 5.0 in
127 draw_string (x+.5.0) (float y) s;
128 draw_string (x+.105.0) (float y) s2;
129 draw_string (x+.345.0) (float y) s3;
130 loop (row+1) rest
132 let help =
133 let onoff b = if b then "on" else "off" in
134 let angles =
135 Printf.sprintf "% f, % f, % f" view.rotx view.roty view.rotz
137 [("Keys (h toggles this screen):", "", "")
138 ;"e", "toggle eye/model rotation", if view.roteye then "eye" else "model"
139 ;"a", "toggle animation", onoff view.animated
140 ;"o", "toggle bounding sphere", onoff view.sphere
141 ;"d", "dump images to dump.rgb", onoff view.dodump
142 ;"q, ESC", "quit", ""
143 ;"z,x,arrows", "rotate", angles
144 ;"0,9", "zoom", Printf.sprintf "%f" view.zoom
145 ;"< , >", "decrease/increase alpha", Printf.sprintf "%1.2f" view.alpha
146 ;"[ , ]", "decrease/increase slerp step", Printf.sprintf "%2.1f" !slerp_step
147 ;"3,4", "decrease/increase ambient", Printf.sprintf "%2.1f" view.ambient
148 ;"5,6", "decrease/increase diffuse", Printf.sprintf "%2.1f" view.diffuse
149 ;"","",""
152 let help =
153 List.fold_left (fun accu draw -> accu @ draw#help) help view.objs
155 loop 1
156 (help @
157 ["", "", ""
158 ;"Move mouse while holding left button pressed to rotate model", "", ""
159 ;"Move mouse while holding right button pressed to zoom", "", ""
160 ;"Move mouse while holding left button and shift pressed to move model", "", ""
161 ;"",
162 (let tx, ty, tz = view.transl in
163 Printf.sprintf "translation % f, % f, % f" tx ty tz),
168 Gl.enable `depth_test;
169 Gl.enable `alpha_test;
170 GlMat.pop ();
171 GlMat.mode `projection;
172 GlMat.pop ();
175 let display () =
176 GlClear.color (0.5, 0.5, 0.5) ~alpha:1.0;
177 GlClear.clear [`color; `depth];
178 GlDraw.color (0.0, 0.0, 0.0);
179 GlFunc.alpha_func `greater view.alpha;
181 if view.sphere then (
182 let cx, cy, cz = view.center in
183 let cx = -.cx and cy = -.cy and cz = -.cz in
184 GlDraw.line_width 1.0;
185 GlMat.mode `modelview;
186 GlMat.push ();
187 GlMat.translate3 (cx, cy, cz);
188 GlDraw.polygon_mode `back `line;
189 GlDraw.polygon_mode `front `line;
190 Gl.disable `texture_2d;
191 GluQuadric.sphere ~radius:(0.7*.view.radial_scale) ~stacks:25 ~slices:25 ();
192 GlMat.pop ();
195 appdraw ();
196 if view.help then help ();
197 Glut.swapBuffers ();
199 if view.dodump then (
200 let pix = GlPix.read 0 0 view.w view.h `rgb `ubyte in
201 let raw = GlPix.to_raw pix in
202 let pitch = view.w * 3 in
203 let size = view.h * pitch in
204 let s = Raw.gets_string raw 0 size in
205 let dc = Lazy.force view.dumpchan in
206 let rec loop pos =
207 let pos = pos - pitch in
208 if pos < 0 then ()
209 else (
210 output dc s pos pitch;
211 loop pos
214 loop size;
218 let get_eye_and_up () =
219 if not view.roteye
220 then
221 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
222 else
223 let q =
224 let rx = deg2rad view.rotx
225 and ry = deg2rad view.roty
226 and rz = deg2rad view.rotz in
227 Qtr.from_euler rz ~-.ry rx
229 let v = Qtr.apply q (Vec.make 0.0 0.0 2.0) in
230 let u = Qtr.apply q (Vec.make 0.0 1.0 0.0) in
231 Vec.elts v, Vec.elts u
234 let setup w h =
235 view.w <- w;
236 view.h <- h;
237 GlDraw.viewport 0 0 w h;
239 let rs = view.zoom /. view.radial_scale in
241 GlMat.mode `projection;
242 GlMat.load_identity ();
243 GlMat.translate3 view.transl;
244 GluMat.perspective
245 ~fovy:45.0
246 ~aspect:(float w /. float h)
247 ~z:(0.1, 10.)
250 GlMat.mode `modelview;
251 GlMat.load_identity ();
253 let eye, up = get_eye_and_up () in
254 GluMat.look_at
255 ~eye
256 ~center:(0.0, 0.0, 0.0)
260 if not view.roteye then (
261 GlMat.rotate ~angle:view.rotx ~x:1.0 ();
262 GlMat.rotate ~angle:view.roty ~y:~-.1.0 ();
263 GlMat.rotate ~angle:view.rotz ~z:1.0 ();
266 GlMat.scale3 (-.rs, rs, rs);
267 GlMat.translate3 view.center;
270 let reshape ~w ~h =
271 setup w h;
274 let idle () =
275 let deadline = view.last_time +. 0.04 in
276 let currtime = Unix.gettimeofday () in
277 if deadline > currtime
278 then
279 let _ = Unix.select [] [] [] (deadline -. currtime) in
280 view.last_time <- Unix.gettimeofday ()
281 else
282 view.last_time <- view.last_time +. 0.04
284 mapchar 'n';
285 Glut.postRedisplay ();
288 let keyboard ~key ~x ~y =
289 begin match Char.chr key with
290 | '\027' | 'q' -> exit 0
291 | '9' -> view.zoom <- view.zoom +. 0.05
292 | '0' -> view.zoom <- view.zoom -. 0.05
293 | 'z' -> view.roty <- view.roty +. view.aincr
294 | 'x' -> view.roty <- view.roty -. view.aincr
295 | 'd' -> view.dodump <- not view.dodump
296 | 'e' -> view.roteye <- not view.roteye
297 | 'o' -> view.sphere <- not view.sphere;
298 | 'h' -> view.help <- not view.help
299 | 'a' ->
300 if view.animated
301 then (
302 view.animated <- false;
303 Glut.idleFunc None
305 else (
306 view.animated <- true; view.
307 last_time <- Unix.gettimeofday ();
308 Glut.idleFunc (Some idle)
310 | ('f' | 'b') as c when not view.animated -> mapchar c;
311 | '<' -> view.alpha <- max (view.alpha -. 0.01) 0.0;
312 | '>' -> view.alpha <- min (view.alpha +. 0.01) 1.0;
313 | '[' -> slerp_step := max (!slerp_step -. 0.1) 0.0;
314 | ']' -> slerp_step := min (!slerp_step +. 0.1) 1.0;
315 | '3' -> view.ambient <- view.ambient -. 0.1;
316 | '4' -> view.ambient <- view.ambient +. 0.1;
317 | '5' -> view.diffuse <- view.diffuse -. 0.1;
318 | '6' -> view.diffuse <- view.diffuse +. 0.1;
319 | c -> mapchar c;
320 end;
321 setup view.w view.h;
322 Glut.postRedisplay ();
325 let special ~key ~x ~y =
326 begin match key with
327 | Glut.KEY_LEFT -> view.rotz <- view.rotz +. view.aincr
328 | Glut.KEY_RIGHT -> view.rotz <- view.rotz -. view.aincr
329 | Glut.KEY_UP -> view.rotx <- view.rotx -. view.aincr
330 | Glut.KEY_DOWN -> view.rotx <- view.rotx +. view.aincr
331 | _ -> ()
332 end;
333 setup view.w view.h;
334 Glut.postRedisplay ();
337 let motion ~x ~y =
338 let dx = (x - view.x) in
339 let dy = (y - view.y) in
340 view.x <- x;
341 view.y <- y;
342 match view.mtype with
343 | `move ->
344 let x, y, z = view.transl in
345 let dx = float dx /. 100.0
346 and dy = float dy /. 100.0 in
347 view.transl <- (x +. dx, y -. dy, z);
348 setup view.w view.h;
349 Glut.postRedisplay ();
350 | `rotate ->
351 view.rotx <- view.rotx +. float dy;
352 view.roty <- view.roty -. float dx;
353 setup view.w view.h;
354 Glut.postRedisplay ();
355 | `zoom ->
356 view.zoom <- view.zoom +. (float dy /. 50.);
357 setup view.w view.h;
358 Glut.postRedisplay ();
359 | `none ->
363 let mouse ~button ~state ~x ~y =
364 if button = Glut.LEFT_BUTTON
365 then (
366 if state = Glut.DOWN
367 then (
368 view.x <- x;
369 view.y <- y;
370 view.mtype <-
371 if Glut.getModifiers () = Glut.active_shift
372 then `move else `rotate;
374 else view.mtype <- `none;
376 else if button = Glut.RIGHT_BUTTON
377 then (
378 if state = Glut.DOWN
379 then (
380 view.x <- x;
381 view.y <- y;
382 view.mtype <- `zoom;
384 else view.mtype <- `none;
388 let main () =
389 let w = 704
390 and h = 576 in
391 let _ = Glut.init Sys.argv in
392 let () = Glut.initDisplayMode ~depth:true ~double_buffer:true () in
393 let () = Glut.initWindowSize w h in
394 let _ = Glut.createWindow "rend (press 'h' to get help)" in
395 Gl.enable `depth_test;
396 Gl.enable `alpha_test;
397 let () = Glut.displayFunc display in
398 let () = Glut.reshapeFunc reshape in
399 let () = Glut.keyboardFunc keyboard in
400 let () = Glut.specialFunc special in
401 let () = Glut.mouseFunc mouse in
402 let () = Glut.motionFunc motion in
403 mapchar '\000'; (* bootstrap *)
404 let () = Glut.mainLoop () in
408 let add_obj draw =
409 view.objs <- draw :: view.objs;
412 let init minmax =
413 let (cx, cy, cz), rs = center_and_radial_scale minmax in
414 view.center <- (-.cx, -.cy, -.cz);
415 view.radial_scale <- rs;
418 let _ =
419 let setsome r s = r := Some s in
420 let spec =
421 ["-slice", Arg.String Slice.openslice, "<path> of file/dir to slice data to"
422 ;"-index", Arg.Set_string Xff.index_path, "<path> to index"
423 ;"-base", Arg.String (setsome Xff.base_path), "<directory> base"
424 ;"-sstep", Arg.Set_float slerp_step, "<float> slerp step"
425 ;"-novbo", Arg.Clear try_vbo, " do not use vertex buffer objects"
426 ;("-skb", Arg.String (setsome skb_name),
427 "<name> use specified skb instead of guessing")
428 ;"-mipmaps", Arg.Set mipmaps, " use mipmaps"
431 Arg.parse (Arg.align spec)
432 (fun s ->
433 if !nmo_name != None
434 then anb_names := s :: !anb_names
435 else nmo_name := Some s;
437 "Usage: dormin [options] model.nmo [animation.anb ...]"