Jump to content
  • Advertisement
Sign in to follow this  
Ambo

clipping planes

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, this is probably an easy one but I've been looking for a couple of days now and have found nothing. All I want to do is to clip points (or preferably pixels) with Z <= 0. That's it. I'm Using C# and HLSL and I've tried clip(x) (I assume this maps to the texkill command) in the pixel shader but it just crashes if the value is anything other than positive. I've also tried setting clipping planes but cannot find out anywhere how to do it. I've setup a plane (0,0,1,0) and transformed it into clip space as I'm using the programmable pipeline but how do I actually use it? The device.clipplanes property is read only so I assume it is set as a render state or somthing but could only find the clipping and clipplaneenable properties there. I'm sure it's fairly easy but I just can't find any decent info on it! What I'm doing by the way is creating an upsidedown image for local refections on water, that's why I need to clip the stuff below z=0 (underwater). Thanks for any help you can give.

Share this post


Link to post
Share on other sites
Advertisement
I can't figure out why clip (== texkill) doesn't work within the shader, what is exactly the problem ? Maybe it's with your driver or graphic card? i'm sorry i can't really help you with the clip planes though. But the clip(z) usually does just fine.

Share this post


Link to post
Share on other sites
i read your post wrong. I know what your talking about and will get back to it soon as I remember reading how this is done. But basically you want to make your water plane a clip plane right?

Zwig

Share this post


Link to post
Share on other sites
AFAICR, setting a clip plane (assuming it's in the correct space) and enabling the render state should be all that's required for the hardware to start clipping; that definately works in the fixed function pipeline, and should work with the programmable pipeline.


To use clip() in HLSL with plane in world space, do something like:


struct VS_OUTPUT
{
...
float3 ClipDistance : TEXCOORD1;
...
}

...

VS_OUTPUT VertexShader( blah )
{
VS_OUTPUT Out = (VS_OUTPUT)0;

// your plane
const float3 PlaneABC = float3(blah, blah, blah);
const float PlaneD = blah;

...

// transform the vertex position from object space into world space
float3 WorldPos = mul(float4(Pos, 1), (float4x3)World);

...

// compute the distance along the plane normal from the plane to the vertex
float d = dot(WorldPos, PlaneABC) - PlaneD;
Out.ClipDistance = d;

...
}


float4 PixelShader( blah, float3 ClipDistance : TEXCOORD1, blah ) : COLOR
{
// discard this pixel if it's behind the plane
clip( ClipDistance );

return blah;
}



I find it odd that clip() is crashing for you though! - are you sure it's not a shader compilation issue that's crashing your application?

Share this post


Link to post
Share on other sites
Quote:
Original post by Zwigby
basically you want to make your water plane a clip plane right?


Correct.


that code fragment is almost exactly what I'm doing but when I first tried it, it just crashed. I tried with a constant value of 1 and it runs fine (but of course nothing gets clipped) but if I try with a constant value of -1 it crashes again.

I'm using a raadeon 9800 Pro with the latest drivers so there should be no problems there.

Is .fx code compiled at compile time with the rest of my C code or is it later when the .fx file is acually loaded? If it's later then it might be a compilation issue, yes.

Here is the pixel shader code:

void blah(
in float4 pos : POSITION,
in float4 col : COLOR0,
in float2 alphaTexCoords : TEXCOORD0,
in float2 texCoords : TEXCOORD1,
in float2 worldZ : TEXCOORD2,
out float4 Diff : COLOR0 )
{
clip(worldZ.x);

float4 r0 = tex2D(TextureSamplerGrass, texCoords);
float4 r1 = lerp(r0, tex2D(TextureSamplerSeaFloor, texCoords), tex2D(TextureSamplerAlpha, alphaTexCoords).r);
float4 r2 = lerp(r1, tex2D(TextureSamplerSand, texCoords), tex2D(TextureSamplerAlpha, alphaTexCoords).g);
float4 r3 = lerp(r2, tex2D(TextureSamplerCliff, texCoords), tex2D(TextureSamplerAlpha, alphaTexCoords).b);
Diff = col * lerp(r3, tex2D(TextureSamplerDirt, texCoords), tex2D(TextureSamplerAlpha, alphaTexCoords).a);

}

worldZ.x contains the z coordinate of the pixel (i.e. the height) in world space so if it's less than 0 it should be clipped.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!