public static void copyVertex(V3D_f _in, ManagedPointer _out) { _out.WriteInt(0, (int)_in.x); _out.WriteInt(sizeof(Int32), (int)_in.y); _out.WriteInt(2 * sizeof(Int32), (int)_in.z); _out.WriteInt(5 * sizeof(Int32), (int)_in.c); }
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); }
/* 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]); }
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); }