Image Noise/OCaml/OpenGL

From Rosetta Code
Revision as of 23:58, 1 November 2011 by rosettacode>Blue Prawn (added ocaml)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Library: glMLite

Compile this program to native-code with the following command:

ocamlopt -o noise_gl.opt bigarray.cmxa unix.cmxa -I +glMLite GL.cmxa Glut.cmxa noise_gl.ml

<lang ocaml>open Glut open GL

let width = 320 let height = 240 let len = width * height

let buff = Bigarray.Array1.create Bigarray.char Bigarray.c_layout len let t_last = ref (Unix.gettimeofday()) let frames = ref 0

let render () =

 glClear [GL_COLOR_BUFFER_BIT];
 for i = 0 to pred len do
   buff.{i} <- char_of_int (Random.int 256)
 done;
 glBitmap width height 0.0 0.0 0.0 0.0 buff;
 glFlush();
 incr frames;
 if !frames = 600 then begin
   let t = Unix.gettimeofday() in
   Printf.printf "- fps: %f\n%!" ((float !frames) /. (t -. !t_last));
   t_last := t;
   frames := 0;
 end;
 glutSwapBuffers()

let () =

 ignore (glutInit Sys.argv);
 glutInitDisplayMode [GLUT_RGB; GLUT_DOUBLE];
 glutInitWindowSize width height;
 ignore (glutCreateWindow "noise");
 glutDisplayFunc render;
 glutIdleFunc render;
 glutMainLoop()</lang>