Make buffer ids GLuint
[dormin.git] / skb.ml
blob538451c7afa23ccb35a4041f84b8c2a5405c16b5
1 open Format;;
3 let xyzofvec v = (v.Vec.x, v.Vec.y, v.Vec.z)
5 let qof f = Qtr.make f.(5) f.(6) f.(7) f.(8);;
6 let vof f = Vec.make f.(1) f.(2) f.(3);;
8 type t = int * (string * int32 array * float array * int array * float);;
10 let rt sbuf strtab =
11 let d = Array.init 3 (fun n -> Xff.r32 sbuf (n*4)) in
12 let f9 = Array.init 9 (fun n -> Xff.rfloat sbuf (12 +n*4)) in
13 let h = Array.init 3 (fun n -> Xff.rint sbuf (48 + n*4)) in
14 let f = Xff.rfloat sbuf 60 in
15 (Xff.rcstrtabent strtab 0 (Int32.to_int d.(0)), d, f9, h, f)
18 let r1 xff sbufxff =
19 if Array.length xff.Xff.sections != 2
20 then
21 Xff.sbuferr sbufxff 0 "number of xff sections is not 2"
23 let sectpos = xff.Xff.sections.(1).Xff.off in
24 let sectbuf = Xff.sbufplus sbufxff sectpos in
25 let skbbuf = Xff.sbufplus sectbuf xff.Xff.entry in
27 let count1 = Xff.rint skbbuf 4 in
28 let _pos1 = Xff.rint skbbuf 8 in
29 let tabpos1 = Xff.rint skbbuf 12 in
30 let strtab1 = Xff.sbufplus sectbuf tabpos1 in
32 let bones = Array.init count1 (fun n ->
33 let pos = Xff.rint skbbuf (44 + n*8) in
34 let sbuf = Xff.sbufplus sectbuf pos in
35 pos + sectpos, rt sbuf strtab1)
37 bones
40 let vertices bones =
41 let parentinfo = Array.make (Array.length bones + 1) (Qtr.id, Vec.origin) in
42 let mapf i (_pos, (_name, _offsets, floats, neighbors, _float)) =
43 let curq = qof floats in
44 let curv = vof floats in
46 let parent = neighbors.(2) in
47 let parentq, parentv = parentinfo.(parent + 1) in
49 let v = Qtr.apply parentq curv in
50 let v = Vec.add v parentv in
52 let q = Qtr.compose curq parentq in
53 parentinfo.(i + 1) <- (q, v);
54 xyzofvec parentv, xyzofvec v;
56 Array.mapi mapf bones;
59 let vertices1 bones rotations poseno =
60 let parentinfo = Array.make (Array.length bones + 1) (Qtr.id, Vec.origin) in
61 let mapf i (pos, (name, offsets, floats, neighbors, float)) =
62 let curq = rotations.(i).(poseno) in
63 let curv = vof floats in
65 let parent = neighbors.(2) in
66 let parentq, parentv = parentinfo.(parent + 1) in
68 let v = Qtr.apply parentq curv in
69 let v = Vec.add v parentv in
71 let q = Qtr.compose curq parentq in
72 parentinfo.(i + 1) <- (q, v);
73 xyzofvec parentv, xyzofvec v;
75 Array.mapi mapf bones;
78 let draw data =
79 let rta = vertices data in
81 let sphere origin =
82 GlMat.mode `modelview;
83 GlMat.push ();
84 GlMat.translate3 (origin);
85 let radius = Rend.view.Rend.radial_scale*.0.009 in
86 GluQuadric.sphere ~radius ~stacks:5 ~slices:5 ();
87 GlMat.pop ()
90 fun () ->
91 GlDraw.polygon_mode `both `line;
92 Gl.disable `depth_test;
93 Array.iteri
94 (fun i (pos, (n, d, m, h, f)) ->
95 GlDraw.line_width 0.1;
96 GlDraw.color (0., 0., 1.);
97 let v0, v1 = rta.(i) in
98 sphere v1;
99 GlDraw.line_width 2.0;
100 GlDraw.begins `lines;
101 GlDraw.vertex3 v0;
102 GlDraw.vertex3 v1;
103 GlDraw.ends ();
104 ) data;
105 Gl.enable `depth_test;
108 let draw1 bones =
109 fun rotations poseno ->
110 let rta = vertices1 bones rotations poseno in
111 fun () ->
112 GlDraw.polygon_mode `both `line;
113 GlDraw.color (0., 0., 1.);
114 Gl.disable `depth_test;
115 for i = 0 to Array.length bones - 1 do
116 (* GlDraw.line_width 0.1; *)
117 let v0, v1 = rta.(i) in
118 (* sphere v1; *)
119 GlDraw.line_width 2.0;
120 GlDraw.begins `lines;
121 GlDraw.vertex3 v0;
122 GlDraw.vertex3 v1;
123 GlDraw.ends ();
124 done;
125 Gl.enable `depth_test;
128 let func bones anim =
129 let posecount, rotations = anim in
130 let draw0 = draw bones in
131 let draw1 = draw1 bones in
132 let rec subfunc dodraw poseno draw = function
133 | Rend.Char ('f'|'b' as c) ->
134 let poseno =
135 if c = 'b' then
136 let poseno = if poseno = 0 then posecount else poseno in
137 (poseno - 1) mod posecount
138 else
139 (poseno + 1) mod posecount
141 let draw = draw1 rotations poseno in
142 Anb.skin rotations poseno;
143 Skin.anim ();
144 Rend.Func (subfunc dodraw poseno draw)
145 | Rend.Char 'r' ->
146 Rend.Func (subfunc dodraw 0 draw0)
147 | Rend.Draw ->
148 if dodraw then draw ();
149 Rend.Func (subfunc dodraw poseno draw)
150 | Rend.Char 's' -> Rend.Func (subfunc (not dodraw) poseno draw)
151 | Rend.Char _ -> Rend.Func (subfunc dodraw poseno draw)
153 subfunc false 0 (draw bones)
156 let dummy draw =
157 let rec subfunc = function
158 | Rend.Draw -> draw (); Rend.Func subfunc
159 | _ -> Rend.Func subfunc
161 subfunc
164 let skin bones =
165 let skel = Array.map
166 (fun (_, (_, _, floats, neighbors, _)) -> (neighbors.(2), floats))
167 bones
169 Skin.set_skel skel;
172 let main name =
173 let func =
175 let name = Filename.chop_extension name ^ ".skb" in
176 let xff, sbuf = Xff.test2 name in
177 let bones = r1 xff sbuf in
178 begin try
179 let anim_name =
180 match !Rend.anb_name with
181 | None -> raise (Failure "no animation set")
182 | Some name ->name
184 let xff, sbuf = Xff.test2 (Filename.basename anim_name) in
185 let anim = Anb.r xff sbuf in
186 skin bones;
187 func bones anim
188 with exn ->
189 prerr_endline (Printexc.to_string exn);
190 dummy (draw bones)
191 end;
192 with exn ->
193 prerr_endline (Printexc.to_string exn);
194 dummy (fun () -> ())
196 Rend.add_func func