예제 #1
0
        public override Vec normal(Vec p)
        {
            Vec r;

            r = Vec.sub(p, c);
            r.normalize();
            return(r);
        }
예제 #2
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++;
                }
            }
        }