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.
16 * Code to render cool external-scene terrain
39 #define GRID_MAX_SIZE 64
40 #define GRID_SCALE i2f(2*20)
41 #define HEIGHT_SCALE f1_0
45 g3s_uvl uvl_list1[] = { {0,0,0}, {f1_0,0,0}, {0,f1_0,0} };
46 g3s_uvl uvl_list2[] = { {f1_0,0,0}, {f1_0,f1_0,0}, {0,f1_0,0} };
51 #define HEIGHT(_i,_j) (height_array[(_i)*grid_w+(_j)])
52 #define LIGHT(_i,_j) light_array[(_i)*grid_w+(_j)]
54 //!!#define HEIGHT(_i,_j) height_array[(grid_h-1-j)*grid_w+(_i)]
55 //!!#define LIGHT(_i,_j) light_array[(grid_h-1-j)*grid_w+(_i)]
57 #define LIGHTVAL(_i,_j) (((fix) LIGHT(_i,_j))<<8)
59 g3s_point save_row[GRID_MAX_SIZE];
61 vms_vector start_point;
63 grs_bitmap *terrain_bm;
65 int terrain_outline=0;
69 int mine_tiles_drawn; //flags to tell if all 4 tiles under mine have drawn
72 // LINT: adding function prototypes
73 void build_light_table(void);
74 void free_light_table(void);
76 // ------------------------------------------------------------------------
77 void draw_cell(int i,int j,g3s_point *p0,g3s_point *p1,g3s_point *p2,g3s_point *p3)
79 g3s_point *pointlist[3];
84 uvl_list1[0].l = LIGHTVAL(i,j);
85 uvl_list1[1].l = LIGHTVAL(i,j+1);
86 uvl_list1[2].l = LIGHTVAL(i+1,j);
88 uvl_list1[0].u = (i)*f1_0/4; uvl_list1[0].v = (j)*f1_0/4;
89 uvl_list1[1].u = (i)*f1_0/4; uvl_list1[1].v = (j+1)*f1_0/4;
90 uvl_list1[2].u = (i+1)*f1_0/4; uvl_list1[2].v = (j)*f1_0/4;
92 g3_check_and_draw_tmap(3,pointlist,uvl_list1,terrain_bm,NULL,NULL);
93 if (terrain_outline) {
94 int lsave=Lighting_on;
96 gr_setcolor(BM_XRGB(31,0,0));
97 g3_draw_line(pointlist[0],pointlist[1]);
98 g3_draw_line(pointlist[2],pointlist[0]);
104 uvl_list2[0].l = LIGHTVAL(i,j+1);
105 uvl_list2[1].l = LIGHTVAL(i+1,j+1);
106 uvl_list2[2].l = LIGHTVAL(i+1,j);
108 uvl_list2[0].u = (i)*f1_0/4; uvl_list2[0].v = (j+1)*f1_0/4;
109 uvl_list2[1].u = (i+1)*f1_0/4; uvl_list2[1].v = (j+1)*f1_0/4;
110 uvl_list2[2].u = (i+1)*f1_0/4; uvl_list2[2].v = (j)*f1_0/4;
112 g3_check_and_draw_tmap(3,pointlist,uvl_list2,terrain_bm,NULL,NULL);
113 if (terrain_outline) {
114 int lsave=Lighting_on;
116 gr_setcolor(BM_XRGB(31,0,0));
117 g3_draw_line(pointlist[0],pointlist[1]);
118 g3_draw_line(pointlist[1],pointlist[2]);
119 g3_draw_line(pointlist[2],pointlist[0]);
123 if (i==org_i && j==org_j)
124 mine_tiles_drawn |= 1;
125 if (i==org_i-1 && j==org_j)
126 mine_tiles_drawn |= 2;
127 if (i==org_i && j==org_j-1)
128 mine_tiles_drawn |= 4;
129 if (i==org_i-1 && j==org_j-1)
130 mine_tiles_drawn |= 8;
132 if (mine_tiles_drawn == 0xf) {
133 render_mine(exit_segnum, 0, 0);
136 //if (ext_expl_playing)
137 // draw_fireball(&external_explosion);
142 vms_vector y_cache[256];
145 extern vms_matrix surface_orient;
147 vms_vector *get_dy_vec(int h)
156 //@@g3_rotate_delta_y(dyp,h*HEIGHT_SCALE);
158 vm_vec_copy_scale(&tv,&surface_orient.uvec,h*HEIGHT_SCALE);
159 g3_rotate_delta_vec(dyp,&tv);
170 void render_terrain(vms_vector *org_point,int org_2dx,int org_2dy)
172 vms_vector delta_i,delta_j; //delta_y;
173 g3s_point p,last_p,save_p_low,save_p_high;
176 int low_i,high_i,low_j,high_j;
177 int viewer_i,viewer_j;
180 mine_tiles_drawn = 0; //clear flags
185 low_i = 0; high_i = grid_w-1;
186 low_j = 0; high_j = grid_h-1;
188 //@@start_point.x = org_point->x - GRID_SCALE*(org_i - low_i);
189 //@@start_point.z = org_point->z - GRID_SCALE*(org_j - low_j);
190 //@@start_point.y = org_point->y;
192 memset(yc_flags,0,256);
195 Interpolation_method = im;
197 vm_vec_copy_scale(&tv,&surface_orient.rvec,GRID_SCALE);
198 g3_rotate_delta_vec(&delta_i,&tv);
199 vm_vec_copy_scale(&tv,&surface_orient.fvec,GRID_SCALE);
200 g3_rotate_delta_vec(&delta_j,&tv);
202 vm_vec_scale_add(&start_point,org_point,&surface_orient.rvec,-(org_i - low_i)*GRID_SCALE);
203 vm_vec_scale_add2(&start_point,&surface_orient.fvec,-(org_j - low_j)*GRID_SCALE);
205 vm_vec_sub(&tv,&Viewer->pos,&start_point);
206 viewer_i = vm_vec_dot(&tv,&surface_orient.rvec) / GRID_SCALE;
207 viewer_j = vm_vec_dot(&tv,&surface_orient.fvec) / GRID_SCALE;
209 //mprintf((0,"viewer_i,j = %d,%d\n",viewer_i,viewer_j));
211 g3_rotate_point(&last_p,&start_point);
214 for (j=low_j;j<=high_j;j++) {
215 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(low_i,j)));
217 save_p_high = last_p;
219 g3_add_delta_vec(&last_p,&last_p,&delta_j);
222 for (i=low_i;i<viewer_i;i++) {
224 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
226 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,low_j)));
228 for (j=low_j;j<viewer_j;j++) {
231 g3_add_delta_vec(&p,&last_p,&delta_j);
232 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j+1)));
234 draw_cell(i,j,&save_row[j],&save_row[j+1],&p2,&last_p2);
237 save_row[j] = last_p2;
242 vm_vec_negate(&delta_j); //don't have a delta sub...
244 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
245 last_p = save_p_high;
246 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,high_j)));
248 for (j=high_j-1;j>=viewer_j;j--) {
251 g3_add_delta_vec(&p,&last_p,&delta_j);
252 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j)));
254 draw_cell(i,j,&save_row[j],&save_row[j+1],&last_p2,&p2);
257 save_row[j+1] = last_p2;
262 save_row[j+1] = last_p2;
264 vm_vec_negate(&delta_j); //restore sign of j
268 //now do i from other end
270 vm_vec_negate(&delta_i); //going the other way now...
272 //@@start_point.x += (high_i-low_i)*GRID_SCALE;
273 vm_vec_scale_add2(&start_point,&surface_orient.rvec,(high_i-low_i)*GRID_SCALE);
274 g3_rotate_point(&last_p,&start_point);
277 for (j=low_j;j<=high_j;j++) {
278 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(high_i,j)));
280 save_p_high = last_p;
282 g3_add_delta_vec(&last_p,&last_p,&delta_j);
285 for (i=high_i-1;i>=viewer_i;i--) {
287 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
289 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,low_j)));
291 for (j=low_j;j<viewer_j;j++) {
294 g3_add_delta_vec(&p,&last_p,&delta_j);
295 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j+1)));
297 draw_cell(i,j,&last_p2,&p2,&save_row[j+1],&save_row[j]);
300 save_row[j] = last_p2;
305 vm_vec_negate(&delta_j); //don't have a delta sub...
307 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
308 last_p = save_p_high;
309 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,high_j)));
311 for (j=high_j-1;j>=viewer_j;j--) {
314 g3_add_delta_vec(&p,&last_p,&delta_j);
315 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j)));
317 draw_cell(i,j,&p2,&last_p2,&save_row[j+1],&save_row[j]);
320 save_row[j+1] = last_p2;
325 save_row[j+1] = last_p2;
327 vm_vec_negate(&delta_j); //restore sign of j
333 void free_height_array()
335 d_free(height_array);
338 void load_terrain(char *filename)
340 grs_bitmap height_bitmap;
345 iff_error = iff_read_bitmap(filename,&height_bitmap,BM_LINEAR,NULL);
346 if (iff_error != IFF_NO_ERROR) {
347 mprintf((1, "File %s - IFF error: %s",filename,iff_errormsg(iff_error)));
348 Error("File %s - IFF error: %s",filename,iff_errormsg(iff_error));
352 d_free(height_array);
354 atexit(free_height_array); //first time
356 grid_w = height_bitmap.bm_w;
357 grid_h = height_bitmap.bm_h;
359 Assert(grid_w <= GRID_MAX_SIZE);
360 Assert(grid_h <= GRID_MAX_SIZE);
362 height_array = height_bitmap.bm_data;
365 for (i=0;i<grid_w;i++)
366 for (j=0;j<grid_h;j++) {
377 for (i=0;i<grid_w;i++)
378 for (j=0;j<grid_h;j++)
379 HEIGHT(i,j) -= min_h;
382 // d_free(height_bitmap.bm_data);
384 terrain_bm = terrain_bitmap;
390 void get_pnt(vms_vector *p,int i,int j)
394 p->y = HEIGHT(i,j)*HEIGHT_SCALE;
397 vms_vector light = {0x2e14,0xe8f5,0x5eb8};
399 fix get_face_light(vms_vector *p0,vms_vector *p1,vms_vector *p2)
403 vm_vec_normal(&norm,p0,p1,p2);
405 return -vm_vec_dot(&norm,&light);
410 fix get_avg_light(int i,int j)
417 get_pnt(&p[0],i-1,j);
418 get_pnt(&p[1],i,j-1);
419 get_pnt(&p[2],i+1,j-1);
420 get_pnt(&p[3],i+1,j);
421 get_pnt(&p[4],i,j+1);
422 get_pnt(&p[5],i-1,j+1);
424 for (f=0,sum=0;f<6;f++)
425 sum += get_face_light(&pp,&p[f],&p[(f+1)%5]);
430 void free_light_table()
437 void build_light_table()
440 fix l,l2,min_l=0x7fffffff,max_l=0;
446 atexit(free_light_table); //first time
448 MALLOC(light_array,ubyte,grid_w*grid_h);
450 for (i=1;i<grid_w;i++)
451 for (j=1;j<grid_h;j++) {
452 l = get_avg_light(i,j);
460 //printf("light %2d,%2d = %8x\n",i,j,l);
463 for (i=1;i<grid_w;i++)
464 for (j=1;j<grid_h;j++) {
466 l = get_avg_light(i,j);
468 if (min_l == max_l) {
473 l2 = fixdiv((l-min_l),(max_l-min_l));
480 //printf("light %2d,%2d = %4x\n",i,j,l2>>8);