Пример #1
0
        /**
         * Returns the shaded color
         * @return The color in Vec form (rgb)
         */
        Vec shade(int level, double weight, Vec P, Vec N, Vec I, Isect hit)
        {
            Vec     tcol;
            Vec     R;
            double  t, diff, spec;
            Surface surf;
            Vec     col;
            int     l;

            col  = new Vec();
            surf = hit.surf;
            R    = new Vec();
            if (surf.shine > 1e-6)
            {
                R = SpecularDirection(I, N);
            }

            // Computes the effectof each light
            for (l = 0; l < lights.Length; l++)
            {
                L.sub2(lights [l].pos, P);
                if (Vec.dot(N, L) >= 0.0)
                {
                    t = L.normalize();

                    tRay.P = P;
                    tRay.D = L;

                    // Checks if there is a shadow
                    if (Shadow(tRay, t) > 0)
                    {
                        diff = Vec.dot(N, L) * surf.kd *
                               lights [l].brightness;

                        col.adds(diff, surf.color);
                        if (surf.shine > 1e-6)
                        {
                            spec = Vec.dot(R, L);
                            if (spec > 1e-6)
                            {
                                spec   = Math.Pow(spec, surf.shine);
                                col.x += spec;
                                col.y += spec;
                                col.z += spec;
                            }
                        }
                    }
                }
            }

            tRay.P = P;
            if (surf.ks * weight > 1e-3)
            {
                tRay.D = SpecularDirection(I, N);
                tcol   = trace(level + 1, surf.ks * weight, tRay);
                col.adds(surf.ks, tcol);
            }
            if (surf.kt * weight > 1e-3)
            {
                if (hit.enter > 0)
                {
                    tRay.D = TransDir(null, surf, I, N);
                }
                else
                {
                    tRay.D = TransDir(surf, null, I, N);
                }
                tcol = trace(level + 1, surf.kt * weight, tRay);
                col.adds(surf.kt, tcol);
            }

            // garbaging...
            tcol = null;
            surf = null;

            return(col);
        }
Пример #2
0
 public Ray(Vec pnt, Vec dir)
 {
     P = new Vec(pnt.x, pnt.y, pnt.z);
     D = new Vec(dir.x, dir.y, dir.z);
     D.normalize();
 }
Пример #3
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++;
                }
            }
        }
Пример #4
0
		public Ray (Vec pnt, Vec dir)
		{
			P = new Vec (pnt.x, pnt.y, pnt.z);
			D = new Vec (dir.x, dir.y, dir.z);
			D.normalize ();
		}