Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created July 7, 2011 00:15
Show Gist options
  • Save neuro-sys/1068650 to your computer and use it in GitHub Desktop.
Save neuro-sys/1068650 to your computer and use it in GitHub Desktop.
3dshit
#include <allegro.h>
#include <stdio.h>
#define V_DIST 100
#define FAR_Z 1000
#define NEAR_Z 20
typedef struct VECTOR3D_TYP {
union {
float M[3];
struct {
float x, y,z;
};
};
} VECTOR3D;
typedef struct STAR3D_TYP {
VECTOR3D vec;
} STAR3D;
typedef struct SPACE_TYP {
STAR3D* stars;
int num_of_stars;
} SPACE;
VECTOR3D cube_model[8] = {
{ 1,-1, 1},
{ 1, 1, 1},
{-1, 1, 1},
{-1,-1, 1},
{ 1,-1,-1},
{ 1, 1,-1},
{-1, 1,-1},
{-1,-1,-1},
};
int cube_faces[6][4] = {
{0, 4, 5, 1},
{1, 5, 6, 2},
{2, 6, 7, 3},
{3, 7, 4, 0},
{4, 5, 6, 7},
{0, 1, 2, 3}
};
void cube_local_to_world(VECTOR3D* cube_local, VECTOR3D world, VECTOR3D* cube_world, int scale)
{
int i;
for (i = 0; i < 8; i++) {
cube_world->x = cube_local->x * scale + world.x;
cube_world->y = cube_local->y * scale + world.y;
cube_world->z = cube_local->z * scale + world.z;
}
}
void cube_draw_face(VECTOR3D* cube_world, int faces[4])
{
int i;
int px, py;
int pv2[4][2];
for (i = 0; i < 4; i++) {
VECTOR3D v = cube_world[faces[i]];
px = screen->w / 2 + (float) V_DIST * v.x / v.z;
py = screen->h / 2 + (float) V_DIST * v.y / v.z;
pv2[i][0] = px;
pv2[i][1] = py;
}
for (i = 0; i < 3; i++) {
line(screen, pv2[i][0], pv2[i][1], pv2[i+1][0], pv2[i+1][1], 0xffffff);
}
line(screen, pv2[3][0], pv2[3][1], pv2[0][0], pv2[0][1], 0xffffff);
}
void cube_draw(VECTOR3D* cube_world)
{
int i;
for (i = 0; i < 6; i++) {
cube_draw_face(cube_world, cube_faces[i]);
}
}
volatile long timer_counter;
void timer_func()
{
timer_counter++;
}
END_OF_FUNCTION(timer_func);
void vector_draw(VECTOR3D v)
{
int px, py;
px = screen->w / 2 + (float) V_DIST * v.x / v.z;
py = screen->h / 2 + (float) V_DIST * v.y / v.z;
if (px < 0 || px >= screen->w || py < 0 || py >= screen->h)
return;
putpixel(screen, px, py, 0xffffff);
}
void space_draw(SPACE* space)
{
int i;
for (i = 0; i < space->num_of_stars; i++) {
VECTOR3D cube_world[8];
cube_local_to_world(cube_model, space->stars[i].vec, cube_world, 5);
cube_draw(cube_world);
vector_draw(space->stars[i].vec);
}
}
void space_move(SPACE* space)
{
int i;
for (i = 0; i < space->num_of_stars; i++) {
space->stars[i].vec.z--;
if (space->stars[i].vec.z < NEAR_Z)
space->stars[i].vec.z = FAR_Z;
}
}
SPACE* space_init(int num)
{
int i;
SPACE* space;
printf("space_init... ");
space = malloc(sizeof(SPACE));
space->stars = malloc(sizeof(STAR3D) * num);
space->num_of_stars = num;
for (i = 0; i < num; i++) {
space->stars[i].vec.x = rand() % (screen->w * 2) - screen->w;
space->stars[i].vec.y = rand() % (screen->h * 2) - screen->h;
space->stars[i].vec.z = rand() % (FAR_Z - NEAR_Z) + NEAR_Z;
}
printf("OK.\n");
return space;
}
void space_destroy(SPACE* space)
{
free(space->stars);
free(space);
}
int main()
{
allegro_init();
install_keyboard();
install_timer();
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
set_color_depth(32);
SPACE* space = space_init(10);
install_int_ex(timer_func,MSEC_TO_TIMER(1000/60));
LOCK_VARIABLE(timer_counter);
LOCK_FUNCTION(timer_func);
while(!keypressed()) {
clear_bitmap(screen);
space_draw(space);
space_move(space);
rest(1000/60);
}
space_destroy(space);
return 0;
}
END_OF_MAIN()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment