2004-07-27 22:56:38 UTC
previous
next
#include "allegro.h"
typedef struct point3d
{
float x, y, z;
} point3d;
typedef struct point2d
{
float x, y;
} point2d;
typedef struct cubeline
{
int a, b;
} cubeline;
int main(void)
{
allegro_init();
install_keyboard();
set_color_depth(desktop_color_depth());
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Couldn't set graphics mode!");
return 1;
}
set_window_title("Circles3d");
BITMAP* buffer = create_bitmap(800, 600);
clear_to_color(buffer, makecol(0, 0, 0));
// Makes the 3d -> 2d mapping thinie give 2d values that are on the screen
set_projection_viewport(0, 0, 800, 600);
// A CUBE HAS 8 COURNERS.
point3d inpoints[8] = {
{-16, -16, -16},
{-16, 16.0, -16},
{16.0, 16.0, -16},
{16.0, -16, -16},
{-16, -16, 16.0},
{-16, 16.0, 16.0},
{16.0, 16.0, 16.0},
{16.0, -16, 16.0}};
// A CUBE HAS 12 LINES.
cubeline cubelines[12] = {
{0, 1},
{1, 2},
{2, 3},
{3, 0},
{4, 5},
{5, 6},
{6, 7},
{7, 4},
{0, 4},
{1, 5},
{2, 6},
{3, 7}};
point2d outpoints[8];
// Translate 3d to 2d
for (int i = 0; i < 8; i++) {
persp_project_f(inpoints[i].x, inpoints[i].y, inpoints[i].z, &outpoints[i].x, &outpoints[i].y);
textprintf_ex(buffer, font, 0, i*10, makecol(255, 255, 255), -1, "persp_project_f(%g, %g, %g, %g, %g);", inpoints[i].x, inpoints[i].y, inpoints[i].z, outpoints[i].x, outpoints[i].y);
}
// Put lines between all the points on the screen
for (int i = 0; i < 12; i++) {
line(buffer, outpoints[cubelines[i].a].x, outpoints[cubelines[i].a].y, outpoints[cubelines[i].b].x, outpoints[cubelines[i].b].y, makecol(255, 255, 255));
}
blit(buffer, screen, 0, 0, 0, 0, 800, 600);
readkey();
}
END_OF_MAIN();