(* 使い方 ocaml -I +lablGL lablgl.cma lablglut.cma unix.cma #use "gl_sample.ml";; main sample1;; main sample2;; ... main sample19;; sample1: ウィンドウを表示 sample2: 青色で背景を描画 sample3: 線を引く sample4: 座標軸を設定(sample3と違いウィンドウサイズを変更しても 図形の大きさが変らない sample5: 位置やサイズを指定してウィンドウを開く sample6: マウスボタンのイベントを受け取る。ボタンを押したり 離したりすると座標を表示。真ん中は使えないようだ sample7: キーボードのイベントを受け取る。q,Qを打つと終了する。 sample8: 再描画するたびに25度回転する。ウィンドウを隠したりする たびに図形の太さが変る sample9: 線画を表示 sample10: 透視投影する sample11: 視点を変更する sample12: 図形を回転させる。マウスの左ボタンで回転。右ボタンで終了。 sample13: sample12にダブルバッファを用いたもの sample14: sample12を塗り潰したもの sample15: sample14の色を変えたもの sample16: デプスバッファを用いて隠面消去したもの sample17: 光を当ててみたもの sample18: 光源を設定したもの sample19: 材質を設定したもの *) (* ウィンドウを表示 *) let sample1 ()= ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample1"); Glut.displayFunc( fun()->() ); Glut.mainLoop();; (* 青色で背景を描画 *) let display2 () = GlClear.clear[`color]; Gl.flush();; let init2 () = GlClear.color (0.0,0.0,1.0);; let sample2 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample2"); Glut.displayFunc(display2); init2 (); Glut.mainLoop ();; (* 線を引く *) let display3 () = GlClear.clear [`color]; GlDraw.begins `line_loop; GlDraw.vertex ~x:~-.0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:0.9 ~y:0.9 (); GlDraw.vertex ~x:0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:~-.0.9 ~y:0.9 (); GlDraw.ends (); Gl.flush();; let init3 () = GlClear.color (0.0,0.0,1.0);; let sample3 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample3"); Glut.displayFunc(display3); init3 (); Glut.mainLoop();; (* 座標軸の設定 *) let resize4 ~w:w ~h:h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); (* 割る数字は何でも良いらしい *) let a = (float_of_int w) /. 200.0 in let b = (float_of_int h) /. 200.0 in GlMat.ortho (~-.a,a) (~-.b,b) (~-.1.0,1.0);; let display4 () = GlClear.clear [`color]; GlDraw.begins `line_loop; GlDraw.vertex ~x:~-.0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:0.9 ~y:0.9 (); GlDraw.vertex ~x:0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:~-.0.9 ~y:0.9 (); GlDraw.ends (); Gl.flush();; let init4 () = GlClear.color (0.0,0.0,1.0);; let sample4 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample4"); Glut.displayFunc(display4); Glut.reshapeFunc(resize4); init4 (); Glut.mainLoop();; (* 位置やサイズを指定してウィンドウを開く *) let resize5 ~w:w ~h:h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); let a = (float_of_int w) /. 200.0 in let b = (float_of_int h) /. 200.0 in GlMat.ortho (~-.a,a) (~-.b,b) (~-.1.0,1.0);; let display5 () = GlClear.clear [`color]; GlDraw.begins `line_loop; GlDraw.vertex ~x:~-.0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:0.9 ~y:0.9 (); GlDraw.vertex ~x:0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:~-.0.9 ~y:0.9 (); GlDraw.ends (); Gl.flush();; let init5 () = GlClear.color (0.0,0.0,1.0);; let sample5 () = Glut.initWindowPosition 100 100; Glut.initWindowSize 320 240; ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample5"); Glut.displayFunc(display5); Glut.reshapeFunc(resize5); init5 (); Glut.mainLoop();; (* マウスボタンをクリックする *) (* ミドルボタンは使えないようだ *) let mouse6 ~button:button ~state:state ~x:x ~y:y = begin match button with Glut.LEFT_BUTTON -> print_string "left" | Glut.MIDDLE_BUTTON -> print_string "middle" | Glut.RIGHT_BUTTON -> print_string "right" end; print_string " button is "; begin match state with Glut.UP -> print_string "up" | Glut.DOWN -> print_string "down" end ; Printf.printf " at (%d , %d)\n" x y; flush stdout;; let resize6 ~w:w ~h:h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); let a = (float_of_int w) /. 200.0 in let b = (float_of_int h) /. 200.0 in GlMat.ortho (~-.a,a) (~-.b,b) (~-.1.0,1.0);; let display6 () = GlClear.clear [`color]; GlDraw.begins `line_loop; GlDraw.vertex ~x:~-.0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:0.9 ~y:0.9 (); GlDraw.vertex ~x:0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:~-.0.9 ~y:0.9 (); GlDraw.ends (); Gl.flush();; let init6 () = GlClear.color (0.0,0.0,1.0);; let sample6 () = Glut.initWindowPosition 100 100; Glut.initWindowSize 320 240; ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample6"); Glut.displayFunc(display6); Glut.reshapeFunc(resize6); Glut.mouseFunc(mouse6); init6 (); Glut.mainLoop ();; (* キーボードをクリックする *) let keyboard7 ~key ~x ~y = match (char_of_int key) with 'q' | 'Q' -> exit 0 | _ -> ();; let resize7 ~w:w ~h:h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); let a = (float_of_int w) /. 200.0 in let b = (float_of_int h) /. 200.0 in GlMat.ortho (~-.a,a) (~-.b,b) (~-.1.0,1.0);; let display7 () = GlClear.clear [`color]; GlDraw.begins `line_loop; GlDraw.vertex ~x:~-.0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:0.9 ~y:0.9 (); GlDraw.vertex ~x:0.9 ~y:~-.0.9 (); GlDraw.vertex ~x:~-.0.9 ~y:0.9 (); GlDraw.ends (); Gl.flush();; let init7 () = GlClear.color (0.0,0.0,1.0);; let sample7 () = Glut.initWindowPosition 100 100; Glut.initWindowSize 320 240; ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample7"); Glut.displayFunc(display7); Glut.reshapeFunc(resize7); Glut.keyboardFunc(keyboard7); init7(); Glut.mainLoop();; (* 3Dの描画 *) (* 再描画するたびに25度回転 *) let display8 () = GlClear.clear [`color]; GlDraw.color (1.0,0.0,0.0); GlMat.rotate ~angle:25.0 ~x:0.0 ~y:1.0 ~z:0.0 (); GlDraw.begins `polygon; GlDraw.color (1.0,0.0,0.0); GlDraw.vertex ~x:~-.0.9 ~y:~-.0.9 (); GlDraw.color (0.0,1.0,0.0); GlDraw.vertex ~x:0.9 ~y:~-.0.9 (); GlDraw.color (0.0,0.0,1.0); GlDraw.vertex ~x:0.9 ~y:0.9 (); GlDraw.color (1.0,1.0,0.0); GlDraw.vertex ~x:~-.0.9 ~y:0.9 (); GlDraw.ends (); Gl.flush();; let init8 () = GlClear.color (0.0,0.0,1.0);; let sample8 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample8"); Glut.displayFunc(display8); init8 (); Glut.mainLoop();; (* 線画を表示する *) let vertex9 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let edge9 = [| [|0;1|]; [|1;2|]; [|2;3|]; [|3;0|]; [|4;5|]; [|5;6|]; [|6;7|]; [|7;4|]; [|0;4|]; [|1;5|]; [|2;6|]; [|3;7|] |];; let vertex3dv9 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let display9 () = GlClear.clear [`color]; GlDraw.color (0.0,0.0,0.0); GlDraw.begins `lines; for i=0 to 11 do vertex3dv9 vertex9.(edge9.(i).(0)); vertex3dv9 vertex9.(edge9.(i).(1)); done; GlDraw.ends (); Gl.flush ();; let resize9 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); GlMat.ortho (-2.0,2.0) (-2.0,2.0) (-2.0,2.0);; let init9 () = GlClear.color (0.0,0.0,1.0);; let sample9 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample9"); Glut.displayFunc(display9); Glut.reshapeFunc(resize9); init9 (); Glut.mainLoop ();; (* 透視投影する *) let vertex10 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let edge10 = [| [|0;1|]; [|1;2|]; [|2;3|]; [|3;0|]; [|4;5|]; [|5;6|]; [|6;7|]; [|7;4|]; [|0;4|]; [|1;5|]; [|2;6|]; [|3;7|] |];; let vertex3dv10 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let display10 () = GlClear.clear [`color]; GlDraw.color (0.0,0.0,0.0); GlDraw.begins `lines; for i=0 to 11 do vertex3dv10 vertex10.(edge10.(i).(0)); vertex3dv10 vertex10.(edge10.(i).(1)); done; GlDraw.ends (); Gl.flush ();; let resize10 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.translate ~x:0.0 ~y:0.0 ~z:~-.5.0 ();; let init10 () = GlClear.color (0.0,0.0,1.0);; let sample10 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample10"); Glut.displayFunc(display10); Glut.reshapeFunc(resize10); init10 (); Glut.mainLoop ();; (* 視点を変更する *) let vertex11 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let edge11 = [| [|0;1|]; [|1;2|]; [|2;3|]; [|3;0|]; [|4;5|]; [|5;6|]; [|6;7|]; [|7;4|]; [|0;4|]; [|1;5|]; [|2;6|]; [|3;7|] |];; let vertex3dv11 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let display11 () = GlClear.clear [`color]; GlDraw.color (0.0,0.0,0.0); GlDraw.begins `lines; for i=0 to 11 do vertex3dv11 vertex11.(edge11.(i).(0)); vertex3dv11 vertex11.(edge11.(i).(1)); done; GlDraw.ends (); Gl.flush ();; let resize11 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0);; let init11 () = GlClear.color (0.0,0.0,1.0);; let sample11 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample11"); Glut.displayFunc(display11); Glut.reshapeFunc(resize11); init11 (); Glut.mainLoop ();; (* アニメーション *) (* 立方体の線画を回転させる *) let vertex12 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let edge12 = [| [|0;1|]; [|1;2|]; [|2;3|]; [|3;0|]; [|4;5|]; [|5;6|]; [|6;7|]; [|7;4|]; [|0;4|]; [|1;5|]; [|2;6|]; [|3;7|] |];; let vertex3dv12 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let r12 = ref 0;; let display12 () = GlClear.clear [`color]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlMat.rotate ~angle:(float_of_int !r12) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `lines; for i=0 to 11 do vertex3dv12 vertex12.(edge12.(i).(0)); vertex3dv12 vertex12.(edge12.(i).(1)); done; GlDraw.ends(); Gl.flush(); r12 := !r12 + 10; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if (!r12>=360) then ( r12 := 0; Glut.idleFunc None; );; let resize12 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse12 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init12 () = GlClear.color (0.0,0.0,1.0);; let sample12 () = ignore(Glut.init Sys.argv); ignore(Glut.createWindow ~title:"sample12"); Glut.displayFunc (display12); Glut.reshapeFunc (resize12); Glut.mouseFunc (mouse12); init12 (); Glut.mainLoop ();; (* sample12にダブルバッファを使用したもの *) let vertex13 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let edge13 = [| [|0;1|]; [|1;2|]; [|2;3|]; [|3;0|]; [|4;5|]; [|5;6|]; [|6;7|]; [|7;4|]; [|0;4|]; [|1;5|]; [|2;6|]; [|3;7|] |];; let vertex3dv13 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let r13 = ref 0;; let display13 () = GlClear.clear [`color]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlMat.rotate ~angle:(float_of_int !r13) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `lines; for i=0 to 11 do vertex3dv13 vertex13.(edge13.(i).(0)); vertex3dv13 vertex13.(edge13.(i).(1)); done; GlDraw.ends (); Glut.swapBuffers (); r13 := !r13 + 1; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if ( !r13>=360 ) then ( r13 := 0; Glut.idleFunc None; );; let resize13 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse13 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init13 () = GlClear.color (0.0,0.0,1.0);; let sample13 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true (); ignore(Glut.createWindow ~title:"sample13"); Glut.displayFunc (display13); Glut.reshapeFunc (resize13); Glut.mouseFunc (mouse13); init13 (); Glut.mainLoop ();; (* 多面体を塗り潰す *) let vertex14 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let face14 = [| [|0;1;2;3|]; [|1;5;6;2|]; [|5;4;7;6|]; [|4;0;3;7|]; [|4;5;1;0|]; [|3;2;6;7|] |];; let vertex3dv14 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let r14 = ref 0;; let display14 () = GlClear.clear [`color]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlMat.rotate ~angle:(float_of_int !r14) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `quads; for j=0 to 5 do for i=0 to 3 do vertex3dv14 vertex14.(face14.(j).(i)); done; done; GlDraw.ends (); Glut.swapBuffers (); r14 := !r14 + 1; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if (!r14>=360) then ( r14 := 0; Glut.idleFunc None; );; let resize14 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse14 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init14 () = GlClear.color (0.0,0.0,1.0);; let sample14 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true (); ignore(Glut.createWindow ~title:"sample14"); Glut.displayFunc (display14); Glut.reshapeFunc (resize14); Glut.mouseFunc (mouse14); init14 (); Glut.mainLoop ();; (* 色を変える *) let vertex15 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let face15 = [| [|0;1;2;3|]; [|1;5;6;2|]; [|5;4;7;6|]; [|4;0;3;7|]; [|4;5;1;0|]; [|3;2;6;7|] |];; let color15 = [| [|1.0;0.0;0.0|]; [|0.0;1.0;0.0|]; [|0.0;0.0;1.0|]; [|1.0;1.0;0.0|]; [|1.0;0.0;1.0|]; [|0.0;1.0;1.0|] |];; let vertex3dv15 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let color3dv15 col = let a = col.(0) and b = col.(1) and c = col.(2) in GlDraw.color (a,b,c);; let r15 = ref 0;; let display15 () = GlClear.clear [`color]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlMat.rotate ~angle:(float_of_int !r15) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `quads; for j=0 to 5 do color3dv15 color15.(j); for i=0 to 3 do vertex3dv15 vertex15.(face15.(j).(i)); done; done; GlDraw.ends (); Glut.swapBuffers (); r15 := !r15 + 10; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if (!r15>=360) then ( r15 := 0; Glut.idleFunc None; );; let resize15 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse15 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init15 () = GlClear.color (0.0,0.0,1.0);; let sample15 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true (); ignore(Glut.createWindow ~title:"sample15"); Glut.displayFunc (display15); Glut.reshapeFunc (resize15); Glut.mouseFunc (mouse15); init15 (); Glut.mainLoop ();; (* デプスバッファを使用して隠面消去 *) let vertex16 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let face16 = [| [|0;1;2;3|]; [|1;5;6;2|]; [|5;4;7;6|]; [|4;0;3;7|]; [|4;5;1;0|]; [|3;2;6;7|] |];; let color16 = [| [|1.0;0.0;0.0|]; [|0.0;1.0;0.0|]; [|0.0;0.0;1.0|]; [|1.0;1.0;0.0|]; [|1.0;0.0;1.0|]; [|0.0;1.0;1.0|] |];; let vertex3dv16 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let color3dv16 col = let a = col.(0) and b = col.(1) and c = col.(2) in GlDraw.color (a,b,c);; let r16 = ref 0;; let display16 () = GlClear.clear [`color;`depth]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlMat.rotate ~angle:(float_of_int !r16) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `quads; for j=0 to 5 do color3dv16 color16.(j); for i=0 to 3 do vertex3dv16 vertex16.(face16.(j).(i)); done; done; GlDraw.ends (); Glut.swapBuffers (); r16 := !r16 + 10; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if (!r16>=360) then ( r16 := 0; Glut.idleFunc None; );; let resize16 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse16 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init16 () = GlClear.color (0.0,0.0,0.0); Gl.enable `depth_test;; let sample16 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true ~depth:true (); ignore(Glut.createWindow ~title:"sample16"); Glut.displayFunc (display16); Glut.reshapeFunc (resize16); Glut.mouseFunc (mouse16); init16 (); Glut.mainLoop ();; (* 光を当ててみる *) let vertex17 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let face17 = [| [|0;1;2;3|]; [|1;5;6;2|]; [|5;4;7;6|]; [|4;0;3;7|]; [|4;5;1;0|]; [|3;2;6;7|] |];; let normal17 = [| [|0.0;0.0;~-.1.0|]; [|1.0;0.0;0.0|]; [|0.0;0.0;1.0|]; [|~-.1.0;0.0;0.0|]; [|0.0;~-.1.0;0.0|]; [|0.0;1.0;0.0|] |];; let vertex3dv17 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let color3dv17 col = let a = col.(0) and b = col.(1) and c = col.(2) in GlDraw.color (a,b,c);; let normal3dv17 nor = let a = nor.(0) and b = nor.(1) and c = nor.(2) in GlDraw.normal ~x:a ~y:b ~z:c ();; let r17 = ref 0;; let display17 () = GlClear.clear [`color;`depth]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlMat.rotate ~angle:(float_of_int !r17) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `quads; for j=0 to 5 do normal3dv17 normal17.(j); for i=0 to 3 do vertex3dv17 vertex17.(face17.(j).(i)); done; done; GlDraw.ends (); Glut.swapBuffers (); r17 := !r17 + 10; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED) = false then if ( !r17 >= 360 ) then ( r17 := 0; Glut.idleFunc None; );; let resize17 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse17 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init17 () = GlClear.color (0.0,0.0,1.0); Gl.enable `depth_test; Gl.enable `cull_face; GlDraw.cull_face `front; Gl.enable `lighting; Gl.enable `light0;; let sample17 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true ~depth:true (); ignore(Glut.createWindow ~title:"sample17"); Glut.displayFunc(display17); Glut.reshapeFunc(resize17); Glut.mouseFunc(mouse17); init17 (); Glut.mainLoop ();; (* 光源を設定する(光源を2つにしてそれぞれの位置と色を変える) *) let vertex18 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let face18 = [| [|0;1;2;3|]; [|1;5;6;2|]; [|5;4;7;6|]; [|4;0;3;7|]; [|4;5;1;0|]; [|3;2;6;7|] |];; let normal18 = [| [|0.0;0.0;~-.1.0|]; [|1.0;0.0;0.0|]; [|0.0;0.0;1.0|]; [|~-.1.0;0.0;0.0|]; [|0.0;~-.1.0;0.0|]; [|0.0;1.0;0.0|] |];; let light0pos18 = (0.0,3.0,5.0,1.0);; let light1pos18 = (5.0,3.0,0.0,1.0);; let green18 = (0.0,1.0,0.0,1.0);; let vertex3dv18 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let color3dv18 col = let a = col.(0) and b = col.(1) and c = col.(2) in GlDraw.color (a,b,c);; let normal3dv18 nor = let a = nor.(0) and b = nor.(1) and c = nor.(2) in GlDraw.normal ~x:a ~y:b ~z:c ();; let r18 = ref 0;; let display18 () = GlClear.clear [`color;`depth]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlLight.light ~num:0 (`position light0pos18); GlLight.light ~num:1 (`position light1pos18); GlMat.rotate ~angle:(float_of_int !r18) ~y:1.0 (); GlDraw.color (0.0,0.0,0.0); GlDraw.begins `quads; for j=0 to 5 do normal3dv18 normal18.(j); for i=0 to 3 do vertex3dv18 vertex18.(face18.(j).(i)); done; done; GlDraw.ends (); Glut.swapBuffers (); r18 := !r18 + 10; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if (!r18>=360) then ( r18 := 0; Glut.idleFunc None; );; let resize18 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse18 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init18 () = GlClear.color (0.0,0.0,1.0); Gl.enable `depth_test; Gl.enable `cull_face; GlDraw.cull_face `front; Gl.enable `lighting; Gl.enable `light0; Gl.enable `light1; GlLight.light ~num:1 (`diffuse green18); GlLight.light ~num:1 (`specular green18);; let sample18 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true ~depth:true (); ignore(Glut.createWindow ~title:"sample18"); Glut.displayFunc(display18); Glut.reshapeFunc(resize18); Glut.mouseFunc(mouse18); init18 (); Glut.mainLoop ();; (* 材質を設定する *) let vertex19 = [| [| 0.0;0.0;0.0 |]; [| 1.0;0.0;0.0 |]; [| 1.0;1.0;0.0 |]; [| 0.0;1.0;0.0 |]; [| 0.0;0.0;1.0 |]; [| 1.0;0.0;1.0 |]; [| 1.0;1.0;1.0 |]; [| 0.0;1.0;1.0 |] |];; let face19 = [| [|0;1;2;3|]; [|1;5;6;2|]; [|5;4;7;6|]; [|4;0;3;7|]; [|4;5;1;0|]; [|3;2;6;7|] |];; let normal19 = [| [|0.0;0.0;~-.1.0|]; [|1.0;0.0;0.0|]; [|0.0;0.0;1.0|]; [|~-.1.0;0.0;0.0|]; [|0.0;~-.1.0;0.0|]; [|0.0;1.0;0.0|] |];; let light0pos19 = (0.0,3.0,5.0,1.0);; let light1pos19 = (5.0,3.0,0.0,1.0);; let green19 = (0.0,1.0,0.0,1.0);; let red19 = (0.8,0.2,0.2,1.0);; let vertex3dv19 vert = let a = vert.(0) in let b = vert.(1) in let c = vert.(2) in GlDraw.vertex ~x:a ~y:b ~z:c ();; let color3dv19 col = let a = col.(0) and b = col.(1) and c = col.(2) in GlDraw.color (a,b,c);; let normal3dv19 nor = let a = nor.(0) and b = nor.(1) and c = nor.(2) in GlDraw.normal ~x:a ~y:b ~z:c ();; let r19 = ref 0;; let display19 () = GlClear.clear [`color;`depth]; GlMat.load_identity (); GluMat.look_at (3.0,4.0,5.0) (0.0,0.0,0.0) (0.0,1.0,0.0); GlLight.light ~num:0 (`position light0pos19); GlLight.light ~num:1 (`position light1pos19); GlMat.rotate ~angle:(float_of_int !r19) ~y:1.0 (); GlLight.material ~face:`both (`diffuse red19); GlDraw.begins `quads; for j=0 to 5 do normal3dv19 normal19.(j); for i=0 to 3 do vertex3dv19 vertex19.(face19.(j).(i)); done; done; GlDraw.ends (); Glut.swapBuffers (); r19 := !r19 + 10; if (Glut.layerGet ~lgtype:Glut.NORMAL_DAMAGED)=false then if (!r19>=360) then ( r19 := 0; Glut.idleFunc None; );; let resize19 ~w ~h = GlDraw.viewport 0 0 w h; GlMat.mode `projection; GlMat.load_identity (); let a = (float_of_int w) and b = (float_of_int h) in GluMat.perspective ~fovy:30.0 ~aspect:(a/.b) ~z:(1.0,100.0); GlMat.mode `modelview;; let mouse19 ~button ~state ~x ~y = match button with Glut.LEFT_BUTTON -> ( match state with Glut.UP -> Glut.idleFunc None; Glut.postRedisplay (); | _ -> (); ); | _ -> ( match state with Glut.UP -> exit 0; | _ -> () );; let init19 () = GlClear.color (0.0,0.0,1.0); Gl.enable `depth_test; Gl.enable `cull_face; GlDraw.cull_face `front; Gl.enable `lighting; Gl.enable `light0; Gl.enable `light1; GlLight.light ~num:1 (`diffuse green19); GlLight.light ~num:1 (`specular green19);; let sample19 () = ignore(Glut.init Sys.argv); Glut.initDisplayMode ~double_buffer:true ~depth:true (); ignore(Glut.createWindow ~title:"sample19"); Glut.displayFunc(display19); Glut.reshapeFunc(resize19); Glut.mouseFunc(mouse19); init19 (); Glut.mainLoop ();; (* メインルーチン *) let main func = if Unix.fork () =0 then func () else ( ignore(Unix.wait ()); ());;