Search code examples
openglvertex-buffervertex-array-objectopenglrenderer

openGL drawing GL_LINES giving incorrect result


I am trying to draw a grid of velocity vectors, I expect the velocity at each grid point to be a line with a slop of 1. A slanting line, but I always end up with a vertical line. I'm not sure what I'm doing wrong. Is there something I'm overlooking?

Here is how my vertex buffer looks :

float vel_pos[6*(N+2)*(N+2)];
int index1 = 0;
for (int i=0;i<N+2;i++)
{
    for (int j=0;j<N+2;j++)
    {
        vel_pos[index1] = float(i);
        vel_pos[index1+1] = float(j);
        vel_pos[index1+2] = 0.0f;
        vel_pos[index1+3] = float(i) +0.5f;
        vel_pos[index1+4] = float(j) + 0.5f;
        vel_pos[index1+5] = 0.0f;
        index1 += 6;
    }
}

Here is how I am creating my VBO and VAO :

unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

// Bind vertex array object first and then bind the vertex buffer objects
glBindVertexArray(VAO);

glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(vel_pos), vel_pos, GL_STREAM_DRAW);


GLint velAttrib = glGetAttribLocation(ourShader.ID, "aPos");

// iterpreting data from buffer 
glVertexAttribPointer(velAttrib, 3, GL_FLOAT, GL_FALSE,  6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

Here is my vertex shader :

  out vec4 vertexColor;
  layout (location = 0) in vec3 aPos; 
  layout (location = 1) in float densitySource; /* source of density */
  uniform mat4 transform;
  uniform mat4 projection;

  void main()
  {
  gl_Position = projection*transform * vec4(aPos, 1.0);
  vertexColor = vec4(1, 0.0, 0.0, 1.0);
  }

And here's my drawing code :

  ourShader.use();

    glm::mat4 trans = glm::mat4(1.0f);
    trans = glm::translate(trans, glm::vec3(-0.5f, -0.5f, 0.0f));
    unsigned int transformMatrixLocation = glGetUniformLocation(ourShader.ID, "transform");
    glUniformMatrix4fv(transformMatrixLocation, 1, GL_FALSE, glm::value_ptr(trans));

    glm::mat4 projection = glm::ortho(-10.0f, 110.0f, -1.0f, 110.0f, -1.0f, 100.0f);

    unsigned int projectionMatrixLocation = glGetUniformLocation(ourShader.ID, "projection");
    glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, glm::value_ptr(projection));

    glBindVertexArray(VAO); 
    glLineWidth(1.0f);
    glDrawArrays(GL_LINES,  0, (N+2)*(N+2));

This is the image I get : resulting image


Solution

  • The 5th parameter (stride) of glVertexAttribPointer is the offset between two vertex coordinates and not between to primitives. Since your vertex coordinates have 3 components of type float, the offset has to be 3 * sizeof(float):

    glVertexAttribPointer(velAttrib, 3, GL_FLOAT, GL_FALSE,  3 * sizeof(float), (void*)0);
    

    Because you set an offset of 6 * sizeof(float), you have skipped every 2 coordinate and have drawn lines between the points of the grid.

    But note, if stride is 0, the generic vertex attributes are understood to be tightly packed in the array. This is the case, so you ca use an offset of 0:

    glVertexAttribPointer(velAttrib, 3, GL_FLOAT, GL_FALSE,  0, (void*)0);