在几何着色器中使用 gl_ClipDistance
我试图在几何着色器中使用 gl_ClipDistance
但我无法让它工作。 我的着色器在变换反馈记录中运行,我想根据 4 个剪切平面剪切三角形。
我阅读了 GLSL lang 规范并试图找到一个完整的示例(没有找到任何)。这是我的着色器的样子(缩短):
#version 150
#extension GL_EXT_gpu_shader4 : require
#extension GL_EXT_geometry_shader4 : require
#pragma optionNV unroll all
layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;
in vec4 io_Position[];
in vec3 io_Normal[];
in vec4 io_MultiTexCoord0[];
in vec4 io_MultiTexCoord1[];
...
out vec3 out_Position;
out vec3 out_Normal;
out vec2 out_MultiTexCoord0;
out vec2 out_MultiTexCoord1;
out float gl_ClipDistance[4]; // do I have to initialize it like that?
void main()
{
...
// emit three vertices
for (int v=0; v<gl_VerticesIn; v++)
{
out_Position = io_Position[v].xyz;
out_Normal = io_Normal[v];
out_MultiTexCoord0 = io_MultiTexCoord0[v].xy;
out_MultiTexCoord1 = io_MultiTexCoord1[v].xy;
// calculate clip distances
for(int c=0; c<4; c++)
{
// From spec: The clip distances will be linearly interpolated across the
// primitive and the portion of the primitive with interpolated distances less than
gl_ClipDistance[c] = distancePlane(clipPlanes[c], triWorldPos[v]);
}
EmitVertex();
}
// create triangle
EndPrimitive();
}
在开始变换反馈之前,我设置:
glEnable(GL_CLIP_PLANE0);
glEnable(GL_CLIP_PLANE1);
glEnable(GL_CLIP_PLANE2);
glEnable(GL_CLIP_PLANE3);
我的planeDistance()返回正距离和负距离,具体取决于顶点所在的一侧。我什至尝试向某些 gl_ClipDistance[c]
写入负值,但它不会剪辑任何内容。
我做错了什么?
I'm trying to make use of gl_ClipDistance
within a geometry-shader but I cannot get it to work.
My shader runs within a transform-feedback recording and I want to cut triangles against 4 clipping planes.
I read the GLSL lang spec and tried to find a complete example (didn't find any). Here is what my shader looks like (shortened):
#version 150
#extension GL_EXT_gpu_shader4 : require
#extension GL_EXT_geometry_shader4 : require
#pragma optionNV unroll all
layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;
in vec4 io_Position[];
in vec3 io_Normal[];
in vec4 io_MultiTexCoord0[];
in vec4 io_MultiTexCoord1[];
...
out vec3 out_Position;
out vec3 out_Normal;
out vec2 out_MultiTexCoord0;
out vec2 out_MultiTexCoord1;
out float gl_ClipDistance[4]; // do I have to initialize it like that?
void main()
{
...
// emit three vertices
for (int v=0; v<gl_VerticesIn; v++)
{
out_Position = io_Position[v].xyz;
out_Normal = io_Normal[v];
out_MultiTexCoord0 = io_MultiTexCoord0[v].xy;
out_MultiTexCoord1 = io_MultiTexCoord1[v].xy;
// calculate clip distances
for(int c=0; c<4; c++)
{
// From spec: The clip distances will be linearly interpolated across the
// primitive and the portion of the primitive with interpolated distances less than
gl_ClipDistance[c] = distancePlane(clipPlanes[c], triWorldPos[v]);
}
EmitVertex();
}
// create triangle
EndPrimitive();
}
Before I start the transform-feedback I set:
glEnable(GL_CLIP_PLANE0);
glEnable(GL_CLIP_PLANE1);
glEnable(GL_CLIP_PLANE2);
glEnable(GL_CLIP_PLANE3);
My planeDistance()
returns positive and negative distances depending on which side the vertex lies. I even tried writing negative values to some of the gl_ClipDistance[c]
, but it doesn't clip anything.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变换反馈输出在几何着色器之后、视口变换或针对平截头体或自定义剪切平面进行剪切之前立即发生。您无法将剪裁后的三角形放入变换输出缓冲区中。
如果您只是使用此缓冲区反馈到管道以供稍后渲染,则可以在此时进行剪辑。或者,如果您需要在变换反馈缓冲区中剪裁三角形,您可以尝试在几何着色器中自己进行剪裁(如果需要的话,精确匹配 GL 的内置剪裁将很困难)。
Transform feedback output happens immediately after the geometry shader, before the viewport transform or clipping against the frustum or custom clip planes. You can't get post-clipping triangles into a transform-output buffer.
If you're just using this buffer to feed back into the pipeline for later rendering, you can do the clipping at that point. Or, if you need clipped triangles in the transform feedback buffer, you can try doing the clipping yourself in the geometry shader (matching GL's built-in clipping exactly would be difficult, if that's a requirement).