File: exzbuf.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (267 lines) | stat: -rw-r--r-- 6,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 *    Example program for the Allegro library, by Bertrand Coconnier.
 *
 *    This program demonstrates how to use Z-buffered polygons and 
 *    floating point 3D math routines. It also provides a simple way
 *    to compute fps (frames per second) using the interrupt handler.
 *
 */


#include <stdio.h>
#include "allegro.h"



typedef struct FACE
{
  int v1, v2, v3, v4;
} FACE;


V3D_f cube1[] =
{
   { -32., -32., -32., 0., 0., 72},
   { -32.,  32., -32., 0., 0., 80},
   {  32.,  32., -32., 0., 0., 95},
   {  32., -32., -32., 0., 0., 88},
   { -32., -32.,  32., 0., 0., 72},
   { -32.,  32.,  32., 0., 0., 80},
   {  32.,  32.,  32., 0., 0., 95},
   {  32., -32.,  32., 0., 0., 88}
};


V3D_f cube2[] =
{
   { -32., -32., -32., 0., 0., 104},
   { -32.,  32., -32., 0., 0., 112},
   {  32.,  32., -32., 0., 0., 127},
   {  32., -32., -32., 0., 0., 120},
   { -32., -32.,  32., 0., 0., 104},
   { -32.,  32.,  32., 0., 0., 112},
   {  32.,  32.,  32., 0., 0., 127},
   {  32., -32.,  32., 0., 0., 120}
};


FACE faces[] =
{
   { 2, 1, 0, 3 },
   { 4, 5, 6, 7 },
   { 0, 1, 5, 4 },
   { 2, 3, 7, 6 },
   { 4, 7, 3, 0 },
   { 1, 2, 6, 5 }
};



volatile int t;


/* timer interrupt handler */
void tick(void)
{
   t++;
}

END_OF_FUNCTION(tick);



/* update cube positions */
void anim_cube(MATRIX_f* matrix1, MATRIX_f* matrix2, V3D_f x1[], V3D_f x2[])
{
   int i;

   for (i=0; i<8; i++) {
      apply_matrix_f(matrix1, cube1[i].x, cube1[i].y, cube1[i].z, &(x1[i].x), &(x1[i].y), &(x1[i].z));
      apply_matrix_f(matrix2, cube2[i].x, cube2[i].y, cube2[i].z, &(x2[i].x), &(x2[i].y), &(x2[i].z));
      persp_project_f(x1[i].x, x1[i].y, x1[i].z, &(x1[i].x), &(x1[i].y));
      persp_project_f(x2[i].x, x2[i].y, x2[i].z, &(x2[i].x), &(x2[i].y));
   }
}



/* cull backfaces and draw cubes */
void draw_cube(BITMAP* buffer, V3D_f x1[], V3D_f x2[])
{
   int i;
   
   for (i=0; i<6; i++) {
      V3D_f vtx1, vtx2, vtx3, vtx4;
      
      vtx1 = x1[faces[i].v1];
      vtx2 = x1[faces[i].v2];
      vtx3 = x1[faces[i].v3];
      vtx4 = x1[faces[i].v4];
      if (polygon_z_normal_f(&vtx1, &vtx2, &vtx3) > 0)
         quad3d_f(buffer, POLYTYPE_GCOL | POLYTYPE_ZBUF, NULL, &vtx1, &vtx2, &vtx3, &vtx4);

      vtx1 = x2[faces[i].v1];
      vtx2 = x2[faces[i].v2];
      vtx3 = x2[faces[i].v3];
      vtx4 = x2[faces[i].v4];
      if (polygon_z_normal_f(&vtx1, &vtx2, &vtx3) > 0)
         quad3d_f(buffer, POLYTYPE_GCOL | POLYTYPE_ZBUF, NULL, &vtx1, &vtx2, &vtx3, &vtx4);
   }
}



int main()
{
   ZBUFFER *zbuf;
   BITMAP *buffer;
   PALETTE pal;
   MATRIX_f matrix1, matrix2;
   V3D_f x1[8], x2[8];

   int i;
   int c = GFX_AUTODETECT;
   int w, h, bpp;
   
   int frame = 0;
   float fps = 0.;
   
   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.;		/* x shift between cubes */
   float tz1 = 100.;		/* cube #1 z coordinate */
   float tz2 = 105.;		/* cube #2 z coordinate */
   
   allegro_init();
   install_keyboard();
   install_mouse();
   install_timer();
   
   LOCK_VARIABLE(t);
   LOCK_FUNCTION(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 = (i-64)*2;
      pal[i].g = pal[i].r = 0;
   }

   /* make a green gradient */
   for (i=96; i<128; i++) {
      pal[i].g = (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("Unable to set any graphic mode\n%s\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(&c, &w, &h, &bpp)) {
      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("Error setting graphics mode\n%s\n", allegro_error);
      return 1;
   }

   set_palette(pal);

   /* 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.;
   rx2 = ry2 = rz2 = 0.;
   
   drx1 = ((rand() & 31) - 16) / 4.;
   dry1 = ((rand() & 31) - 16) / 4.;
   drz1 = ((rand() & 31) - 16) / 4.;

   drx2 = ((rand() & 31) - 16) / 4.;
   dry2 = ((rand() & 31) - 16) / 4.;
   drz2 = ((rand() & 31) - 16) / 4.;

   /* set the transformation matrices */
   get_transformation_matrix_f(&matrix1, 1., rx1, ry1, rz1, tx, 0., tz1);
   get_transformation_matrix_f(&matrix2, 1., rx2, ry2, rz2, -tx, 0., 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(1) {
      clear_bitmap(buffer);
      clear_zbuffer(zbuf, 0.);
      
      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(&matrix1, 1., rx1, ry1, rz1, tx, 0., tz1);
      get_transformation_matrix_f(&matrix2, 1., rx2, ry2, rz2, -tx, 0., tz2);

      textprintf(buffer, font, 10, 1, palette_color[1], "Z-buffered polygons (%.1f fps)", fps);
   
      vsync();
      blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
      frame++;
      
      if (t > 100) {
	 fps = (100. * frame) / t;
	 t = 0;
	 frame = 0;
      }

      if (keypressed()){
	 if ((readkey() & 0xFF) == 27)
	    break;
      }
   }

   destroy_bitmap(buffer);
   destroy_zbuffer(zbuf);
   
   return 0;
}

END_OF_MAIN();