void render(byte[] buf, List <Sphere> spheres, int pixelWidth)
        {
            float tm = _timer.gameTime() * 2.0f;

            int stride      = (pixelWidth * 32) / 8;
            int pixelHeight = buf.Length / stride;

            int width  = pixelWidth;
            int height = pixelHeight;

            float invWidth    = 1.0f / (float)width;
            float invHeight   = 1.0f / (float)height;
            float fov         = 80.0f;
            float aspectratio = width / (float)height;
            float angle       = (float)Math.Tan(M_PI * 0.5f * fov / 180.0f);

            List <Task> tasks = new List <Task>();

            for (int i = 0; i < lineTos.Length; i++)
            {
                Task task = new Task(obj => {
                    // Trace rays
                    lineToRender toRender = lineTos[(int)obj];
                    for (int y = toRender.from; y < toRender.to; ++y)
                    {
                        for (int x = 0; x < width; ++x)
                        {
                            float xx     = (2.0f * (((float)x + 0.5f) * invWidth) - 1.0f) * angle * aspectratio;
                            float yy     = (1.0f - 2.0f * (((float)y + 0.5f) * invHeight)) * angle;
                            Vec3f raydir = new Vec3f(xx, yy, -1.0f);
                            raydir.normalize();

                            Vec3f raydir2 = new Vec3f(Mat4f.RotationYMatrix(-tm) * raydir);
                            //Vec3f raydir2 = raydir;

                            //Vec3f rayorig = new Vec3f(0.0f, 1.0f + (float)Math.Sin(tm) * 4.0f, -20.0f) + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 1.0f, 20.0f));
                            Vec3f rayorig = new Vec3f(0.0f, 5.0f, -25.0f) + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 5.0f, 25.0f));
                            //Vec3f rayorig = spheres[1].center + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 3.0f, 5.0f + tm));
                            //Vec3f rayorig = new Vec3f(0.0f);
                            //Vec3f rayorig = new Vec3f(0.0f, 10.0f, 0.0f);
                            Vec3f pixel = trace(rayorig, raydir2, spheres, 0);

                            printPixel(buf, x, y, new pBGRA((byte)(pixel.z * 255.0f), (byte)(pixel.y * 255.0f), (byte)(pixel.x * 255.0f), 255), pixelWidth);
                        }
                    }
                }, i);
                task.Start();
                tasks.Add(task);
            }

            foreach (Task t in tasks)
            {
                t.Wait();
            }
        }
Exemple #2
0
        void render(byte[] buf, List <Sphere> spheres, int pixelWidth)
        {
            float tm = _timer.gameTime() * 2.0f;

            int stride      = (pixelWidth * 32) / 8;
            int pixelHeight = buf.Length / stride;

            int width  = pixelWidth;
            int height = pixelHeight;

            float invWidth    = 1.0f / (float)width;
            float invHeight   = 1.0f / (float)height;
            float fov         = 40.0f;
            float aspectratio = width / (float)height;
            float angle       = (float)Math.Tan(M_PI * 0.5f * fov / 180.0f);

            Parallel.For(0, lineTos.Length, i => {
                lineToRender toRender = lineTos[i];
                for (int y = toRender.from; y < toRender.to; ++y)
                {
                    for (int x = 0; x < width; ++x)
                    {
                        float xx     = (2.0f * (((float)x + 0.5f) * invWidth) - 1.0f) * angle * aspectratio;
                        float yy     = (1.0f - 2.0f * (((float)y + 0.5f) * invHeight)) * angle;
                        Vec3f raydir = new Vec3f(xx, yy, -1.0f);
                        raydir.normalize();

                        Vec3f raydir2 = new Vec3f(Mat4f.RotationYMatrix(-tm) * raydir);
                        Vec3f rayorig = new Vec3f(0.0f, 5.0f, -25.0f) + new Vec3f(Mat4f.RotationYMatrix(-tm) * new Vec3f(0.0f, 5.0f, 25.0f));
                        Vec3f pixel   = trace(rayorig, raydir2, spheres, 0);

                        printPixel(buf, x, y, new pBGRA((byte)(pixel.z * 255.0f), (byte)(pixel.y * 255.0f), (byte)(pixel.x * 255.0f), 255), pixelWidth);
                    }
                }
            });
        }