Sign in to follow this  
jad_salloum

HLSL how to put 2 lights in my shader ??

Recommended Posts

hi guys i am beginner in using HLSL and i want to put 2 lights in my .fx file but i need some help in it , so can anybody help me with that ??? i tried to make multiple passes in my shader but if i have a big mesh the FPS will become too low and 1 light is passed not 2 lights (2 passes but only 1 light is lighting my scene), so i think i shouldn't make 2 passes for the same type of lighting and i should add the lights inside the shader . i looked at the www.mdxinfo.com tutorials but i didn't find any tutorial that is using more than 1 light so any help would be gr8 thanks alot guys. 1 more thing is using HLSL affect the performance of the game ?? does it decrease the FPS ??

Share this post


Link to post
Share on other sites
All you want to do is to perform the same lighting equations twice, but just add the results. In my shaders, I can do up to 3 or 4 lights per pass (directional, point, and spot) in this manner. If you want something simple, you can do it like (psuedocode):


float4 lightOneDir;
float4 lightTwoDir;

VertexShader()
{
// Blah blah blah, do all your stuff
OUT.diffuseColor = (float4)0;

// Perform lighting calculations for lightOne
OUT.diffuseColor += lightOneResult;

// Perform lighting calculations for lightTwo
OUT.diffuseColor += lightTwoResult;
}


It would kinda go something like that.

Share this post


Link to post
Share on other sites
can u help me with this code i am using in my shader


float4x4 worldMatrix : WORLD; // World matrix for object
float4x4 worldViewProjection : WORLDVIEWPROJECTION; // World * View * Projection matrix
float4 lightDirection; // Direction of the light
float4 eyeVector; // Vector for the eye location

texture SceneTexture;

sampler SceneSampler =
sampler_state
{
Texture = <SceneTexture>;
};

//--------------------------------------------------------------------------------------
// Transformation with single directional light plus per pixel specular
//--------------------------------------------------------------------------------------

struct PER_PIXEL_OUT
{
float4 Position : POSITION;
float2 TexCoords : TEXCOORD0;
float3 LightDirection : TEXCOORD1;
float3 Normal : TEXCOORD2;
float3 EyeWorld : TEXCOORD3;
};

PER_PIXEL_OUT TransformSpecularPerPixel(float4 pos : POSITION, float3 normal : NORMAL,
float2 uv : TEXCOORD0)
{
PER_PIXEL_OUT Output = (PER_PIXEL_OUT)0;

// Transform position
Output.Position = mul(pos, worldViewProjection);

// Store uv coords
Output.TexCoords = uv;

// Store the light direction
Output.LightDirection = lightDirection;

// Transform the normals into the world matrix and normalize them
Output.Normal = normalize(mul(normal, worldMatrix));

// Transform the world position of the vertex
float3 worldPosition = normalize(mul(pos, worldMatrix));

// Store the eye vector
Output.EyeWorld = normalize(eyeVector - worldPosition);

// Return the data
return Output;
}

float4 TextureColorPerPixel(
float2 uvCoords : TEXCOORD0,
float3 lightDirection : TEXCOORD1,
float3 normal : TEXCOORD2,
float3 eye : TEXCOORD3) : COLOR0
{
// Normalize our vectors
float3 normalized = normalize(normal);
float3 light = normalize(lightDirection);
float3 eyeDirection = normalize(eye);
// Store our diffuse component
float4 diffuse = saturate(dot(light, normalized));

// Calculate specular component
float3 reflection = normalize(2 * diffuse * normalized - light);
float4 specular = pow(saturate(dot(reflection, eyeDirection)), 8);

float4 textureColorFromSampler = tex2D(SceneSampler, uvCoords);
// Return the combined color
return textureColorFromSampler * diffuse + specular;
};


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

technique RenderSceneSpecularPerPixel
{
pass P0
{
VertexShader = compile vs_1_1 TransformSpecularPerPixel();
PixelShader = compile ps_2_0 TextureColorPerPixel();
}

pass P1
{
VertexShader = compile vs_1_1 TransformSpecularPerPixel();
PixelShader = compile ps_2_0 TextureColorPerPixel();
}
}


Share this post


Link to post
Share on other sites
Please allow for 24hrs or until a thread falls off the first page of listings before bumping - thanks! [smile]

It's pretty much exactly as Dustin posted [wink]

To modify your shader, add an additional entry to the PER_PIXEL_OUT structure so you have LightDirection1 and LightDirection2 (for example). Make sure that the vertex shader passes both directions through - it needs do no other computation.

Then, in the pixel shader, duplicate all code except for the last tex2D() part. End up with something like:

return textureColorFromSampler * diffuse1 + textureFromSampler * diffuse2 + specular1 + specular2;

hth
Jack

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