示例#1
0
        static void draw_cube(ref MATRIX_f matrix)
        {
            for (int j = 0; j < 4; j++)
            {
                apply_matrix_f(ref matrix, quad[j].x, quad[j].y, quad[j].z, ref quad[j].x, ref quad[j].y, ref quad[j].z);
            }

            for (int j = 0; j < 4; j++)
            {
                persp_project_f(quad[j].x, quad[j].y, quad[j].z, ref quad[j].x, ref quad[j].y);
            }

            ManagedPointer vtx = new ManagedPointer(4 * sizeof(Int32));

            for (int j = 0; j < 4; j++)
            {
                ManagedPointer v = new ManagedPointer(6 * sizeof(Int32));
                v.WriteInt(0, (int)quad[j].x);
                v.WriteInt(sizeof(Int32), (int)quad[j].y);
                v.WriteInt(2 * sizeof(Int32), (int)quad[j].z);
                v.WriteInt(3 * sizeof(Int32), (int)quad[j].u);
                v.WriteInt(4 * sizeof(Int32), (int)quad[j].v);
                v.WriteInt(5 * sizeof(Int32), (int)quad[j].c);
                vtx.WritePointer(j * sizeof(Int32), v);
            }

            if (scene_polygon3d_f(POLYTYPE_GCOL, NULL, 4, vtx) != 0)
            {
                allegro_message("Lack of rendering routine");
            }
        }
示例#2
0
        /* draws a set of objects that demonstrate whats going on. It consists
         * of a cube, an arrow showing the 'to' orientation, an another arrow
         * showing the 'from' orientation, and another arrow showing the
         * interpolated orientation.
         */
        static void render_demo_box(BITMAP b, ref MATRIX_f from, ref MATRIX_f _in, ref MATRIX_f to, int col1, int col2, int col3)
        {
            float[,] tmp_points = new float[8, 3];

            render_wireframe_object(ref _in, b, box_points, tmp_points, box_edges, 8, 12, col1);
            render_wireframe_object(ref from, b, arrow_points, tmp_points, arrow_edges, 4, 3, col3);
            render_wireframe_object(ref to, b, arrow_points, tmp_points, arrow_edges, 4, 3, col3);
            render_wireframe_object(ref _in, b, arrow_points, tmp_points, arrow_edges, 4, 3, col2);
        }
示例#3
0
        /* update cube positions */
        static void anim_cube(MATRIX_f matrix1, MATRIX_f matrix2, V3D_f[] x1, V3D_f[] x2)
        {
            int i;

            for (i = 0; i < 8; i++)
            {
                apply_matrix_f(ref matrix1, cube1[i].x, cube1[i].y, cube1[i].z,
                               ref (x1[i].x), ref (x1[i].y), ref (x1[i].z));
                apply_matrix_f(ref matrix2, cube2[i].x, cube2[i].y, cube2[i].z,
                               ref (x2[i].x), ref (x2[i].y), ref (x2[i].z));
                persp_project_f(x1[i].x, x1[i].y, x1[i].z, ref (x1[i].x), ref (x1[i].y));
                persp_project_f(x2[i].x, x2[i].y, x2[i].z, ref (x2[i].x), ref (x2[i].y));
            }
        }
示例#4
0
        public static void _3D()
        {
            BITMAP buffer = create_bitmap(SCREEN_W, SCREEN_H);

            create_scene(24, 6);
            set_projection_viewport(0, 0, SCREEN_W, SCREEN_H);
            MATRIX_f matrix = new MATRIX_f(), matrix_r = new MATRIX_f(), matrix_t = new MATRIX_f();
            int      rx = 0, ry = 0, tz = 40;
            int      rot = 0, inc = 1;
            int      k = 1;

            while (!key[KEY_ESC])
            {
                clear_bitmap(buffer);
                clear_scene(buffer);

                get_rotation_matrix_f(ref matrix_r, rx, ry, 0);

                get_translation_matrix_f(ref matrix_t, 0, 0, tz + k * 40);

                matrix_mul_f(ref matrix_r, ref matrix_t, out matrix);

                matrix = matrix_r;

                draw_cube(ref matrix);

                render_scene();

                tz -= 2;
                if (tz == 0)
                {
                    tz = 40;
                }
                rx  += 4;
                ry  += 4;
                rot += inc;
                if ((rot >= 25) || (rot <= -25))
                {
                    inc = -inc;
                }
            }
            destroy_bitmap(buffer);
            destroy_scene();
        }
