-1

I am creating and storingthe position of the vertices of a 256*256 grid in a simple for loop like this:

ind=0;
for(i=0, i<256, i++){
  for(j=0, j<256, j++){
       vertexPosition[ind].x= i;
       vertexPosition[ind].y= 1.0;
       vertexPosition[ind].z= j;
       ind++;

I am sending these vertices to the shader using vertex arrays. However, when drawing this with glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 256*256)

glBindVertexArray(0);

I get the following result. enter image description here

I understand that this has to do with how opengl draws triangles? I am creating my vertices row by row but it seems that this is not how opengl draws the triangles (obviously). I am quite stuck here and would be glad if anyone could point me in the right direction in how to get the grid to display properly

This is how I store the vertex info in buffers

//Terrain Vertex Position
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(3, VBO);

glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, nrOfVertices * 3 * sizeof(GLfloat), terrainVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(sizeof(glm::vec3) * 0));


//Terrain Normals

glBindBuffer(GL_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ARRAY_BUFFER, nrOfVertices * 3 * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glEnableVertexAttribArray(4);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(sizeof(glm::vec3) * 3));



//Terrain UV
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ARRAY_BUFFER, nrOfVertices * 2 * sizeof(GLfloat), uv, GL_STATIC_DRAW);
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2), BUFFER_OFFSET(sizeof(glm::vec3) * 6));
glBindVertexArray(0); //End the array here

1 Answers1

-2
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, nrOfVertices * 3 * sizeof(GLfloat), terrainVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT,GL_FALSE, sizeof(glm::vec3), BUFFER_OFFSET(sizeof(glm::vec3) * 0));
                                               ^^^^^^^^^^^^^^^^^

No. Your vertices buffer VBO[0] is XYZXYZXYZ. So no stride is needed. Set it to 0.

If your buffer was XYZnnXYZnnXYZ then stride=2 (the two 'n').

Correction: The stride is a number of bytes. If you have two 'n' interleaved and your are reading each vale in buffer as a float, then the stride for each XYZ is 2*sizeof(float)

Same for Normals and UV

Ripi2
  • 7,031
  • 1
  • 17
  • 33
  • Oh well. Seem to get the same result even with the stride set to 0. – Abdominator Jan 25 '17 at 17:49
  • Your glDrawArrays works like this: Let's make a triangle with the first 9 values (3 vert of xyz) from buffer VBO[0]. Then another triangle with next 9 values. Yet more with 9 next... You have not arranged your 'terrainVertices' that way, but with a grid. – Ripi2 Jan 25 '17 at 17:50
  • 1
    This answer is wrong. The `stride` is the byte offset between two consecutive array element start addresses. `sizeof(glm::vec3)` is the correct stride, `0` is just a convenience shortcut for the tightly-packed case. Switching it to 0 will make no difference whatsoever, and using `2` for your interleaved example is completely off. – derhass Jan 25 '17 at 19:49
  • From https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml "[stride]Specifies the byte offset between consecutive generic **vertex attributes**". Not array elements, floats in this case. Using `sizeof(glm::vec3)`will skip every odd vertex. – Ripi2 Jan 26 '17 at 00:46
  • @Ripi2: no, it will not. – derhass Jan 28 '17 at 11:44
  • @derhass Yes you're right and I'm wrong because the OP didn't bind a second vertex attribute to the same XYZXYZXYZ buffer. – Ripi2 Jan 28 '17 at 15:26