2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * $Source: /cvs/cvsroot/d2x/main/terrain.c,v $
18 * $Date: 2001-11-08 10:21:36 $
20 * FIXME: put description here
22 * $Log: not supported by cvs2svn $
48 #define GRID_MAX_SIZE 64
49 #define GRID_SCALE i2f(2*20)
50 #define HEIGHT_SCALE f1_0
54 g3s_uvl uvl_list1[] = { {0,0,0}, {f1_0,0,0}, {0,f1_0,0} };
55 g3s_uvl uvl_list2[] = { {f1_0,0,0}, {f1_0,f1_0,0}, {0,f1_0,0} };
60 #define HEIGHT(_i,_j) (height_array[(_i)*grid_w+(_j)])
61 #define LIGHT(_i,_j) light_array[(_i)*grid_w+(_j)]
63 //!!#define HEIGHT(_i,_j) height_array[(grid_h-1-j)*grid_w+(_i)]
64 //!!#define LIGHT(_i,_j) light_array[(grid_h-1-j)*grid_w+(_i)]
66 #define LIGHTVAL(_i,_j) (((fix) LIGHT(_i,_j))<<8)
68 g3s_point save_row[GRID_MAX_SIZE];
70 vms_vector start_point;
72 grs_bitmap *terrain_bm;
74 int terrain_outline=0;
76 void render_mine(int start_seg_num,fix eye_offset);
80 int mine_tiles_drawn; //flags to tell if all 4 tiles under mine have drawn
83 // LINT: adding function prototypes
84 void build_light_table(void);
85 void free_light_table(void);
88 // ------------------------------------------------------------------------
89 void draw_cell(int i,int j,g3s_point *p0,g3s_point *p1,g3s_point *p2,g3s_point *p3)
91 g3s_point *pointlist[3];
96 uvl_list1[0].l = LIGHTVAL(i,j);
97 uvl_list1[1].l = LIGHTVAL(i,j+1);
98 uvl_list1[2].l = LIGHTVAL(i+1,j);
100 uvl_list1[0].u = (i)*f1_0/4; uvl_list1[0].v = (j)*f1_0/4;
101 uvl_list1[1].u = (i)*f1_0/4; uvl_list1[1].v = (j+1)*f1_0/4;
102 uvl_list1[2].u = (i+1)*f1_0/4; uvl_list1[2].v = (j)*f1_0/4;
104 g3_check_and_draw_tmap(3,pointlist,uvl_list1,terrain_bm,NULL,NULL);
105 if (terrain_outline) {
106 int lsave=Lighting_on;
108 gr_setcolor(BM_XRGB(31,0,0));
109 g3_draw_line(pointlist[0],pointlist[1]);
110 g3_draw_line(pointlist[2],pointlist[0]);
116 uvl_list2[0].l = LIGHTVAL(i,j+1);
117 uvl_list2[1].l = LIGHTVAL(i+1,j+1);
118 uvl_list2[2].l = LIGHTVAL(i+1,j);
120 uvl_list2[0].u = (i)*f1_0/4; uvl_list2[0].v = (j+1)*f1_0/4;
121 uvl_list2[1].u = (i+1)*f1_0/4; uvl_list2[1].v = (j+1)*f1_0/4;
122 uvl_list2[2].u = (i+1)*f1_0/4; uvl_list2[2].v = (j)*f1_0/4;
124 g3_check_and_draw_tmap(3,pointlist,uvl_list2,terrain_bm,NULL,NULL);
125 if (terrain_outline) {
126 int lsave=Lighting_on;
128 gr_setcolor(BM_XRGB(31,0,0));
129 g3_draw_line(pointlist[0],pointlist[1]);
130 g3_draw_line(pointlist[1],pointlist[2]);
131 g3_draw_line(pointlist[2],pointlist[0]);
135 if (i==org_i && j==org_j)
136 mine_tiles_drawn |= 1;
137 if (i==org_i-1 && j==org_j)
138 mine_tiles_drawn |= 2;
139 if (i==org_i && j==org_j-1)
140 mine_tiles_drawn |= 4;
141 if (i==org_i-1 && j==org_j-1)
142 mine_tiles_drawn |= 8;
144 if (mine_tiles_drawn == 0xf) {
145 render_mine(exit_segnum,0);
148 //if (ext_expl_playing)
149 // draw_fireball(&external_explosion);
154 vms_vector y_cache[256];
157 extern vms_matrix surface_orient;
159 vms_vector *get_dy_vec(int h)
168 //@@g3_rotate_delta_y(dyp,h*HEIGHT_SCALE);
170 vm_vec_copy_scale(&tv,&surface_orient.uvec,h*HEIGHT_SCALE);
171 g3_rotate_delta_vec(dyp,&tv);
182 void render_terrain(vms_vector *org_point,int org_2dx,int org_2dy)
184 vms_vector delta_i,delta_j; //delta_y;
185 g3s_point p,last_p,save_p_low,save_p_high;
188 int low_i,high_i,low_j,high_j;
189 int viewer_i,viewer_j;
192 mine_tiles_drawn = 0; //clear flags
197 low_i = 0; high_i = grid_w-1;
198 low_j = 0; high_j = grid_h-1;
200 //@@start_point.x = org_point->x - GRID_SCALE*(org_i - low_i);
201 //@@start_point.z = org_point->z - GRID_SCALE*(org_j - low_j);
202 //@@start_point.y = org_point->y;
204 memset(yc_flags,0,256);
207 Interpolation_method = im;
209 vm_vec_copy_scale(&tv,&surface_orient.rvec,GRID_SCALE);
210 g3_rotate_delta_vec(&delta_i,&tv);
211 vm_vec_copy_scale(&tv,&surface_orient.fvec,GRID_SCALE);
212 g3_rotate_delta_vec(&delta_j,&tv);
214 vm_vec_scale_add(&start_point,org_point,&surface_orient.rvec,-(org_i - low_i)*GRID_SCALE);
215 vm_vec_scale_add2(&start_point,&surface_orient.fvec,-(org_j - low_j)*GRID_SCALE);
217 vm_vec_sub(&tv,&Viewer->pos,&start_point);
218 viewer_i = vm_vec_dot(&tv,&surface_orient.rvec) / GRID_SCALE;
219 viewer_j = vm_vec_dot(&tv,&surface_orient.fvec) / GRID_SCALE;
221 //mprintf((0,"viewer_i,j = %d,%d\n",viewer_i,viewer_j));
223 g3_rotate_point(&last_p,&start_point);
226 for (j=low_j;j<=high_j;j++) {
227 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(low_i,j)));
229 save_p_high = last_p;
231 g3_add_delta_vec(&last_p,&last_p,&delta_j);
234 for (i=low_i;i<viewer_i;i++) {
236 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
238 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,low_j)));
240 for (j=low_j;j<viewer_j;j++) {
243 g3_add_delta_vec(&p,&last_p,&delta_j);
244 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j+1)));
246 draw_cell(i,j,&save_row[j],&save_row[j+1],&p2,&last_p2);
249 save_row[j] = last_p2;
254 vm_vec_negate(&delta_j); //don't have a delta sub...
256 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
257 last_p = save_p_high;
258 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,high_j)));
260 for (j=high_j-1;j>=viewer_j;j--) {
263 g3_add_delta_vec(&p,&last_p,&delta_j);
264 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j)));
266 draw_cell(i,j,&save_row[j],&save_row[j+1],&last_p2,&p2);
269 save_row[j+1] = last_p2;
274 save_row[j+1] = last_p2;
276 vm_vec_negate(&delta_j); //restore sign of j
280 //now do i from other end
282 vm_vec_negate(&delta_i); //going the other way now...
284 //@@start_point.x += (high_i-low_i)*GRID_SCALE;
285 vm_vec_scale_add2(&start_point,&surface_orient.rvec,(high_i-low_i)*GRID_SCALE);
286 g3_rotate_point(&last_p,&start_point);
289 for (j=low_j;j<=high_j;j++) {
290 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(high_i,j)));
292 save_p_high = last_p;
294 g3_add_delta_vec(&last_p,&last_p,&delta_j);
297 for (i=high_i-1;i>=viewer_i;i--) {
299 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
301 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,low_j)));
303 for (j=low_j;j<viewer_j;j++) {
306 g3_add_delta_vec(&p,&last_p,&delta_j);
307 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j+1)));
309 draw_cell(i,j,&last_p2,&p2,&save_row[j+1],&save_row[j]);
312 save_row[j] = last_p2;
317 vm_vec_negate(&delta_j); //don't have a delta sub...
319 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
320 last_p = save_p_high;
321 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,high_j)));
323 for (j=high_j-1;j>=viewer_j;j--) {
326 g3_add_delta_vec(&p,&last_p,&delta_j);
327 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j)));
329 draw_cell(i,j,&p2,&last_p2,&save_row[j+1],&save_row[j]);
332 save_row[j+1] = last_p2;
337 save_row[j+1] = last_p2;
339 vm_vec_negate(&delta_j); //restore sign of j
345 void free_height_array()
347 d_free(height_array);
350 void load_terrain(char *filename)
352 grs_bitmap height_bitmap;
357 iff_error = iff_read_bitmap(filename,&height_bitmap,BM_LINEAR,NULL);
358 if (iff_error != IFF_NO_ERROR) {
359 mprintf((1, "File %s - IFF error: %s",filename,iff_errormsg(iff_error)));
360 Error("File %s - IFF error: %s",filename,iff_errormsg(iff_error));
364 d_free(height_array);
366 atexit(free_height_array); //first time
368 grid_w = height_bitmap.bm_w;
369 grid_h = height_bitmap.bm_h;
371 Assert(grid_w <= GRID_MAX_SIZE);
372 Assert(grid_h <= GRID_MAX_SIZE);
374 height_array = height_bitmap.bm_data;
377 for (i=0;i<grid_w;i++)
378 for (j=0;j<grid_h;j++) {
389 for (i=0;i<grid_w;i++)
390 for (j=0;j<grid_h;j++)
391 HEIGHT(i,j) -= min_h;
394 // d_free(height_bitmap.bm_data);
396 terrain_bm = terrain_bitmap;
402 void get_pnt(vms_vector *p,int i,int j)
406 p->y = HEIGHT(i,j)*HEIGHT_SCALE;
409 vms_vector light = {0x2e14,0xe8f5,0x5eb8};
411 fix get_face_light(vms_vector *p0,vms_vector *p1,vms_vector *p2)
415 vm_vec_normal(&norm,p0,p1,p2);
417 return -vm_vec_dot(&norm,&light);
422 fix get_avg_light(int i,int j)
429 get_pnt(&p[0],i-1,j);
430 get_pnt(&p[1],i,j-1);
431 get_pnt(&p[2],i+1,j-1);
432 get_pnt(&p[3],i+1,j);
433 get_pnt(&p[4],i,j+1);
434 get_pnt(&p[5],i-1,j+1);
436 for (f=0,sum=0;f<6;f++)
437 sum += get_face_light(&pp,&p[f],&p[(f+1)%5]);
442 void free_light_table()
449 void build_light_table()
452 fix l,l2,min_l=0x7fffffff,max_l=0;
458 atexit(free_light_table); //first time
460 MALLOC(light_array,ubyte,grid_w*grid_h);
462 for (i=1;i<grid_w;i++)
463 for (j=1;j<grid_h;j++) {
464 l = get_avg_light(i,j);
472 //printf("light %2d,%2d = %8x\n",i,j,l);
475 for (i=1;i<grid_w;i++)
476 for (j=1;j<grid_h;j++) {
478 l = get_avg_light(i,j);
480 if (min_l == max_l) {
485 l2 = fixdiv((l-min_l),(max_l-min_l));
492 //printf("light %2d,%2d = %4x\n",i,j,l2>>8);