示例#5
0
        /* draw an object defined as a set of points and edges */
        static void render_wireframe_object(ref MATRIX_f m, BITMAP b, float[,] p, float[,] t, int[,] e, int np, int ne, int c)
        {
            int index, from, to;

            /* transform the points and store them in a buffer */
            for (index = 0; index < np; index++)
            {
                apply_matrix_f(ref m, p[index, 0], p[index, 1], p[index, 2],
                               ref (t[index, 0]), ref (t[index, 1]), ref (t[index, 2]));

                persp_project_f(t[index, 0], t[index, 1], t[index, 2],
                                ref (t[index, 0]), ref (t[index, 1]));
            }

            /* draw the edges */
            for (index = 0; index < ne; index++)
            {
                from = e[index, 0];
                to   = e[index, 1];

                line(b, (int)(t[from, 0]), (int)(t[from, 1]), (int)(t[to, 0]), (int)(t[to, 1]), c);
            }
        }
示例#6
0
        /* draw everything */
        static void render(BITMAP bmp)
        {
            MATRIX_f roller = new MATRIX_f(), camera = new MATRIX_f();
            int      x, y, w, h;
            float    xfront, yfront, zfront;
            float    xup = 0, yup = 0, zup = 0;

            /* clear the background */
            clear_to_color(bmp, makecol(255, 255, 255));

            /* set up the viewport region */
            x = (SCREEN_W - viewport_w) / 2;
            y = (SCREEN_H - viewport_h) / 2;
            w = viewport_w;
            h = viewport_h;

            set_projection_viewport(x, y, w, h);
            rect(bmp, x - 1, y - 1, x + w, y + h, makecol(255, 0, 0));
            set_clip_rect(bmp, x, y, x + w - 1, y + h - 1);

            /* calculate the in-front vector */
            xfront = (float)(Math.Sin(heading) * Math.Cos(pitch));
            yfront = (float)Math.Sin(pitch);
            zfront = (float)(Math.Cos(heading) * Math.Cos(pitch));

            /* rotate the up vector around the in-front vector by the roll angle */
            get_vector_rotation_matrix_f(ref roller, xfront, yfront, zfront,
                                         (float)(roll * 128.0 / M_PI));
            apply_matrix_f(ref roller, 0, -1, 0, ref xup, ref yup, ref zup);

            /* build the camera matrix */
            get_camera_matrix_f(ref camera,
                                xpos, ypos, zpos,       /* camera position */
                                xfront, yfront, zfront, /* in-front vector */
                                xup, yup, zup,          /* up vector */
                                fov,                    /* field of view */
                                aspect);                /* aspect ratio */

            /* draw the grid of squares */
            for (x = 0; x < GRID_SIZE; x++)
            {
                for (y = 0; y < GRID_SIZE; y++)
                {
                    draw_square(bmp, ref camera, x, y);
                }
            }

            /* overlay some text */
            set_clip_rect(bmp, 0, 0, bmp.w, bmp.h);
            textprintf_ex(bmp, font, 0, 0, makecol(0, 0, 0), -1,
                          string.Format("Viewport width: {0} (w/W changes)", viewport_w));
            textprintf_ex(bmp, font, 0, 8, makecol(0, 0, 0), -1,
                          string.Format("Viewport height: {0} (h/H changes)", viewport_h));
            textprintf_ex(bmp, font, 0, 16, makecol(0, 0, 0), -1,
                          string.Format("Field of view: {0} (f/F changes)", fov));
            textprintf_ex(bmp, font, 0, 24, makecol(0, 0, 0), -1,
                          string.Format("Aspect ratio: {0:0.00} (a/A changes)", aspect));
            textprintf_ex(bmp, font, 0, 32, makecol(0, 0, 0), -1,
                          string.Format("X position: {0:0.00} (x/X changes)", xpos));
            textprintf_ex(bmp, font, 0, 40, makecol(0, 0, 0), -1,
                          string.Format("Y position: {0:0.00} (y/Y changes)", ypos));
            textprintf_ex(bmp, font, 0, 48, makecol(0, 0, 0), -1,
                          string.Format("Z position: {0:0.00} (z/Z changes)", zpos));
            textprintf_ex(bmp, font, 0, 56, makecol(0, 0, 0), -1,
                          string.Format("Heading: {0:0.00} deg (left/right changes)", DEG(heading)));
            textprintf_ex(bmp, font, 0, 64, makecol(0, 0, 0), -1,
                          string.Format("Pitch: {0:0.00} deg (pgup/pgdn changes)", DEG(pitch)));
            textprintf_ex(bmp, font, 0, 72, makecol(0, 0, 0), -1,
                          string.Format("Roll: {0:0.00} deg (r/R changes)", DEG(roll)));
            textprintf_ex(bmp, font, 0, 80, makecol(0, 0, 0), -1,
                          string.Format("Front vector: {0:0.00}, {1:0.00}, {2:0.00}", xfront, yfront, zfront));
            textprintf_ex(bmp, font, 0, 88, makecol(0, 0, 0), -1,
                          string.Format("Up vector: {0:0.00}, {1:0.00}, {2:0.00}", xup, yup, zup));
            textprintf_ex(bmp, font, 0, 96, makecol(0, 0, 0), -1,
                          string.Format("Frames per second: {0}", fps));
        }
