public bool intersectionTriangle(ray r, Vector2 v, primitives p) { //box pixelcoordinates to world coordinates float tx = TX(p.X); float ty = TY(p.Y); float tw = TW(p.W); float th = TW(p.H); //3 vectors, for every line of the triangle 1 Vector2 blc = new Vector2(tx, ty + th); Vector2 brc = new Vector2(tx + tw, ty + th); Vector2 tc = new Vector2(tx + (tw / 2), ty); ////ensure that it is not inside the triangle. Else you have an intersection immediatly. //if (p.X > blc.X && p.X < trc.X && p.Y < blc.Y && p.Y > trc.Y && r.o.X > blc.X && r.o.X < trc.X && r.o.Y < blc.Y && r.o.Y > trc.Y) //{ // return true; //} // check for intersection for every vector of the triangle. if (intersectionLine(r, v, blc, tc)) { return(true); } if (intersectionLine(r, v, tc, brc)) { return(true); } if (intersectionLine(r, v, brc, blc)) { return(true); } return(false); }
public bool intersectionPrimitives(ray r, Vector2 v) { for (int z = 0; z < prim.Count; z++) { if (prim[z].TYPE == "box") { if (intersectionBox(r, v, prim[z]) == true) { return(true); } } if (prim[z].TYPE == "circle") { if (intersectionCircle(r, v, prim[z]) == true) { return(true); } } if (prim[z].TYPE == "triangle") { if (intersectionTriangle(r, v, prim[z]) == true) { return(true); } } } return(false); }
public void plot(int x, int y) { float offset = 0.00001f; ray ray = new ray(); for (int i = 0; i < (screen.width - 1); i++) { for (int j = x; j < y; j++) { floatbuffer[i, j] = new float[] { c[i, j][0], c[i, j][1], c[i, j][2] }; for (int k = 0; k < lightbuffer.Length; k += 5) { Vector2 pos = new Vector2(i, j); ray.o = new Vector2(lightbuffer[k], lightbuffer[k + 1]); ray.t = distanceToLight(ray, pos) - (2f * offset); ray.d = normalizedDirectionToLight(ray, pos); ray.o = new Vector2(lightbuffer[k] + (ray.d.X * offset), lightbuffer[k + 1] + (ray.d.X * offset)); if (intersectionPrimitives(ray, pos) == false) { //Console.WriteLine("i= " + i + " j= " + j + "" + intersectionPrimitives(ray, pos)); floatbuffer[i, j][0] += lightbuffer[k + 2] / (float)((ray.t) + 1); floatbuffer[i, j][1] += lightbuffer[k + 3] / (float)((ray.t) + 1); floatbuffer[i, j][2] += lightbuffer[k + 4] / (float)((ray.t) + 1); } } } } }
public bool intersectionLine(ray r, Vector2 p, Vector2 v1, Vector2 v2) { float d = ((v2.Y - v1.Y) * (-r.o.X + p.X) - (v2.X - v1.X) * (-r.o.Y + p.Y)); float n = ((v2.X - v1.X) * (r.o.Y - v1.Y) - (v2.Y - v1.Y) * (r.o.X - v1.X)); float n2 = ((-r.o.X + p.X) * (r.o.Y - v1.Y) - (-r.o.Y + p.Y) * (r.o.X - v1.X)); if (d == 0f) { return(false); } float ua = n / d; float ub = n2 / d; return(ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f); }
public bool intersectionBox(ray r, Vector2 p, primitives b) { //box pixelcoordinates to world coordinates float bx1 = TX(b.X); float by1 = TY(b.Y); float bx2 = (TX(b.X)) + (TW(b.W)); float by2 = (TY(b.Y)) + (TW(b.H)); //4 vectors, for every line of the box 1 Vector2 blc = new Vector2(bx1, by2); Vector2 brc = new Vector2(bx2, by2); Vector2 tlc = new Vector2(bx1, by1); Vector2 trc = new Vector2(bx2, by1); //ensure that it is not inside the box. Else you have an intersection immediatly. if (p.X > blc.X && p.X < trc.X && p.Y < blc.Y && p.Y > trc.Y && r.o.X > blc.X && r.o.X < trc.X && r.o.Y < blc.Y && r.o.Y > trc.Y) { return(true); } // check for intersection for every vector of the box. if (intersectionLine(r, p, tlc, blc)) { return(true); } if (intersectionLine(r, p, blc, brc)) { return(true); } if (intersectionLine(r, p, tlc, trc)) { return(true); } if (intersectionLine(r, p, trc, brc)) { return(true); } return(false); }
public bool intersectionCircle(ray r, Vector2 v, primitives p) { float radius = ((TW(p.W) / 2)); Vector2 vec = new Vector2(TX(p.X), TY(p.Y)); Vector2 c = vec - r.o; float t = Vector2.Dot(c, r.d); Vector2 q = c - (t * r.d); float p2 = Vector2.Dot(q, q); float tocircle = t - (float)Math.Sqrt(((radius * radius) - p2)); if (tocircle > r.t || t < 0) { return(false); } if ((radius * radius) < p2) { return(false); } else { r.t = t; return(true); } }
public Vector2 normalizedDirectionToLight(ray ray, Vector2 pos) { return(Vector2.Normalize(pos - ray.o)); }
public float distanceToLight(ray ray, Vector2 pos) { return((pos - ray.o).Length); }