2
ciglut : a demo of using caml-images, glut, and opengl to draw a textured
5
Copyright (c) 2002 Issac J. Trotts. LGPL
16
let endl = print_newline;;
21
while !i >= 0 && (!y land (1 lsl !i)) == 0 do
28
let p2f = pow2floor x in
29
if p2f = x then x else (pow2floor x) lsl 1;;
31
let i2f i = float_of_int i;;
32
let f2i f = int_of_float f;;
34
let raw_of_camlimg cimg =
35
let w = cimg#width and h = cimg#height in
36
let image = GlPix.create `ubyte ~format:`rgb ~width:w ~height:h in
39
let pixel = cimg#get i j in (* pixel is a Color.rgb *)
40
Raw.sets (GlPix.to_raw image) ~pos:(3*(i*h+j))
41
[| pixel.r; pixel.g; pixel.b |];
47
(* scale the image up so it's a power of two along each axis.
48
(IMPROVEME: this takes too long) *)
50
let newimg = img#resize None (pow2ceil img#width) (pow2ceil img#height) in
54
let initialize ci_img =
55
printf "initializing..."; endl();
56
GlClear.color (0.0, 0.0, 0.0);
57
(* save the original width and height *)
58
let w = ci_img#width and h = ci_img#height in
61
let ci_img = if pow2floor w <> w || pow2floor h <> h
62
then rescale ci_img else ci_img in
63
let gl_image = raw_of_camlimg ci_img in
64
GlPix.store (`unpack_alignment 1);
65
GlTex.image2d gl_image;
66
List.iter (GlTex.parameter ~target:`texture_2d)
70
`min_filter `linear ];
71
GlTex.env (`mode `decal);
72
Gl.enable `texture_2d;
73
GlDraw.shade_model `flat;
74
printf "done"; endl();
77
(* -- ui callbacks -- *)
80
let disp_called = ref false
83
if not(!disp_called) then begin
84
Glut.reshapeWindow !width !height;
85
GluMat.ortho2d ~x:(0.0, i2f !width) ~y:(0.0, i2f !height);
89
GlClear.clear [`color];
91
let w = i2f !width and h = i2f !height in
92
GlTex.coord2(1.0, 0.0); GlDraw.vertex3(0.0, 0.0, 0.0);
93
GlTex.coord2(1.0, 1.0); GlDraw.vertex3(w, 0.0, 0.0);
94
GlTex.coord2(0.0, 1.0); GlDraw.vertex3(w, h, 0.0);
95
GlTex.coord2(0.0, 0.0); GlDraw.vertex3(0.0, h, 0.0);
99
GlDraw.color(1.0, 0.0, 0.0);
100
GlDraw.vertex2(0.0, 0.0);
101
GlDraw.vertex2(1.0, 0.0);
103
GlDraw.color(0.0, 1.0, 0.0);
104
GlDraw.vertex2(0.0, 0.0);
105
GlDraw.vertex2(0.0, 1.0);
112
let on_keyboard ~key ~x ~y =
118
let view_with_glut img =
119
(* open a couple of Glut windows and display the file directly
120
and via texture on a square *)
121
ignore(Glut.init Sys.argv);
122
Glut.initDisplayMode ~double_buffer:false ~depth:false ();
123
Glut.initWindowSize 256 256;
124
ignore(Glut.createWindow "ocimgview");
125
GlDraw.shade_model `flat;
126
GlClear.color(0.0,0.0,0.0);
127
(* GluMat.ortho2d ~x:(0.0,1.0) ~y:(0.0,1.0); *)
129
Glut.displayFunc (fun () -> display());
130
Glut.keyboardFunc (fun ~key ~x ~y -> on_keyboard ~key ~x ~y);
131
Glut.postRedisplay();
136
Bitmap.maximum_live := 15000000; (* 60MB *)
137
Bitmap.maximum_block_size := !Bitmap.maximum_live / 16;
139
r.Gc.max_overhead <- 30;
144
let filename = ref None in
146
(* "-scale", Arg.Float (fun sc -> scale := sc), "scale"; *)
148
Arg.parse argfmt (fun s -> filename := Some s)
150
let filename = match !filename with
151
| None -> Arg.usage argfmt "ocimgview file"; exit(-1);
154
printf "Reading in %s" filename; endl();
155
let img = OImage.load filename [] in
156
let img = OImage.rgb24 img in