示例#7
0
        /* render a tile of the grid */
        static void draw_square(BITMAP bmp, ref MATRIX_f camera, int x, int z)
        {
            V3D_f[] _v = new V3D_f[4], _vout = new V3D_f[8], _vtmp = new V3D_f[8];
            V3D_f[] v = new V3D_f[4], vout = new V3D_f[8], vtmp = new V3D_f[8];
            int[]   flags = new int[4], _out = new int[8];
            int     c, vc = 0;

            for (c = 0; c < 4; c++)
            {
                v[c] = _v[c];
            }

            for (c = 0; c < 8; c++)
            {
                vout[c] = _vout[c];
                vtmp[c] = _vtmp[c];
            }

            /* set up four vertices with the world-space position of the tile */
            v[0].x = x - GRID_SIZE / 2;
            v[0].y = 0;
            v[0].z = z - GRID_SIZE / 2;

            v[1].x = x - GRID_SIZE / 2 + 1;
            v[1].y = 0;
            v[1].z = z - GRID_SIZE / 2;

            v[2].x = x - GRID_SIZE / 2 + 1;
            v[2].y = 0;
            v[2].z = z - GRID_SIZE / 2 + 1;

            v[3].x = x - GRID_SIZE / 2;
            v[3].y = 0;
            v[3].z = z - GRID_SIZE / 2 + 1;

            /* apply the camera matrix, translating world space . view space */
            for (c = 0; c < 4; c++)
            {
                apply_matrix_f(ref camera, v[c].x, v[c].y, v[c].z,
                               ref v[c].x, ref v[c].y, ref v[c].z);

                flags[c] = 0;

                /* set flags if this vertex is off the edge of the screen */
                if (v[c].x < -v[c].z)
                {
                    flags[c] |= 1;
                }
                else if (v[c].x > v[c].z)
                {
                    flags[c] |= 2;
                }

                if (v[c].y < -v[c].z)
                {
                    flags[c] |= 4;
                }
                else if (v[c].y > v[c].z)
                {
                    flags[c] |= 8;
                }

                if (v[c].z < 0.1)
                {
                    flags[c] |= 16;
                }
            }

            /* quit if all vertices are off the same edge of the screen */
            if ((flags[0] & flags[1] & flags[2] & flags[3]) != 0)
            {
                return;
            }

            //if ((flags[0] | flags[1] | flags[2] | flags[3]) != 0)
            //{
            //    /* clip if any vertices are off the edge of the screen */
            //    //vc = clip3d_f(POLYTYPE_FLAT, 0.1f, 0.1f, 4, v,
            //    //  vout, vtmp, _out);
            //    IntPtr w = Marshal.AllocHGlobal(24 * 4);
            //    IntPtr wout = Marshal.AllocHGlobal(24 * 8);
            //    IntPtr wtmp = Marshal.AllocHGlobal(24 * 8);
            //    IntPtr iout = Marshal.AllocHGlobal(sizeof(Int32) * 8);
            //    vc = clip3d_f(POLYTYPE_FLAT, 0.1f, 0.1f, 4, w, wout, wtmp, iout);

            //    if (vc <= 0)
            //        return;
            //}
            //else
            //{
            /* no need to bother clipping this one */
            vout[0] = v[0];
            vout[1] = v[1];
            vout[2] = v[2];
            vout[3] = v[3];

            vc = 4;
            //}

            /* project view space -> screen space */
            for (c = 0; c < vc; c++)
            {
                persp_project_f(vout[c].x, vout[c].y, vout[c].z,
                                ref vout[c].x, ref vout[c].y);
            }

            /* set the color */
            vout[0].c = ((x + z) & 1) > 0 ? makecol(0, 255, 0) : makecol(255, 255, 0);

            /* render the polygon */
            //ManagedPointer wout = Marshal.AllocHGlobal(4 * sizeof(Int32));
            //IntPtr[] wout = new IntPtr[4];
            V3D_f[] wout = new V3D_f[4];
            V3D_p   v0   = new V3D_p(vout[0].x, vout[0].y, vout[0].z, vout[0].u, vout[0].v, vout[0].c);
            V3D_p   v1   = new V3D_p(vout[1].x, vout[1].y, vout[1].z, vout[1].u, vout[1].v, vout[1].c);
            V3D_p   v2   = new V3D_p(vout[2].x, vout[2].y, vout[2].z, vout[2].u, vout[2].v, vout[2].c);
            V3D_p   v3   = new V3D_p(vout[3].x, vout[3].y, vout[3].z, vout[3].u, vout[3].v, vout[3].c);
            //ManagedPointer v0 = Marshal.AllocHGlobal(6 * sizeof(Int32));
            //ManagedPointer v1 = Marshal.AllocHGlobal(6 * sizeof(Int32));
            //ManagedPointer v2 = Marshal.AllocHGlobal(6 * sizeof(Int32));
            //ManagedPointer v3 = Marshal.AllocHGlobal(6 * sizeof(Int32));
            //copyVertex(vout[0], v0);
            //copyVertex(vout[1], v1);
            //copyVertex(vout[2], v2);
            //copyVertex(vout[3], v3);
            //wout.WritePointer(0, v0);
            //wout.WritePointer(sizeof(Int32), v1);
            //wout.WritePointer(2 * sizeof(Int32), v2);
            //wout.WritePointer(3 * sizeof(Int32), v3);
            //wout[0] = v0;
            //wout[1] = v1;
            //wout[2] = v2;
            //wout[3] = v3;
            //wout[0] = vout[0];
            //wout[1] = vout[1];
            //wout[2] = vout[2];
            //wout[3] = vout[3];
            //polygon3d_f(bmp, POLYTYPE_FLAT, NULL, vc, vout);
            //polygon3d_f(bmp, POLYTYPE_FLAT, NULL, vc, ref wout);
            //IntPtr[] wwout = new IntPtr[4];
            //wwout[0] = v0;
            //wwout[1] = v1;
            //wwout[2] = v2;
            //wwout[3] = v3;
            ManagedPointer wwout = new ManagedPointer(4 * sizeof(Int32));

            wwout.WritePointer(0, v0);
            wwout.WritePointer(sizeof(Int32), v1);
            wwout.WritePointer(2 * sizeof(Int32), v2);
            wwout.WritePointer(3 * sizeof(Int32), v3);
            polygon3d_f(bmp, POLYTYPE_FLAT, NULL, 4, wwout.pointer);
            //quad3d_f(bmp, POLYTYPE_FLAT, NULL, ref vout[0], ref vout[1], ref vout[2], ref vout[3]);
        }
