Question

I want to draw circular splats, however my only data is the vertex position. So I draw the points in C++ and they appear at the right position. My point size is set to 20pixels, thus the points are big enough to discard some pixels to get a circle.

I thought I could send the vertex pixel position from the vertex shader to the fragment shader and then calculate the distance between the fragment position and the vertex position. This results in circular splats, however most of them will be hidden. What am I doing wrong?

CG Vertex Shader:

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)

Was it helpful?

Solution

When calculating the position in screen space from the vertex shader, remember that the graphics pipeline performs the so called perspective division to convert the point in clip-space (x,y,z,w) to normalized device coordinates (x/w,y/w,z/w,1). Only then will the position be in the range of [-1,1] and is your transformation to screen space correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top