在线关注示例,这是顶点着色器中的代码:
属性定义如下:
-version 3.0
在顶点着色器中,使用它们:
// Option 1:
layout (location = 0) in vec3 colorvec3;
layout (location = 1) in mat4x2 xyInBaseImageMat4x2;
out vec2 xyInElementTextureImage;
out vec3 elementTintColorVec3;
但结果很奇怪,完全随机。有时它是黑色的,有时它是随机的形状。
但如果我改用:
// Option 1
vec2 xyInBaseImageVec2 = xyInBaseImageMat4x2[gl_VertexID];
gl_Position = vec4(xyInBaseImageVec2, 0.0, 1.0);
elementTintColorVec3 = colorvec3;
在顶点着色器中:
// Option 2:
layout (location = 0) in vec3 colorvec3;
layout (location = 1) in vec2 xyInBaseImageVec2_p0;
layout (location = 2) in vec2 xyInBaseImageVec2_p1;
layout (location = 3) in vec2 xyInBaseImageVec2_p2;
layout (location = 4) in vec2 xyInBaseImageVec2_p3;
out vec2 xyInElementTextureImage;
out vec3 elementTintColorVec3;
然后它按预期工作。
两个示例的颜色和顶点位置的数据缓冲区相同。 [更新:添加下面的喂食代码]
// Option 2:
vec2 xyInBaseImageVec2;
if (gl_VertexID==0) {
xyInBaseImageVec2 = xyInBaseImageVec2_p0;
} else if (gl_VertexID==1) {
xyInBaseImageVec2 = xyInBaseImageVec2_p1;
} else if (gl_VertexID==2) {
xyInBaseImageVec2 = xyInBaseImageVec2_p2;
} else if (gl_VertexID==3) {
xyInBaseImageVec2 = xyInBaseImageVec2_p3;
}
gl_Position = vec4(xyInBaseImageVec2, 0.0, 1.0);
elementTintColorVec3 = colorvec3;
第一个选项出了什么问题?