Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active August 29, 2015 14:06
Show Gist options
  • Save kjunichi/b940dd97c93fca963ec8 to your computer and use it in GitHub Desktop.
Save kjunichi/b940dd97c93fca963ec8 to your computer and use it in GitHub Desktop.
OpenGLで画像データを表示すには

いろんな言語でOpenGLが扱える

だから、OpenGLな実装を覚えておけば、いつでもどんな言語でも画像を表示できるハズ。

WebGL編

OCaml編

let reshape ~w ~h =
  let w = max 1 w and h = max 1 h in
  dim := (w, h);
  GlDraw.viewport 0 0 w h;
  GlMat.mode `projection;
  GlMat.load_identity ();
  GlMat.ortho ~x:(-1., 1.) ~y:(-1., 1.) ~z:(0., 1.);
  GlMat.mode `modelview;;

let display () =
  let w, h = !dim in
  GlDraw.begins `points;
  for a = 0 to w - 1 do
    for b = 0 to h - 1 do
      let idx_x = int_of_float((float a /. float w) *. float (fst !dim)) in
      let idx_y = int_of_float((float b /. float h) *. float (snd !dim)) in
      GlDraw.color (imgBuf.(0).(idx_x).(idx_y), imgBuf.(1).(idx_x).(idx_y), imgBuf.(2).(idx_x).(idx_y));
      let x = 2. *. float a /. float w -. 1. in
      let y = 2. *. float b /. float h -. 1. in
      GlDraw.vertex ~x ~y ()
    done;
  done;
  GlDraw.ends ();
  Gl.flush ();;

let () =
  let argv' = Glut.init Sys.argv in
  Glut.initWindowSize ~w:(fst !dim) ~h:(snd !dim);
  ignore (Glut.createWindow ~title:"タイトル");
  Glut.reshapeFunc ~cb:reshape;
  Glut.displayFunc ~cb:display;
  Glut.keyboardFunc ~cb:(fun ~key ~x ~y -> if key=27 then exit 0);
  Glut.mainLoop ();;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment