Home > other >  Vector 4 not representing the colors of all the verticii
Vector 4 not representing the colors of all the verticii

Time:03-09

I'm trying to have 4 integers represent the colors of all the verticii in a VBO by having the stride on the color vertex attribute pointer, however, It seems to only take the value once for the color, and, as a result, assigns the rest of the verticii as black as in the picture: picture. The expected result is that all the verticii will be white.

Here is the relevant pieces of code:

int triangleData[18] = 
 {
  2147483647,2147483647,2147483647,2147483647,//opaque white
  0,100, //top
  100,-100, //bottom right
  -100,-100 //bottom left
 };
 unsigned int colorVAO, colorVBO;
 glGenVertexArrays(1, &colorVAO);
 glGenBuffers(1, &colorVBO);
 glBindVertexArray(colorVAO);
 glBindBuffer(GL_ARRAY_BUFFER, colorVBO);
 glBufferData(GL_ARRAY_BUFFER, sizeof(triangleData), triangleData, GL_STATIC_DRAW);
 glVertexAttribPointer(0, 2, GL_INT, GL_FALSE, 2 * sizeof(int), (void*)(4*sizeof(int)));
 glEnableVertexAttribArray(0);
 glVertexAttribPointer(1, 4, GL_INT, GL_TRUE, 0, (void*)0);
 glEnableVertexAttribArray(1);

Vertex shader:

#version 330 core
 layout (location = 0) in vec2 aPos;
 layout (location = 1) in vec4 aColor;

 out vec4 Color;

 uniform mat4 model;
 uniform mat4 view;
 uniform mat4 ortho;

 void main()
 {

    gl_Position = ortho * view * model * vec4(aPos, 1.0, 1.0);
    Color = aColor;
 }

Fragment shader:

 #version 330 core
 out vec4 FragColor;
  
 in vec4 Color;

 void main()
 {
    FragColor = Color;
 }

CodePudding user response:

From the documentation of glVertexAttribPointer:

stride
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array.

Setting the stride to 0 does not mean that the same data is read for each vertex. It means that the data is packed one after the other in the buffer.

If you want all the vertices to use the same data, you can either disable the attribute and use glVertexAttrib, or you can use the separate vertex format (available starting from OpenGL 4.3 or with ARB_vertex_attrib_binding) similar to:

glBindVertexBuffer(index, buffer, offset, 0);

where a stride of 0 really means no stride.

  • Related