The effect file

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/The_effect_file.php

This particular tutorial has caused me a bit of a headache, the question I have to ask myself is, do we continue to use the effect file or go down the route of using a basic effect? The former meaning we may run into other problems in later tutorials meaning I need to rewrite parts of it again for 4.0. The latter risking having to create some new tutorials later when we need an effects file!

Eventually I have decided to go down the route of using the effects file, to keep in sync with the tutorials.

So the first problem I cam across with the effects file was that it didn’t compile due to XNA’s minimum requirement of pixel shader 2.0. So to fix this I opened up effects.fx in Visual Studio and changed each occurence of

PixelShader  = compile ps_1_1

to

PixelShader = compile ps_2_0

So this will instruct the compiler to only use Pixel Shader 2!

Next I had to remove the point sprite code from effects.fx since XNA 4.0 no longer supports point sprites for a number of reasons.

Rather than try to explain them see Shawn Hargreaves blog at
http://blogs.msdn.com/b/shawnhar/archive/2010/03/22/point-sprites-in-xna-game-studio-4-0.aspx

But for the purposes of this particular series we don’t need point sprites, if im not mistaken we only need them for series 2 in the flight sim when creating bullets so I’ll still need to find a workaround when I get to that point!

So that means removing everything after the following line:

//——- Technique: PointSprites ——–

And last but not least later in this series we are  required to use the Colored technique but VertexPositionColor is not compatible with the Colored technique due to the ColoredVS method expecting normal data and we will also use a custom vertex structure in another tutorial, so to maintain compatibility with both we will create another technique to help us out with that.

So what needs to be done is copy all the code in the section:

//——- Technique: Colored ——-

and replace each occurence of Colored with ColoredNormal in the pasted code

now the original code for the Colored technique we want to change from

VertexToPixel ColoredVS( float4 inPos : POSITION, float4 inColor: COLOR, float3 inNormal: NORMAL)
{
VertexToPixel Output = (VertexToPixel)0;
float4x4 preViewProjection = mul (xView, xProjection);
float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);

Output.Position = mul(inPos, preWorldViewProjection);
Output.Color = inColor;

float3 Normal = normalize(mul(normalize(inNormal), xWorld));
Output.LightingFactor = 1;
if (xEnableLighting)
Output.LightingFactor = saturate(dot(Normal, -xLightDirection));

return Output;
}

to this:

VertexToPixel ColoredVS( float4 inPos : POSITION, float4 inColor: COLOR)
{
VertexToPixel Output = (VertexToPixel)0;
float4x4 preViewProjection = mul (xView, xProjection);
float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);

Output.Position = mul(inPos, preWorldViewProjection);
Output.Color = inColor;

Output.LightingFactor = 1;

return Output;
}

What we did there was to remove the parameter  float3 inNormal: NORMAL from the method signature and any code that made use of that parameter!

And that’s it for effects.fx

The completed file can be downloaded from http://jump.fm/SHGKH

Now for the changes to the effects API

the lines effect.Begin(); and effect.End(); are no longer necessary!

also when we iterate through the effect passes the methods pass.Begin() and pass.End() have been simplified into one statement pass.Apply() so the loop

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();

pass.End();
}
effect.End();

becomes

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

}

Nice and easy that part! So that concludes this tutorial, next step drawing a triangle.

Advertisements

2 thoughts on “The effect file

  1. thanks for taking some time updating reimers tutorial to xna 4.0. this is a big help especially for Noob’s. hope you do too on other popular tutorials.

    1. It’s great to hear that this has been a help! Assuming you mean Riemer’s other tutorials when you say “other popular tutorials” I will be working on some of those in the next week or so.

      I just want to read through the flight simulator project and get a feel for it before I dive in since this is a bigger challenge as I think writing the code for the bullets is going to require a completely different approach, due to point sprites being removed. As my old sales employer used to drum in to us Prior Planning Prevents Piss Poor Performance!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s