Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of using textures #26

Open
tari3x opened this issue Apr 7, 2019 · 0 comments
Open

Add an example of using textures #26

tari3x opened this issue Apr 7, 2019 · 0 comments

Comments

@tari3x
Copy link

tari3x commented Apr 7, 2019

It would be great to add an example of using textures given how important those are. Here's a patch of the change I made while playing with it myself, in case it is useful. Sadly, github seems disliking attachments right now, so I'll just paste the whole diff. Note it doesn't clean up correctly.

diff --git a/test/trigl4.ml b/test/trigl4.ml
index f135afb..f9524f2 100644
--- a/test/trigl4.ml
+++ b/test/trigl4.ml
@@ -43,18 +43,25 @@ let vertex_shader v = str "
   #version %s core
   in vec3 vertex;
   in vec3 color;
+  in vec2 texture_coord;
   out vec4 v_color;
+  out vec2 v_texture_coord;
   void main()
   {
     v_color = vec4(color, 1.0);
     gl_Position = vec4(vertex, 1.0);
+    v_texture_coord = texture_coord;
   }" v
 
 let fragment_shader v = str "
   #version %s core
   in vec4 v_color;
+  in vec2 v_texture_coord;
   out vec4 color;
-  void main() { color = v_color; }" v
+  uniform sampler2D texture;
+  void main() {
+    color = texture2D(texture, v_texture_coord);
+  }" v
 
 (* Geometry *)
 
@@ -62,6 +69,10 @@ let set_3d ba i x y z =
   let start = i * 3 in
   ba.{start} <- x; ba.{start + 1} <- y; ba.{start + 2} <- z
 
+let set_2d ba i x y =
+  let start = i * 2 in
+  ba.{start} <- x; ba.{start + 1} <- y
+
 let vertices =
   let vs = bigarray_create Bigarray.float32 (3 * 3) in
   set_3d vs 0 (-0.8) (-0.8) 0.0;
@@ -69,6 +80,13 @@ let vertices =
   set_3d vs 2 0.0    0.8    0.0;
   vs
 
+let texture_coords =
+  let vs = bigarray_create Bigarray.float32 (3 * 2) in
+  set_2d vs 0 0. 1.;
+  set_2d vs 1 1. 0.;
+  set_2d vs 2 1. 1.;
+  vs
+
 let colors =
   let cs = bigarray_create Bigarray.float32 (3 * 3) in
   set_3d cs 0 1.0 0.0 0.0;
@@ -81,6 +99,16 @@ let indices =
   set_3d is 0 0 1 2;
   is
 
+let texture_rows = 100
+let texture_cols = 100
+
+let texture_data =
+  let data = bigarray_create Bigarray.int8_unsigned (texture_rows * texture_cols * 3) in
+  for i = 0 to texture_rows * texture_cols - 1 do
+    set_3d data i 255 255 0
+  done;
+  data
+
 (* OpenGL setup *)
 
 let create_buffer b =
@@ -93,11 +121,34 @@ let create_buffer b =
 let delete_buffer bid =
   set_int (Gl.delete_buffers 1) bid
 
-let create_geometry () =
+let create_texture pid =
+  let texture = get_int (Gl.gen_textures 1) in
+  Gl.active_texture Gl.texture0;
+  Gl.bind_texture Gl.texture_2d texture;
+  Gl.uniform1i (Gl.get_uniform_location pid "texture") 0;
+  Gl.tex_image2d
+    Gl.texture_2d
+    0
+    Gl.rgb
+    texture_cols
+    texture_rows
+    0
+    Gl.rgb
+    Gl.unsigned_byte
+    (`Data texture_data);
+  Gl.tex_parameteri Gl.texture_2d Gl.texture_wrap_s Gl.clamp_to_edge;
+  Gl.tex_parameteri Gl.texture_2d Gl.texture_wrap_t Gl.clamp_to_edge;
+  Gl.tex_parameteri Gl.texture_2d Gl.texture_min_filter Gl.linear;
+  Gl.tex_parameteri Gl.texture_2d Gl.texture_mag_filter Gl.linear;
+  Gl.enable Gl.texture_2d;
+  Ok ()
+
+let create_geometry pid =
   let gid = get_int (Gl.gen_vertex_arrays 1) in
   let iid = create_buffer indices in
   let vid = create_buffer vertices in
   let cid = create_buffer colors in
+  let tid = create_buffer texture_coords in
   let bind_attrib id loc dim typ =
     Gl.bind_buffer Gl.array_buffer id;
     Gl.enable_vertex_attrib_array loc;
@@ -105,12 +156,16 @@ let create_geometry () =
   in
   Gl.bind_vertex_array gid;
   Gl.bind_buffer Gl.element_array_buffer iid;
-  bind_attrib vid 0 3 Gl.float;
-  bind_attrib cid 1 3 Gl.float;
+  let vertex_loc  = Gl.get_attrib_location pid "vertex" in
+  let color_loc   = Gl.get_attrib_location pid "color" in
+  let texture_loc = Gl.get_attrib_location pid "texture_coord" in
+  bind_attrib vid vertex_loc  3 Gl.float;
+  bind_attrib cid color_loc   3 Gl.float;
+  bind_attrib tid texture_loc 3 Gl.float;
   Gl.bind_vertex_array 0;
   Gl.bind_buffer Gl.array_buffer 0;
   Gl.bind_buffer Gl.element_array_buffer 0;
-  Ok (gid, [iid; vid; cid])
+  Ok (gid, [iid; vid; cid; tid])
 
 let delete_geometry gid bids =
   set_int (Gl.delete_vertex_arrays 1) gid;
@@ -134,8 +189,6 @@ let create_program glsl_v =
   let get_program pid e = get_int (Gl.get_programiv pid e) in
   Gl.attach_shader pid vid; Gl.delete_shader vid;
   Gl.attach_shader pid fid; Gl.delete_shader fid;
-  Gl.bind_attrib_location pid 0 "vertex";
-  Gl.bind_attrib_location pid 1 "color";
   Gl.link_program pid;
   if get_program pid Gl.link_status = Gl.true_ then Ok pid else
   let len = get_program pid Gl.info_log_length in
@@ -220,8 +273,9 @@ let event_loop win draw =
 let tri ~gl:(maj, min as gl) =
   Sdl.init Sdl.Init.video          >>= fun () ->
   create_window ~gl                >>= fun (win, ctx) ->
-  create_geometry ()               >>= fun (gid, bids) ->
   create_program (glsl_version gl) >>= fun pid ->
+  create_geometry pid              >>= fun (gid, bids) ->
+  create_texture pid               >>= fun () ->
   event_loop win (draw pid gid)    >>= fun () ->
   delete_program pid               >>= fun () ->
   delete_geometry gid bids         >>= fun () ->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants