我将一条线传递给几何着色器并输出一个长方体。我在线的每一端创建了4个新点,通过在x或y方向上添加常量“厚度”来传入。如果我在几何着色器中设置厚度,它可以正常工作,但是当我尝试将它作为顶点数据的一部分传递给着色器时,我似乎无法在几何着色器中访问它。当我设置
几何着色器中的thickness = vThickness[0]
,则不会绘制任何内容。
我可以访问顶点着色器中的厚度数据,它似乎是有效的(我将输出颜色设置为厚度,并且长方体被绘制为白色,这很好,因为它应该是一个整数> = 1)。问题似乎出现在顶点着色器和几何着色器之间的界面中。
顶点着色器:
#version 150 core
uniform vec3 uColor;
in vec3 position;
in int thickness;
out vec4 vColor;
out int vThickness;
void main() {
vColor = vec4(uColor, 1.0);
vThickness = thickness;
gl_Position = vec4(position, 1.0);
}
几何着色器:
#version 150 core
uniform mat4 uProjection;
uniform mat4 uModel;
in vec4 vColor[];
in int vThickness[];
out vec4 gColor;
out vec3 normalV;
out vec3 points;
layout (lines) in;
layout (triangle_strip, max_vertices = 20) out;
void main() {
gColor = vColor[0];
vec3 z = (gl_in[1].gl_Position - gl_in[0].gl_Position).xyz;
vec3 x = cross(vec3(0.0, 1.0, 0.0), z);
float thickness = vColor[0];
// float thickness = 0.5; // This worked
x = thickness * normalize(x);
vec3 y = cross(z, x);
y = thickness * normalize(y);
vec4 endTL = uProjection * uModel * (gl_in[1].gl_Position - vec4(x, 0.0) + vec4(y, 0.0));
vec4 endTR = uProjection * uModel * (gl_in[1].gl_Position + vec4(x, 0.0) + vec4(y, 0.0));
vec4 endBL = uProjection * uModel * (gl_in[1].gl_Position - vec4(x, 0.0) - vec4(y, 0.0));
vec4 endBR = uProjection * uModel * (gl_in[1].gl_Position + vec4(x, 0.0) - vec4(y, 0.0));
vec4 begTL = uProjection * uModel * (gl_in[0].gl_Position - vec4(x, 0.0) + vec4(y, 0.0));
vec4 begTR = uProjection * uModel * (gl_in[0].gl_Position + vec4(x, 0.0) + vec4(y, 0.0));
vec4 begBL = uProjection * uModel * (gl_in[0].gl_Position - vec4(x, 0.0) - vec4(y, 0.0));
vec4 begBR = uProjection * uModel * (gl_in[0].gl_Position + vec4(x, 0.0) - vec4(y, 0.0));
gl_Position = begTL; // 0
points = begTL.xyz;
normalV = normalize(-x + y);
EmitVertex();
gl_Position = endTL; // 1
points = endTL.xyz;
normalV = normalize(-x + y);
EmitVertex();
gl_Position = begTR; // 2
points = begTR.xyz;
normalV = normalize(x + y);
EmitVertex();
gl_Position = endTR; // 3
points = endTR.xyz;
normalV = normalize(x + y);
EmitVertex();
gl_Position = begBR; // 4
points = begBR.xyz;
normalV = normalize(-x + y);
EmitVertex();
gl_Position = endBR; // 5
points = endBR.xyz;
normalV = normalize(x - y);
EmitVertex();
gl_Position = begBL; // 6
points = begBL.xyz;
normalV = normalize(x - y);
EmitVertex();
gl_Position = endBL; // 7
points = endBL.xyz;
normalV = normalize(-x - y);
EmitVertex();
gl_Position = begTL; // 8
points = begTL.xyz;
normalV = normalize(-x + y);
EmitVertex();
gl_Position = endTL; // 9
points = endTL.xyz;
normalV = normalize(-x + y);
EmitVertex();
}
Fragment Shader:
#version 150 core
vec3 intensities = vec3(0.2f,0.2f,0.2f);
in vec4 gColor;
in vec3 normalV;
in vec3 points;
out vec4 fColor;
void main() {
vec3 surfaceToLight = normalize(vec3(1,1,0));
float brightness = dot(normalV, surfaceToLight);
brightness = clamp(brightness, 0, 1);
vec4 surfaceColor = gColor;
fColor = vec4(brightness * intensities + surfaceColor.rgb * 0.5f, surfaceColor.a);
}
我传递的数据:
struct Vertex
{
Vector3 position;
uint32 thickness;
};
GLint positionAttribLocation = glGetAttribLocation(renderContext->branchesContext.program, "position");
glEnableVertexAttribArray(positionAttribLocation);
glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
GLint thicknessAttribLocation = glGetAttribLocation(renderContext->branchesContext.program, "thickness");
glEnableVertexAttribArray(thicknessAttribLocation);
glVertexAttribPointer(thicknessAttribLocation, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
答案 0 :(得分:2)
您正在使用不正确的函数为thickness
建立顶点属性指针。
glVertexAttribPointer(thicknessAttribLocation, 1, GL_UNSIGNED_INT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
厚度的类型为int
,您使用的是glVertexAttribPointer (...)
。该函数旨在将顶点数据转换为浮点数,仅适用于float
,vec<N>
和矩阵数据类型。
要正确地为您的顶点属性指定数据指针,您需要使用glVertexAttribIPointer (...)
。这将仅保留整数数据并将其直接传递给着色器,它适用于{u}int
和[i|u]vec<N>
属性。
glVertexAttribIPointer(thicknessAttribLocation, 1, GL_UNSIGNED_INT, sizeof(Vertex), (void*)(3 * sizeof(float)));
您会注意到此函数调用中只有一个参数,这是因为浮点归一化不是整数顶点属性的选项。