public Triangle(Vector pointA, Vector pointB, Vector pointC, Matrices transform, Color color) : base(transform,color) { this.pointA = pointA; this.pointB = pointB; this.pointC = pointC; }
public Object(Matrices transform, Color color) { this.Color = color; this.Transform = new Matrices(4,4); this.Transform.Matrix = (transform * new Matrices(4, 4)).Matrix ; this.Transform.Invers = Matrices.InversCalculate(transform.Matrix); // this.Transform = temp }
public override bool IsIntersect(Ray ray, Matrices transform) { //if (a == 0) //{ // ray.IntersectDistance = -c / b; // return true; //} //else if (D < 0) // return false; //else //{ Vector L = ray.Position - this.center; double a = Vector.DotProduct(ray.Direction, ray.Direction); double b = 2 * Vector.DotProduct(ray.Direction, L); double c = Vector.DotProduct(L, L) - this.radius * this.radius; double D = b * b - 4 * a * c; double q = (b > 0) ? -0.5 * (b + Math.Sqrt(D)) : -0.5 * (b - Math.Sqrt(D)); double[] t = new double[2]; t[0] = q / a; t[1] = c/q; //if (t[0] > 0 && t[1] > 0) //{ Vector temp1 = ray.Direction * Math.Min(t[0], t[1]); Matrices temp2 = new Matrices(temp1.x, temp1.y, temp1.z, 1); temp2 = transform.Matrix * temp2; temp1.x = temp2.Matrix[0, 0]; temp1.y = temp2.Matrix[1, 0]; temp1.z = temp2.Matrix[2, 0]; ray.IntersectDistance = temp1.Distance(); //ray.IntersectDistance = Math.Min(t[0], t[1]); return true; //} //else if (t[0] < 0 && t[1] > 0) //{ // ray.IntersectDistance = t[1]; // return true; //} //else if (t[0] > 0 && t[1] < 0) //{ // ray.IntersectDistance = t[0]; // return true; //} //else return false; //} }
public override bool IsIntersect(Ray ray, Matrices transform) { Vector temp = Vector.CrossProduct((this.pointC - this.pointA),(this.pointB - this.pointA)); Vector n = temp / temp.Distance(); double a = Vector.DotProduct(this.pointA, n); double b = Vector.DotProduct(ray.GetPosition(), n); double c = Vector.DotProduct(ray.GetDirection(), n); double t = (a - b) / c; Vector pointT = ray.GetPosition() + (ray.GetDirection() * t); if (PointInTriangle(pointT)) { Vector temp1 = ray.Direction * t; Matrices temp2 = new Matrices(temp1.x, temp1.y, temp1.z, 1); temp2 = transform.Matrix * temp2; temp1.x = temp2.Matrix[0, 0]; temp1.y = temp2.Matrix[1, 0]; temp1.z = temp2.Matrix[2, 0]; ray.IntersectDistance = temp1.Distance() ; //ray.IntersectDistance = t ; //ray.IntersectWith = this; return true; } else return false; }
public Sphere(Vector center, double radius,Matrices transform, Color color) : base(transform,color) { this.center = center; this.radius = radius; }
public static Ray Intersection(Ray ray, Scene scene) { double mindist = double.MaxValue; foreach(Object obj in scene.ObjectList) { //for (int i = 0; i < obj.Transform.Matrix.GetLength(0); i++) //{ // for (int j = 0; j < obj.Transform.Matrix.GetLength(1); j++) // Console.Write("\t" + obj.Transform.Matrix[i, j]); // Console.WriteLine(); //} // //Console.WriteLine(); // //for(int i = 0 ; i < obj.Transform.Invers.GetLength(0) ; i++) //{ // for(int j = 0 ; j < obj.Transform.Invers.GetLength(1) ; j++) // Console.Write("\t" + obj.Transform.Invers[i,j]); // Console.WriteLine(); //} //Console.WriteLine(); //Console.WriteLine("---------------------------------"); //Console.WriteLine(); //Console.ReadKey(); Matrices pos = new Matrices(ray.Position.x, ray.Position.y, ray.Position.z, 1); //Console.WriteLine(pos.Matrix[0, 0] + " " + pos.Matrix[1, 0] + " " + pos.Matrix[2, 0] + " " + pos.Matrix[3, 0]); pos = obj.Transform.Invers * pos; //Console.WriteLine(pos.Matrix[0, 0] + " " + pos.Matrix[1, 0] + " " + pos.Matrix[2, 0] + " " + pos.Matrix[3, 0]); //Console.ReadKey(); Matrices dir = new Matrices(ray.Direction.x, ray.Direction.y, ray.Direction.z, 0); dir = obj.Transform.Invers * dir; //Debug-------------------------------------------------- //Console.WriteLine(ray.Position.x + " " + ray.Position.y + " " + ray.Position.z); //Console.WriteLine(pos.Matrix[0, 0] + " " + pos.Matrix[1, 0] + " " + pos.Matrix[2, 0]); //Console.ReadKey(); //------------------------------------------------------- ray.Position.x = pos.Matrix[0,0]; ray.Position.y = pos.Matrix[1,0]; ray.Position.z = pos.Matrix[2,0]; ray.Direction.x = dir.Matrix[0, 0]; ray.Direction.y = dir.Matrix[1, 0]; ray.Direction.z = dir.Matrix[2, 0]; if (obj.IsIntersect(ray,obj.Transform) && ray.IntersectDistance < mindist) { mindist = ray.IntersectDistance; ray.IntersectWith = obj; } // kali transform biasa ray direction(1) + position(0) pos = obj.Transform.Matrix * pos; dir = obj.Transform.Matrix * dir; ray.Position.x = pos.Matrix[0, 0]; ray.Position.y = pos.Matrix[1, 0]; ray.Position.z = pos.Matrix[2, 0]; ray.Direction.x = dir.Matrix[0, 0]; ray.Direction.y = dir.Matrix[1, 0]; ray.Direction.z = dir.Matrix[2, 0]; } ray.IntersectDistance = mindist; return ray; }
public static Matrices operator /(Matrices matrix1 ,double n) { Matrices hasil = new Matrices(matrix1.Row, matrix1.Col); for (int row = 0; row < hasil.Row; row++) { for (int col = 0; col < hasil.Col; col++) { hasil.Matrix[row, col] = matrix1.Matrix[row, col] / n; } } return hasil; }
public static Matrices operator -(Matrices matrix1, Matrices matrix2) { int n = matrix1.Row; Matrices hasil = new Matrices(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { hasil.Matrix[i, j] = matrix1.Matrix[i, j] - matrix2.Matrix[i, j]; } } return hasil; }
public static Matrices operator *(Matrices matrix1, Matrices matrix2) { Matrices hasil = new Matrices(matrix1.Row, matrix2.Col); for(int row = 0 ; row < hasil.Row ; row++) { for(int col = 0 ; col < hasil.Col ; col++) { hasil.Matrix[row, col] = 0; for(int count = 0 ; count < matrix1.Col ; count++) { hasil.Matrix[row,col] += matrix1.Matrix[row, count] * matrix2.Matrix[count, col]; } } } return hasil; }
public static Matrices operator *(double[,] matrix1, Matrices matrix2) { Matrices hasil = new Matrices(matrix1.GetLength(0), matrix2.Col); for (int row = 0; row < hasil.Row; row++) { for (int col = 0; col < hasil.Col; col++) { double temp = 0.0; for (int count = 0; count < matrix2.Row; count++) { temp += matrix1[row, count] * matrix2.Matrix[count, col]; } hasil.Matrix[row, col] = temp; } } return hasil; }
public abstract bool IsIntersect(Ray ray, Matrices transform);