I think its defuse lightning I am doing.
Here is the code for it:
// We have now looped through all the spheres in the scene and found
// the closest one
// Find the color of it
if ( closestSphere != -1 )
{
//std::cout << closesSphereDistance << std::endl;
float r, g, b = 0;
// Check which if there is a light affecting the sphere
for (int currentLight = 0; currentLight <= thisScene.getAmountOfObjects(); currentLight++)
{
Vector3D sphereRayIntersection;
Point rayOrg = currentRay.getOrigin();
// Add the start point of the ray and the direction of the sphere together
// and mult. it with the distance to closest sphere
sphereRayIntersection = currentRay.getDirection();
sphereRayIntersection.setX(rayOrg.getX() + (sphereRayIntersection.getX() * closesSphereDistance ));
sphereRayIntersection.setY(rayOrg.getY() + (sphereRayIntersection.getY() * closesSphereDistance ));
sphereRayIntersection.setZ(rayOrg.getZ() + (sphereRayIntersection.getZ() * closesSphereDistance ));
// Find the normal to the sphere at point of intersection
Sphere thisSphere = thisScene.getSphere(closestSphere);
Vector3D normal;
float oneOverRadius = 1/thisSphere.getRadius();
normal = sphereRayIntersection - thisSphere.getPosition();
normal.scale(oneOverRadius);
normal.normalize();
// Get the light direction
Vector3D lightDir;
Sphere thisLight = thisScene.getLight(currentLight);
lightDir = thisLight.getPosition() - sphereRayIntersection;
lightDir.normalize();
// Find light coefficient
float lightCoef = AbsDot(lightDir,normal);
if (lightCoef < 0)
lightCoef = 0;
// Get the color of the light and how much it affects the sphere
Material thisMaterial = thisLight.getMaterial();
float lightR = thisMaterial.getR() * lightCoef;
float lightG = thisMaterial.getG() * lightCoef;
float lightB = thisMaterial.getB() * lightCoef;
// Add the effect of the light to the color of the sphere
Material sphereColor = thisSphere.getMaterial();
r = ((sphereColor.getR() * lightR) / 255 );
g = ((sphereColor.getG() * lightG) / 255);
b = ((sphereColor.getB() * lightB) / 255);
// Make sure that the color is not higher than 255
if (r > 255)
r = 255;
if (g > 255)
g = 255;
if (b > 255)
b = 255;
//std::cout << r << "," << g << "," << b << std::endl;
}
// Assign the color to the pixel
int colorR = r;
int colorG = g;
int colorB = b;
colorBuffer[arrayTracer + 0] = colorR;
colorBuffer[arrayTracer + 1] = colorG;
colorBuffer[arrayTracer + 2] = colorB;
}
Going to do some reading today on the topic of adding light, as I am not 100% sure how its done.
) so I was wondering if someone could comment in a bit or something to give me a clue of what is going one.

