Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm reading riemers tutorial on how to make a water effect and trying to translate it from xna 3 to xna 4. So far I figured out I have to create a shader in order to clip the planes for my refraction map. I'm new to hlsl and how to implement custom shaders into my program, so I am wondering what I am doing wrong here.

  float4x4 World; 
  float4x4 View; 
  float4x4 Projection; 

  float4 ClipPlane0; 

  void vs(inout float4 position : POSITION0, out float4 clipDistances : TEXCOORD0) 
  { 

clipDistances.y = 0; 
clipDistances.z = 0; 
clipDistances.w = 0; 

position = mul(mul(mul(position, World), View), Projection); 

    clipDistances.x = dot(position, ClipPlane0); 
  } 

  float4 ps(float4 clipDistances : TEXCOORD0) : COLOR0 
  { 
clip(clipDistances); 

return float4(0, 0, 0, 0); // TODO: whatever other shading logic you want 
  } 

  technique 
 { 
pass 
{ 
    VertexShader = compile vs_2_0 vs(); 
    PixelShader = compile ps_2_0 ps(); 
} 
  } 

This is the fx file that I found on an xna forum just for reference.

Major edit:

I was able to get the program running successfully but when I viewed the saved screenshot of the refraction map, nothing was clipped.

    public void DrawRefractionMap(Effect clipEffect, Camera camera, GraphicsDevice device)
    {
        Plane refractionPlane = CreatePlane(waterHeight + 1.5f, new Vector3(0, -1, 0), camera, false);
        clipEffect.Parameters["ClipPlane0"].SetValue(new Vector4(refractionPlane.Normal, refractionPlane.D));

        device.SetRenderTarget(refractionRenderTarget);

        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1, 0);

        foreach (EffectPass pass in clipEffect.CurrentTechnique.Passes)
        {
            pass.Apply();

            DrawTerrain(clipEffect, camera, device);
        }

        device.SetRenderTarget(null);
        refractionTexture = (Texture2D)refractionRenderTarget;

        FileStream stream = File.OpenWrite("Screenshot33.png");

        refractionTexture.SaveAsJpeg(stream, refractionTexture.Width, refractionTexture.Height);
    }
share|improve this question

closed as off-topic by Anko, Vaughan Hilts, Josh Petrie Dec 15 '14 at 17:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Anko, Vaughan Hilts, Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

You seem to be only setting the clip distance.x value also where is this clip function defined maybe that can lend some light on the subject?

If memory serves, reimers approach was to use a plane and effectively reflect / refract the scene off that.

Then combining the reflection and reflection and refraction maps with some peturbation results in water.

The shader code shouldn't need updating at all since you can still use them as they are (its perfectly valid to use an older shader model with newer cpu code which it looks like your rewrite is doing anyway).

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.