1 type cmd
= | Char
of char
| Draw
;;
2 type func
= cmd
-> func_ret
3 and helpfunc
= (unit -> (string * string * string) list
)
4 and func_ret
= Func
of (func
* helpfunc
)
6 let nmo_name = ref None
7 let anb_name = ref None
8 let skb_name = ref None
9 let mipmaps = ref false
10 let slerp_step = ref 1.0
15 ; mutable rotx
: float
16 ; mutable roty
: float
17 ; mutable rotz
: float
18 ; mutable zoom
: float
19 ; mutable center
: (float * float * float)
20 ; mutable radial_scale
: float
21 ; mutable func
: func list
22 ; mutable helpfunc
: helpfunc 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
34 ; mutable mtype
: [`none
|`zoom
|`rotate
|`move
]
35 ; mutable transl
: (float * float * float)
36 ; mutable alpha
: float
41 ; rotx
= 0.0; roty
= 0.0; rotz
= 0.0
42 ; center
= (0.0, 0.0, 0.0)
50 ; dumpchan
= lazy (open_out_bin
"dump.rgb")
59 ; transl
= (0.0, 0.0, 0.0)
64 let deg2rad deg
= deg
/. 180.0 *. acos ~
-.1.;;
66 let center_and_radial_scale (minx
, maxx
, miny
, maxy
, minz
, maxz
) =
67 let xc = (maxx
+. minx
) /. 2.0 in
68 let yc = (maxy
+. miny
) /. 2.0 in
69 let zc = (maxz
+. minz
) /. 2.0 in
71 let rs = maxx
-. minx
in
72 let rs = max
rs (maxy
-. miny
) in
73 let rs = max
rs (maxz
-. minz
) in
78 Format.eprintf
"x (% f, % f)@." minx maxx
;
79 Format.eprintf
"y (% f, % f)@." miny maxy
;
80 Format.eprintf
"z (% f, % f)@." minz maxz
;
81 Format.eprintf
"c (% f, % f, % f)@." xc yc zc;
82 Format.eprintf
"rs %f@." rs;
88 let font = Glut.BITMAP_HELVETICA_18
in
89 let draw_string x y s
=
90 GlPix.raster_pos ~x ~y
();
91 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
93 GlMat.mode `projection
;
95 GlMat.load_identity
();
96 GlMat.mode `modelview
;
98 GlMat.load_identity
();
100 ~x
:(0.0, float view.w
)
101 ~y
:(0.0, float view.h
)
105 Gl.disable `depth_test
;
106 Gl.disable `alpha_test
;
108 GlDraw.polygon_mode `both `fill
;
110 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
111 GlDraw.color
(0., 0., 0.) ~alpha
:0.3;
112 GlDraw.rect
(0., 0.) (float view.w
, float view.h
);
115 GlDraw.color
(1., 1., 1.);
116 let rec loop row
= function
118 | (s
, s2
, s3
) :: rest
->
119 let y = view.h
- row
* 18 - 2 in
120 let x = if row
= 1 then 0.0 else 5.0 in
121 draw_string (x+.5.0) (float y) s
;
122 draw_string (x+.105.0) (float y) s2
;
123 draw_string (x+.345.0) (float y) s3
;
127 let onoff b
= if b
then "on" else "off" in
129 Printf.sprintf
"% f, % f, % f" view.rotx
view.roty
view.rotz
132 ;"h", "toggle help", ""
133 ;"e", "toggle eye/model rotation", if view.roteye
then "eye" else "model"
134 ;"a", "toggle animation", onoff view.animated
135 ;"o", "toggle bounding sphere", onoff view.sphere
136 ;"d", "dump images to dump.rgb", onoff view.dodump
137 ;"q, ESC", "quit", ""
138 ;"z,x,arrows", "rotate", angles
139 ;"0,9", "zoom", Printf.sprintf
"%f" view.zoom
140 ;"1,2", "go to first/last pose", ""
141 ;"< , >", "decrease/increase alpha", Printf.sprintf
"%1.2f" view.alpha
142 ;"[ , ]", "decrease/increase slerp step", Printf.sprintf
"%2.1f" !slerp_step
147 List.fold_left
(fun accu hf
-> accu
@ hf
()) help view.helpfunc
152 ;"Move mouse while holding left button pressed to rotate model", "", ""
153 ;"Move mouse while holding right button pressed to zoom", "", ""
154 ;"Move mouse while holding left button and shift pressed to move model", "", ""
156 (let tx, ty
, tz
= view.transl
in
157 Printf.sprintf
"translation % f, % f, % f" tx ty tz
),
162 Gl.enable `depth_test
;
163 Gl.enable `alpha_test
;
165 GlMat.mode `projection
;
170 GlClear.color
(0.5, 0.5, 0.5) ~alpha
:1.0;
171 GlClear.clear
[`color
; `depth
];
172 GlDraw.color
(0.0, 0.0, 0.0);
173 GlFunc.alpha_func `greater
view.alpha
;
175 if view.sphere
then (
176 let cx, cy
, cz
= view.center
in
177 let cx = -.cx and cy
= -.cy
and cz
= -.cz
in
178 GlDraw.line_width
1.0;
179 GlMat.mode `modelview
;
181 GlMat.translate3
(cx, cy
, cz
);
182 GlDraw.polygon_mode `back `line
;
183 GlDraw.polygon_mode `front `line
;
184 Gl.disable `texture_2d
;
185 GluQuadric.sphere ~radius
:(0.7*.view.radial_scale
) ~stacks
:25 ~slices
:25 ();
189 List.iter
(fun f
-> ignore
(f Draw
)) view.func
;
190 if view.help then help ();
193 if view.dodump
then (
194 let pix = GlPix.read
0 0 view.w
view.h `rgb `ubyte
in
195 let raw = GlPix.to_raw
pix in
196 let pitch = view.w
* 3 in
197 let size = view.h
* pitch in
198 let s = Raw.gets_string
raw 0 size in
199 let dc = Lazy.force
view.dumpchan
in
201 let pos = pos - pitch in
204 output
dc s pos pitch;
212 let get_eye_and_up () =
215 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
218 let rx = deg2rad view.rotx
219 and ry
= deg2rad view.roty
220 and rz
= deg2rad view.rotz
in
221 Qtr.from_euler rz ~
-.ry
rx
223 let v = Qtr.apply
q (Vec.make
0.0 0.0 2.0) in
224 let u = Qtr.apply
q (Vec.make
0.0 1.0 0.0) in
225 Vec.elts
v, Vec.elts
u
231 GlDraw.viewport
0 0 w h
;
233 let rs = view.zoom
/. view.radial_scale
in
235 GlMat.mode `projection
;
236 GlMat.load_identity
();
237 GlMat.translate3
view.transl
;
240 ~aspect
:(float w
/. float h
)
244 GlMat.mode `modelview
;
245 GlMat.load_identity
();
247 let eye, up
= get_eye_and_up () in
250 ~center
:(0.0, 0.0, 0.0)
254 if not
view.roteye
then (
255 GlMat.rotate ~angle
:view.rotx ~
x:1.0 ();
256 GlMat.rotate ~angle
:view.roty ~
y:~
-.1.0 ();
257 GlMat.rotate ~angle
:view.rotz ~z
:1.0 ();
260 GlMat.scale3
(-.rs, rs, rs);
261 GlMat.translate3
view.center
;
271 List.map
(fun f -> let Func (fr
, hf
) = f cmd
in fr
, hf
) view.func
279 let deadline = view.last_time
+. 0.04 in
280 let currtime = Unix.gettimeofday
() in
281 if deadline > currtime
283 let _ = Unix.select
[] [] [] (deadline -. currtime) in
284 view.last_time
<- Unix.gettimeofday
()
286 view.last_time
<- view.last_time
+. 0.04
289 Glut.postRedisplay
();
292 let keyboard ~key ~
x ~
y =
293 begin match Char.chr key
with
294 | '
\027'
| '
q'
-> exit
0
295 | '
9'
-> view.zoom
<- view.zoom
+. 0.05
296 | '
0'
-> view.zoom
<- view.zoom
-. 0.05
297 | 'z'
-> view.roty
<- view.roty
+. view.aincr
298 | '
x'
-> view.roty
<- view.roty
-. view.aincr
299 | 'd'
-> view.dodump
<- not
view.dodump
300 | 'e'
-> view.roteye
<- not
view.roteye
301 | 'o'
-> view.sphere
<- not
view.sphere
;
302 | 'h'
-> view.help <- not
view.help
306 view.animated
<- false;
310 view.animated
<- true; view.
311 last_time
<- Unix.gettimeofday
();
312 Glut.idleFunc
(Some
idle)
314 | '
f'
| 'b'
when not
view.animated
-> allfunc (Char
(Char.chr key
))
315 | '
<'
-> view.alpha
<- max
(view.alpha
-. 0.01) 0.0;
316 | '
>'
-> view.alpha
<- min
(view.alpha
+. 0.01) 1.0;
317 | '
['
-> slerp_step := max
(!slerp_step -. 0.1) 0.0;
318 | '
]'
-> slerp_step := min
(!slerp_step +. 0.1) 1.0;
319 | c
-> allfunc (Char c
)
322 Glut.postRedisplay
();
325 let special ~key ~
x ~
y =
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
334 Glut.postRedisplay
();
338 let dx = (x - view.x) in
339 let dy = (y - view.y) in
342 match view.mtype
with
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
);
349 Glut.postRedisplay
();
351 view.rotx
<- view.rotx
+. float dy;
352 view.roty
<- view.roty
-. float dx;
354 Glut.postRedisplay
();
356 view.zoom
<- view.zoom
+. (float dy /. 50.);
358 Glut.postRedisplay
();
363 let mouse ~button ~state ~
x ~
y =
364 if button
= Glut.LEFT_BUTTON
371 if Glut.getModifiers
() = Glut.active_shift
372 then `move
else `rotate
;
374 else view.mtype
<- `none
;
376 else if button
= Glut.RIGHT_BUTTON
384 else view.mtype
<- `none
;
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 allfunc (Char '
\000'
);
404 let () = Glut.mainLoop
() in
409 view.func
<- func
:: view.func
;
413 let (cx, cy
, cz
), rs = center_and_radial_scale minmax
in
414 view.center
<- (-.cx, -.cy
, -.cz
);
415 view.radial_scale
<- rs;
419 let setsome r
s = r
:= Some
s in
421 ["-slice", Arg.String
Slice.openslice
, "<path> of file to slice data to"
422 ;"-index", Arg.Set_string
Xff.index_path
, "<path> of index"
423 ;"-base", Arg.String
(setsome Xff.base_path
), "<directory> base"
424 ;"-sstep", Arg.Set_float
slerp_step, "<float> slerp step"
425 ;("-skb", Arg.String
(setsome skb_name),
426 "<name> use specified skb instead of guessing")
427 ;"-mipmaps", Arg.Set
mipmaps, " use mipmaps"
430 Arg.parse
(Arg.align
spec)
432 if !nmo_name != None
then (
434 then raise
(Arg.Bad
"invalid usage")
435 else anb_name := Some
s;
436 ) else nmo_name := Some
s;
438 "Usage: dormin [options] model.nmo [animation.anb]"