示例#8
0
        static int Main()
        {
            ZBUFFER  zbuf;
            BITMAP   buffer;
            PALETTE  pal = new PALETTE();
            MATRIX_f matrix1 = new MATRIX_f(), matrix2 = new MATRIX_f();

            V3D_f[] x1 = new V3D_f[8], x2 = new V3D_f[8];

            int i;
            int c = GFX_AUTODETECT;
            int w, h, bpp;

            int   frame = 0;
            float fps   = 0.0F;

            float rx1, ry1, rz1;    /* cube #1 rotations */
            float drx1, dry1, drz1; /* cube #1 rotation speed */
            float rx2, ry2, rz2;    /* cube #2 rotations */
            float drx2, dry2, drz2; /* cube #1 rotation speed */
            float tx  = 16.0F;      /* x shift between cubes */
            float tz1 = 100.0F;     /* cube #1 z coordinate */
            float tz2 = 105.0F;     /* cube #2 z coordinate */

            if (allegro_init() != 0)
            {
                return(1);
            }
            install_keyboard();
            install_mouse();
            install_timer();

            LOCK_VARIABLE(t);
            LOCK_FUNCTION(t_tick);

            install_int(tick, 10);

            /* color 0 = black */
            pal[0].r = pal[0].g = pal[0].b = 0;
            /* color 1 = red */
            pal[1].r = 255;
            pal[1].g = pal[1].b = 0;

            /* copy the desktop palette */
            for (i = 1; i < 64; i++)
            {
                pal[i] = desktop_palette[i];
            }

            /* make a blue gradient */
            for (i = 64; i < 96; i++)
            {
                pal[i].b = (byte)((i - 64) * 2);
                pal[i].g = pal[i].r = 0;
            }

            /* make a green gradient */
            for (i = 96; i < 128; i++)
            {
                pal[i].g = (byte)((i - 96) * 2);
                pal[i].r = pal[i].b = 0;
            }

            /* set the graphics mode */
            if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0)
            {
                set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
                allegro_message(string.Format("Unable to set any graphic mode\n{0}\n", allegro_error));
                return(1);
            }
            set_palette(desktop_palette);

            w   = SCREEN_W;
            h   = SCREEN_H;
            bpp = bitmap_color_depth(screen);
            if (gfx_mode_select_ex(ref c, ref w, ref h, ref bpp) == 0)
            {
                allegro_exit();
                return(1);
            }

            set_color_depth(bpp);

            if (set_gfx_mode(c, w, h, 0, 0) != 0)
            {
                set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
                allegro_message(string.Format("Error setting graphics mode\n{0}\n", allegro_error));
                return(1);
            }

            set_palette_range(pal, 0, 127, FALSE);

            /* double buffer the animation and create the Z-buffer */
            buffer = create_bitmap(SCREEN_W, SCREEN_H);
            zbuf   = create_zbuffer(buffer);
            set_zbuffer(zbuf);

            /* set up the viewport for the perspective projection */
            set_projection_viewport(0, 0, SCREEN_W, SCREEN_H);

            /* compute rotations and speed rotation */
            rx1 = ry1 = rz1 = 0.0F;
            rx2 = ry2 = rz2 = 0.0F;

            drx1 = (float)(((AL_RAND() & 31) - 16) / 4.0);
            dry1 = (float)(((AL_RAND() & 31) - 16) / 4.0);
            drz1 = (float)(((AL_RAND() & 31) - 16) / 4.0);

            drx2 = (float)(((AL_RAND() & 31) - 16) / 4.0);
            dry2 = (float)(((AL_RAND() & 31) - 16) / 4.0);
            drz2 = (float)(((AL_RAND() & 31) - 16) / 4.0);

            /* set the transformation matrices */
            get_transformation_matrix_f(ref matrix1, 1.0F, rx1, ry1, rz1, tx, 0.0F, tz1);
            get_transformation_matrix_f(ref matrix2, 1.0F, rx2, ry2, rz2, -tx, 0.0F, tz2);

            /* set colors */
            for (i = 0; i < 8; i++)
            {
                x1[i].c = palette_color[cube1[i].c];
                x2[i].c = palette_color[cube2[i].c];
            }

            /* main loop */
            while (true)
            {
                clear_bitmap(buffer);
                clear_zbuffer(zbuf, 0.0F);

                anim_cube(matrix1, matrix2, x1, x2);
                draw_cube(buffer, x1, x2);

                /* update transformation matrices */
                rx1 += drx1;
                ry1 += dry1;
                rz1 += drz1;
                rx2 += drx2;
                ry2 += dry2;
                rz2 += drz2;
                get_transformation_matrix_f(ref matrix1, 1.0F, rx1, ry1, rz1, tx, 0.0F, tz1);
                get_transformation_matrix_f(ref matrix2, 1.0F, rx2, ry2, rz2, -tx, 0.0F, tz2);

                //textprintf_ex(buffer, font, 10, 1, palette_color[1], 0,
                //  string.Format("Z-buffered polygons ({0:.0} fps)", fps));
                textprintf_ex(buffer, font, 10, 1, makecol(255, 0, 0), 0,
                              string.Format("Z-buffered polygons ({0:.0} fps)", fps));

                vsync();
                blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                frame++;

                if (t > 100)
                {
                    fps   = (float)((100.0 * frame) / t);
                    t     = 0;
                    frame = 0;
                }

                if (keypressed())
                {
                    if ((readkey() & 0xFF) == 27)
                    {
                        break;
                    }
                }
            }

            destroy_bitmap(buffer);
            destroy_zbuffer(zbuf);

            return(0);
        }
示例#9
0
    static void draw_square(BITMAP bmp, MATRIX_f camera, int x, int z)
    {
        V3D_f[] _v = new V3D_f[4], _vout = new V3D_f[8], _vtmp = new V3D_f[8];
        //V3D_f *v[4], *vout[8], *vtmp[8];
        int[] flags = new int[4], _out = new int[8];
        int   c, vc;

        //for (c=0; c<4; c++)
        //   v[c] = &_v[c];

        //for (c=0; c<8; c++) {
        //   vout[c] = &_vout[c];
        //   vtmp[c] = &_vtmp[c];
        //}

        /* set up four vertices with the world-space position of the tile */
        _v[0].x = x - GRID_SIZE / 2;
        _v[0].y = 0;
        _v[0].z = z - GRID_SIZE / 2;

        _v[1].x = x - GRID_SIZE / 2 + 1;
        _v[1].y = 0;
        _v[1].z = z - GRID_SIZE / 2;

        _v[2].x = x - GRID_SIZE / 2 + 1;
        _v[2].y = 0;
        _v[2].z = z - GRID_SIZE / 2 + 1;

        _v[3].x = x - GRID_SIZE / 2;
        _v[3].y = 0;
        _v[3].z = z - GRID_SIZE / 2 + 1;

        /* apply the camera matrix, translating world space -> view space */
        for (c = 0; c < 4; c++)
        {
            apply_matrix_f(ref camera, _v[c].x, _v[c].y, _v[c].z,
                           ref _v[c].x, ref _v[c].y, ref _v[c].z);

            flags[c] = 0;

            //   /* set flags if this vertex is off the edge of the screen */
            if (_v[c].x < -_v[c].z)
            {
                flags[c] |= 1;
            }
            else if (_v[c].x > _v[c].z)
            {
                flags[c] |= 2;
            }

            if (_v[c].y < -_v[c].z)
            {
                flags[c] |= 4;
            }
            else if (_v[c].y > _v[c].z)
            {
                flags[c] |= 8;
            }

            if (_v[c].z < 0.1)
            {
                flags[c] |= 16;
            }
        }

        /* quit if all vertices are off the same edge of the screen */
        if ((flags[0] & flags[1] & flags[2] & flags[3]) > 0)
        {
            return;
        }

        if ((flags[0] | flags[1] | flags[2] | flags[3]) > 0)
        {
            /* clip if any vertices are off the edge of the screen */
            //vc = clip3d_f(POLYTYPE_FLAT, 0.1, 0.1, 4, (AL_CONST V3D_f **)v,
            //      vout, vtmp, out);

            //if (vc <= 0)
            return;
        }
        else
        {
            /* no need to bother clipping this one */
            _vout[0] = _v[0];
            _vout[1] = _v[1];
            _vout[2] = _v[2];
            _vout[3] = _v[3];

            vc = 4;
        }

        /* project view space -> screen space */
        //for (c=0; c<vc; c++)
        //   persp_project_f(vout[c].x, vout[c].y, vout[c].z,
        //           &vout[c].x, &vout[c].y);

        /* set the color */
        _vout[0].c = ((x + z) & 1) > 0 ? makecol(0, 255, 0) : makecol(255, 255, 0);

        vc = 4;

        /* render the polygon */
        //IntPtr p = new IntPtr();
        //Marshal.

        //polygon3d_f(bmp, POLYTYPE_FLAT, NULL, vc, ref _vout[0]);
        //polygon3d_f(bmp, POLYTYPE_FLAT, NULL, vc, p);
    }
