1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
| open GL
open Glu
open Glut
let anglex = ref 0.
let angley = ref 0.
(* display cubes z-sorting *)
let assoc_color =
let tbl = Hashtbl.create 30 in
let get = Hashtbl.find tbl in
fun i j k ->
let ndx = (i, j, k) in
try get ndx
with Not_found ->
let r,g,b =
match Random.int 4 with
| 0 -> (1.0, 0.0, 0.0)
| 1 -> (0.1, 0.8, 0.0)
| 2 -> (0.1, 0.4, 1.0)
| 3 -> (0.7, 0.0, 0.6)
| _ -> raise Exit
in
Hashtbl.add tbl ndx (r,g,b);
(r,g,b)
;;
let mat_get_z = function
[| [| _; _; _; _ |];
[| _; _; _; _ |];
[| _; _; _; _ |];
[| _; _; z; _ |]; |] -> z
| _ ->
invalid_arg "matrix"
;;
let mat_get_y = function
[| [| _; _; _; _ |];
[| _; _; _; _ |];
[| _; _; _; _ |];
[| _; y; _; _ |]; |] -> y
| _ ->
invalid_arg "matrix"
;;
let assoc_vis, step_vis =
let tbl = Hashtbl.create 30 in
let get = Hashtbl.find tbl in
(fun i j k ->
let ndx = (i, j, k) in
try get ndx
with Not_found ->
let vis = Random.bool() in
Hashtbl.add tbl ndx (vis);
(vis)),
(fun i j k ->
if (Random.int 3000) = 0 then
let ndx = (i, j, k)
and vis = Random.bool() in
Hashtbl.replace tbl ndx vis)
;;
let gradient_switch = ref true
let gradient_toggle () =
if (Random.int 30_000_000) < 100_000 then
gradient_switch := not(!gradient_switch);
;;
let display_cubes_z_sorting n () =
if !gradient_switch
then glClearColor 0.3 0.25 0.2 0.0
else glClearColor 0.4 0.2 0.0 0.0;
glClear [GL_COLOR_BUFFER_BIT];
glLoadIdentity();
glTranslate 0. 0. (-. 4.0);
glRotate (!angley) 1.0 0.0 0.0;
glRotate (!anglex) 0.0 1.0 0.0;
glScale 0.5 0.5 0.5;
let prop = ref [] in
let step = 0.8 in
let half = step *. (float(pred n)) /. 2.0 in
for i=0 to pred n do
for j=0 to pred n do
for k=0 to pred n do
let r,g,b = assoc_color i j k
and vis = assoc_vis i j k
and x = ((float i)*. step) -. half
and y = ((float j)*. step) -. half
and z = ((float k)*. step) -. half in
step_vis i j k;
prop := (x,y,z, r,g,b, vis) :: !prop;
done;
done;
done;
let item_get_mat (x,y,z, r,g,b, vis) =
glPushMatrix();
glTranslate x y z;
let m = glGetMatrix Get.GL_MODELVIEW_MATRIX in
glPopMatrix();
(m, r,g,b, vis)
in
let items_prop = List.map (item_get_mat) !prop in
(* sort the items along the Z axis *)
let z_sort_vert (m1,_,_,_,_) (m2,_,_,_,_) =
let z1 = mat_get_z m1
and z2 = mat_get_z m2 in
if z1 < z2 then -1 else 1
in
let sorted_items = List.sort z_sort_vert items_prop in
let gradient (m, r,g,b, vis) =
let y = mat_get_y m in
let r = sqrt(half ** 2. *. 3.) *. 0.5 in
let v = y +. r in
let v = v /. r /. 2. in
let v = v *. 1.4 in
let r = v
and b = 1.0 -. v
and g = 0.0 in
(m, r,g,b, vis)
in
let sorted_items =
if !gradient_switch
then List.map (gradient) sorted_items
else sorted_items
in
gradient_toggle();
let draw_item (m, r,g,b, vis) =
if vis then
begin
glLoadIdentity();
glMultMatrix m;
glScale 0.5 0.5 0.5;
glColor3 0.0 0.0 0.0;
glutSolidCube 1.0;
glColor3 r g b;
glutSolidCube 0.89;
glColor3 1.0 1.0 1.0;
glutWireCube 0.89;
end
in
List.iter (draw_item) sorted_items;
glutSwapBuffers();
;;
(* timers *)
let sleep_ticks = 16 ;;
let rec rot_timer ~value =
angley := !angley +. 0.2;
anglex := !anglex +. 0.02;
glutTimerFunc ~msecs:sleep_ticks ~timer:rot_timer ~value:0;
glutPostRedisplay();
;;
let time = ref 0
let rec time_tracker ~value =
incr time;
glutTimerFunc ~msecs:100 ~timer:time_tracker ~value:0;
;;
let display = display_cubes_z_sorting 6 ;;
(* reshape *)
let field = 10. ;;
let reshape ~width:w ~height:h =
glViewport 0 0 w h;
;;
let keyboard ~key ~x ~y =
match key with
| 'q' | '\027' -> exit 0;
| _ -> ()
;;
let gl_init() =
glShadeModel GL_FLAT;
glDisable GL_DEPTH_TEST;
glClearColor 0.4 0.0 0.0 0.0;
;;
(* main *)
let () =
Random.self_init();
ignore(glutInit Sys.argv);
glutInitDisplayMode [GLUT_RGB; GLUT_DOUBLE]; (* dont use GLUT_DEPTH *)
glutInitWindowSize 640 480;
ignore(glutCreateWindow "demo");
glutFullScreen();
glutSetCursor GLUT_CURSOR_NONE;
gl_init();
(* Parameters of perspective projection *)
glMatrixMode GL_PROJECTION;
glLoadIdentity();
gluPerspective ~fovy:60.0 ~aspect:1.0 ~zNear:0.5 ~zFar:100.0;
glMatrixMode GL_MODELVIEW;
(* callbacks *)
glutDisplayFunc ~display;
glutKeyboardFunc ~keyboard;
glutReshapeFunc ~reshape;
glutTimerFunc ~msecs:sleep_ticks ~timer:rot_timer ~value:0;
glutTimerFunc ~msecs:100 ~timer:time_tracker ~value:0;
glutMainLoop();
;; |