private static Lighting GetIntensity(IMaterial material, Tuple4 lightDirection, double intensity, Tuple4 eyeVector, Tuple4 pointOnSurface, Tuple4 surfaceNormal) { var diffuse = 0.0; var specular = 0.0; var cosine = Tuple4.DotProduct(lightDirection, surfaceNormal); if (cosine >= 0) { diffuse = cosine * material.Diffuse; if (material.IsShining()) { // var reflected = Tuple4.Subtract(Tuple4.Scale(surfaceNormal, 2 * Tuple4.DotProduct(surfaceNormal, lightDirection)), lightDirection); var reflected = Tuple4.Reflect(Tuple4.Negate(lightDirection), surfaceNormal); var reflectedDotDir = Tuple4.DotProduct(reflected, eyeVector); if (reflectedDotDir > 0) { specular = material.Specular * Math.Pow(reflectedDotDir / (reflected.Length() * eyeVector.Length()), material.Shininess); } } } return(diffuse, specular, intensity); }
public void TupleReflectTest() { var t1 = new Tuple4(1, 7, 3, TupleFlavour.Vector); var t2 = new Tuple4(4, 5, 6, TupleFlavour.Vector); var expected = new Tuple4(-455, -563, -681, TupleFlavour.Vector); Assert.Equal(expected, Tuple4.Reflect(t1, t2)); }
public virtual HitResult[] AllHits(Tuple4 origin, Tuple4 dir) { var intersections = GetIntersections(new Ray(origin, dir)); if (intersections == null) { return(new HitResult[] { HitResult.NoHit }); } var result = new HitResult[intersections.Length]; for (int i = 0; i < intersections.Length; ++i) { var figure = intersections[i].figure; var distance = intersections[i].t; var pointOnSurface = Tuple4.Geometry3D.MovePoint(origin, dir, distance); // orig + dir*dist // TODO: Remove this cast to BaseFigure (var surfaceNormal, var objectPoint) = ((BaseFigure)figure).GetTransformedNormal(figure, pointOnSurface, intersections[i].u, intersections[i].v); var eyeVector = Tuple4.Negate(dir); var isInside = false; if (Tuple4.DotProduct(surfaceNormal, eyeVector) < 0) { isInside = true; surfaceNormal = Tuple4.Negate(surfaceNormal); } var pointOverSurface = Tuple4.Add(pointOnSurface, Tuple4.Scale(surfaceNormal, Constants.Epsilon)); var pointUnderSurface = Tuple4.Subtract(pointOnSurface, Tuple4.Scale(surfaceNormal, Constants.Epsilon)); var reflectionVector = Tuple4.Reflect(dir, surfaceNormal); result[i] = new HitResult(true, figure, distance, intersections[i].u, intersections[i].v, objectPoint, pointOnSurface, pointOverSurface, pointUnderSurface, surfaceNormal, eyeVector, reflectionVector, isInside); } return(result); }
public void When_reflect(string id, string v, string n) { cache[id] = Tuple4.Reflect(cache[v], cache[n]); }