获取片段着色器中的顶点位置

时间:2015-02-10 16:50:47

标签: opengl opengl-es glsl webgl

出于某种原因,我需要获得在片段着色器中形成基元(三角形)的所有顶点的位置。

我有三种方法可以做到:附加属性,制服和via几何着色器。

属性

// vertex shader
in vec3 vPosition;
in vec3 vposA;
in vec3 vposB;
in vec3 vposC;
out vec3 posA;
out vec3 posB;
out vec3 posC;

void main() {
    // ....
    posA = vposA;
    posB = vposB;
    posC = vposC;
}

问题是我需要发送额外的属性,这意味着在VBO中使用额外的内存。

制服

// fragment shader
uniform vec3 posA;
uniform vec3 posB;
uniform vec3 posC;

void main() {
    // ...
}

主要缺点显然是需要为每个绘制的三角形绑定制服,所以我只能为每个绘制一个三角形画一个三角形。

GS

layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

out vec3 posA;
out vec3 posB;
out vec3 posC;

void main()
{
    posA = gl_in[0].gl_Position.xyz;
    posB = gl_in[1].gl_Position.xyz;
    posC = gl_in[2].gl_Position.xyz;

    gl_Position = gl_in[0].gl_Position;
    EmitVertex();
    gl_Position = gl_in[1].gl_Position;
    EmitVertex();
    gl_Position = gl_in[2].gl_Position;
    EmitVertex();
    EndPrimitive();
}

使用GS我不需要在内存中存储任何内容,我可以绘制我想要的三角形,但问题是正在使用整个新的着色器阶段。

我还考虑使用flat关键字,但这不会在这里做。

问题是,有没有其他选择,我可能会遗漏哪些?

感谢。

1 个答案:

答案 0 :(得分:3)

几何着色器是最实用的方法,但通常会排除WebGL。

您可以考虑从可编程顶点拉动中获取一个页面,其中您的实际顶点数据存储在缓冲区纹理中,并使用索引来查找位置值。我无法对性能发表评论,但这样做需要 显着 减少每个顶点的存储空间。

以下是原始基于属性的尝试的修改版本:

// vertex shader
in int vIdxA; // Index of vtx 0 in current triangle
in int vIdxB; // Index of vtx 1 in current triangle
in int vIdxC; // Index of vtx 2 in current triangle

out vec3 posA;
out vec3 posB;
out vec3 posC;

uniform samplerBuffer vtx_buf; // Actual vertex position array, as a buffer texture

void main() {
    int vtx = gl_VertexID % 3;

    // ....
    posA = texelFetch (vtx_buf, vIdxA);
    posB = texelFetch (vtx_buf, vIdxB);
    posC = texelFetch (vtx_buf, vIdxC);

    if (vtx == 0)
        gl_Position = posA;
    else if (vtx == 1)
        gl_Position = posB;
    else
        gl_Position = posC;
}

实施后,这也将排除WebGL,但是这种方法应该比基于几何着色器的方法更容易适应OpenGL ES。

相关问题