Fix 's' so that it really toggle between on/off
[dormin.git] / rend.ml
blob91e2498eec5062b3f6c80cd6f267565cc60bad8f
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 nmo_name = ref None
8 let anb_names = ref []
9 let skb_name = ref None
10 let mipmaps = ref false
11 let slerp_step = ref 1.0
13 type view =
14 { mutable w : int
15 ; mutable h : int
16 ; mutable rotx : float
17 ; mutable roty : float
18 ; mutable rotz : float
19 ; mutable zoom : float
20 ; mutable center : (float * float * float)
21 ; mutable radial_scale : float
22 ; mutable objs : draw list
23 ; mutable persp : bool
24 ; mutable last_time : float
25 ; mutable animated : bool
26 ; mutable dumpchan : out_channel Lazy.t
27 ; mutable dodump : bool
28 ; mutable aincr : float
29 ; mutable roteye: bool
30 ; mutable sphere : bool
31 ; mutable help : bool
32 ; mutable x : int
33 ; mutable y : int
34 ; mutable mtype : [`none|`zoom|`rotate|`move]
35 ; mutable transl : (float * float * float)
36 ; mutable alpha : float
37 ; mutable ambient : float
38 ; mutable diffuse : float
41 let view =
42 { w = 0; h = 0
43 ; rotx = 0.0; roty = 0.0; rotz = 0.0
44 ; center = (0.0, 0.0, 0.0)
45 ; radial_scale = 0.0
46 ; zoom = 1.0
47 ; objs = []
48 ; persp = true
49 ; last_time = 0.0
50 ; animated = false
51 ; dumpchan = lazy (open_out_bin "dump.rgb")
52 ; dodump = false
53 ; aincr = 3.0
54 ; roteye = true
55 ; sphere = false
56 ; help = false
57 ; x = 0
58 ; y = 0
59 ; mtype = `none
60 ; transl = (0.0, 0.0, 0.0)
61 ; alpha = 0.04
62 ; ambient = 1.3
63 ; diffuse = 0.5
67 let deg2rad deg = deg /. 180.0 *. acos ~-.1.;;
69 let center_and_radial_scale (minx, maxx, miny, maxy, minz, maxz) =
70 let xc = (maxx +. minx) /. 2.0 in
71 let yc = (maxy +. miny) /. 2.0 in
72 let zc = (maxz +. minz) /. 2.0 in
73 let rs =
74 let rs = maxx -. minx in
75 let rs = max rs (maxy -. miny) in
76 let rs = max rs (maxz -. minz) in
79 if false
80 then (
81 Format.eprintf "x (% f, % f)@." minx maxx;
82 Format.eprintf "y (% f, % f)@." miny maxy;
83 Format.eprintf "z (% f, % f)@." minz maxz;
84 Format.eprintf "c (% f, % f, % f)@." xc yc zc;
85 Format.eprintf "rs %f@." rs;
87 ((xc, yc, zc), rs)
90 let help () =
91 let font = Glut.BITMAP_HELVETICA_18 in
92 let draw_string x y s =
93 GlPix.raster_pos ~x ~y ();
94 String.iter (fun c -> Glut.bitmapCharacter ~font ~c:(Char.code c)) s
96 GlMat.mode `projection;
97 GlMat.push ();
98 GlMat.load_identity ();
99 GlMat.mode `modelview;
100 GlMat.push ();
101 GlMat.load_identity ();
102 GlMat.ortho
103 ~x:(0.0, float view.w)
104 ~y:(0.0, float view.h)
105 ~z:(-1., 1.)
108 Gl.disable `depth_test;
109 Gl.disable `alpha_test;
111 GlDraw.polygon_mode `both `fill;
112 Gl.enable `blend;
113 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
114 GlDraw.color (0., 0., 0.) ~alpha:0.3;
115 GlDraw.rect (0., 0.) (float view.w, float view.h);
116 Gl.disable `blend;
118 GlDraw.color (1., 1., 1.);
119 let rec loop row = function
120 | [] -> ()
121 | (s, s2, s3) :: rest ->
122 let y = view.h - row * 18 - 2 in
123 let x = if row = 1 then 0.0 else 5.0 in
124 draw_string (x+.5.0) (float y) s;
125 draw_string (x+.105.0) (float y) s2;
126 draw_string (x+.345.0) (float y) s3;
127 loop (row+1) rest
129 let help =
130 let onoff b = if b then "on" else "off" in
131 let angles =
132 Printf.sprintf "% f, % f, % f" view.rotx view.roty view.rotz
134 [("Keys (h toggles this screen):", "", "")
135 ;"e", "toggle eye/model rotation", if view.roteye then "eye" else "model"
136 ;"a", "toggle animation", onoff view.animated
137 ;"o", "toggle bounding sphere", onoff view.sphere
138 ;"d", "dump images to dump.rgb", onoff view.dodump
139 ;"q, ESC", "quit", ""
140 ;"z,x,arrows", "rotate", angles
141 ;"0,9", "zoom", Printf.sprintf "%f" view.zoom
142 ;"< , >", "decrease/increase alpha", Printf.sprintf "%1.2f" view.alpha
143 ;"[ , ]", "decrease/increase slerp step", Printf.sprintf "%2.1f" !slerp_step
144 ;"3,4", "decrease/increase ambient", Printf.sprintf "%2.1f" view.ambient
145 ;"5,6", "decrease/increase diffuse", Printf.sprintf "%2.1f" view.diffuse
146 ;"","",""
149 let help =
150 List.fold_left (fun accu draw -> accu @ draw#help) help view.objs
152 loop 1
153 (help @
154 ["", "", ""
155 ;"Move mouse while holding left button pressed to rotate model", "", ""
156 ;"Move mouse while holding right button pressed to zoom", "", ""
157 ;"Move mouse while holding left button and shift pressed to move model", "", ""
158 ;"",
159 (let tx, ty, tz = view.transl in
160 Printf.sprintf "translation % f, % f, % f" tx ty tz),
165 Gl.enable `depth_test;
166 Gl.enable `alpha_test;
167 GlMat.pop ();
168 GlMat.mode `projection;
169 GlMat.pop ();
172 let display () =
173 GlClear.color (0.5, 0.5, 0.5) ~alpha:1.0;
174 GlClear.clear [`color; `depth];
175 GlDraw.color (0.0, 0.0, 0.0);
176 GlFunc.alpha_func `greater view.alpha;
178 if view.sphere then (
179 let cx, cy, cz = view.center in
180 let cx = -.cx and cy = -.cy and cz = -.cz in
181 GlDraw.line_width 1.0;
182 GlMat.mode `modelview;
183 GlMat.push ();
184 GlMat.translate3 (cx, cy, cz);
185 GlDraw.polygon_mode `back `line;
186 GlDraw.polygon_mode `front `line;
187 Gl.disable `texture_2d;
188 GluQuadric.sphere ~radius:(0.7*.view.radial_scale) ~stacks:25 ~slices:25 ();
189 GlMat.pop ();
192 List.iter (fun draw -> draw#draw) view.objs;
193 if view.help then help ();
194 Glut.swapBuffers ();
196 if view.dodump then (
197 let pix = GlPix.read 0 0 view.w view.h `rgb `ubyte in
198 let raw = GlPix.to_raw pix in
199 let pitch = view.w * 3 in
200 let size = view.h * pitch in
201 let s = Raw.gets_string raw 0 size in
202 let dc = Lazy.force view.dumpchan in
203 let rec loop pos =
204 let pos = pos - pitch in
205 if pos < 0 then ()
206 else (
207 output dc s pos pitch;
208 loop pos
211 loop size;
215 let get_eye_and_up () =
216 if not view.roteye
217 then
218 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
219 else
220 let q =
221 let rx = deg2rad view.rotx
222 and ry = deg2rad view.roty
223 and rz = deg2rad view.rotz in
224 Qtr.from_euler rz ~-.ry rx
226 let v = Qtr.apply q (Vec.make 0.0 0.0 2.0) in
227 let u = Qtr.apply q (Vec.make 0.0 1.0 0.0) in
228 Vec.elts v, Vec.elts u
231 let setup w h =
232 view.w <- w;
233 view.h <- h;
234 GlDraw.viewport 0 0 w h;
236 let rs = view.zoom /. view.radial_scale in
238 GlMat.mode `projection;
239 GlMat.load_identity ();
240 GlMat.translate3 view.transl;
241 GluMat.perspective
242 ~fovy:45.0
243 ~aspect:(float w /. float h)
244 ~z:(0.1, 10.)
247 GlMat.mode `modelview;
248 GlMat.load_identity ();
250 let eye, up = get_eye_and_up () in
251 GluMat.look_at
252 ~eye
253 ~center:(0.0, 0.0, 0.0)
257 if not view.roteye then (
258 GlMat.rotate ~angle:view.rotx ~x:1.0 ();
259 GlMat.rotate ~angle:view.roty ~y:~-.1.0 ();
260 GlMat.rotate ~angle:view.rotz ~z:1.0 ();
263 GlMat.scale3 (-.rs, rs, rs);
264 GlMat.translate3 view.center;
267 let reshape ~w ~h =
268 setup w h;
271 let mchar c draw = draw#char c;;
272 let mdraw draw = draw#draw; draw;;
273 let allfunc f = view.objs <- List.map f view.objs;;
275 let idle () =
276 let deadline = view.last_time +. 0.04 in
277 let currtime = Unix.gettimeofday () in
278 if deadline > currtime
279 then
280 let _ = Unix.select [] [] [] (deadline -. currtime) in
281 view.last_time <- Unix.gettimeofday ()
282 else
283 view.last_time <- view.last_time +. 0.04
285 allfunc (mchar 'n');
286 Glut.postRedisplay ();
289 let keyboard ~key ~x ~y =
290 begin match Char.chr key with
291 | '\027' | 'q' -> exit 0
292 | '9' -> view.zoom <- view.zoom +. 0.05
293 | '0' -> view.zoom <- view.zoom -. 0.05
294 | 'z' -> view.roty <- view.roty +. view.aincr
295 | 'x' -> view.roty <- view.roty -. view.aincr
296 | 'd' -> view.dodump <- not view.dodump
297 | 'e' -> view.roteye <- not view.roteye
298 | 'o' -> view.sphere <- not view.sphere;
299 | 'h' -> view.help <- not view.help
300 | 'a' ->
301 if view.animated
302 then (
303 view.animated <- false;
304 Glut.idleFunc None
306 else (
307 view.animated <- true; view.
308 last_time <- Unix.gettimeofday ();
309 Glut.idleFunc (Some idle)
311 | ('f' | 'b') as c when not view.animated -> allfunc (mchar c);
312 | '<' -> view.alpha <- max (view.alpha -. 0.01) 0.0;
313 | '>' -> view.alpha <- min (view.alpha +. 0.01) 1.0;
314 | '[' -> slerp_step := max (!slerp_step -. 0.1) 0.0;
315 | ']' -> slerp_step := min (!slerp_step +. 0.1) 1.0;
316 | '3' -> view.ambient <- view.ambient -. 0.1;
317 | '4' -> view.ambient <- view.ambient +. 0.1;
318 | '5' -> view.diffuse <- view.diffuse -. 0.1;
319 | '6' -> view.diffuse <- view.diffuse +. 0.1;
320 | c -> allfunc (mchar c)
321 end;
322 setup view.w view.h;
323 Glut.postRedisplay ();
326 let special ~key ~x ~y =
327 begin match key with
328 | Glut.KEY_LEFT -> view.rotz <- view.rotz +. view.aincr
329 | Glut.KEY_RIGHT -> view.rotz <- view.rotz -. view.aincr
330 | Glut.KEY_UP -> view.rotx <- view.rotx -. view.aincr
331 | Glut.KEY_DOWN -> view.rotx <- view.rotx +. view.aincr
332 | _ -> ()
333 end;
334 setup view.w view.h;
335 Glut.postRedisplay ();
338 let motion ~x ~y =
339 let dx = (x - view.x) in
340 let dy = (y - view.y) in
341 view.x <- x;
342 view.y <- y;
343 match view.mtype with
344 | `move ->
345 let x, y, z = view.transl in
346 let dx = float dx /. 100.0
347 and dy = float dy /. 100.0 in
348 view.transl <- (x +. dx, y -. dy, z);
349 setup view.w view.h;
350 Glut.postRedisplay ();
351 | `rotate ->
352 view.rotx <- view.rotx +. float dy;
353 view.roty <- view.roty -. float dx;
354 setup view.w view.h;
355 Glut.postRedisplay ();
356 | `zoom ->
357 view.zoom <- view.zoom +. (float dy /. 50.);
358 setup view.w view.h;
359 Glut.postRedisplay ();
360 | `none ->
364 let mouse ~button ~state ~x ~y =
365 if button = Glut.LEFT_BUTTON
366 then (
367 if state = Glut.DOWN
368 then (
369 view.x <- x;
370 view.y <- y;
371 view.mtype <-
372 if Glut.getModifiers () = Glut.active_shift
373 then `move else `rotate;
375 else view.mtype <- `none;
377 else if button = Glut.RIGHT_BUTTON
378 then (
379 if state = Glut.DOWN
380 then (
381 view.x <- x;
382 view.y <- y;
383 view.mtype <- `zoom;
385 else view.mtype <- `none;
389 let main () =
390 let w = 704
391 and h = 576 in
392 let _ = Glut.init Sys.argv in
393 let () = Glut.initDisplayMode ~depth:true ~double_buffer:true () in
394 let () = Glut.initWindowSize w h in
395 let _ = Glut.createWindow "rend (press 'h' to get help)" in
396 Gl.enable `depth_test;
397 Gl.enable `alpha_test;
398 let () = Glut.displayFunc display in
399 let () = Glut.reshapeFunc reshape in
400 let () = Glut.keyboardFunc keyboard in
401 let () = Glut.specialFunc special in
402 let () = Glut.mouseFunc mouse in
403 let () = Glut.motionFunc motion in
404 allfunc (mchar '\000');
405 let () = Glut.mainLoop () in
409 let add_obj draw =
410 view.objs <- draw :: view.objs;
413 let init minmax =
414 let (cx, cy, cz), rs = center_and_radial_scale minmax in
415 view.center <- (-.cx, -.cy, -.cz);
416 view.radial_scale <- rs;
419 let _ =
420 let setsome r s = r := Some s in
421 let spec =
422 ["-slice", Arg.String Slice.openslice, "<path> of file to slice data to"
423 ;"-index", Arg.Set_string Xff.index_path, "<path> of index"
424 ;"-base", Arg.String (setsome Xff.base_path), "<directory> base"
425 ;"-sstep", Arg.Set_float slerp_step, "<float> slerp step"
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 ...]"