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

1

6

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);
Castiglione answered 21/10, 2019 at 21:9 Comment(10)
The indices are set for rendering triangles respectively quads by 2 triangles. Do you want to render a line raster?Talkathon
@Talkathon yes exactly I need to render a line raster.Castiglione
post full code plz, same problem here.Rinehart
@vinilara I post the final codeCastiglione
thanks, but your code is not reproducible.Rinehart
@vinilara what you need?Castiglione
I need to do the same thing you did but when I paste your code in my code the window turns white and nothing is showed. Can you show me the entire code?Rinehart
I will need some time. I need to extract the relevant code from my project. I can not just copy and paste.Castiglione
@vinilara checkout here github.com/thewoz/MPLCastiglione
Weird that the lines seem to get thicker in the distance. I guess that's a side effect of making the thickness of the lines related to the pixels on the screen as opposed to a technique that takes perspective into account (like rendering the grid as a texture)Earle
T
7

[...] 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;
Talkathon answered 21/10, 2019 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.