XNA Meeting Point
  • Home
  • English
    • FAQ
    • Site Architecture
    • Who we are
    • Contact Us
    • XNA Tutorials
    • XNA-inspired technology>
      • MonoGame Tutorials
    • Game Development
    • Math
    • Physics
    • Computer Science>
      • Programming
      • Data Transmission
      • Hardware
    • Useful Links
    • Ressources
    • Community Spotlight
    • Game Renders
    • News Blog
  • Français
    • FAQ
    • Plan du site
    • Qui sommes-nous ?
    • Contactez-nous
    • Tutoriaux XNA
    • Technos inspirées de XNA>
      • Tutoriaux MonoGame
    • Développement de jeux
    • Maths
    • Physique
    • Informatique générale>
      • Programmation
      • Transmission de données
      • Hardware (machine)
    • Liens utiles
    • Ressources
    • Community Spotlight
    • Captures d'écran
    • Nouveautés / Blog
  • Español
    • FAQ
    • Estructura del sitio
    • ¿Quiénes somos?
    • Contactarnos
    • Tutoriales XNA
    • Desarrollo de videojuegos
    • "Links" utiles
    • Recursos
    • Community Spotlight
    • Capturas de pantalla
    • Actualidad / Blog
  • Search
  • Tutorial Contests
    • XTC 2010
  • XN'net
    • English
    • Français
    • Español
  • YouTube
  • About me
    • School Projects>
      • Numerical Analysis (Python)
      • C Programming
      • Lisp Programming
      • Presentations
    • GCC Projects>
      • GCC Presentations
    • Pollen Hunt
  • RSS
    • XNA News!
    • Other Projects

Post Process Negative

by Petri Wilhelmsen
Dark Codex Studios
Microsoft XNA MVP
author's website
Photo
Adapted to XNA 4.0 by A. Nadif. To see the original article for XNA 3.1, follow this link.
This tutorial builds on Tutorial 9, post process wiggling. If you haven't read that yet, you should in order to fully understand what's going on here.

The source-code and binaries can be found in the end of the chapter.

Negative Image

This shader is also very simple, but, I got a lot of questions about this so I decided I wanted to write a short tutorial on it.. and it's really short ;)
To get the color from a texture sampler, you usually do this:
float4 Color = tex2D(ColorMapSampler, Tex); 
To get the negative color, you simply subtracts each channel on Color with one, or, simply:
float4 ColorInverse = 1.0f - tex2D(ColorMapSampler, Tex); 
And, thats it!

Implementing the shader

Let's start by implementing the shader. Its pretty short so I'll just add the code:
sampler ColorMapSampler : register(s0);
// Negative image
float4 PixelShaderGo(float2 Tex: TEXCOORD0) : COLOR
{
    float4 Color = 1.0f - tex2D(ColorMapSampler, Tex); 
 
    // Keep our alphachannel at 1.
    Color.a = 1.0f;
  
    return Color;
}
technique PostProcess
{
    pass P0
    {
        // A post process shader only needs a pixel shader.
        PixelShader = compile ps_2_0 PixelShaderGo();
    }
}
First, we subtract the color from our sampler, from 1.0f to get the inverse.
But, when doing this, we are inverting the Alpha channel as well. If this is what you want, go ahead, but if you want to keep the alpha-values from the original image, you must set it back to what it was, or set it to something manually.
In this case, we want the image to have the alpha of 1.0, so we set the a component of Color to 1.0, making our image fully opaque if we have shading enabled.

That's it for this tutorial. I'll keep adding some post process effects during the next days so keep coming back :)

Download Sample
Powered by Create your own unique website with customizable templates.