~ubuntu-branches/ubuntu/hardy/lablgl/hardy

« back to all changes in this revision

Viewing changes to LablGlut/examples/lablGL/texturesurf.ml

  • Committer: Bazaar Package Importer
  • Author(s): Sven Luther
  • Date: 2004-05-26 09:39:17 UTC
  • Revision ID: james.westby@ubuntu.com-20040526093917-uakgrsrv5keom5kn
Tags: upstream-1.00
ImportĀ upstreamĀ versionĀ 1.00

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* $Id: texturesurf.ml,v 1.1 2003/09/25 13:54:10 raffalli Exp $ *)
 
2
(* Converted to lablglut by Issac Trotts on July 25, 2002 *)
 
3
 
 
4
open StdLabels
 
5
 
 
6
let texpts =
 
7
  [|[|0.0; 0.0;  0.0; 1.0|];
 
8
    [|1.0; 0.0;  1.0; 1.0|]|]
 
9
 
 
10
let ctrlpoints =
 
11
  [|[|-1.5; -1.5; 4.9;  -0.5; -1.5; 2.0;  0.5; -1.5; -1.0; 1.5; -1.5; 2.0|];
 
12
    [|-1.5; -0.5; 1.0;  -0.5; -0.5; 3.0;  0.5; -0.5; 0.0;  1.5; -0.5; -1.0|];
 
13
    [|-1.5; 0.5; 4.0;   -0.5; 0.5; 0.0;   0.5; 0.5; 3.0;   1.5; 0.5; 4.0|];
 
14
    [|-1.5; 1.5; -2.0;  -0.5; 1.5; -2.0;  0.5; 1.5; 0.0;   1.5; 1.5; -1.0|]|]
 
15
 
 
16
let image_width = 64
 
17
and image_height = 64
 
18
 
 
19
let pi = acos (-1.0)
 
20
 
 
21
let display () =
 
22
  GlClear.clear [`color;`depth];
 
23
  GlDraw.color (1.0,1.0,1.0);
 
24
  GlMap.eval_mesh2 ~mode:`fill ~range1:(0,20) ~range2:(0,20);
 
25
  Gl.flush ();
 
26
  Glut.swapBuffers ()
 
27
 
 
28
let make_image () =
 
29
  let image =
 
30
    GlPix.create `ubyte ~height:image_height ~width:image_width ~format:`rgb in
 
31
  let raw = GlPix.to_raw image
 
32
  and pos = GlPix.raw_pos image in
 
33
  for i = 0 to image_width - 1 do
 
34
    let ti = 2.0 *. pi *. float i /. float image_width in
 
35
    for j = 0 to image_height - 1 do
 
36
      let tj = 2.0 *. pi *. float j /. float image_height in
 
37
      Raw.sets raw ~pos:(pos ~x:j ~y:i)
 
38
        (Array.map ~f:(fun x -> truncate (127.0 *. (1.0 +. x)))
 
39
           [|sin ti; cos (2.0 *. ti); cos (ti +. tj)|]);
 
40
      done;
 
41
  done;
 
42
  image
 
43
 
 
44
let myinit () =
 
45
  let ctrlpoints = Raw.of_matrix ~kind:`double ctrlpoints
 
46
  and texpts = Raw.of_matrix ~kind:`double texpts in
 
47
  GlMap.map2 ~target:`vertex_3
 
48
    (0.0, 1.0) ~order:4 (0.0, 1.0) ~order:4 ctrlpoints;
 
49
  GlMap.map2 ~target:`texture_coord_2
 
50
    (0.0,1.0) ~order:2 (0.0,1.0) ~order:2 texpts;
 
51
  Gl.enable `map2_texture_coord_2;
 
52
  Gl.enable `map2_vertex_3;
 
53
  GlMap.grid2 ~n1:20 ~range1:(0.0,1.0) ~n2:20 ~range2:(0.0,1.0);
 
54
  let image = make_image () in
 
55
  GlTex.env (`mode `decal);
 
56
  List.iter ~f:(GlTex.parameter ~target:`texture_2d)
 
57
    [ `wrap_s `repeat;
 
58
      `wrap_t `repeat;
 
59
      `mag_filter `nearest;
 
60
      `min_filter `nearest ];
 
61
  GlTex.image2d image;
 
62
  List.iter ~f:Gl.enable [`texture_2d;`depth_test;`normalize];
 
63
  GlDraw.shade_model `flat
 
64
 
 
65
let my_reshape ~w ~h =
 
66
  GlDraw.viewport ~x:0 ~y:0 ~w ~h;
 
67
  GlMat.mode `projection;
 
68
  GlMat.load_identity ();
 
69
  let r = float h /. float w in
 
70
  if w <= h then
 
71
    GlMat.ortho ~x:(-4.0, 4.0) ~y:(-4.0 *. r, 4.0 *. r) ~z:(-4.0, 4.0)
 
72
  else
 
73
    GlMat.ortho ~x:(-4.0 /. r, 4.0 /. r) ~y:(-4.0, 4.0) ~z:(-4.0, 4.0);
 
74
  GlMat.mode `modelview;
 
75
  GlMat.load_identity ();
 
76
  GlMat.rotate ~angle:85. ~x:1. ~y:1. ~z:1. ()
 
77
 
 
78
let main () =
 
79
  ignore(Glut.init Sys.argv);
 
80
  Glut.initDisplayMode ~alpha:true ~depth:true ~double_buffer:true () ;
 
81
  Glut.initWindowSize ~w:300 ~h:300 ;
 
82
  ignore(Glut.createWindow ~title:"Texture Surf");
 
83
  myinit ();
 
84
  Glut.reshapeFunc ~cb:my_reshape ;
 
85
  Glut.displayFunc ~cb:display ;
 
86
  Glut.specialFunc ~cb:(fun ~key ~x ~y ->
 
87
      match key with 
 
88
      | Glut.KEY_UP -> GlMat.rotate ~angle:(-5.) ~z:1.0 (); display ()
 
89
      | Glut.KEY_DOWN -> GlMat.rotate ~angle:(5.) ~z:1.0 (); display ()
 
90
      | Glut.KEY_LEFT -> GlMat.rotate ~angle:(5.) ~x:1.0 (); display ()
 
91
      | Glut.KEY_RIGHT -> GlMat.rotate ~angle:(-5.) ~x:1.0 (); display ()
 
92
      | _ -> ());
 
93
  Glut.keyboardFunc ~cb:(fun ~key ~x ~y ->
 
94
      match key with
 
95
      | 27 (*esc*) -> exit 0
 
96
      | _ -> ());
 
97
  Glut.mainLoop ()
 
98
 
 
99
let _ = main ()