O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying HLSL Using Textures

Coloring a rectangle red requires only the simplest HLSL shader, and it’s something you’ll rarely find in the latest video games. Typically, you’ll be applying a texture to an object and then tweaking the way the texture appears by applying shininess or fog or some other effect.

In this section, you’ll apply a custom HLSL file to your rectangle while applying color from the trees texture as well.

Open your Red.fx file again and replace the code in the file with the code shown here:

float4x4 xWorldViewProjection;

Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state
{ texture = <xColoredTexture> ;
magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR;
AddressU = mirror; AddressV = mirror;};struct VertexIn
{
    float4 position : POSITION;
    float2 textureCoordinates : TEXCOORD0;
};

struct VertexOut
{
    float4 Position : POSITION;
    float2 textureCoordinates : TEXCOORD0;
};

VertexOut VertexShaderFunction(VertexIn input)
{
    VertexOut Output = (VertexOut)0;
    Output.Position =mul(input.position, xWorldViewProjection);
    Output.textureCoordinates = input.textureCoordinates;

    return Output;
}

float4 PixelShaderFunction(VertexOut input) : COLOR0
{
    float4 output = tex2D(ColoredTextureSampler,
        input.textureCoordinates);
    return output;
}

technique Textured
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 VertexShaderFunction(  );
        PixelShader = compile ps_2_0 PixelShaderFunction(  );
    }
}

Even though you’re still new to HLSL, this file isn’t incredibly complex, and for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required