示例#10
0
        static int Main()
        {
            BITMAP   buffer;
            PALETTE  pal = new PALETTE();
            MATRIX_f matrix = new MATRIX_f(), matrix1 = new MATRIX_f(), matrix2 = new MATRIX_f(), matrix3 = new MATRIX_f();
            int      rx = 0, ry = 0, tz = 40;
            int      rot = 0, inc = 1;
            int      i, j, k;

            int   frame = 0;
            float fps   = 0.0f;

            if (allegro_init() != 0)
            {
                return(1);
            }
            install_keyboard();
            install_mouse();
            install_timer();

            LOCK_VARIABLE(t);
            LOCK_FUNCTION(t_tick);

            install_int(t_tick, 10);

            /* set up graphics mode */
            if (init_gfx(pal) > 0)
            {
                return(1);
            }

            set_palette(pal);

            /* initialize buffers and viewport */
            buffer = create_bitmap(SCREEN_W, SCREEN_H);
            create_scene(24 * MAX_CUBES * MAX_CUBES * MAX_CUBES,
                         6 * MAX_CUBES * MAX_CUBES * MAX_CUBES);
            set_projection_viewport(0, 0, SCREEN_W, SCREEN_H);

            /* initialize pointers */
            for (j = 0; j < 4; j++)
            {
                //pv[j] = &v[j];
                pv[j] = v[j];
            }

            for (j = 0; j < 12; j++)
            {
                //pvtmp[j] = &vtmp[j];
                //pvout[j] = &vout[j];
                pvtmp[j] = vtmp[j];
                pvout[j] = vout[j];
            }


            while (!key[KEY_ESC])
            {
                clear_bitmap(buffer);
                clear_scene(buffer);

                /* matrix2: rotates cube */
                get_rotation_matrix_f(ref matrix2, rx, ry, 0);
                /* matrix3: turns head right/left */
                get_rotation_matrix_f(ref matrix3, 0, rot, 0);

                for (k = MAX_CUBES - 1; k >= 0; k--)
                {
                    for (j = 0; j < MAX_CUBES; j++)
                    {
                        for (i = 0; i < MAX_CUBES; i++)
                        {
                            /* matrix1: locates cubes */
                            get_translation_matrix_f(ref matrix1, j * 40 - MAX_CUBES * 20 + 20,
                                                     i * 40 - MAX_CUBES * 20 + 20, tz + k * 40);

                            /* matrix: rotates cube THEN locates cube THEN turns
                             * head right/left */
                            matrix_mul_f(ref matrix2, ref matrix1, out matrix);
                            matrix_mul_f(ref matrix, ref matrix3, out matrix);

                            /* cubes are just added to the scene.
                             * No sorting is done at this stage.
                             */
                            draw_cube(ref matrix, 6);
                        }
                    }
                }

                /* sorts and renders polys */
                render_scene();
                textprintf_ex(buffer, font, 2, 2, palette_color[1], -1,
                              string.Format("({0:f1} fps)", fps));
                blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
                frame++;

                /* manage cubes movement */
                tz -= 2;
                if (tz == 0)
                {
                    tz = 40;
                }
                rx  += 4;
                ry  += 4;
                rot += inc;
                if ((rot >= 25) || (rot <= -25))
                {
                    inc = -inc;
                }

                /* computes fps */
                if (t > 100)
                {
                    fps   = (100.0f * frame) / t;
                    t     = 0;
                    frame = 0;
                }
            }

            destroy_bitmap(buffer);
            destroy_scene();

            return(0);
        }
