Friday, November 8, 2024
HomeGame Developmentopengl - cannot make drawElements work

opengl – cannot make drawElements work


Whereas I’m able to rotate a easy textured dice with drawArrays, I simply cannot make drawElements work, it is nearly infuriating.

Here’s what I’ve

  • Buffers (vbo and ebo) are appropriately crammed. I’m positive of that, as a result of glGetSubBuffer returns anticipated information simply inside my draw name.
  • Shaders look appropriate, as a result of switching to drawArrays with similar shaders works appropriately.
  • I can not think about what else may very well be improper?

Right here is condensed model of initialization:

  let vao_id = get_int (Gl.gen_vertex_arrays 1) in
  let vbo = get_int (Gl.gen_buffers 1) in
  let ebo = get_int (Gl.gen_buffers 1) in
  Gl.bind_vertex_array vao_id ;
  let vertex_data_sz = Gl.bigarray_byte_size mannequin.vertices in
  let ebo_data_sz = Gl.bigarray_byte_size mannequin.faces in
  Gl.bind_buffer Gl.array_buffer vbo ;
  Gl.buffer_data Gl.array_buffer vertex_data_sz (Some mannequin.vertices) Gl.static_draw ;
  Gl.bind_buffer Gl.element_array_buffer ebo ;
  Gl.buffer_data Gl.element_array_buffer ebo_data_sz (Some mannequin.faces) Gl.static_draw ;
  (* 12 as a result of we now have 3 elements of 4 bytes *)
  Gl.vertex_attrib_pointer 0 vertex_data_sz Gl.float false 12 (`Offset 0) ;
  <create program...> 

Information is an easy .obj file (ought to present a rectangle):

v 0.5 0.5 0.0
v 0.5 -0.5 0.0
v -0.5 -0.5 0.0 
v -0.5 0.5 0.0 
f 1 2 4
f 2 3 4

Inside draw name, i’ve positioned a debug loop to see what’s inside my buffers (glGetBufferSubData: they appear appropriately crammed):

2022-07-15 21:39:13.364 INFO : ***dbg face: 0 1 3
2022-07-15 21:39:13.364 INFO : ***dbg face: 1 2 3
2022-07-15 21:39:13.364 INFO : ***dbg vertex: 0.500000 0.500000 0.000000
2022-07-15 21:39:13.364 INFO : ***dbg vertex: 0.500000 -0.500000 0.000000
2022-07-15 21:39:13.364 INFO : ***dbg vertex: -0.500000 -0.500000 0.000000
2022-07-15 21:39:13.364 INFO : ***dbg vertex: -0.500000 0.500000 0.00000

Right here is condensed model of draw routine:

    Gl.clear_color 0.5 0.5 0.5 1. ;
    Gl.polygon_mode Gl.front_and_back Gl.line ;
    Gl.use_program pg.p_id ;
    Gl.bind_vertex_array vao_id ;
      (* DEBUG *)
      let dbg1 = bigarray_create Bigarray.Int16_unsigned 6 in
      Gl.get_buffer_sub_data Gl.element_array_buffer 0 12 dbg1 ;
      for i = 0 to 2 - 1 do
        Dolog.Log.data "***dbg face: %i %i %i"
          dbg1.{i * 3}
          dbg1.{(i * 3) + 1}
          dbg1.{(i * 3) + 2}
      accomplished ;
      let dbg2 = bigarray_create Bigarray.Float32 12 in
      (* 4 vertices x 3 elements x 4 bytes per element *)
      Gl.get_buffer_sub_data Gl.array_buffer 0 48 dbg2 ;
      for i = 0 to 4 - 1 do
        Dolog.Log.data "***dbg vertex: %f %f %f"
          dbg2.{i * 3}
          dbg2.{(i * 3) + 1}
          dbg2.{(i * 3) + 2}
      accomplished ;
      (* END DEBUG *)

    let view_loc, view_type = Hashtbl.discover pg.uniforms "view" in
    let proj_loc, proj_type = Hashtbl.discover pg.uniforms "proj" in
    Gl.uniform_matrix4fv view_loc 1 false (Digicam.matrix !global_camera) ;
    Gl.uniform_matrix4fv proj_loc 1 false (Projection.matrix !global_camera) ;
    Gl.draw_elements Gl.triangles 2 Gl.unsigned_short (`Offset 0)
    Sdl.gl_swap_window win

Right here is the vertex shader:

#model 400 core
format(location = 0) in vec3 vertex;
uniform mat4 view, proj;
void predominant()
{
  gl_Position = proj * view * vec4(vertex, 1.0);
}

Frag shader:

#model 400 core
out vec4 f_color; void predominant() { f_color = vec4(1,1, 0, 1); }

Solely gray display screen is being drawn; any concepts?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments