light_position = new Vector3(0, 0, 1);
normal = new Vector3(0, 0, 1);
Vector3 eye = new Vector3(0, 0, 1);
int specularExponent = 3;
float ambient = 0f;
float kSpecular = 0f;
float kDiffuse = 0.90f;
Vector3 lightColor = ColorToVec3(Color.White); //funkcja pomocnicza
float halfX = b.Width / 2f; //b to bitmapa
float halfY = b.Height / 2f;
float spotAngle = 1f; // jakiś tam kąt w radianach
for (int i = 0; i <= b.Width - 1; i++)
{
for (int i2 = 0; i2 <= b.Height - 1; i2++)
{
Vector3 pos = new Vector3((i - halfX) / halfX, (i2 - halfY) / halfY, 0);// skalowanie od -1 do 1
Vector3 temp = pos - light_position;
float distance = temp.Lenght();
float specularDist = distance + eye.Lenght();
distance = distance * distance; //można zoptymalizować - wiem
specularDist = specularDist * specularDist;// j.w
Vector3 lightToSurface = temp / distance;
Vector3 reflection = lightToSurface - ((2 * Dot(lightToSurface, normal)) * normal);
float specularAngle = Dot(reflection, (eye - pos).Normalize());
float specular = Math.Max((float)(Math.Pow(specularAngle, specularExponent) * kSpecular / specularDist), 0);
float angle = Dot(normal, -lightToSurface);
float spotLight = 0;
if (Math.Cos(spotAngle) < angle)
spotLight = (float)Math.Pow(angle, 2) * kDiffuse / distance;
Vector4 texture = ColorToVec4(b.GetPixel(i, i2));
Color final = Vec4ToColor(new Vector4(((ambient + spotLight) * texture.XYZ() + specular * new Vector3(1f, 1f, 1f)) * lightColor, texture.I));// ten Vector3(1, 1 ,1) to specularMapa
b.SetPixel(i, i2, final);
}
}