我将一条线传递给几何着色器并输出一个长方体。我通过在 x 或 y 方向上添加一个恒定的“厚度”来在该线的每一端创建 4 个新点。如果我在几何着色器中设置厚度,它可以正常工作,但是当我尝试将它作为顶点数据的一部分传递给着色器时,我似乎无法在几何着色器中访问它。当我
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();
}
片段着色器:
#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)));