Sign in to follow this  
Freakdesign

Changing Light when Object is scaled?? using shaders

Recommended Posts

Hello ok heres my problem... i create a mesh from xfile... fine i place it in the world....fine i use Vertex/Pixelshader 3.0, but im very new to that :) it changes Lightning when scaled... scale bigger gets brighter scale smaller gets darker with FixedFunction Pipe i can use Renderstate.NormalizeNormals how can i use that in my shader? IMG
static const int MAX_LIGHTS = 20;

//--------------------------------------------------------------------------------------
//	Variables from Outer Space
//--------------------------------------------------------------------------------------

//Matrizen
float4x4 eff_World			: WORLD;		// World
float4x4 eff_VP				: VIEWPROJECTION;	// View + Projektion

//Materials
float4 eff_MaterialAmbient	: MATERIALAMBIENT;		// Material's ambient color
float4 eff_MaterialDiffuse	: MATERIALDIFFUSE;		// Material's diffuse color

//Light
float4 eff_LightAmbient;					// Ambient Light
float4 eff_LightDiffuse[MAX_LIGHTS];		// Array of  Light's diffuse color
float4 eff_LightPosition[MAX_LIGHTS];		// Array Light's Position in world space


//Texture
bool eff_HasTexture;
texture eff_Texture1;           // Color texture for mesh

int eff_NumberLights = 1;




//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler =  sampler_state
{
    Texture = <eff_Texture1>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
struct VSTEXTURE_OUTPUT
{
    float4 Position : POSITION;
    float4 Diffuse  : COLOR;
    float2 TexCoord : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------




//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
VSTEXTURE_OUTPUT VSTexture(
    float4 VertexPosition : POSITION, 
    float3 VertexNormal   : NORMAL,
    float2 VertexTexCoord : TEXCOORD0
    )
{

    VSTEXTURE_OUTPUT Out;
	
	//-----------------------------------------------------------------------------
	//Calc VertexPosition + Normale
	//-----------------------------------------------------------------------------

    // position (view space)
	float4 tPositionWorldSpace = mul( VertexPosition, eff_World);
	// Normal (World)
    float3 tNormalWorldSpace = mul(VertexNormal, (float3x3)eff_World);
	

	//-----------------------------------------------------------------------------
	// scan Lights and calculate total Diffuse
	//-----------------------------------------------------------------------------
    float4 vTotalLightDiffuse = float4(0,0,0,0);	
	float power =2;

    for(int i=0; i<eff_NumberLights; i++ )
    {
		float3 tNormalLightDir = normalize(tPositionWorldSpace - eff_LightPosition[0]);
		float4 LightFact = max(0,dot( tNormalWorldSpace, -tNormalLightDir));


		// Look here....Calculating Lightning
		// Baselight(Ambiente) + Ambiente * AmbLight + Diffuse * DiffLight
		// Thought it has to be: 
		// Ambiente * AmbLight + Diffuse * DiffLight
		// but its soooo dark
		vTotalLightDiffuse.rgb += eff_LightAmbient[i] * LightFact * power
					+ eff_MaterialAmbient * eff_LightAmbient[i] * LightFact*power
					+ eff_MaterialDiffuse * eff_LightDiffuse[i] * LightFact*power;
	}


	//-----------------------------------------------------------------------------
	// Output Section
	//-----------------------------------------------------------------------------

	// position transformed to View+Projecion
	Out.Position = mul(tPositionWorldSpace, eff_VP);
	
	Out.Diffuse.a = eff_MaterialDiffuse.a;
	Out.Diffuse.rgb = vTotalLightDiffuse;

	
	
	//-----------------------------------------------------------------------------
	// TextureCoords, only pass through
	//-----------------------------------------------------------------------------
	if ( eff_HasTexture )
	{
		Out.TexCoord = VertexTexCoord;
    }
	else
	{
		Out.TexCoord =0;
	}
	
    return Out;    
}
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------






//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Pixel shader Input structure
//--------------------------------------------------------------------------------------
struct PS_INPUT
{
	float2 TextureUV  : TEXCOORD0;
	float4 Diffuse    : COLOR0;    
};
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};



//--------------------------------------------------------------------------------------
//PIXEL SHADER
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( PS_INPUT In)
{ 
    PS_OUTPUT Output;
    // Lookup mesh texture and modulate it with diffuse
    
	if( eff_HasTexture )
	{
		Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;

	}
	else
	{
		Output.RGBColor = In.Diffuse;
	}



    return Output;
}
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------



//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------

technique TSkinOnly
{
    pass P0
    {   
        VertexShader = compile vs_3_0 VSTexture();
        PixelShader  = compile ps_3_0 RenderScenePS();
    }
}




I'm sure it something very simple.... heeelp

Share this post


Link to post
Share on other sites
arrrg :)

ok its late, too much coffee and too much other things
i found it

float3 tNormalWorldSpace = mul(VertexNormal, (float3x3)eff_World);

changed to :

float3 tNormalWorldSpace = normalize(mul(VertexNormal, (float3x3)eff_World));

and it works..
sometime its better to take a break..... i do so now..


Greets from Germany

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this