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
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
30 ; mutable mtype
: [`none
|`zoom
|`rotate
|`move
]
31 ; mutable transl
: (float * float * float)
32 ; mutable alpha
: float
37 ; rotx
= 0.0; roty
= 0.0; rotz
= 0.0
38 ; center
= (0.0, 0.0, 0.0)
45 ; dumpchan
= lazy (open_out_bin
"dump.rgb")
54 ; transl
= (0.0, 0.0, 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
66 let rs = maxx
-. minx
in
67 let rs = max
rs (maxy
-. miny
) in
68 let rs = max
rs (maxz
-. minz
) in
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;
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
;
90 GlMat.load_identity
();
91 GlMat.mode `modelview
;
93 GlMat.load_identity
();
95 ~x
:(0.0, float view.w
)
96 ~y
:(0.0, float view.h
)
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
);
106 Gl.disable `depth_test
;
107 Gl.disable `alpha_test
;
108 GlDraw.color
(1., 1., 1.);
109 let rec loop row
= function
112 let y = view.h
- row
* 18 in
113 draw_string 0.0 (float y) s
;
114 draw_string 100.0 (float y) s2
;
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"
135 ;" <,>", "increase/decrease alpha"
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
;
145 GlMat.mode `projection
;
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
;
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 ();
168 List.iter
(fun f
-> ignore
(f Draw
)) view.func
;
169 if view.help then help ();
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
180 let pos = pos - pitch in
183 output
dc s pos pitch;
191 let get_eye_and_up () =
194 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
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
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
;
219 ~aspect
:(float w
/. float h
)
223 GlMat.mode `modelview
;
224 GlMat.load_identity
();
226 let eye, up
= get_eye_and_up () in
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
;
248 view.func
<- List.map
(fun f
-> let Func fr
= f cmd
in fr
) view.func
;
252 let deadline = view.last_time
+. 0.04 in
253 let currtime = Unix.gettimeofday
() in
254 if deadline > currtime
256 let _ = Unix.select
[] [] [] (deadline -. currtime) in
257 view.last_time
<- Unix.gettimeofday
()
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
279 view.animated
<- false;
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
)
293 Glut.postRedisplay
();
296 let special ~key ~x ~
y =
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
305 Glut.postRedisplay
();
309 let dx = (x
- view.x
) in
310 let dy = (y - view.y) in
313 match view.mtype
with
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
);
320 Glut.postRedisplay
();
322 view.rotx
<- view.rotx
+. float dy;
323 view.roty
<- view.roty
-. float dx;
325 Glut.postRedisplay
();
327 view.zoom
<- view.zoom
+. (float dy /. 50.);
329 Glut.postRedisplay
();
334 let mouse ~button ~state ~
x ~
y =
335 if button
= Glut.LEFT_BUTTON
342 if Glut.getModifiers
() = Glut.active_shift
343 then `move
else `rotate
;
345 else view.mtype
<- `none
;
347 else if button
= Glut.RIGHT_BUTTON
355 else view.mtype
<- `none
;
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
379 view.func
<- func
:: view.func
;
383 let (cx, cy
, cz
), rs = center_and_radial_scale minmax
in
384 view.center
<- (-.cx, -.cy
, -.cz
);
385 view.radial_scale
<- rs;
389 let setsome r
s = r
:= Some
s in
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)
399 if !nmo_name != None
then (
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]"