Displaying a circle with a pixel shader in DirectX

I would like to make a circle into a couple of triangles using a pixel shader written in HLSL. There is some pseudocode here for this , but I am resorting to one problem after another while implementing it. This will work on Windows RT, so I'll limit myself to DirectX 9.3 API Direct3D v11 members.

What is the general shape of a shader? For example, what parameters should be passed to the main method?

Any advice or working code would be greatly appreciated!

+3


source to share


1 answer


You have to use texture coordinates as inputs to display the parametric circle.

Here is the question I asked about the anti-aliasing of a circle represented in HLSL. Here is the code from that question with a slight change to make it TEXCOORD0

more clear to use :

float4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
    float dist = texCoord.x * texCoord.x
               + texCoord.y * texCoord.y;
    if(dist < 1)
        return float4(0, 0, 0, 1);
    else
        return float4(1, 1, 1, 1);
}

      



He uses the formula for the circle, which is rΒ² = xΒ² + yΒ² . It uses a constant 1

for the square of the radius (i.e., a circle of radius 1, measured in texture coordinates). All points inside the circle are colored black, and the outer ones are white.

To use it, specify triangles with texture coordinates in the range [-1, 1]. If you want to use the more traditional [0, 1], you have to include some code to scale and shift coordinates in the shader, or you end up with a quarter circle.

After that, you can experiment to add other features (like anti-aliasing in my related question).

+2


source







All Articles