XNA Game Development Forums
2012/05/18 06:19:35 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: What's wrong with my shader? :'(  (Read 2636 times)
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« on: 2007/03/06 04:13:42 »

The model doesn't show... And as you see I edited the basic effect from the HM tutorial...

Code:
float4x4 World;
float4x4 View;
float4x4 Project;
sampler TextureSampler;

float3 EyePosition;

bool UseDiffuse;
float4 diffuseColor;

bool UseAmbient;
float4 ambientColor;

bool UseLight;
float3 lightPosition;
float4 lightColor;

bool UseSpecular;
float specularPower;
float4 specularColor;

struct VS_INPUT {
    float4 Position : POSITION0;
    float2 Texcoord : TEXCOORD0;
    float3 Normal : NORMAL0;
};

struct VS_OUTPUT {
    float4 Position : POSITION0;
    float2 Texcoord : TEXCOORD0;
    float3 Normal : TEXCOORD1;
    float3 ViewDirection : TEXCOORD2;
    float3 LightDirection : TEXCOORD3;
};

VS_OUTPUT Process(VS_INPUT Input) {
    VS_OUTPUT Output;

    Output.Position = mul(Input.Position, World * View * Project);
    Output.Texcoord = Input.Texcoord;
    Output.Normal = mul(Input.Normal, World);
    Output.ViewDirection = EyePosition - Output.Position;
    Output.LightDirection = lightPosition - Output.Position;
    return Output;
}

struct PS_INPUT {
    float2 Texcoord : TEXCOORD0;
    float3 Normal : TEXCOORD1;
    float3 ViewDirection : TEXCOORD2;
    float3 LightDirection : TEXCOORD3;
};

float4 Material(PS_INPUT Input) : COLOR0 {
    float3 Normal = normalize(Input.Normal);
    float3 ViewDirection = normalize(Input.ViewDirection);
    float3 LightDirection = normalize(Input.LightDirection);

    float EdgeComponent = dot(Normal, ViewDirection);
   
    float4 TotalAmbient;
    if (UseAmbient)
        TotalAmbient = saturate(ambientColor * EdgeComponent);
    else
        TotalAmbient = (0.0f, 0.0f, 0.0f, 1.0f);

float NDotL = dot(Normal, LightDirection);
float4 DiffuseAverage;
float4 TotalDiffuse;

    if (UseLight)   
    {
        if (UseDiffuse)
        {
        DiffuseAverage = (diffuseColor * lightColor) * 0.5f;
        TotalDiffuse = saturate(DiffuseAverage * NDotL);
        }
        else
        {
        DiffuseAverage = lightColor * 0.5f;
        TotalDiffuse = saturate(DiffuseAverage * NDotL);
        }
    }
    else
    {
        if (UseDiffuse)
        {
            TotalDiffuse = diffuseColor * 0.5f;
        }
        else
        {
            TotalDiffuse = (0.0f, 0.0f, 0.0f, 1.0f);
        }
    }
   
    float4 TextureColor = tex2D(TextureSampler, Input.Texcoord);
    float4 TotalSpecular;
   
    if (UseSpecular)
    {
        float3 Reflection = normalize(2.0f * NDotL * Normal - LightDirection);
        float RDotV = max(0.0f, dot(Reflection, ViewDirection));
        TotalSpecular = saturate(specularColor * pow(RDotV, specularPower));
    }
    else
    {
        TotalSpecular = (0.0f, 0.0f, 0.0f, 1.0f);
    }
   
    if (TextureColor.a == 0.0f) TextureColor.a = 1.0f;
   
    float4 FinalColor = saturate(TotalAmbient + TotalDiffuse + TextureColor + TotalSpecular);
    return FinalColor;
};

technique ProcessMaterial {
    pass P0 {
        VertexShader = compile vs_2_0 Process();
        PixelShader  = compile ps_2_0 Material();
    }
}
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #1 on: 2007/03/06 06:08:15 »

How are you passing the World, View and Projection parameters?
Logged
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #2 on: 2007/03/06 11:53:52 »

They're all ok, I'm passing them in my shader class. I know they're ok because I can render models with the transform texture shader in the tutorial 3. Of course, i changed the shader to pass world, view and projection separate.

Code:
if (shader.Parameters["World"] != null)
            {
                Matrix world = Matrix.CreateScale(Object.Scaling) *
                    Matrix.CreateFromQuaternion(Object.Rotation) *
                    Matrix.CreateTranslation(Object.Position);

                shader.Parameters["World"].SetValue(world);
            }

etc...
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #3 on: 2007/03/06 13:13:02 »

When you say your model does not show at all, do you mean there is nothing there or that you just get a black blob for the model?
Logged
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #4 on: 2007/03/06 14:28:25 »

I don't see it at all...
Logged
Nemo Krad
Global Moderator
Hero Member
*****
Offline Offline

Posts: 512


I have seen the fnords


View Profile WWW
« Reply #5 on: 2007/03/06 15:28:24 »

You had a few issues there, I have altered the Vertex shader to get it to work. I commented out the bits that upset the rendering of the models.

Code:
VS_OUTPUT Process(VS_INPUT Input) {
    VS_OUTPUT Output;

    //Output.Position = mul(Input.Position, World * View * Projection);   
    Output.Position = mul(Input.Position,mul(mul(World, View), Projection));
    Output.Texcoord = Input.Texcoord;
    Output.Normal = mul(Input.Normal, World);
    //Output.ViewDirection = EyePosition - Output.Position;
    Output.ViewDirection = EyePosition - mul(Input.Position, World);   
    //Output.LightDirection = lightPosition - Output.Position;
    Output.LightDirection = lightPosition - mul(Input.Position, World);
   
    return Output;
}

Also you declare Projection as Project, don't know if this was intensional or not, but my engine uses Projection for this parameter so altered it in the above fix.

Hope this helps Smiley
Logged
EclipsE
Full Member
***
Offline Offline

Posts: 111


View Profile
« Reply #6 on: 2007/03/06 15:43:17 »

Now it works! You deserve a million thank yous!

 Grin

I'm so happy!
Logged
Pages: [1]
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Shader compiler General Discussion endre 2 2367 Last post 2006/08/12 01:24:40
by Chr0n1x
Shader implemented fixed function Hazy Mind XNA Engine mikeschuld 2 2358 Last post 2006/11/07 02:07:22
by mikeschuld
Shader tutorials General Discussion endre 2 2240 Last post 2006/12/22 18:12:48
by mikeschuld
My Shader Hell... General Discussion « 1 2 » Nemo Krad 17 5360 Last post 2007/02/09 21:12:14
by Chr0n1x
Bumpmap Shader General Discussion « 1 2 3 4 5 » Nemo Krad 70 21674 Last post 2007/09/06 13:15:22
by Nemo Krad
Point Light Shader General Discussion Nemo Krad 0 2034 Last post 2007/03/18 07:18:13
by Nemo Krad
Model & Shader Problems Hazy Mind XNA Engine Themodem 5 2064 Last post 2007/03/26 11:12:53
by Tiago
Playing with lighting : intermediate shader techniques? Hazy Mind XNA Engine 5parrowhawk 3 2297 Last post 2009/03/18 23:04:27
by Chr0n1x
Powered by MySQL Powered by PHP Powered by SMF 1.1.12 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.065 seconds with 18 queries.