Mail Archives: djgpp/1998/06/27/23:16:01
Kevin Smith wrote:
> its only got 4 verticies. not 8. just like a sqaure except using
> allegros quad function to draw it.
I hope you were requesting an example for that, it's attached ;)
uses gouraud shading for a quad. use x,X,y,Y,z,Z keys to rotate
#include <allegro.h>
int main(void)
{
V3D_f points[4]; //since we want a square, we have 4 vertices
V3D_f Tpoints[4]; // 4 transformed vertices
MATRIX_f rot; //the matrix we will construct for rotation
MATRIX_f tr; //the matrix we will construct for translation
float Xr=0,Yr=0,Zr=0; //rotations on 3 axis
BITMAP* back; //backbuffer
int i;
//allegro init stuff
allegro_init();
install_keyboard();
set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
//initialise our data
//make a better palette
for(i=0;i<64;i++)
{
desktop_palette[i].r=0;
desktop_palette[i].g=0;
desktop_palette[i].b=i;
}
for(i=64;i<128;i++)
{
desktop_palette[i].r=(i-64)/2;
desktop_palette[i].g=0;
desktop_palette[i].b=34;
}
for(i=128;i<192;i++)
{
desktop_palette[i].r=0;
desktop_palette[i].g=(i-128)/2;
desktop_palette[i].b=34;
}
for(i=192;i<256;i++)
{
desktop_palette[i].r=i-192;
desktop_palette[i].g=i-192;
desktop_palette[i].b=34;
}
//create buffers
set_palette(desktop_palette);
back=create_bitmap(SCREEN_W,SCREEN_H);
rgb_map=malloc(sizeof(RGB_MAP));
create_rgb_table(rgb_map,desktop_palette,NULL);
//create the square
points[0].z=points[1].z=points[2].z=points[3].z=0;
points[0].y=points[1].y=-100;
points[2].y=points[3].y=100;
points[0].x=points[3].x=-100;
points[1].x=points[2].x=100;
//color stuff for gouraud shading
Tpoints[0].c=0xababcc; //some blue
Tpoints[1].c=0x4421aa;
Tpoints[2].c=0x222244;
Tpoints[3].c=0xac11af;
set_projection_viewport(0,0,SCREEN_W,SCREEN_H);
while(!key[KEY_ESC])
{
clear(back);
if(key[KEY_X])
{
if(key_shifts&KB_SHIFT_FLAG)
Xr+=0.5;
else
Xr-=0.5;
}
if(key[KEY_Y])
{
if(key_shifts&KB_SHIFT_FLAG)
Yr+=0.5;
else
Yr-=0.5;
}
if(key[KEY_Z])
{
if(key_shifts&KB_SHIFT_FLAG)
Zr+=0.5;
else
Zr-=0.5;
}
get_rotation_matrix_f(&rot,Xr,Yr,Zr); //construct our transformation
matrixes
get_translation_matrix_f(&tr,0,0,200);
for(i=0;i<4;i++)
{
//apply matrices and do a perspective projection
apply_matrix_f(&rot,points[i].x,points[i].y,points[i].z,&Tpoints[i].x,&Tpoints[i].y,&Tpoints[i].z);
apply_matrix_f(&tr,Tpoints[i].x,Tpoints[i].y,Tpoints[i].z,&Tpoints[i].x,&Tpoints[i].y,&Tpoints[i].z);
persp_project_f(Tpoints[i].x,Tpoints[i].y,Tpoints[i].z,&Tpoints[i].x,&Tpoints[i].y);
}
//draw
quad3d_f(back,POLYTYPE_GRGB,NULL,&Tpoints[0],
&Tpoints[1],&Tpoints[2],&Tpoints[3]);
blit(back,screen,0,0,0,0,SCREEN_W,SCREEN_H);
}
free(rgb_map);
destroy_bitmap(back);
return 0;
}
- Raw text -