-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.ml
executable file
·93 lines (84 loc) · 2.94 KB
/
texture.ml
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
module Tga = struct
let load file_name =
let ch = open_in_bin file_name in
ignore (input_byte ch);
ignore (input_byte ch);
ignore (input_byte ch);
ignore (input_byte ch); ignore (input_byte ch);
ignore (input_byte ch); ignore (input_byte ch);
ignore (input_byte ch);
ignore (input_byte ch); ignore (input_byte ch);
ignore (input_byte ch); ignore (input_byte ch);
let input_word ch =
let b1 = input_byte ch in
let b2 = input_byte ch in
b1 lor (b2 lsl 8) in
let w = input_word ch in
let h = input_word ch in
Printf.printf "%d %d\n" w h;
let pixel_array = Array.create (h*w) (0.,0.,0.,0.) in
let pix n = float_of_int n /. 255.0 in
let n = input_byte ch / 8 in
ignore (input_byte ch);
for y = 0 to h-1 do
for x = 0 to w-1 do
let b = pix (input_byte ch) in
let g = pix (input_byte ch) in
let r = pix (input_byte ch) in
let a = if n = 4 then pix (input_byte ch) else 1.0 in
pixel_array.(y*w+x) <- (r,g,b,a)
done;
done;
close_in ch;
pixel_array
let image_width = 256
and image_height = 256
(* let make_image arr = *)
(* let image = *)
(* glPixCreate `ubyte ~height:image_height ~width:image_width ~format:`rgba in *)
(* let raw = GlPix.to_raw image *)
(* and pos = GlPix.raw_pos image in *)
(* for i = 0 to image_width - 1 do *)
(* for j = 0 to image_height - 1 do *)
(* let r,g,b,a = arr.(image_width*i+j) in *)
(* (\* let v = arr.(image_width*i+j) in *\) *)
(* Raw.sets raw ~pos:(pos ~x:j ~y:i) *)
(* (ArrayLabels.map ~f:(fun x -> truncate (255.0 *. x)) *)
(* [|r;g;b;a|]); *)
(* done; *)
(* done; *)
(* image *)
(* let gl_load file_name = *)
(* let tex = load file_name in *)
(* let image = make_image tex in *)
(* let tid = GlTex.gen_texture() in *)
(* GlTex.bind_texture `texture_2d tid; *)
(* GlTex.parameter ~target:`texture_2d (`mag_filter `nearest); *)
(* GlTex.parameter ~target:`texture_2d (`min_filter `nearest); *)
(* GlTex.image2d image; *)
(* tid *)
(* let gl_maketex arr = *)
(* let image = make_image arr in *)
(* let tid = GlTex.gen_texture() in *)
(* GlTex.bind_texture `texture_2d tid; *)
(* GlTex.parameter ~target:`texture_2d (`mag_filter `nearest); *)
(* GlTex.parameter ~target:`texture_2d (`min_filter `nearest); *)
(* GlTex.image2d image; *)
(* tid *)
open GL
open Glu
let gl_maketex prev_tid texture =
BatOption.may (fun texture -> glDeleteTexture ~texture) prev_tid;
let tid = glGenTexture() in
glBindTexture BindTex.GL_TEXTURE_2D tid;
glTexParameter ~target:TexParam.GL_TEXTURE_2D ~param:(TexParam.GL_TEXTURE_MAG_FILTER Mag.GL_NEAREST);
glTexParameter ~target:TexParam.GL_TEXTURE_2D ~param:(TexParam.GL_TEXTURE_MIN_FILTER Min.GL_NEAREST);
gluBuild2DMipmaps
InternalFormat.GL_RGBA
256 256
GL_RGBA
GL_UNSIGNED_BYTE
texture;
tid
;;
end