通过非均匀阵列着色器 [英] pass non uniform array to shader

查看:158
本文介绍了通过非均匀阵列着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ckecks如果当前片段是一个多边形内片段着色器的想法。该多边形顶点值应该由float数组被转移到该着色器。问题是彩车的均匀阵列被丢弃(位置= -1),因为它不直接使用着色器。

I have an idea of fragment shader that ckecks if current fragment is inside a polygon. Vertex values of that polygon are supposed to be transfered to that shader by an array of floats. Problem is that uniform array of floats gets dropped (location = -1) since it is not directly used by shader.

我不知道该怎么数组由其他方法均匀值传递给着色器。有人能告诉我到一个更好的解决方案?

I don't know how to pass an array to a shader by other method as by uniform values. Can someone direct me to a better solution?

TIA。

编辑:(更好的解释)

我有一些重多边形的present顶点浮点值的数组。我计算最小值最大值从阵列,并通过这两个(最小/最大)向量几何着色器的哪个多边形下产生四。现在,我的片段着色器应该采取多边形顶点数组,如果它是多边形内部分析每个片段。如果里面,得出了一些颜色,如果外面画一些其他的颜色。

I have an array of float values which represent vertices of some polygon. I compute min max from that array and pass those two (min/max) vectors to geometry shader which produces a quad under that polygon. Now, my fragment shader is supposed to take that array of polygon vertices and analyze each fragment if it is inside that polygon. If inside, draw some color, if outside draw some other color.

顶点:

in vec3 inPosition;

void main(void)
{
    gl_Position = vec4(inPosition, 1.0);
}

几何:

layout(lines) in;
layout(triangle_strip) out;
layout(max_vertices = 4) out;

uniform mat4 u_proj;

void main()
{

    vec4 pos0 = u_proj * gl_in[0].gl_Position;
    vec4 pos1 = u_proj * gl_in[1].gl_Position;

    //left up
    gl_Position.x = pos0.x;
    gl_Position.y = pos1.y;
    gl_Position.w = pos0.w;
    EmitVertex();
    //left down
    gl_Position = pos0;
    EmitVertex();
    //right up
    gl_Position = pos1;
    EmitVertex();
    //right down
    gl_Position.x = pos1.x;
    gl_Position.y = pos0.y;
    gl_Position.w = pos1.w;
    EmitVertex();
    EndPrimitive();
}

片段:

uniform float points[570];

out vec4 outColor;

void main()
{
    int nvert = 570;

    float testx = gl_FragCoord.x;
    float testy = gl_FragCoord.y;

    int i;
    int j;
    bool c = false;
    for (i = 0, j = nvert - 3; i < nvert; i = i + 3, j = i)
    {
            if (((points[i + 1]>testy) != (points[j + 1] > testy)) && (testx < (points[j] - points[i]) * (testy - points[i + 1]) / (points[j + 1] - points[i + 1]) + points[i]))
            {
                    c = !c;
            }
    }

    if (c == true)
    {
            outColor = vec4(1.0, 1.0, 1.0, 1.0);
    }
    else
    {
            outColor = vec4(0.0, 0.0, 0.0, 1.0);
    }
}

这些:

m_FILL_uniProjId = glGetUniformLocation(m_FillProgram, "u_proj");
m_FILL_uniCount = glGetUniformLocation(m_FillProgram, "count");
m_FILL_uniPoints = glGetUniformLocation(m_FillProgram, "points");

每个返回-1。

这是我如何填写我的均匀阵列:​​

this is how i fill my uniform array:

for (int i = 0; i < 570; ++i)
{
    glUniform1fv(m_FILL_uniPoints + i, 1, &m_Points->at(i));
}

'570'只是在我的测试场景中使用,因为我知道多边形的大小,我使用的是临时值。

'570' is just a temporary value used in my test scene as i know the size of polygon i use.

推荐答案

您可以通过多边形顶点从几何着色器不同的

You can pass polygon vertices as varying from geometry shader

这篇关于通过非均匀阵列着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