staticVoid
May 12th, 2008, 05:52 AM
hi, I am trying to add multiple light sources to a scene just using simple flat shading. The problem is that i'm not sure how to blend the different colors and light intensities. my code (at the moment) looks a bit like this:
struct SLight
{
3dvector position;
unsigned color;
float colorIntensity;
float radius;
};
struct SLights
{
int nLights;
SLight *lights;
};
void flatShade(const polygon &p, const Lights &lights)
{
pseudocode
3dvector average;
for all points in poly (i)
average += poly.point[i]
next i
average /= poly.nPoints
for all lights (lights.nLights) (i)
3dvector light = lights.lights[i].position - average; // get the light vector
lightIntensity = light.getLength() / lights.lights[i].radius; // intensity of light(distance)
if lightIntensity > 1 then continue; // surface too far from light source
light.normalize()
float final = light.dotProduct(poly.normal) * (1 - lightIntensity) // get the cosine of the angle and multiply it by the lightIntensity
color = poly.color * final // modify the surface color
finalColor = interpolate(color, finalColor) // blend this color with any previous color
next i
}
the problem I am having is when there are more than one light source. how do you normally handle multiple lights in flat shading?
struct SLight
{
3dvector position;
unsigned color;
float colorIntensity;
float radius;
};
struct SLights
{
int nLights;
SLight *lights;
};
void flatShade(const polygon &p, const Lights &lights)
{
pseudocode
3dvector average;
for all points in poly (i)
average += poly.point[i]
next i
average /= poly.nPoints
for all lights (lights.nLights) (i)
3dvector light = lights.lights[i].position - average; // get the light vector
lightIntensity = light.getLength() / lights.lights[i].radius; // intensity of light(distance)
if lightIntensity > 1 then continue; // surface too far from light source
light.normalize()
float final = light.dotProduct(poly.normal) * (1 - lightIntensity) // get the cosine of the angle and multiply it by the lightIntensity
color = poly.color * final // modify the surface color
finalColor = interpolate(color, finalColor) // blend this color with any previous color
next i
}
the problem I am having is when there are more than one light source. how do you normally handle multiple lights in flat shading?