Jump to content
  • Advertisement
Sign in to follow this  
ChrisMelling

Multiple point lights, shaders.

This topic is 2310 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

Currently using a point light shader that fits my game perfect but It would be nice It I could have more than 1 point light on the scene at one time. I've tired to look at other examples at how they do it but none seem to look anything like my shader, so some advice would be appreciated!

Here is the shader I'm using now.


//////////////////////////////////////////////////////////////////////////////////////////////////
// File: ParallaxMapPointLight.fx
// Author: Chris Smith
// Date Created: 12/17/06
// Description: Renders a scene with parallax mapping from a point light
// Disclaimer: Use this however you want, but I am not responsible for anything
//////////////////////////////////////////////////////////////////////////////////////////////////
float4x4 WorldViewProj; //World * View * Projection Matrix
float4x4 World; //World matrix of object
float4 EyePos; //Position of eye in world space
float4 LightPos; //Light position in world space
float4 LightColor; //Color of the light
texture ColorMap; //Color texture
texture NormalMap; //Normal map texture
texture HeightMap; //Height map texture
float Falloff; // Distance until light begins to falloff
float Ambient; // Ambient amount of lighting
float DepthScale = 0.04f; //Depth of the parallax map
float Bias = 0.02f; //Bias of the parallax map
float scale;
sampler colorSampler = sampler_state
{
Texture = (ColorMap);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler normalSampler = sampler_state
{
Texture = (NormalMap);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
sampler heightSampler = sampler_state
{
Texture = (HeightMap);
MIPFILTER = LINEAR;
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
};
//////////////////////////////////////////////////////////////
//Structures
//////////////////////////////////////////////////////////////
//Application to vertex shader
struct A2V
{
float4 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord0 : TEXCOORD0;
float3 Tangent : TANGENT;
float3 Binormal : BINORMAL;
};
//Vertex shader to pixel shader
struct V2P
{
float4 Position : POSITION;
float2 TexCoord0 : TEXCOORD0;
float3 LightDir : TEXCOORD1;
float3 ViewDir : TEXCOORD2;
};
//////////////////////////////////////////////////////////////
//Vertex Shader
//////////////////////////////////////////////////////////////
void VS( in A2V IN, out V2P OUT )
{
IN.Position.xyz *= scale;
//Transform the position from view space to homogeneous projection space
OUT.Position = mul(IN.Position, WorldViewProj);
//Compute world space position
float4 WorldPos = mul(IN.Position, World);

//Calculate Binormal and set Tangent Binormal and Normal matrix
float3x3 TBNMatrix = mul(float3x3(IN.Binormal, IN.Tangent , IN.Normal), (float3x3)World);
//Compute light direction
OUT.LightDir = LightPos - WorldPos;
//Compute light direction * TBN Matrix
OUT.LightDir = mul(TBNMatrix, OUT.LightDir);

//Compute view direction * TBN Matrix
OUT.ViewDir = mul(TBNMatrix, EyePos - WorldPos);

//Copy the texture coordinate as is
OUT.TexCoord0 = IN.TexCoord0;
}
//////////////////////////////////////////////////////////////
//Pixel Shader
//////////////////////////////////////////////////////////////
float4 PS( in V2P IN ) : COLOR0
{
IN.ViewDir = normalize(IN.ViewDir);

//Determine the height of this pixel
float Height = DepthScale * tex2D(heightSampler, IN.TexCoord0) - Bias;

//Compute the new texture coorddinate to use
float2 TexCorrected = Height * IN.ViewDir + IN.TexCoord0;

//calculate the color and the normal
float4 Color = tex2D(colorSampler, TexCorrected);
//Uncompress the normal map
float3 Normal = 2.0f * tex2D(normalSampler, TexCorrected).rgb - 1.0f;
//Pixel to light vector
float LenSq = dot( IN.LightDir, IN.LightDir );
IN.LightDir = normalize( IN.LightDir );

//Compute the light's attenuation
float Attn = min(( Falloff * Falloff ) / LenSq, 1.0f);
//Compute the diffuse lighting amount
float Diffuse = Attn * saturate(dot(Normal, IN.LightDir));
//Lighting amount * Texture Color * Light Color
return Color = (Diffuse + Ambient) * Color * LightColor;
}
//////////////////////////////////////////////////////////////
//Technique
//////////////////////////////////////////////////////////////
technique ParallaxMapPointLight
{
pass p0
{
vertexshader = compile vs_2_0 VS();
pixelshader = compile ps_2_0 PS();
}
}

Share this post


Link to post
Share on other sites
Advertisement
This is an old problem that has many solutions. Some are:

  1. Let’s say that you know the engine needs in several scenarios three point lights and one point light in others. You could implement a technique for three point lights and one for one point light and you switch to whatever you need.
  2. I think Unreal uses a multi pass shader. I suppose they use additive blending.
  3. Deferred renderers. My favorite!!! You can render the points lights you need. 100? Ok, you have it and with a very good performance. The problem is that you have to re implement all your pipeline tongue.png

If you only ask how to add the second point light to your shader, just calculate a second LightDir in your vertex shader (with the second point light position) and add the diffuse result of the second light to the diffuse result of the first.

And try to work in linear space: http://http.develope...gems3_ch24.html

Bye!!!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!