Ejemplo n.º 1
0
		public override Isect intersect (Ray ry)
		{
			double b, disc, t;
			Isect ip;
			v.sub2 (c, ry.P);
			b = Vec.dot (v, ry.D);
			disc = b * b - Vec.dot (v, v) + r2;
			if (disc < 0.0) {
				return null;
			}
			disc = Math.Sqrt (disc);
			t = (b - disc < 1e-6) ? b + disc : b - disc;
			if (t < 1e-6) {
				return null;
			}
			ip = new Isect ();
			ip.t = t;
			ip.enter = Vec.dot (v, v) > r2 + 1e-6 ? 1 : 0;
			ip.prim = this;
			ip.surf = surf;
			return ip;
		}
Ejemplo n.º 2
0
		/**
	 * Launches a ray
	 */
		Vec trace (int level, double weight, Ray r)
		{
			Vec P, N;
			bool hit;

			// Checks the recursion level
			if (level > 6) {
				return new Vec ();
			}

			hit = intersect (r, 1e6);
			if (hit) {
				P = r.point (inter.t);
				N = inter.prim.normal (P);
				if (Vec.dot (r.D, N) >= 0.0) {
					N.negate ();
				}
				return shade (level, weight, P, N, r.D, inter);
			}
			// no intersection --> col = 0,0,0
			return voidVec;
		}
Ejemplo n.º 3
0
		/**
	 * Checks if there is a shadow
	 * @param r The ray
	 * @return Returns 1 if there is a shadow, 0 if there isn't
	 */
		int Shadow (Ray r, double tmax)
		{
			if (intersect (r, tmax))
				return 0;
			return 1;
		}
Ejemplo n.º 4
0
		bool intersect (Ray r, double maxt)
		{
			Isect tp;
			int i, nhits;

			nhits = 0;
			inter.t = 1e9;
			for (i = 0; i < prim.Length; i++) {
				// uses global temporary Prim (tp) as temp.object for speedup
				tp = prim [i].intersect (r);
				if (tp != null && tp.t < inter.t) {
					inter.t = tp.t;
					inter.prim = tp.prim;
					inter.surf = tp.surf;
					inter.enter = tp.enter;
					nhits++;
				}
			}
			return nhits > 0 ? true : false;
		}
Ejemplo n.º 5
0
		public void render (Interval interval)
		{
			int[] row = new int[interval.width * (interval.yto - interval.yfrom)];
			int pixCounter = 0; //iterator

			int x, y, red, green, blue;
			double xlen, ylen;
			Vec viewVec;

			viewVec = Vec.sub (view.at, view.from);

			viewVec.normalize ();

			Vec tmpVec = new Vec (viewVec);
			tmpVec.scale (Vec.dot (view.up, viewVec));

			Vec upVec = Vec.sub (view.up, tmpVec);
			upVec.normalize ();

			Vec leftVec = Vec.cross (view.up, viewVec);
			leftVec.normalize ();

			double frustrumwidth = view.dist * Math.Tan (view.angle);

			upVec.scale (-frustrumwidth);
			leftVec.scale (view.aspect * frustrumwidth);

			Ray r = new Ray (view.from, voidVec);
			Vec col = new Vec ();

			// For each line
			for (y = interval.yfrom; y < interval.yto; y++) {
				ylen = (double)(2.0 * y) / (double)interval.width - 1.0;
				for (x = 0; x < interval.width; x++) {
					xlen = (double)(2.0 * x) / (double)interval.width - 1.0;
					r.D = Vec.comb (xlen, leftVec, ylen, upVec);
					r.D.add (viewVec);
					r.D.normalize ();
					col = trace (0, 1.0, r);

					// computes the color of the ray
					red = (int)(col.x * 255.0);
					if (red > 255)
						red = 255;
					green = (int)(col.y * 255.0);
					if (green > 255)
						green = 255;
					blue = (int)(col.z * 255.0);
					if (blue > 255)
						blue = 255;

					checksum += red;
					checksum += green;
					checksum += blue;

					// Sets the pixels
					row [pixCounter] = alpha | (red << 16) | (green << 8) | (blue);
					pixCounter++;
				}
			}

		}
Ejemplo n.º 6
0
		public abstract Isect intersect (Ray ry);