Basic Pixel/Vertex Shader example needed

Started by
3 comments, last by Headkaze 16 years ago
I'm having some problems trying to get a basic pixel/vertex shader to work. I'm not entirely sure but I think I'm not interpreting my Vertex format correctly. Here is my Vertex format (Position | Normal | Diffuse | Texture2)
public struct PositionNormalColored2Textured
{
	public Vector3 Position;
	public Vector3 Normal;
	public int Color;
	public float Tu1;
	public float Tv1;
	public float Tu2;
	public float Tv2;
}
Does that look like this in PS format?
struct PS_INPUT
{
    float4 Position : POSITION;
    float4 Normal   : NORMAL;
    float4 Color    : COLOR;
    float2 UV1      : TEXCOORD0;
    float2 UV2      : TEXCOORD1;
};
All I'd like to do is, say, invert the colors of a texture just to see if I can get it working. I'm using MDX 1.1 and need it in .fx file format. Anyone that can give me a simple example?
Advertisement
Vertex format has nothing to do with Pixel Shader, but with Vertex Shader. For example - pixels don't have normals.

Your vertex format Position | Normal | Diffuse | Texture would correspond to a Vertex Shader input structure in a form similar to this:

struct VS_INPUT{    float4 Position : POSITION;    float4 Normal   : NORMAL;    float4 Color    : COLOR;    float2 UV1      : TEXCOORD0;};


Your chosen testing task - invert the colors of a texture - is a task for pixel shader. If you want to do just this, you wont need to write any Vertex Shader at all, DirectX will use the fixed pipeline for vertex transformations.
You would just write a pixel shader with this input:
struct PS_INPUT{    float4 Diffuse  : COLOR;    float2 UV1      : TEXCOORD0;};

and use UV1 to do texture lookup, invert the color and return it as PS's output (or eventually combine it with diffuse color which also is in the input structure).
For texture lookup you'll need a sampler, a texture and then just the tex2D function.
Thanks for your reply Tom.

I (sort of) have things working. Well I can apply the pixel shader effect, but it's not doing what I want it to. It's not inverting the colors, well in effect it probably is, but it's just making the alpha channel pixels white and the colored pixels alpha. So I guess it is making it inverse, but is there any way to change this to ignore the alpha channel?

sampler TextureSampler;void InverseTextureColor( in float2 textureCoords : TEXCOORD0, out float4 diffuseColor : COLOR0){    // Get the inverse texture color    diffuseColor = 1.0f - tex2D(TextureSampler, textureCoords);};technique InverseTexture{    pass P0    {        // shaders        VertexShader = NULL;        PixelShader  = compile ps_1_1 InverseTextureColor();    }}
If you don't use alpha and it's always 1, you can just set it back like so:

diffuseColor = 1.0f - tex2D(TextureSampler, textureCoords);
diffuseColor.a = 1.0f;

If there is alpha channel in the texture and you want to keep it, you can do something like:

float4 texcolor = tex2D(TextureSampler, textureCoords);
diffuseColor = 1.0f - texcolor;
diffuseColor.a = texcolor.a;

Or more illustratively:

float4 texcolor = tex2D(TextureSampler, textureCoords);
diffuseColor = float4(1.0f - texcolor.r, 1.0f - texcolor.g, 1.0f - texcolor.b, texcolor.a);

Which clearly shows that Red, Green and Blue components are inverted, but Alpha is just reused.
Thanks again Tom, it works :)

This topic is closed to new replies.

Advertisement