Jump to content
  • Advertisement
Sign in to follow this  
Freakdesign

Changing Light when Object is scaled?? using shaders

This topic is 4621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 * LightFact * power
					+ eff_MaterialAmbient * eff_LightAmbient * LightFact*power
					+ eff_MaterialDiffuse * eff_LightDiffuse * 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!