从顶点位置绘制圆形splats

时间:2014-04-04 10:55:41

标签: opengl shader fragment-shader vertex-shader cg

我想绘制圆形splats,但我唯一的数据是顶点位置。所以我用C ++绘制点,它们出现在正确的位置。我的点大小设置为20像素,因此这些点足以丢弃一些像素以获得圆圈。

我以为我可以将顶点像素位置从顶点着色器发送到片段着色器,然后计算片段位置和顶点位置之间的距离。这导致圆形splats,但是大多数将被隐藏。我做错了什么?

CG顶点着色器:

void main(
    float4 pvec: POSITION,

    uniform float4x4 modelView,
    uniform float4x4 modelViewIT,
    uniform float4x4 modelViewProj,
    uniform float2 wsize,
    uniform float near,
    uniform float top,
    uniform float bottom,

    out float4 pout: POSITION,
    out float4 color: COLOR,
    out float2 vpos: TEXCOORD0
)
{
    //position of point correctly projected
    pout = mul(modelViewProj, pvec);

    vpos = float2((pout.xy * 0.5 + 0.5) * wsize);
    color = float4(0.0, 0.0, 1.0, 1.0);
}

CG Fragment Shader:

void main(
    float4 col : COLOR, 
    float2 wpos : WPOS,
    float2 vpos: TEXCOORD0,

    uniform float2 unproj_scale, 
    uniform float2 unproj_offset,
    uniform float f_near,
    uniform float zb_scale, 
    uniform float zb_offset,
    uniform float epsilon,

    out float4 colorout : COLOR
)
{
    float xDiff = abs(vpos.x - wpos.x);
    float yDiff = abs(vpos.y - wpos.y);

    if ((xDiff * xDiff + yDiff * yDiff) > 20.0) {
        col.r = 1.0;
    }

    colorout = col;
}

Screenshot, blue circles, purple is discarded (now just colored) Screenshot, blue circles, purple is discarded (now just colored)

1 个答案:

答案 0 :(得分:1)

从顶点着色器计算屏幕空间中的位置时,请记住图形管道执行所谓的透视分割,以将剪辑空间(x,y,z,w)中的点转换为规范化设备坐标(x/w,y/w,z/w,1)。只有这样,该位置才会在[-1,1]的范围内,并且您对屏幕空间的转换是正确的。