1 class type draw
= object
3 method char
: char
-> draw
4 method help
: (string * string * string) list
8 let nmo_name = ref None
10 let skb_name = ref None
11 let mipmaps = ref false
12 let slerp_step = ref 1.0
17 ; mutable rotx
: float
18 ; mutable roty
: float
19 ; mutable rotz
: float
20 ; mutable zoom
: float
21 ; mutable center
: (float * float * float)
22 ; mutable radial_scale
: float
23 ; mutable objs
: draw list
24 ; mutable persp
: bool
25 ; mutable last_time
: float
26 ; mutable animated
: bool
27 ; mutable dumpchan
: out_channel
Lazy.t
28 ; mutable dodump
: bool
29 ; mutable aincr
: float
30 ; mutable roteye
: bool
31 ; mutable sphere
: bool
35 ; mutable mtype
: [`none
|`zoom
|`rotate
|`move
]
36 ; mutable transl
: (float * float * float)
37 ; mutable alpha
: float
38 ; mutable ambient
: float
39 ; mutable diffuse
: float
44 ; rotx
= 4.0; roty
= 16.0; rotz
= 0.0
45 ; center
= (0.0, 0.0, 0.0)
52 ; dumpchan
= lazy (open_out_bin
"dump.rgb")
61 ; transl
= (0.0, 0.0, 0.0)
68 let mapchar c
= view.objs
<- List.map
(fun draw
-> draw#char c
) view.objs
;;
69 let appdraw () = List.iter
(fun draw
-> draw#draw
) view.objs
;;
70 let deg2rad deg
= deg
/. 180.0 *. acos ~
-.1.;;
72 let center_and_radial_scale (minx
, maxx
, miny
, maxy
, minz
, maxz
) =
73 let xc = (maxx
+. minx
) /. 2.0 in
74 let yc = (maxy
+. miny
) /. 2.0 in
75 let zc = (maxz
+. minz
) /. 2.0 in
77 let rs = maxx
-. minx
in
78 let rs = max
rs (maxy
-. miny
) in
79 let rs = max
rs (maxz
-. minz
) in
84 Format.eprintf
"x (% f, % f)@." minx maxx
;
85 Format.eprintf
"y (% f, % f)@." miny maxy
;
86 Format.eprintf
"z (% f, % f)@." minz maxz
;
87 Format.eprintf
"c (% f, % f, % f)@." xc yc zc;
88 Format.eprintf
"rs %f@." rs;
94 let font = Glut.BITMAP_HELVETICA_18
in
95 let draw_string x y s
=
96 GlPix.raster_pos ~x ~y
();
97 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
99 GlMat.mode `projection
;
101 GlMat.load_identity
();
102 GlMat.mode `modelview
;
104 GlMat.load_identity
();
106 ~x
:(0.0, float view.w
)
107 ~y
:(0.0, float view.h
)
111 Gl.disable `depth_test
;
112 Gl.disable `alpha_test
;
114 GlDraw.polygon_mode `both `fill
;
116 GlFunc.blend_func `src_alpha `one_minus_src_alpha
;
117 GlDraw.color
(0., 0., 0.) ~alpha
:0.3;
118 GlDraw.rect
(0., 0.) (float view.w
, float view.h
);
121 GlDraw.color
(1., 1., 1.);
122 let rec loop row
= function
124 | (s
, s2
, s3
) :: rest
->
125 let y = view.h
- row
* 18 - 2 in
126 let x = if row
= 1 then 0.0 else 5.0 in
127 draw_string (x+.5.0) (float y) s
;
128 draw_string (x+.105.0) (float y) s2
;
129 draw_string (x+.345.0) (float y) s3
;
133 let onoff b
= if b
then "on" else "off" in
135 Printf.sprintf
"% f, % f, % f" view.rotx
view.roty
view.rotz
137 [("Keys (h toggles this screen):", "", "")
138 ;"e", "toggle eye/model rotation", if view.roteye
then "eye" else "model"
139 ;"a", "toggle animation", onoff view.animated
140 ;"o", "toggle bounding sphere", onoff view.sphere
141 ;"d", "dump images to dump.rgb", onoff view.dodump
142 ;"q, ESC", "quit", ""
143 ;"z,x,arrows", "rotate", angles
144 ;"0,9", "zoom", Printf.sprintf
"%f" view.zoom
145 ;"< , >", "decrease/increase alpha", Printf.sprintf
"%1.2f" view.alpha
146 ;"[ , ]", "decrease/increase slerp step", Printf.sprintf
"%2.1f" !slerp_step
147 ;"3,4", "decrease/increase ambient", Printf.sprintf
"%2.1f" view.ambient
148 ;"5,6", "decrease/increase diffuse", Printf.sprintf
"%2.1f" view.diffuse
153 List.fold_left
(fun accu draw
-> accu
@ draw#
help) help view.objs
158 ;"Move mouse while holding left button pressed to rotate model", "", ""
159 ;"Move mouse while holding right button pressed to zoom", "", ""
160 ;"Move mouse while holding left button and shift pressed to move model", "", ""
162 (let tx, ty
, tz
= view.transl
in
163 Printf.sprintf
"translation % f, % f, % f" tx ty tz
),
168 Gl.enable `depth_test
;
169 Gl.enable `alpha_test
;
171 GlMat.mode `projection
;
176 GlClear.color
(0.5, 0.5, 0.5) ~alpha
:1.0;
177 GlClear.clear
[`color
; `depth
];
178 GlDraw.color
(0.0, 0.0, 0.0);
179 GlFunc.alpha_func `greater
view.alpha
;
181 if view.sphere
then (
182 let cx, cy
, cz
= view.center
in
183 let cx = -.cx and cy
= -.cy
and cz
= -.cz
in
184 GlDraw.line_width
1.0;
185 GlMat.mode `modelview
;
187 GlMat.translate3
(cx, cy
, cz
);
188 GlDraw.polygon_mode `back `line
;
189 GlDraw.polygon_mode `front `line
;
190 Gl.disable `texture_2d
;
191 GluQuadric.sphere ~radius
:(0.7*.view.radial_scale
) ~stacks
:25 ~slices
:25 ();
196 if view.help then help ();
199 if view.dodump
then (
200 let pix = GlPix.read
0 0 view.w
view.h `rgb `ubyte
in
201 let raw = GlPix.to_raw
pix in
202 let pitch = view.w
* 3 in
203 let size = view.h
* pitch in
204 let s = Raw.gets_string
raw 0 size in
205 let dc = Lazy.force
view.dumpchan
in
207 let pos = pos - pitch in
210 output
dc s pos pitch;
218 let get_eye_and_up () =
221 (0.0, 0.0, 2.0), (0.0, 1.0, 0.0)
224 let rx = deg2rad view.rotx
225 and ry
= deg2rad view.roty
226 and rz
= deg2rad view.rotz
in
227 Qtr.from_euler rz ~
-.ry
rx
229 let v = Qtr.apply
q (Vec.make
0.0 0.0 2.0) in
230 let u = Qtr.apply
q (Vec.make
0.0 1.0 0.0) in
231 Vec.elts
v, Vec.elts
u
237 GlDraw.viewport
0 0 w h
;
239 let rs = view.zoom
/. view.radial_scale
in
241 GlMat.mode `projection
;
242 GlMat.load_identity
();
243 GlMat.translate3
view.transl
;
246 ~aspect
:(float w
/. float h
)
250 GlMat.mode `modelview
;
251 GlMat.load_identity
();
253 let eye, up
= get_eye_and_up () in
256 ~center
:(0.0, 0.0, 0.0)
260 if not
view.roteye
then (
261 GlMat.rotate ~angle
:view.rotx ~
x:1.0 ();
262 GlMat.rotate ~angle
:view.roty ~
y:~
-.1.0 ();
263 GlMat.rotate ~angle
:view.rotz ~z
:1.0 ();
266 GlMat.scale3
(-.rs, rs, rs);
267 GlMat.translate3
view.center
;
275 let deadline = view.last_time
+. 0.04 in
276 let currtime = Unix.gettimeofday
() in
277 if deadline > currtime
279 let _ = Unix.select
[] [] [] (deadline -. currtime) in
280 view.last_time
<- Unix.gettimeofday
()
282 view.last_time
<- view.last_time
+. 0.04
285 Glut.postRedisplay
();
288 let keyboard ~key ~
x ~
y =
289 begin match Char.chr key
with
290 | '
\027'
| '
q'
-> exit
0
291 | '
9'
-> view.zoom
<- view.zoom
+. 0.05
292 | '
0'
-> view.zoom
<- view.zoom
-. 0.05
293 | 'z'
-> view.roty
<- view.roty
+. view.aincr
294 | '
x'
-> view.roty
<- view.roty
-. view.aincr
295 | 'd'
-> view.dodump
<- not
view.dodump
296 | 'e'
-> view.roteye
<- not
view.roteye
297 | 'o'
-> view.sphere
<- not
view.sphere
;
298 | 'h'
-> view.help <- not
view.help
302 view.animated
<- false;
306 view.animated
<- true; view.
307 last_time
<- Unix.gettimeofday
();
308 Glut.idleFunc
(Some
idle)
310 | ('f'
| 'b'
) as c
when not
view.animated
-> mapchar c
;
311 | '
<'
-> view.alpha
<- max
(view.alpha
-. 0.01) 0.0;
312 | '
>'
-> view.alpha
<- min
(view.alpha
+. 0.01) 1.0;
313 | '
['
-> slerp_step := max
(!slerp_step -. 0.1) 0.0;
314 | '
]'
-> slerp_step := min
(!slerp_step +. 0.1) 1.0;
315 | '
3'
-> view.ambient
<- view.ambient
-. 0.1;
316 | '
4'
-> view.ambient
<- view.ambient
+. 0.1;
317 | '
5'
-> view.diffuse
<- view.diffuse
-. 0.1;
318 | '
6'
-> view.diffuse
<- view.diffuse
+. 0.1;
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 mapchar '
\000'
; (* bootstrap *)
404 let () = Glut.mainLoop
() in
409 view.objs
<- draw
:: view.objs
;
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/dir to slice data to"
422 ;"-index", Arg.Set_string
Xff.index_path
, "<path> to index"
423 ;"-base", Arg.String
(setsome Xff.base_path
), "<directory> base"
424 ;"-sstep", Arg.Set_float
slerp_step, "<float> slerp step"
425 ;"-novbo", Arg.Clear
try_vbo, " do not use vertex buffer objects"
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 ...]"