Improve help messages readability
[dormin.git] / rend.ml
blobeddade4d3f13928ca32bd087201f15636faf8dcd
1 type cmd = | Char of char | Draw;;
2 type func = cmd -> func_ret
3 and func_ret = Func of func
5 let nmo_name = ref None
6 let anb_name = ref None
7 let mipmaps = ref false
9 type view =
10 { mutable w : int
11 ; mutable h : int
12 ; mutable rotx : float
13 ; mutable roty : float
14 ; mutable rotz : float
15 ; mutable zoom : float
16 ; mutable center : (float * float * float)
17 ; mutable radial_scale : float
18 ; mutable func : func list
19 ; mutable persp : bool
20 ; mutable last_time : float
21 ; mutable animated : bool
22 ; mutable dumpchan : out_channel Lazy.t
23 ; mutable dodump : bool
24 ; mutable aincr : float
25 ; mutable roteye: bool
26 ; mutable sphere : bool
27 ; mutable help : bool
28 ; mutable x : int
29 ; mutable y : int
30 ; mutable mtype : [`none|`zoom|`rotate|`move]
31 ; mutable transl : (float * float * float)
32 ; mutable alpha : float
35 let view =
36 { w = 0; h = 0
37 ; rotx = 0.0; roty = 0.0; rotz = 0.0
38 ; center = (0.0, 0.0, 0.0)
39 ; radial_scale = 0.0
40 ; zoom = 1.0
41 ; func = []
42 ; persp = true
43 ; last_time = 0.0
44 ; animated = false
45 ; dumpchan = lazy (open_out_bin "dump.rgb")
46 ; dodump = false
47 ; aincr = 3.0
48 ; roteye = true
49 ; sphere = false
50 ; help = false
51 ; x = 0
52 ; y = 0
53 ; mtype = `none
54 ; transl = (0.0, 0.0, 0.0)
55 ; alpha = 0.0
59 let deg2rad deg = deg /. 180.0 *. acos ~-.1.;;
61 let center_and_radial_scale (minx, maxx, miny, maxy, minz, maxz) =
62 let xc = (maxx -. minx) /. 2.0 +. minx in
63 let yc = (maxy -. miny) /. 2.0 +. miny in
64 let zc = (maxz -. minz) /. 2.0 +. minz in
65 let rs =
66 let rs = maxx -. minx in
67 let rs = max rs (maxy -. miny) in
68 let rs = max rs (maxz -. minz) in
71 if false
72 then (
73 Format.eprintf "x (% f, % f)@." minx maxx;
74 Format.eprintf "y (% f, % f)@." miny maxy;
75 Format.eprintf "z (% f, % f)@." minz maxz;
76 Format.eprintf "c (% f, % f, % f)@." xc yc zc;
77 Format.eprintf "rs %f@." rs;
79 ((xc, yc, zc), rs)
82 let help () =
83 let font = Glut.BITMAP_HELVETICA_18 in
84 let draw_string x y s =
85 GlPix.raster_pos ~x ~y ();
86 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
88 GlMat.mode `projection;
89 GlMat.push ();
90 GlMat.load_identity ();
91 GlMat.mode `modelview;
92 GlMat.push ();
93 GlMat.load_identity ();
94 GlMat.ortho
95 ~x:(0.0, float view.w)
96 ~y:(0.0, float view.h)
97 ~z:(-1., 1.)
100 Gl.enable `blend;
101 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
102 GlDraw.color (0., 0., 0.) ~alpha:0.3;
103 GlDraw.rect (0., 0.) (float view.w, float view.h);
104 Gl.disable `blend;
106 Gl.disable `depth_test;
107 Gl.disable `alpha_test;
108 GlDraw.color (1., 1., 1.);
109 let rec loop row = function
110 | [] -> ()
111 | (s, s2) :: rest ->
112 let y = view.h - row * 18 in
113 draw_string 0.0 (float y) s;
114 draw_string 100.0 (float y) s2;
115 loop (row+1) rest
117 loop 1
118 [("Keys:", "")
119 ;" h", "toggle help"
120 ;" e", "toggle eye/model rotation"
121 ;" s", "toggle skeleton"
122 ;" t", "toggle texturing"
123 ;" l", "toggle lighting"
124 ;" m", "toggle model"
125 ;" w", "toggle wireframe"
126 ;" a", "toggle animation"
127 ;" o", "toggle bounding sphere"
128 ;" c", "toggle color material"
129 ;" f", "forward one frame"
130 ;" b", "backward one frame"
131 ;" r", "bring skeleton to rest pose and set frame number to 0"
132 ;" d", "dump images to dump.rgb"
133 ;" z,x,arrows", "rotate"
134 ;" 0,9", "zoom"
135 ;" <,>", "increase/decrease alpha"
136 ;"", ""
137 ;"Move mouse while holding left button pressed to rotate model", ""
138 ;"Move mouse while holding right button pressed to zoom", ""
139 ;"Move mouse while holding left button and shift pressed to move model", ""
142 Gl.enable `depth_test;
143 Gl.enable `alpha_test;
144 GlMat.pop ();
145 GlMat.mode `projection;
146 GlMat.pop ();
149 let display () =
150 GlClear.color (0.5, 0.5, 0.5) ~alpha:1.0;
151 GlClear.clear [`color; `depth];
152 GlDraw.color (0.0, 0.0, 0.0);
153 GlFunc.alpha_func `greater view.alpha;
155 if view.sphere then (
156 let cx, cy, cz = view.center in
157 let cx = -.cx and cy = -.cy and cz = -.cz in
158 GlMat.mode `modelview;
159 GlMat.push ();
160 GlMat.translate3 (cx, cy, cz);
161 GlDraw.polygon_mode `back `line;
162 GlDraw.polygon_mode `front `line;
163 Gl.disable `texture_2d;
164 GluQuadric.sphere ~radius:(0.7*.view.radial_scale) ~stacks:25 ~slices:25 ();
165 GlMat.pop ();
168 List.iter (fun f -> ignore (f Draw)) view.func;
169 if view.help then help ();
170 Glut.swapBuffers ();
172 if view.dodump then (
173 let pix = GlPix.read 0 0 view.w view.h `rgb `ubyte in
174 let raw = GlPix.to_raw pix in
175 let pitch = view.w * 3 in
176 let size = view.h * pitch in
177 let s = Raw.gets_string raw 0 size in
178 let dc = Lazy.force view.dumpchan in
179 let rec loop pos =
180 let pos = pos - pitch in
181 if pos < 0 then ()
182 else (
183 output dc s pos pitch;
184 loop pos
187 loop size;
191 let get_eye_and_up () =
192 if not view.roteye
193 then
194 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
195 else
196 let q =
197 let rx = deg2rad view.rotx
198 and ry = deg2rad view.roty
199 and rz = deg2rad view.rotz in
200 Qtr.from_euler rz ~-.ry rx
202 let v = Qtr.apply q (Vec.make 0.0 0.0 2.0) in
203 let u = Qtr.apply q (Vec.make 0.0 1.0 0.0) in
204 Vec.elts v, Vec.elts u
207 let setup w h =
208 view.w <- w;
209 view.h <- h;
210 GlDraw.viewport 0 0 w h;
212 let rs = view.zoom /. view.radial_scale in
214 GlMat.mode `projection;
215 GlMat.load_identity ();
216 GlMat.translate3 view.transl;
217 GluMat.perspective
218 ~fovy:45.0
219 ~aspect:(float w /. float h)
220 ~z:(0.1, 10.)
223 GlMat.mode `modelview;
224 GlMat.load_identity ();
226 let eye, up = get_eye_and_up () in
227 GluMat.look_at
228 ~eye
229 ~center:(0.0, 0.0, 0.0)
233 if not view.roteye then (
234 GlMat.rotate ~angle:view.rotx ~x:1.0 ();
235 GlMat.rotate ~angle:view.roty ~y:~-.1.0 ();
236 GlMat.rotate ~angle:view.rotz ~z:1.0 ();
239 GlMat.scale3 (-.rs, rs, rs);
240 GlMat.translate3 view.center;
243 let reshape ~w ~h =
244 setup w h;
247 let allfunc cmd =
248 view.func <- List.map (fun f -> let Func fr = f cmd in fr) view.func;
251 let idle () =
252 let deadline = view.last_time +. 0.04 in
253 let currtime = Unix.gettimeofday () in
254 if deadline > currtime
255 then
256 let _ = Unix.select [] [] [] (deadline -. currtime) in
257 view.last_time <- Unix.gettimeofday ()
258 else
259 view.last_time <- view.last_time +. 0.04
261 view.func <- List.map (fun f -> let Func fr = f (Char 'f') in fr) view.func;
262 Glut.postRedisplay ();
265 let keyboard ~key ~x ~y =
266 begin match Char.chr key with
267 | '\027' | 'q' -> exit 0
268 | '9' -> view.zoom <- view.zoom +. 0.05
269 | '0' -> view.zoom <- view.zoom -. 0.05
270 | 'z' -> view.roty <- view.roty +. view.aincr
271 | 'x' -> view.roty <- view.roty -. view.aincr
272 | 'd' -> view.dodump <- not view.dodump
273 | 'e' -> view.roteye <- not view.roteye
274 | 'o' -> view.sphere <- not view.sphere;
275 | 'h' -> view.help <- not view.help
276 | 'a' ->
277 if view.animated
278 then (
279 view.animated <- false;
280 Glut.idleFunc None
282 else (
283 view.animated <- true; view.
284 last_time <- Unix.gettimeofday ();
285 Glut.idleFunc (Some idle)
287 | 'f' | 'b' when not view.animated -> allfunc (Char (Char.chr key))
288 | '<' -> view.alpha <- min (view.alpha +. 0.01) 1.0;
289 | '>' -> view.alpha <- max (view.alpha -. 0.01) 0.0;
290 | c -> allfunc (Char c)
291 end;
292 setup view.w view.h;
293 Glut.postRedisplay ();
296 let special ~key ~x ~y =
297 begin match key with
298 | Glut.KEY_LEFT -> view.rotz <- view.rotz +. view.aincr
299 | Glut.KEY_RIGHT -> view.rotz <- view.rotz -. view.aincr
300 | Glut.KEY_UP -> view.rotx <- view.rotx -. view.aincr
301 | Glut.KEY_DOWN -> view.rotx <- view.rotx +. view.aincr
302 | _ -> ()
303 end;
304 setup view.w view.h;
305 Glut.postRedisplay ();
308 let motion ~x ~y =
309 let dx = (x - view.x) in
310 let dy = (y - view.y) in
311 view.x <- x;
312 view.y <- y;
313 match view.mtype with
314 | `move ->
315 let x, y, z = view.transl in
316 let dx = float dx /. 100.0
317 and dy = float dy /. 100.0 in
318 view.transl <- (x +. dx, y -. dy, z);
319 setup view.w view.h;
320 Glut.postRedisplay ();
321 | `rotate ->
322 view.rotx <- view.rotx +. float dy;
323 view.roty <- view.roty -. float dx;
324 setup view.w view.h;
325 Glut.postRedisplay ();
326 | `zoom ->
327 view.zoom <- view.zoom +. (float dy /. 50.);
328 setup view.w view.h;
329 Glut.postRedisplay ();
330 | `none ->
334 let mouse ~button ~state ~x ~y =
335 if button = Glut.LEFT_BUTTON
336 then (
337 if state = Glut.DOWN
338 then (
339 view.x <- x;
340 view.y <- y;
341 view.mtype <-
342 if Glut.getModifiers () = Glut.active_shift
343 then `move else `rotate;
345 else view.mtype <- `none;
347 else if button = Glut.RIGHT_BUTTON
348 then (
349 if state = Glut.DOWN
350 then (
351 view.x <- x;
352 view.y <- y;
353 view.mtype <- `zoom;
355 else view.mtype <- `none;
359 let main () =
360 let w = 704
361 and h = 576 in
362 let _ = Glut.init Sys.argv in
363 let () = Glut.initDisplayMode ~depth:true ~double_buffer:true () in
364 let () = Glut.initWindowSize w h in
365 let _ = Glut.createWindow "rend (press 'h' to get help)" in
366 Gl.enable `depth_test;
367 Gl.enable `alpha_test;
368 let () = Glut.displayFunc display in
369 let () = Glut.reshapeFunc reshape in
370 let () = Glut.keyboardFunc keyboard in
371 let () = Glut.specialFunc special in
372 let () = Glut.mouseFunc mouse in
373 let () = Glut.motionFunc motion in
374 let () = Glut.mainLoop () in
378 let add_func func =
379 view.func <- func :: view.func;
382 let init minmax =
383 let (cx, cy, cz), rs = center_and_radial_scale minmax in
384 view.center <- (-.cx, -.cy, -.cz);
385 view.radial_scale <- rs;
388 let _ =
389 let setsome r s = r := Some s in
390 let spec =
391 ["-slice", Arg.String Slice.openslice, " <path> of file to slice data to"
392 ;"-index", Arg.Set_string Xff.index_path, " <path> of index"
393 ;"-base", Arg.String (setsome Xff.base_path), " <directory> base"
394 ;"-mipmaps", Arg.Set mipmaps, "use mipmaps"
397 Arg.parse (Arg.align spec)
398 (fun s ->
399 if !nmo_name != None then (
400 if !anb_name != None
401 then raise (Arg.Bad "invalid usage")
402 else anb_name := Some s;
403 ) else nmo_name := Some s;
405 "Usage: dormin [options] model.nmo [animation.anb]"