Mail Archives: djgpp/1997/03/16/07:57:29
In article <bihl$LAZFsKzEwE2 AT talula DOT demon DOT co DOT uk>, Shawn Hargreaves
<Shawn AT talula DOT demon DOT co DOT uk> writes
>David Jenkins writes:
>>Could somebody send me 2 source files.
>>1-With just one simple quad3d() face drawn on the screen.
>>
>>No rotation or anything just the most SIMPLE way to get a quad3d face
>>drawn onto a screen.
>
>Here's a really basic quad drawer. You might also want to have a look at
>the excellent 3d tutorial by Tero Parvinen, which can be found on
>http://www.niksula.cs.hut.fi/~tparvine/allegro3d/.
>
>
>#include <allegro.h>
>
>int main()
>{
> /* Declare four vertices defining the corners of the quad. In a real
> * program these would of course be calculated on the fly, rather
Why doesn't the following work???
Sorry about the mess. ;-) It's just something of my own I've hacked
apart to try the quad3d_f() functions.
#include <stdio.h>
#include <allegro.h>
#include <math.h>
#include <time.h>
#define NUM_POINTS 4
#define CENT_X 160
#define CENT_Y 100
#define SPEED 1
#define LENS 256
void init_display();
void define_cube();
void draw_cube();
void rotatexyz(int, float, float);
struct points {
float x,
y,
z;
} *current, *plotted;
struct act2d {
float x,
y;
};
struct act2d v2d[4];
BITMAP *buffer;
float angle = 0.1;
void init_display()
{
int d;
PALLETE pal;
pal[0].r = 0;
pal[0].g = 0;
pal[0].b = 0;
pal[1].r = 0;
pal[1].g = 63;
pal[1].b = 0;
allegro_init();
set_gfx_mode(GFX_VGA, 320, 200, 0, 0);
set_pallete(pal);
buffer = create_bitmap(SCREEN_W, SCREEN_H);
install_keyboard();
}
void define_cube()
{
int dum1;
current[0].x = -50;
current[0].y = -50;
current[0].z = -50;
current[1].x = 50;
current[1].y = -50;
current[1].z = -50;
current[2].x = 50;
current[2].y = 50;
current[2].z = -50;
current[3].x = -50;
current[3].y = 50;
current[3].z = -50;
}
void draw_cube()
{
int d;
float dist,
cos_a,
sin_a;
angle += 0.01;
if (angle > 6.28)
angle = 0;
cos_a = cos(angle);
sin_a = sin(angle);
for (d = 0 ; d < NUM_POINTS ; d++) {
rotatexyz(d, cos_a, sin_a);
dist = 256 - plotted[d].z;
v2d[d].x = CENT_X - (LENS * plotted[d].x / dist);
v2d[d].y = CENT_Y - (LENS * plotted[d].y / dist);
}
/***************
This is where I get a "parse before vtx1 error".
*******/
V3D_f vtx1 = { v2d[0].x, v2d[0].y, 0, 0, 0, 255 };
V3D_f vtx2 = { v2d[1].x, v2d[1].y, 0, 0, 0, 10 };
V3D_f vtx3 = { v2d[2].x, v2d[2].y, 0, 0, 0, 128 };
V3D_f vtx4 = { v2d[3].x, v2d[3].y, 0, 0, 0, 160 };
quad3d_f(buffer, POLYTYPE_FLAT, NULL, &vtx1, &vtx2, &vtx3, &vtx4);
}
void rotatexyz(int d, float cos_a, float sin_a)
{
float new_x,
new_y,
new_z,
old_x,
old_y,
old_z;
old_x = current[d].x;
old_y = current[d].y;
old_z = current[d].z;
// rotate x axis
new_y = old_y * cos_a - old_z * sin_a;
new_z = old_z * cos_a + old_y * sin_a;
old_y = new_y;
old_z = new_z;
// rotate z axis
new_x = old_x * cos_a - old_y * sin_a;
plotted[d].y = old_y * cos_a + old_x * sin_a;
old_x = new_x;
// rotate y axis
plotted[d].z = old_z * cos_a - old_x * sin_a;
plotted[d].x = old_x * cos_a + old_z * sin_a;
}
int main()
{
char tim[50];
int ti;
current = malloc(sizeof(struct points) * NUM_POINTS);
plotted = malloc(sizeof(struct points) * NUM_POINTS);
init_display();
define_cube();
while (!keypressed()) {
ti = uclock();
draw_cube();
sprintf(tim, "tim=%d", (int) uclock() - ti);
textout(buffer, font, tim, 0, 0, 1);
vsync();
blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
clear(buffer);
}
return 0;
}
--
http://www.jenkinsdavid.demo.co.uk for Newbie C programers and a larf. ;-)
David Jenkins
- Raw text -