1 class type draw
= object
3 method char
: char
-> draw
4 method help
: (string * string * string) list
7 let nmo_name = ref None
9 let skb_name = ref None
10 let mipmaps = ref false
11 let slerp_step = ref 1.0
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
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
43 ; rotx
= 0.0; roty
= 0.0; rotz
= 0.0
44 ; center
= (0.0, 0.0, 0.0)
51 ; dumpchan
= lazy (open_out_bin
"dump.rgb")
60 ; transl
= (0.0, 0.0, 0.0)
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
74 let rs = maxx
-. minx
in
75 let rs = max
rs (maxy
-. miny
) in
76 let rs = max
rs (maxz
-. minz
) in
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;
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
;
98 GlMat.load_identity
();
99 GlMat.mode `modelview
;
101 GlMat.load_identity
();
103 ~x
:(0.0, float view.w
)
104 ~y
:(0.0, float view.h
)
108 Gl.disable `depth_test
;
109 Gl.disable `alpha_test
;
111 GlDraw.polygon_mode `both `fill
;
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
);
118 GlDraw.color
(1., 1., 1.);
119 let rec loop row
= function
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
;
130 let onoff b
= if b
then "on" else "off" in
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
150 List.fold_left
(fun accu draw
-> accu
@ draw#
help) help view.objs
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", "", ""
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
;
168 GlMat.mode `projection
;
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
;
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 ();
192 List.iter
(fun draw
-> draw#draw
) view.objs
;
193 if view.help then help ();
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
204 let pos = pos - pitch in
207 output
dc s pos pitch;
215 let get_eye_and_up () =
218 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
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
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
;
243 ~aspect
:(float w
/. float h
)
247 GlMat.mode `modelview
;
248 GlMat.load_identity
();
250 let eye, up
= get_eye_and_up () in
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
;
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
;;
276 let deadline = view.last_time
+. 0.04 in
277 let currtime = Unix.gettimeofday
() in
278 if deadline > currtime
280 let _ = Unix.select
[] [] [] (deadline -. currtime) in
281 view.last_time
<- Unix.gettimeofday
()
283 view.last_time
<- view.last_time
+. 0.04
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
303 view.animated
<- false;
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
)
323 Glut.postRedisplay
();
326 let special ~key ~
x ~
y =
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
335 Glut.postRedisplay
();
339 let dx = (x - view.x) in
340 let dy = (y - view.y) in
343 match view.mtype
with
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
);
350 Glut.postRedisplay
();
352 view.rotx
<- view.rotx
+. float dy;
353 view.roty
<- view.roty
-. float dx;
355 Glut.postRedisplay
();
357 view.zoom
<- view.zoom
+. (float dy /. 50.);
359 Glut.postRedisplay
();
364 let mouse ~button ~state ~
x ~
y =
365 if button
= Glut.LEFT_BUTTON
372 if Glut.getModifiers
() = Glut.active_shift
373 then `move
else `rotate
;
375 else view.mtype
<- `none
;
377 else if button
= Glut.RIGHT_BUTTON
385 else view.mtype
<- `none
;
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
410 view.objs
<- draw
:: view.objs
;
414 let (cx, cy
, cz
), rs = center_and_radial_scale minmax
in
415 view.center
<- (-.cx, -.cy
, -.cz
);
416 view.radial_scale
<- rs;
420 let setsome r
s = r
:= Some
s in
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)
434 then anb_names := s :: !anb_names
435 else nmo_name := Some
s;
437 "Usage: dormin [options] model.nmo [animation.anb ...]"