1 /* $Id: terrain.c,v 1.5 2004-08-28 23:17:45 schaffner Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Code to render cool external-scene terrain
45 #define GRID_MAX_SIZE 64
46 #define GRID_SCALE i2f(2*20)
47 #define HEIGHT_SCALE f1_0
51 g3s_uvl uvl_list1[] = { {0,0,0}, {f1_0,0,0}, {0,f1_0,0} };
52 g3s_uvl uvl_list2[] = { {f1_0,0,0}, {f1_0,f1_0,0}, {0,f1_0,0} };
57 #define HEIGHT(_i,_j) (height_array[(_i)*grid_w+(_j)])
58 #define LIGHT(_i,_j) light_array[(_i)*grid_w+(_j)]
60 //!!#define HEIGHT(_i,_j) height_array[(grid_h-1-j)*grid_w+(_i)]
61 //!!#define LIGHT(_i,_j) light_array[(grid_h-1-j)*grid_w+(_i)]
63 #define LIGHTVAL(_i,_j) (((fix) LIGHT(_i,_j))<<8)
65 g3s_point save_row[GRID_MAX_SIZE];
67 vms_vector start_point;
69 grs_bitmap *terrain_bm;
71 int terrain_outline=0;
75 int mine_tiles_drawn; //flags to tell if all 4 tiles under mine have drawn
78 // LINT: adding function prototypes
79 void build_light_table(void);
80 void free_light_table(void);
82 // ------------------------------------------------------------------------
83 void draw_cell(int i,int j,g3s_point *p0,g3s_point *p1,g3s_point *p2,g3s_point *p3)
85 g3s_point *pointlist[3];
90 uvl_list1[0].l = LIGHTVAL(i,j);
91 uvl_list1[1].l = LIGHTVAL(i,j+1);
92 uvl_list1[2].l = LIGHTVAL(i+1,j);
94 uvl_list1[0].u = (i)*f1_0/4; uvl_list1[0].v = (j)*f1_0/4;
95 uvl_list1[1].u = (i)*f1_0/4; uvl_list1[1].v = (j+1)*f1_0/4;
96 uvl_list1[2].u = (i+1)*f1_0/4; uvl_list1[2].v = (j)*f1_0/4;
98 g3_check_and_draw_tmap(3,pointlist,uvl_list1,terrain_bm,NULL,NULL);
99 if (terrain_outline) {
100 int lsave=Lighting_on;
102 gr_setcolor(BM_XRGB(31,0,0));
103 g3_draw_line(pointlist[0],pointlist[1]);
104 g3_draw_line(pointlist[2],pointlist[0]);
110 uvl_list2[0].l = LIGHTVAL(i,j+1);
111 uvl_list2[1].l = LIGHTVAL(i+1,j+1);
112 uvl_list2[2].l = LIGHTVAL(i+1,j);
114 uvl_list2[0].u = (i)*f1_0/4; uvl_list2[0].v = (j+1)*f1_0/4;
115 uvl_list2[1].u = (i+1)*f1_0/4; uvl_list2[1].v = (j+1)*f1_0/4;
116 uvl_list2[2].u = (i+1)*f1_0/4; uvl_list2[2].v = (j)*f1_0/4;
118 g3_check_and_draw_tmap(3,pointlist,uvl_list2,terrain_bm,NULL,NULL);
119 if (terrain_outline) {
120 int lsave=Lighting_on;
122 gr_setcolor(BM_XRGB(31,0,0));
123 g3_draw_line(pointlist[0],pointlist[1]);
124 g3_draw_line(pointlist[1],pointlist[2]);
125 g3_draw_line(pointlist[2],pointlist[0]);
129 if (i==org_i && j==org_j)
130 mine_tiles_drawn |= 1;
131 if (i==org_i-1 && j==org_j)
132 mine_tiles_drawn |= 2;
133 if (i==org_i && j==org_j-1)
134 mine_tiles_drawn |= 4;
135 if (i==org_i-1 && j==org_j-1)
136 mine_tiles_drawn |= 8;
138 if (mine_tiles_drawn == 0xf) {
139 render_mine(exit_segnum, 0, 0);
142 //if (ext_expl_playing)
143 // draw_fireball(&external_explosion);
148 vms_vector y_cache[256];
151 extern vms_matrix surface_orient;
153 vms_vector *get_dy_vec(int h)
162 //@@g3_rotate_delta_y(dyp,h*HEIGHT_SCALE);
164 vm_vec_copy_scale(&tv,&surface_orient.uvec,h*HEIGHT_SCALE);
165 g3_rotate_delta_vec(dyp,&tv);
176 void render_terrain(vms_vector *org_point,int org_2dx,int org_2dy)
178 vms_vector delta_i,delta_j; //delta_y;
179 g3s_point p,last_p,save_p_low,save_p_high;
182 int low_i,high_i,low_j,high_j;
183 int viewer_i,viewer_j;
186 mine_tiles_drawn = 0; //clear flags
191 low_i = 0; high_i = grid_w-1;
192 low_j = 0; high_j = grid_h-1;
194 //@@start_point.x = org_point->x - GRID_SCALE*(org_i - low_i);
195 //@@start_point.z = org_point->z - GRID_SCALE*(org_j - low_j);
196 //@@start_point.y = org_point->y;
198 memset(yc_flags,0,256);
201 Interpolation_method = im;
203 vm_vec_copy_scale(&tv,&surface_orient.rvec,GRID_SCALE);
204 g3_rotate_delta_vec(&delta_i,&tv);
205 vm_vec_copy_scale(&tv,&surface_orient.fvec,GRID_SCALE);
206 g3_rotate_delta_vec(&delta_j,&tv);
208 vm_vec_scale_add(&start_point,org_point,&surface_orient.rvec,-(org_i - low_i)*GRID_SCALE);
209 vm_vec_scale_add2(&start_point,&surface_orient.fvec,-(org_j - low_j)*GRID_SCALE);
211 vm_vec_sub(&tv,&Viewer->pos,&start_point);
212 viewer_i = vm_vec_dot(&tv,&surface_orient.rvec) / GRID_SCALE;
213 viewer_j = vm_vec_dot(&tv,&surface_orient.fvec) / GRID_SCALE;
215 //mprintf((0,"viewer_i,j = %d,%d\n",viewer_i,viewer_j));
217 g3_rotate_point(&last_p,&start_point);
220 for (j=low_j;j<=high_j;j++) {
221 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(low_i,j)));
223 save_p_high = last_p;
225 g3_add_delta_vec(&last_p,&last_p,&delta_j);
228 for (i=low_i;i<viewer_i;i++) {
230 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
232 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,low_j)));
234 for (j=low_j;j<viewer_j;j++) {
237 g3_add_delta_vec(&p,&last_p,&delta_j);
238 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j+1)));
240 draw_cell(i,j,&save_row[j],&save_row[j+1],&p2,&last_p2);
243 save_row[j] = last_p2;
248 vm_vec_negate(&delta_j); //don't have a delta sub...
250 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
251 last_p = save_p_high;
252 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,high_j)));
254 for (j=high_j-1;j>=viewer_j;j--) {
257 g3_add_delta_vec(&p,&last_p,&delta_j);
258 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j)));
260 draw_cell(i,j,&save_row[j],&save_row[j+1],&last_p2,&p2);
263 save_row[j+1] = last_p2;
268 save_row[j+1] = last_p2;
270 vm_vec_negate(&delta_j); //restore sign of j
274 //now do i from other end
276 vm_vec_negate(&delta_i); //going the other way now...
278 //@@start_point.x += (high_i-low_i)*GRID_SCALE;
279 vm_vec_scale_add2(&start_point,&surface_orient.rvec,(high_i-low_i)*GRID_SCALE);
280 g3_rotate_point(&last_p,&start_point);
283 for (j=low_j;j<=high_j;j++) {
284 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(high_i,j)));
286 save_p_high = last_p;
288 g3_add_delta_vec(&last_p,&last_p,&delta_j);
291 for (i=high_i-1;i>=viewer_i;i--) {
293 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
295 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,low_j)));
297 for (j=low_j;j<viewer_j;j++) {
300 g3_add_delta_vec(&p,&last_p,&delta_j);
301 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j+1)));
303 draw_cell(i,j,&last_p2,&p2,&save_row[j+1],&save_row[j]);
306 save_row[j] = last_p2;
311 vm_vec_negate(&delta_j); //don't have a delta sub...
313 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
314 last_p = save_p_high;
315 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,high_j)));
317 for (j=high_j-1;j>=viewer_j;j--) {
320 g3_add_delta_vec(&p,&last_p,&delta_j);
321 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j)));
323 draw_cell(i,j,&p2,&last_p2,&save_row[j+1],&save_row[j]);
326 save_row[j+1] = last_p2;
331 save_row[j+1] = last_p2;
333 vm_vec_negate(&delta_j); //restore sign of j
339 void free_height_array()
341 d_free(height_array);
344 void load_terrain(char *filename)
346 grs_bitmap height_bitmap;
351 iff_error = iff_read_bitmap(filename,&height_bitmap,BM_LINEAR,NULL);
352 if (iff_error != IFF_NO_ERROR) {
353 mprintf((1, "File %s - IFF error: %s",filename,iff_errormsg(iff_error)));
354 Error("File %s - IFF error: %s",filename,iff_errormsg(iff_error));
358 d_free(height_array);
360 atexit(free_height_array); //first time
362 grid_w = height_bitmap.bm_w;
363 grid_h = height_bitmap.bm_h;
365 Assert(grid_w <= GRID_MAX_SIZE);
366 Assert(grid_h <= GRID_MAX_SIZE);
368 height_array = height_bitmap.bm_data;
371 for (i=0;i<grid_w;i++)
372 for (j=0;j<grid_h;j++) {
383 for (i=0;i<grid_w;i++)
384 for (j=0;j<grid_h;j++)
385 HEIGHT(i,j) -= min_h;
388 // d_free(height_bitmap.bm_data);
390 terrain_bm = terrain_bitmap;
396 void get_pnt(vms_vector *p,int i,int j)
400 p->y = HEIGHT(i,j)*HEIGHT_SCALE;
403 vms_vector light = {0x2e14,0xe8f5,0x5eb8};
405 fix get_face_light(vms_vector *p0,vms_vector *p1,vms_vector *p2)
409 vm_vec_normal(&norm,p0,p1,p2);
411 return -vm_vec_dot(&norm,&light);
416 fix get_avg_light(int i,int j)
423 get_pnt(&p[0],i-1,j);
424 get_pnt(&p[1],i,j-1);
425 get_pnt(&p[2],i+1,j-1);
426 get_pnt(&p[3],i+1,j);
427 get_pnt(&p[4],i,j+1);
428 get_pnt(&p[5],i-1,j+1);
430 for (f=0,sum=0;f<6;f++)
431 sum += get_face_light(&pp,&p[f],&p[(f+1)%5]);
436 void free_light_table()
443 void build_light_table()
446 fix l,l2,min_l=0x7fffffff,max_l=0;
452 atexit(free_light_table); //first time
454 MALLOC(light_array,ubyte,grid_w*grid_h);
456 for (i=1;i<grid_w;i++)
457 for (j=1;j<grid_h;j++) {
458 l = get_avg_light(i,j);
466 //printf("light %2d,%2d = %8x\n",i,j,l);
469 for (i=1;i<grid_w;i++)
470 for (j=1;j<grid_h;j++) {
472 l = get_avg_light(i,j);
474 if (min_l == max_l) {
479 l2 = fixdiv((l-min_l),(max_l-min_l));
486 //printf("light %2d,%2d = %4x\n",i,j,l2>>8);