示例#11
0
        /* draw_cube:
         *  Translates, rotates, clips, projects, culls backfaces and draws a cube.
         */
        static void draw_cube(ref MATRIX_f matrix, int num_poly)
        {
            int i, j, nv;

            int[] _out = new int[12];
            //IntPtr _out = Marshal.AllocHGlobal(12 * sizeof(Int32));

            for (i = 0; i < num_poly; i++)
            {
                for (j = 0; j < 4; j++)
                {
                    v[j] = vertex[cube[i].v[j]];
                    V3D_t _v = new V3D_t(v[j]);
                    float x = 0, y = 0, z = 0;
                    apply_matrix_f(ref matrix, _v.x, _v.y, _v.z,
                                   ref x, ref y, ref z);
                    _v.x = x;
                    _v.y = y;
                    _v.z = z;
                }

                for (int t = 0; t < pv.Length; t++)
                {
                    pv[t] = v[t];
                }
                for (int t = 0; t < pvout.Length; t++)
                {
                    pvout[t] = new V3D_t(0, 0, 0, 0, 0, makecol(255, 255, 0));
                    pvtmp[t] = new V3D_t(0, 0, 0, 0, 0, makecol(255, 255, 0));
                }


                //   /* nv: number of vertices after clipping is done */
                //nv = clip3d_f(POLYTYPE_GCOL, 0.1f, 1000.0f, 4, pv, pvout,
                //      pvtmp, _out);
                //if (nv > 0)
                //{
                //    for (j = 0; j < nv; j++)
                //    {
                //        float x = 0f, y = 0f;
                //        V3D_t _vout = new V3D_t(vout[j]);
                //        persp_project_f(_vout.x, _vout.y, _vout.z, ref x,
                //                ref y);
                //        _vout.x = x;
                //        _vout.y = y;
                //    }

                //    V3D_t vt0 = new V3D_t(vout[0]);
                //    V3D_t vt1 = new V3D_t(vout[0]);
                //    V3D_t vt2 = new V3D_t(vout[0]);

                //    V3D_f[] _vout2 = new V3D_f[] {
                //           new V3D_f(vt0.x, vt0.y, vt0.z, vt0.u, vt0.v, vt0.c),
                //           new V3D_f(vt1.x, vt1.y, vt1.z, vt1.u, vt1.v, vt1.c),
                //           new V3D_f(vt2.x, vt2.y, vt2.z, vt2.u, vt2.v, vt2.c)
                //       };

                //    //if (polygon_z_normal_f(ref _vout2[0], ref _vout2[1], ref _vout2[2]) > 0)
                //    //    scene_polygon3d_f(POLYTYPE_GCOL, NULL, nv, pvout);
                //}
            }
        }