]> icculus.org git repositories - btb/d2x.git/blob - main/terrain.c
remove rcs tags
[btb/d2x.git] / main / terrain.c
1 /*
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.
12 */
13
14 /*
15  *
16  * Code to render cool external-scene terrain
17  *
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "3d.h"
30 #include "error.h"
31 #include "gr.h"
32 #include "texmap.h"
33 #include "iff.h"
34 #include "u_mem.h"
35 #include "mono.h"
36
37 #include "inferno.h"
38 #include "textures.h"
39 #include "object.h"
40 #include "endlevel.h"
41 #include "fireball.h"
42 #include "render.h"
43
44 #define GRID_MAX_SIZE   64
45 #define GRID_SCALE      i2f(2*20)
46 #define HEIGHT_SCALE    f1_0
47
48 int grid_w,grid_h;
49
50 g3s_uvl uvl_list1[] = { {0,0,0}, {f1_0,0,0},  {0,f1_0,0} };
51 g3s_uvl uvl_list2[] = { {f1_0,0,0}, {f1_0,f1_0,0},  {0,f1_0,0} };
52
53 ubyte *height_array;
54 ubyte *light_array;
55
56 #define HEIGHT(_i,_j) (height_array[(_i)*grid_w+(_j)])
57 #define LIGHT(_i,_j) light_array[(_i)*grid_w+(_j)]
58
59 //!!#define HEIGHT(_i,_j)   height_array[(grid_h-1-j)*grid_w+(_i)]
60 //!!#define LIGHT(_i,_j)    light_array[(grid_h-1-j)*grid_w+(_i)]
61
62 #define LIGHTVAL(_i,_j) (((fix) LIGHT(_i,_j))<<8)
63
64 g3s_point save_row[GRID_MAX_SIZE];
65
66 vms_vector start_point;
67
68 grs_bitmap *terrain_bm;
69
70 int terrain_outline=0;
71
72 int org_i,org_j;
73
74 int mine_tiles_drawn;    //flags to tell if all 4 tiles under mine have drawn
75
76
77 // LINT: adding function prototypes
78 void build_light_table(void);
79 void free_light_table(void);
80
81 // ------------------------------------------------------------------------
82 void draw_cell(int i,int j,g3s_point *p0,g3s_point *p1,g3s_point *p2,g3s_point *p3)
83 {
84         g3s_point *pointlist[3];
85
86         pointlist[0] = p0;
87         pointlist[1] = p1;
88         pointlist[2] = p3;
89         uvl_list1[0].l = LIGHTVAL(i,j);
90         uvl_list1[1].l = LIGHTVAL(i,j+1);
91         uvl_list1[2].l = LIGHTVAL(i+1,j);
92
93         uvl_list1[0].u = (i)*f1_0/4; uvl_list1[0].v = (j)*f1_0/4;
94         uvl_list1[1].u = (i)*f1_0/4; uvl_list1[1].v = (j+1)*f1_0/4;
95         uvl_list1[2].u = (i+1)*f1_0/4;   uvl_list1[2].v = (j)*f1_0/4;
96
97         g3_check_and_draw_tmap(3,pointlist,uvl_list1,terrain_bm,NULL,NULL);
98         if (terrain_outline) {
99                 int lsave=Lighting_on;
100                 Lighting_on=0;
101                 gr_setcolor(BM_XRGB(31,0,0));
102                 g3_draw_line(pointlist[0],pointlist[1]);
103                 g3_draw_line(pointlist[2],pointlist[0]);
104                 Lighting_on=lsave;
105         }
106
107         pointlist[0] = p1;
108         pointlist[1] = p2;
109         uvl_list2[0].l = LIGHTVAL(i,j+1);
110         uvl_list2[1].l = LIGHTVAL(i+1,j+1);
111         uvl_list2[2].l = LIGHTVAL(i+1,j);
112
113         uvl_list2[0].u = (i)*f1_0/4; uvl_list2[0].v = (j+1)*f1_0/4;
114         uvl_list2[1].u = (i+1)*f1_0/4;   uvl_list2[1].v = (j+1)*f1_0/4;
115         uvl_list2[2].u = (i+1)*f1_0/4;   uvl_list2[2].v = (j)*f1_0/4;
116
117         g3_check_and_draw_tmap(3,pointlist,uvl_list2,terrain_bm,NULL,NULL);
118         if (terrain_outline) {
119                 int lsave=Lighting_on;
120                 Lighting_on=0;
121                 gr_setcolor(BM_XRGB(31,0,0));
122                 g3_draw_line(pointlist[0],pointlist[1]);
123                 g3_draw_line(pointlist[1],pointlist[2]);
124                 g3_draw_line(pointlist[2],pointlist[0]);
125                 Lighting_on=lsave;
126         }
127
128         if (i==org_i && j==org_j)
129                 mine_tiles_drawn |= 1;
130         if (i==org_i-1 && j==org_j)
131                 mine_tiles_drawn |= 2;
132         if (i==org_i && j==org_j-1)
133                 mine_tiles_drawn |= 4;
134         if (i==org_i-1 && j==org_j-1)
135                 mine_tiles_drawn |= 8;
136
137         if (mine_tiles_drawn == 0xf) {
138                 render_mine(exit_segnum, 0, 0);
139                 //draw_exit_model();
140                 mine_tiles_drawn=-1;
141                 //if (ext_expl_playing)
142                 //      draw_fireball(&external_explosion);
143         }
144
145 }
146
147 vms_vector y_cache[256];
148 ubyte yc_flags[256];
149
150 extern vms_matrix surface_orient;
151
152 vms_vector *get_dy_vec(int h)
153 {
154         vms_vector *dyp;
155
156         dyp = &y_cache[h];
157
158         if (!yc_flags[h]) {
159                 vms_vector tv;
160
161                 //@@g3_rotate_delta_y(dyp,h*HEIGHT_SCALE);
162
163                 vm_vec_copy_scale(&tv,&surface_orient.uvec,h*HEIGHT_SCALE);
164                 g3_rotate_delta_vec(dyp,&tv);
165
166                 yc_flags[h] = 1;
167         }
168
169         return dyp;
170
171 }
172
173 int im=1;
174
175 void render_terrain(vms_vector *org_point,int org_2dx,int org_2dy)
176 {
177         vms_vector delta_i,delta_j;             //delta_y;
178         g3s_point p,last_p,save_p_low,save_p_high;
179         g3s_point last_p2;
180         int i,j;
181         int low_i,high_i,low_j,high_j;
182         int viewer_i,viewer_j;
183         vms_vector tv;
184
185         mine_tiles_drawn = 0;   //clear flags
186
187         org_i = org_2dy;
188         org_j = org_2dx;
189
190         low_i = 0;  high_i = grid_w-1;
191         low_j = 0;  high_j = grid_h-1;
192
193         //@@start_point.x = org_point->x - GRID_SCALE*(org_i - low_i);
194         //@@start_point.z = org_point->z - GRID_SCALE*(org_j - low_j);
195         //@@start_point.y = org_point->y;
196
197         memset(yc_flags,0,256);
198
199         //Lighting_on = 0;
200         Interpolation_method = im;
201
202         vm_vec_copy_scale(&tv,&surface_orient.rvec,GRID_SCALE);
203         g3_rotate_delta_vec(&delta_i,&tv);
204         vm_vec_copy_scale(&tv,&surface_orient.fvec,GRID_SCALE);
205         g3_rotate_delta_vec(&delta_j,&tv);
206
207         vm_vec_scale_add(&start_point,org_point,&surface_orient.rvec,-(org_i - low_i)*GRID_SCALE);
208         vm_vec_scale_add2(&start_point,&surface_orient.fvec,-(org_j - low_j)*GRID_SCALE);
209
210         vm_vec_sub(&tv,&Viewer->pos,&start_point);
211         viewer_i = vm_vec_dot(&tv,&surface_orient.rvec) / GRID_SCALE;
212         viewer_j = vm_vec_dot(&tv,&surface_orient.fvec) / GRID_SCALE;
213
214 //mprintf((0,"viewer_i,j = %d,%d\n",viewer_i,viewer_j));
215
216         g3_rotate_point(&last_p,&start_point);
217         save_p_low = last_p;
218
219         for (j=low_j;j<=high_j;j++) {
220                 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(low_i,j)));
221                 if (j==high_j)
222                         save_p_high = last_p;
223                 else
224                         g3_add_delta_vec(&last_p,&last_p,&delta_j);
225         }
226
227         for (i=low_i;i<viewer_i;i++) {
228
229                 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
230                 last_p = save_p_low;
231                 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,low_j)));
232                 
233                 for (j=low_j;j<viewer_j;j++) {
234                         g3s_point p2;
235
236                         g3_add_delta_vec(&p,&last_p,&delta_j);
237                         g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j+1)));
238
239                         draw_cell(i,j,&save_row[j],&save_row[j+1],&p2,&last_p2);
240
241                         last_p = p;
242                         save_row[j] = last_p2;
243                         last_p2 = p2;
244
245                 }
246
247                 vm_vec_negate(&delta_j);                        //don't have a delta sub...
248
249                 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
250                 last_p = save_p_high;
251                 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i+1,high_j)));
252                 
253                 for (j=high_j-1;j>=viewer_j;j--) {
254                         g3s_point p2;
255
256                         g3_add_delta_vec(&p,&last_p,&delta_j);
257                         g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i+1,j)));
258
259                         draw_cell(i,j,&save_row[j],&save_row[j+1],&last_p2,&p2);
260
261                         last_p = p;
262                         save_row[j+1] = last_p2;
263                         last_p2 = p2;
264
265                 }
266
267                 save_row[j+1] = last_p2;
268
269                 vm_vec_negate(&delta_j);                //restore sign of j
270
271         }
272
273         //now do i from other end
274
275         vm_vec_negate(&delta_i);                //going the other way now...
276
277         //@@start_point.x += (high_i-low_i)*GRID_SCALE;
278         vm_vec_scale_add2(&start_point,&surface_orient.rvec,(high_i-low_i)*GRID_SCALE);
279         g3_rotate_point(&last_p,&start_point);
280         save_p_low = last_p;
281
282         for (j=low_j;j<=high_j;j++) {
283                 g3_add_delta_vec(&save_row[j],&last_p,get_dy_vec(HEIGHT(high_i,j)));
284                 if (j==high_j)
285                         save_p_high = last_p;
286                 else
287                         g3_add_delta_vec(&last_p,&last_p,&delta_j);
288         }
289
290         for (i=high_i-1;i>=viewer_i;i--) {
291
292                 g3_add_delta_vec(&save_p_low,&save_p_low,&delta_i);
293                 last_p = save_p_low;
294                 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,low_j)));
295                 
296                 for (j=low_j;j<viewer_j;j++) {
297                         g3s_point p2;
298
299                         g3_add_delta_vec(&p,&last_p,&delta_j);
300                         g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j+1)));
301
302                         draw_cell(i,j,&last_p2,&p2,&save_row[j+1],&save_row[j]);
303
304                         last_p = p;
305                         save_row[j] = last_p2;
306                         last_p2 = p2;
307
308                 }
309
310                 vm_vec_negate(&delta_j);                        //don't have a delta sub...
311
312                 g3_add_delta_vec(&save_p_high,&save_p_high,&delta_i);
313                 last_p = save_p_high;
314                 g3_add_delta_vec(&last_p2,&last_p,get_dy_vec(HEIGHT(i,high_j)));
315                 
316                 for (j=high_j-1;j>=viewer_j;j--) {
317                         g3s_point p2;
318
319                         g3_add_delta_vec(&p,&last_p,&delta_j);
320                         g3_add_delta_vec(&p2,&p,get_dy_vec(HEIGHT(i,j)));
321
322                         draw_cell(i,j,&p2,&last_p2,&save_row[j+1],&save_row[j]);
323
324                         last_p = p;
325                         save_row[j+1] = last_p2;
326                         last_p2 = p2;
327
328                 }
329
330                 save_row[j+1] = last_p2;
331
332                 vm_vec_negate(&delta_j);                //restore sign of j
333
334         }
335
336 }
337
338 void free_height_array()
339 {
340         d_free(height_array);
341 }
342
343 void load_terrain(char *filename)
344 {
345         grs_bitmap height_bitmap;
346         int iff_error;
347         int i,j;
348         ubyte h,min_h,max_h;
349
350         iff_error = iff_read_bitmap(filename,&height_bitmap,BM_LINEAR,NULL);
351         if (iff_error != IFF_NO_ERROR) {
352                 mprintf((1, "File %s - IFF error: %s",filename,iff_errormsg(iff_error)));
353                 Error("File %s - IFF error: %s",filename,iff_errormsg(iff_error));
354         }
355
356         if (height_array)
357                 d_free(height_array);
358         else
359                 atexit(free_height_array);              //first time
360
361         grid_w = height_bitmap.bm_w;
362         grid_h = height_bitmap.bm_h;
363
364         Assert(grid_w <= GRID_MAX_SIZE);
365         Assert(grid_h <= GRID_MAX_SIZE);
366
367         height_array = height_bitmap.bm_data;
368
369         max_h=0; min_h=255;
370         for (i=0;i<grid_w;i++)
371                 for (j=0;j<grid_h;j++) {
372
373                         h = HEIGHT(i,j);
374
375                         if (h > max_h)
376                                 max_h = h;
377
378                         if (h < min_h)
379                                 min_h = h;
380                 }
381
382         for (i=0;i<grid_w;i++)
383                 for (j=0;j<grid_h;j++)
384                         HEIGHT(i,j) -= min_h;
385         
386
387 //      d_free(height_bitmap.bm_data);
388
389         terrain_bm = terrain_bitmap;
390
391         build_light_table();
392 }
393
394
395 void get_pnt(vms_vector *p,int i,int j)
396 {
397         p->x = GRID_SCALE*i;
398         p->z = GRID_SCALE*j;
399         p->y = HEIGHT(i,j)*HEIGHT_SCALE;
400 }
401
402 vms_vector light = {0x2e14,0xe8f5,0x5eb8};
403
404 fix get_face_light(vms_vector *p0,vms_vector *p1,vms_vector *p2)
405 {
406         vms_vector norm;
407
408         vm_vec_normal(&norm,p0,p1,p2);
409
410         return -vm_vec_dot(&norm,&light);
411
412 }
413
414
415 fix get_avg_light(int i,int j)
416 {
417         vms_vector pp,p[6];
418         fix sum;
419         int f;
420
421         get_pnt(&pp,i,j);
422         get_pnt(&p[0],i-1,j);
423         get_pnt(&p[1],i,j-1);
424         get_pnt(&p[2],i+1,j-1);
425         get_pnt(&p[3],i+1,j);
426         get_pnt(&p[4],i,j+1);
427         get_pnt(&p[5],i-1,j+1);
428
429         for (f=0,sum=0;f<6;f++)
430                 sum += get_face_light(&pp,&p[f],&p[(f+1)%5]);
431
432         return sum/6;
433 }
434
435 void free_light_table()
436 {
437         if (light_array)
438                 d_free(light_array);
439
440 }
441
442 void build_light_table()
443 {
444         int i,j;
445         fix l,l2,min_l=0x7fffffff,max_l=0;
446
447
448         if (light_array)
449                 d_free(light_array);
450         else
451                 atexit(free_light_table);               //first time
452
453         MALLOC(light_array,ubyte,grid_w*grid_h);
454
455         for (i=1;i<grid_w;i++)
456                 for (j=1;j<grid_h;j++) {
457                         l = get_avg_light(i,j);
458
459                         if (l > max_l)
460                                 max_l = l;
461
462                         if (l < min_l)
463                                 min_l = l;
464
465                         //printf("light %2d,%2d = %8x\n",i,j,l);
466                 }
467
468         for (i=1;i<grid_w;i++)
469                 for (j=1;j<grid_h;j++) {
470
471                         l = get_avg_light(i,j);
472
473                         if (min_l == max_l) {
474                                 LIGHT(i,j) = l>>8;
475                                 continue;
476                         }
477
478                         l2 = fixdiv((l-min_l),(max_l-min_l));
479
480                         if (l2==f1_0)
481                                 l2--;
482
483                         LIGHT(i,j) = l2>>8;
484
485                         //printf("light %2d,%2d = %4x\n",i,j,l2>>8);
486
487                 }
488 }