public double IsAtPoint(int colorNumber, double ks, Vector3 Normal, Vector3 WorldCoordinates, Vector3 lightSource, Vector3 lightColor, Vector3 camera, int n) { Vector3 viewDirectionAtPoint = camera - WorldCoordinates; if (isblinn) { Vector3 LV = lightSource - WorldCoordinates + viewDirectionAtPoint; Vector3 H = LV / Math.Sqrt(LV.first * LV.first + LV.second * LV.second + LV.third * LV.third); return(lightColor[colorNumber] * ks *Math.Pow(Math.Max(Vector3.DotProduct(Normal, H.ToVersor()), 0), n)); } else { Vector3 lightDirectionAtPoint = (lightSource - WorldCoordinates).ToVersor(); double NL = Vector3.DotProduct(lightDirectionAtPoint, Normal); Vector3 R = ((2 * NL) * Normal) - lightDirectionAtPoint; return(lightColor[colorNumber] * ks *Math.Pow(Math.Max(Vector3.DotProduct(R.ToVersor(), viewDirectionAtPoint.ToVersor()), 0), n)); } }
public double ColorIluminationAtPoint(int colorNumber, Color4 color, double ka, double kd, double ks, Vertex point, Light[] lightData, Vector3 camera, int n) { double Iaval = Ia(colorNumber, color, ka); double returnedValue = Ia(colorNumber, color, ka); Vector3 pointNormal = point.Normal.ToVersor(); double dot; foreach (Light light in lightData) { if (Vector3.DotProduct(light.Coordinates, light.Normal.ToVersor(), point.WorldCoordinates) >= light.angle) { Vector3 reflectedLight = new Vector3(Math.Max(0, light.Color.first - 1 + color.Red), Math.Max(0, light.Color.second + color.Green - 1), Math.Max(0, light.Color.third + color.Blue - 1)); returnedValue += IdAtPoint(colorNumber, reflectedLight, kd, pointNormal, point.WorldCoordinates, light.Coordinates, out dot); if (dot > 0) { returnedValue += IsAtPoint(colorNumber, ks, pointNormal, point.WorldCoordinates, light.Coordinates, reflectedLight, camera, n); } } } return(Math.Min(returnedValue, 1)); }
public static Matrix ViewMatrix(Matrix M) { Vector3 CameraPosition = new Vector3(M[0, 0], M[1, 0], M[2, 0]); Vector3 CameraTarget = new Vector3(M[0, 1], M[1, 1], M[2, 1]); Vector3 UpVector = new Vector3(M[0, 2], M[1, 2], M[2, 2]); UpVector = UpVector.ToVersor(); Vector3 ZAxis = CameraPosition - CameraTarget; ZAxis = ZAxis.ToVersor(); Vector3 XAxis = Vector3.CrossProduct(UpVector, ZAxis); XAxis = XAxis.ToVersor(); Vector3 YAxis = Vector3.CrossProduct(ZAxis, XAxis); return(new Matrix(new double[, ] { { XAxis.first, XAxis.second, XAxis.third, 0 }, { YAxis.first, YAxis.second, YAxis.third, 0 }, { -ZAxis.first, -ZAxis.second, -ZAxis.third, 0 }, { -Vector3.DotProduct(XAxis, CameraPosition), -Vector3.DotProduct(YAxis, CameraPosition), Vector3.DotProduct(ZAxis, CameraPosition), 1 } })); }
public double IdAtPoint(int colorNumber, Vector3 reflectedLight, double kd, Vector3 Normal, Vector3 WorldCoordinates, Vector3 lightSource, out double dot) { dot = Vector3.DotProduct(WorldCoordinates, Normal, lightSource); return(Math.Max(reflectedLight[colorNumber] * kd * dot, 0)); }