1 /* $Id: terrain.c,v 1.2 2002-08-02 10:57:12 btb 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 * FIXME: put description here
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);
83 // ------------------------------------------------------------------------
84 void draw_cell(int i,int j,g3s_point *p0,g3s_point *p1,g3s_point *p2,g3s_point *p3)
86 g3s_point *pointlist[3];
91 uvl_list1[0].l = LIGHTVAL(i,j);
92 uvl_list1[1].l = LIGHTVAL(i,j+1);
93 uvl_list1[2].l = LIGHTVAL(i+1,j);
95 uvl_list1[0].u = (i)*f1_0/4; uvl_list1[0].v = (j)*f1_0/4;
96 uvl_list1[1].u = (i)*f1_0/4; uvl_list1[1].v = (j+1)*f1_0/4;
97 uvl_list1[2].u = (i+1)*f1_0/4; uvl_list1[2].v = (j)*f1_0/4;
99 g3_check_and_draw_tmap(3,pointlist,uvl_list1,terrain_bm,NULL,NULL);
100 if (terrain_outline) {
101 int lsave=Lighting_on;
103 gr_setcolor(BM_XRGB(31,0,0));
104 g3_draw_line(pointlist[0],pointlist[1]);
105 g3_draw_line(pointlist[2],pointlist[0]);
111 uvl_list2[0].l = LIGHTVAL(i,j+1);
112 uvl_list2[1].l = LIGHTVAL(i+1,j+1);
113 uvl_list2[2].l = LIGHTVAL(i+1,j);
115 uvl_list2[0].u = (i)*f1_0/4; uvl_list2[0].v = (j+1)*f1_0/4;
116 uvl_list2[1].u = (i+1)*f1_0/4; uvl_list2[1].v = (j+1)*f1_0/4;
117 uvl_list2[2].u = (i+1)*f1_0/4; uvl_list2[2].v = (j)*f1_0/4;
119 g3_check_and_draw_tmap(3,pointlist,uvl_list2,terrain_bm,NULL,NULL);
120 if (terrain_outline) {
121 int lsave=Lighting_on;
123 gr_setcolor(BM_XRGB(31,0,0));
124 g3_draw_line(pointlist[0],pointlist[1]);
125 g3_draw_line(pointlist[1],pointlist[2]);
126 g3_draw_line(pointlist[2],pointlist[0]);
130 if (i==org_i && j==org_j)
131 mine_tiles_drawn |= 1;
132 if (i==org_i-1 && j==org_j)
133 mine_tiles_drawn |= 2;
134 if (i==org_i && j==org_j-1)
135 mine_tiles_drawn |= 4;
136 if (i==org_i-1 && j==org_j-1)
137 mine_tiles_drawn |= 8;
139 if (mine_tiles_drawn == 0xf) {
140 render_mine(exit_segnum, 0, 0);
143 //if (ext_expl_playing)
144 // draw_fireball(&external_explosion);
149 vms_vector y_cache[256];
152 extern vms_matrix surface_orient;
154 vms_vector *get_dy_vec(int h)
163 //@@g3_rotate_delta_y(dyp,h*HEIGHT_SCALE);
165 vm_vec_copy_scale(&tv,&surface_orient.uvec,h*HEIGHT_SCALE);
166 g3_rotate_delta_vec(dyp,&tv);
177 void render_terrain(vms_vector *org_point,int org_2dx,int org_2dy)
179 vms_vector delta_i,delta_j; //delta_y;
180 g3s_point p,last_p,save_p_low,save_p_high;
183 int low_i,high_i,low_j,high_j;
184 int viewer_i,viewer_j;
187 mine_tiles_drawn = 0; //clear flags
192 low_i = 0; high_i = grid_w-1;
193 low_j = 0; high_j = grid_h-1;
195 //@@start_point.x = org_point->x - GRID_SCALE*(org_i - low_i);
196 //@@start_point.z = org_point->z - GRID_SCALE*(org_j - low_j);
197 //@@start_point.y = org_point->y;
199 memset(yc_flags,0,256);
202 Interpolation_method = im;
204 vm_vec_copy_scale(&tv,&surface_orient.rvec,GRID_SCALE);
205 g3_rotate_delta_vec(&delta_i,&tv);
206 vm_vec_copy_scale(&tv,&surface_orient.fvec,GRID_SCALE);
207 g3_rotate_delta_vec(&delta_j,&tv);
209 vm_vec_scale_add(&start_point,org_point,&surface_orient.rvec,-(org_i - low_i)*GRID_SCALE);
210 vm_vec_scale_add2(&start_point,&surface_orient.fvec,-(org_j - low_j)*GRID_SCALE);
212 vm_vec_sub(&tv,&Viewer->pos,&start_point);
213 viewer_i = vm_vec_dot(&tv,&surface_orient.rvec) / GRID_SCALE;
214 viewer_j = vm_vec_dot(&tv,&surface_orient.fvec) / GRID_SCALE;
216 //mprintf((0,"viewer_i,j = %d,%d\n",viewer_i,viewer_j));
218 g3_rotate_point(&last_p,&start_point);
221 for (j=low_j;j<=high_j;j++) {
222 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(low_i,j)));
224 save_p_high = last_p;
226 g3_add_delta_vec(&last_p,&last_p,&delta_j);
229 for (i=low_i;i<viewer_i;i++) {
231 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
233 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,low_j)));
235 for (j=low_j;j<viewer_j;j++) {
238 g3_add_delta_vec(&p,&last_p,&delta_j);
239 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j+1)));
241 draw_cell(i,j,&save_row[j],&save_row[j+1],&p2,&last_p2);
244 save_row[j] = last_p2;
249 vm_vec_negate(&delta_j); //don't have a delta sub...
251 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
252 last_p = save_p_high;
253 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,high_j)));
255 for (j=high_j-1;j>=viewer_j;j--) {
258 g3_add_delta_vec(&p,&last_p,&delta_j);
259 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j)));
261 draw_cell(i,j,&save_row[j],&save_row[j+1],&last_p2,&p2);
264 save_row[j+1] = last_p2;
269 save_row[j+1] = last_p2;
271 vm_vec_negate(&delta_j); //restore sign of j
275 //now do i from other end
277 vm_vec_negate(&delta_i); //going the other way now...
279 //@@start_point.x += (high_i-low_i)*GRID_SCALE;
280 vm_vec_scale_add2(&start_point,&surface_orient.rvec,(high_i-low_i)*GRID_SCALE);
281 g3_rotate_point(&last_p,&start_point);
284 for (j=low_j;j<=high_j;j++) {
285 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(high_i,j)));
287 save_p_high = last_p;
289 g3_add_delta_vec(&last_p,&last_p,&delta_j);
292 for (i=high_i-1;i>=viewer_i;i--) {
294 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
296 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,low_j)));
298 for (j=low_j;j<viewer_j;j++) {
301 g3_add_delta_vec(&p,&last_p,&delta_j);
302 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j+1)));
304 draw_cell(i,j,&last_p2,&p2,&save_row[j+1],&save_row[j]);
307 save_row[j] = last_p2;
312 vm_vec_negate(&delta_j); //don't have a delta sub...
314 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
315 last_p = save_p_high;
316 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,high_j)));
318 for (j=high_j-1;j>=viewer_j;j--) {
321 g3_add_delta_vec(&p,&last_p,&delta_j);
322 g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j)));
324 draw_cell(i,j,&p2,&last_p2,&save_row[j+1],&save_row[j]);
327 save_row[j+1] = last_p2;
332 save_row[j+1] = last_p2;
334 vm_vec_negate(&delta_j); //restore sign of j
340 void free_height_array()
342 d_free(height_array);
345 void load_terrain(char *filename)
347 grs_bitmap height_bitmap;
352 iff_error = iff_read_bitmap(filename,&height_bitmap,BM_LINEAR,NULL);
353 if (iff_error != IFF_NO_ERROR) {
354 mprintf((1, "File %s - IFF error: %s",filename,iff_errormsg(iff_error)));
355 Error("File %s - IFF error: %s",filename,iff_errormsg(iff_error));
359 d_free(height_array);
361 atexit(free_height_array); //first time
363 grid_w = height_bitmap.bm_w;
364 grid_h = height_bitmap.bm_h;
366 Assert(grid_w <= GRID_MAX_SIZE);
367 Assert(grid_h <= GRID_MAX_SIZE);
369 height_array = height_bitmap.bm_data;
372 for (i=0;i<grid_w;i++)
373 for (j=0;j<grid_h;j++) {
384 for (i=0;i<grid_w;i++)
385 for (j=0;j<grid_h;j++)
386 HEIGHT(i,j) -= min_h;
389 // d_free(height_bitmap.bm_data);
391 terrain_bm = terrain_bitmap;
397 void get_pnt(vms_vector *p,int i,int j)
401 p->y = HEIGHT(i,j)*HEIGHT_SCALE;
404 vms_vector light = {0x2e14,0xe8f5,0x5eb8};
406 fix get_face_light(vms_vector *p0,vms_vector *p1,vms_vector *p2)
410 vm_vec_normal(&norm,p0,p1,p2);
412 return -vm_vec_dot(&norm,&light);
417 fix get_avg_light(int i,int j)
424 get_pnt(&p[0],i-1,j);
425 get_pnt(&p[1],i,j-1);
426 get_pnt(&p[2],i+1,j-1);
427 get_pnt(&p[3],i+1,j);
428 get_pnt(&p[4],i,j+1);
429 get_pnt(&p[5],i-1,j+1);
431 for (f=0,sum=0;f<6;f++)
432 sum += get_face_light(&pp,&p[f],&p[(f+1)%5]);
437 void free_light_table()
444 void build_light_table()
447 fix l,l2,min_l=0x7fffffff,max_l=0;
453 atexit(free_light_table); //first time
455 MALLOC(light_array,ubyte,grid_w*grid_h);
457 for (i=1;i<grid_w;i++)
458 for (j=1;j<grid_h;j++) {
459 l = get_avg_light(i,j);
467 //printf("light %2d,%2d = %8x\n",i,j,l);
470 for (i=1;i<grid_w;i++)
471 for (j=1;j<grid_h;j++) {
473 l = get_avg_light(i,j);
475 if (min_l == max_l) {
480 l2 = fixdiv((l-min_l),(max_l-min_l));
487 //printf("light %2d,%2d = %4x\n",i,j,l2>>8);