c++openglvao

How to create a grid in OpenGL and drawing it with lines


I need to create a grid like this:

  ----------------
  |  |  |  |  |  |
  ----------------
  |  |  |  |  |  |
  ----------------
  |  |  |  |  |  |
  ----------------

and rendering it just with lines. This is how I create the vertices and the indices:

  std::vector<glm::vec3> vertices;
  std::vector<glm::uvec3> indices;

  for(int j=0; j<=slices; ++j) {
    for(int i=0; i<=slices; ++i) {
      float x = (float)i/(float)slices;
      float y = 0;
      float z = (float)j/(float)slices;
      vertices.push_back(glm::vec3(x, y, z));
    }
  }

  for(int j=0; j<slices; ++j) {
    for(int i=0; i<slices; ++i) {

      int row1 = j * (slices+1);
      int row2 = (j+1) * (slices+1);

      indices.push_back(glm::uvec3(row1+i, row1+i+1, row2+i+1));
      indices.push_back(glm::uvec3(row1+i, row2+i+1, row2+i));

    }
  }

  glGenVertexArrays( 1, &vao );
  glBindVertexArray( vao );

  GLuint vbo;
  glGenBuffers( 1, &vbo );
  glBindBuffer( GL_ARRAY_BUFFER, vbo );
  glBufferData( GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), glm::value_ptr(vertices[0]), GL_STATIC_DRAW );
  glEnableVertexAttribArray( 0 );
  glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, nullptr );

  GLuint ibo;
  glGenBuffers( 1, &ibo );
  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
  glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec3), glm::value_ptr(indices[0]), GL_STATIC_DRAW );

  glBindVertexArray( 0 );
  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
  glBindBuffer( GL_ARRAY_BUFFER, 0 );

  lenght = (GLuint)indices.size()*3;

here how I render it:

  glBindVertexArray(vao);

  glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

  glBindVertexArray(0);

The results is not what I want. I also try with GL_LINE_LOOP etc I think that the problems is in the creation of the vertices and indices

UPDATE:

I made the changes suggested by Rabbit76 looks perfect!!

enter image description here

Here the final code to create the vertices and the indices:

  std::vector<glm::vec3> vertices;
  std::vector<glm::uvec4> indices;

  for(int j=0; j<=slices; ++j) {
    for(int i=0; i<=slices; ++i) {
      float x = (float)i/(float)slices;
      float y = 0;
      float z = (float)j/(float)slices;
      vertices.push_back(glm::vec3(x, y, z));
    }
  }

  for(int j=0; j<slices; ++j) {
    for(int i=0; i<slices; ++i) {

      int row1 =  j    * (slices+1);
      int row2 = (j+1) * (slices+1);

      indices.push_back(glm::uvec4(row1+i, row1+i+1, row1+i+1, row2+i+1));
      indices.push_back(glm::uvec4(row2+i+1, row2+i, row2+i, row1+i));

    }
  }

  glGenVertexArrays( 1, &vao );
  glBindVertexArray( vao );

  GLuint vbo;
  glGenBuffers( 1, &vbo );
  glBindBuffer( GL_ARRAY_BUFFER, vbo );
  glBufferData( GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3), glm::value_ptr(vertices[0]), GL_STATIC_DRAW );
  glEnableVertexAttribArray( 0 );
  glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, nullptr );

  GLuint ibo;
  glGenBuffers( 1, &ibo );
  glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
  glBufferData( GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec4), glm::value_ptr(indices[0]), GL_STATIC_DRAW);

  glBindVertexArray(0);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  glBindBuffer(GL_ARRAY_BUFFER, 0);

  lenght = (GLuint)indices.size()*4;

this the code for render the grid:

  glEnable(GL_DEPTH_TEST);

  glBindVertexArray(vao);

  glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);

  glBindVertexArray(0);

  glDisable(GL_DEPTH_TEST);

Solution

  • [...] I need to render a line raster [...]

    There are a lot of possibilities. Based on this code, an option with very few changes is to create four line segments for each quad.

    Change the type of the indices:

    std::vector<glm::uvec4> indices;
    

    Add the line segments:

    indices.push_back(glm::uvec4(row1+i, row1+i+1, row1+i+1, row2+i+1));
    indices.push_back(glm::uvec4(row2+i+1, row2+i, row2+i, row1+i));
    

    Take care of the changed type when you create and initialize the data store of the buffer:

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(glm::uvec4),
                 indices.data(), GL_STATIC_DRAW);
    

    respectively (even more elegant)

    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(*indices.data()),
                 indices.data(), GL_STATIC_DRAW);
    

    Finally calculate the proper number of indices

    lenght = (GLuint)indices.size()*4;
    

    Draw the line segments

    glBindVertexArray(vao);
    glDrawElements(GL_LINES, lenght, GL_UNSIGNED_INT, NULL);
    

    Another option would be to render line segments across the entire grid:

    std::vector<glm::uvec2> indices;
    
    int rowLen = slices+1;
    int noVert = (int)vertices.size();
    for(int j=0; j<slices; ++j) {
        indices.push_back(glm::uvec2(j * rowLen, (j+1) * rowLen - 1));
        indices.push_back(glm::uvec2(j, noVert - rowLen + j));
    }
    
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(*indices.data()),
                 indices.data(), GL_STATIC_DRAW);
    lenght = (GLuint)indices.size()*2;