向量 4 不代表所有 verticii 的颜色
Vector 4 not representing the colors of all the verticii
我试图通过在颜色顶点属性指针上跨步来让 4 个整数代表 VBO 中所有顶点的颜色,但是,它似乎只为颜色取值一次,并且,结果,将其余的 verticii 分配为黑色,如图所示:picture。预期结果是所有verticii都会变白
相关代码如下:
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);
顶点着色器:
#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;
}
片段着色器:
#version 330 core
out vec4 FragColor;
in vec4 Color;
void main()
{
FragColor = Color;
}
来自 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.
将步幅设置为0并不意味着为每个顶点读取相同的数据。表示数据在buffer中一个接一个打包。
如果您希望所有顶点都使用相同的数据,您可以禁用该属性并使用 glVertexAttrib
, or you can use the separate vertex format(从 OpenGL 4.3 或 ARB_vertex_attrib_binding 开始可用)类似于:
glBindVertexBuffer(index, buffer, offset, 0);
步幅为 0 意味着没有步幅。
我试图通过在颜色顶点属性指针上跨步来让 4 个整数代表 VBO 中所有顶点的颜色,但是,它似乎只为颜色取值一次,并且,结果,将其余的 verticii 分配为黑色,如图所示:picture。预期结果是所有verticii都会变白
相关代码如下:
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);
顶点着色器:
#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;
}
片段着色器:
#version 330 core
out vec4 FragColor;
in vec4 Color;
void main()
{
FragColor = Color;
}
来自 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.
将步幅设置为0并不意味着为每个顶点读取相同的数据。表示数据在buffer中一个接一个打包。
如果您希望所有顶点都使用相同的数据,您可以禁用该属性并使用 glVertexAttrib
, or you can use the separate vertex format(从 OpenGL 4.3 或 ARB_vertex_attrib_binding 开始可用)类似于:
glBindVertexBuffer(index, buffer, offset, 0);
步幅为 0 意味着没有步幅。