Debian Codegraph @

similar/main/terrain.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Code to render cool external-scene terrain
23 *
24 */
25
26 #include <bitset>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <type_traits>
31
32 #include "3d.h"
33 #include "dxxerror.h"
34 #include "gr.h"
35 #include "texmap.h"
36 #include "iff.h"
37 #include "u_mem.h"
38 #include "inferno.h"
39 #include "textures.h"
40 #include "object.h"
41 #include "endlevel.h"
42 #include "render.h"
43 #include "player.h"
44 #include "segment.h"
45 #include "terrain.h"
46 #include <memory>
47
48 #define GRID_MAX_SIZE 64
49 #define GRID_SCALE i2f(2*20)
50 #define HEIGHT_SCALE f1_0
51
52 namespace dcx {
53
54 namespace {
55
56 static int grid_w,grid_h;
57
58 static RAIIdmem<ubyte[]> height_array;
59 static std::unique_ptr<uint8_t[]> light_array;
60
61 #define HEIGHT(_i,_j) (height_array[(_i)*grid_w+(_j)])
62 #define LIGHT(_i,_j) light_array[(_i)*grid_w+(_j)]
63
64 //!!#define HEIGHT(_i,_j) height_array[(grid_h-1-j)*grid_w+(_i)]
65 //!!#define LIGHT(_i,_j) light_array[(grid_h-1-j)*grid_w+(_i)]
66
67 #define LIGHTVAL(_i,_j) (static_cast<fix>(LIGHT(_i, _j)) << 8)
68
69 static grs_bitmap *terrain_bm;
70 static int org_i,org_j;
71 [[nodiscard]]
72 static std::unique_ptr<uint8_t[]> build_light_table(std::size_t grid_w, std::size_t grid_h);
73
74 }
75
76 }
77
78 namespace {
79
80 // ------------------------------------------------------------------------
81 static void draw_cell(grs_canvas &canvas, const vms_vector &Viewer_eye, const int i, const int j, g3_draw_tmap_point &p0, g3_draw_tmap_point &p1, g3_draw_tmap_point &p2, g3_draw_tmap_point &p3, int &mine_tiles_drawn)
82 {
83 std::array<g3_draw_tmap_point *, 3> pointlist{{
84 &p0,
85 &p1,
86 &p3,
87 }};
88
89 const fix lightval_i0j0{LIGHTVAL(i, j)};
90 const fix lightval_i0j1{LIGHTVAL(i, j + 1)};
91 const fix lightval_i1j0{LIGHTVAL(i + 1, j)};
92 g3_check_and_draw_tmap(canvas, pointlist, (std::array<g3s_uvl, 3>{{
93 {
94 .u = i * f1_0 / 4,
95 .v = j * f1_0 / 4,
96 .l = lightval_i0j0,
97 },
98 {
99 .u = i * f1_0 / 4,
100 .v = (j + 1) * f1_0 / 4,
101 .l = lightval_i0j1,
102 },
103 {
104 .u = (i + 1) * f1_0 / 4,
105 .v = j * f1_0 / 4,
106 .l = lightval_i1j0,
107 },
108 }}), (std::array<g3s_lrgb, 3>{{
109 {
110 .r = lightval_i0j0,
111 .g = lightval_i0j0,
112 .b = lightval_i0j0,
113 },
114 {
115 .r = lightval_i0j1,
116 .g = lightval_i0j1,
117 .b = lightval_i0j1,
118 },
119 {
120 .r = lightval_i1j0,
121 .g = lightval_i1j0,
122 .b = lightval_i1j0,
123 },
124 }}), *terrain_bm, draw_tmap);
125
126 pointlist[0] = &p1;
127 pointlist[1] = &p2;
128 const fix lightval_i1j1{LIGHTVAL(i + 1, j + 1)};
129 g3_check_and_draw_tmap(canvas, pointlist, (std::array<g3s_uvl, 3>{{
130 {
131 .u = i * f1_0 / 4,
132 .v = (j + 1) * f1_0 / 4,
133 .l = lightval_i0j1,
134 },
135 {
136 .u = (i + 1) * f1_0 / 4,
137 .v = (j + 1) * f1_0 / 4,
138 .l = lightval_i1j1,
139 },
140 {
141 .u = (i + 1) * f1_0 / 4,
142 .v = j * f1_0 / 4,
143 .l = lightval_i1j0,
144 },
145 }}), (std::array<g3s_lrgb, 3>{{
146 {
147 .r = lightval_i0j1,
148 .g = lightval_i0j1,
149 .b = lightval_i0j1,
150 },
151 {
152 .r = lightval_i1j1,
153 .g = lightval_i1j1,
154 .b = lightval_i1j1,
155 },
156 {
157 .r = lightval_i1j0,
158 .g = lightval_i1j0,
159 .b = lightval_i1j0,
160 },
161 }}), *terrain_bm, draw_tmap);
162
163 if (i==org_i && j==org_j)
164 mine_tiles_drawn |= 1;
165 if (i==org_i-1 && j==org_j)
166 mine_tiles_drawn |= 2;
167 if (i==org_i && j==org_j-1)
168 mine_tiles_drawn |= 4;
169 if (i==org_i-1 && j==org_j-1)
170 mine_tiles_drawn |= 8;
171
172 if (mine_tiles_drawn == 0xf) {
173 //draw_exit_model();
174 mine_tiles_drawn=-1;
175 window_rendered_data window;
176 render_mine(canvas, Viewer_eye, PlayerUniqueEndlevelState.exit_segnum, 0, window);
177 //if (ext_expl_playing)
178 // draw_fireball(&external_explosion);
179 }
180 }
181
182 class terrain_y_cache
183 {
184 static const std::size_t cache_size = 256;
185 std::bitset<cache_size> yc_flags;
186 std::array<vms_vector, cache_size> y_cache;
187 public:
188 vms_vector &operator()(uint_fast32_t h);
189 };
190
191 vms_vector &terrain_y_cache::operator()(uint_fast32_t h)
192 {
193 auto &dyp = y_cache[h];
194 if (auto &&ycf = yc_flags[h])
195 {
196 }
197 else
198 {
199 ycf = 1;
200 dyp = g3_rotate_delta_vec(vm_vec_copy_scale(surface_orient.uvec, h * HEIGHT_SCALE));
201 }
202 return dyp;
203 }
204
205 }
206
207 void render_terrain(grs_canvas &canvas, const vms_vector &Viewer_eye, const vms_vector &org_point,int org_2dx,int org_2dy)
208 {
209 g3s_point p,save_p_low,save_p_high;
210 g3s_point last_p2;
211 int i,j;
212 int low_i,high_i,low_j,high_j;
213 int viewer_i,viewer_j;
214 org_i = org_2dy;
215 org_j = org_2dx;
216
217 low_i = 0; high_i = grid_w-1;
218 low_j = 0; high_j = grid_h-1;
219
220 //@@start_point.x = org_point->x - GRID_SCALE*(org_i - low_i);
221 //@@start_point.z = org_point->z - GRID_SCALE*(org_j - low_j);
222 //@@start_point.y = org_point->y;
223 terrain_y_cache get_dy_vec;
224
225 #if !DXX_USE_OGL
226 Interpolation_method = 1;
227 #endif
228
229 auto delta_i{g3_rotate_delta_vec(vm_vec_copy_scale(surface_orient.rvec, GRID_SCALE))};
230 auto delta_j{g3_rotate_delta_vec(vm_vec_copy_scale(surface_orient.fvec, GRID_SCALE))};
231
232 auto start_point{vm_vec_scale_add(org_point, surface_orient.rvec, -(org_i - low_i) * GRID_SCALE)};
233 vm_vec_scale_add2(start_point,surface_orient.fvec,-(org_j - low_j)*GRID_SCALE);
234
235 {
236 const auto tv{vm_vec_build_sub(Viewer->pos, start_point)};
237 viewer_i = vm_vec_build_dot(tv,surface_orient.rvec) / GRID_SCALE;
238 viewer_j = vm_vec_build_dot(tv,surface_orient.fvec) / GRID_SCALE;
239 }
240
241 auto last_p = g3_rotate_point(start_point);
242 save_p_low = last_p;
243
244 g3s_point save_row[GRID_MAX_SIZE]{};
245 // Is this needed?
246 for (j=low_j;j<=high_j;j++) {
247 g3_add_delta_vec(save_row[j],last_p,get_dy_vec(HEIGHT(low_i,j)));
248 if (j==high_j)
249 save_p_high = last_p;
250 else
251 g3_add_delta_vec(last_p,last_p,delta_j);
252 }
253
254 int mine_tiles_drawn{0}; //flags to tell if all 4 tiles under mine have drawn
255 for (i=low_i;i<viewer_i;i++) {
256
257 g3_add_delta_vec(save_p_low,save_p_low,delta_i);
258 last_p = save_p_low;
259 g3_add_delta_vec(last_p2,last_p,get_dy_vec(HEIGHT(i+1,low_j)));
260
261 for (j=low_j;j<viewer_j;j++) {
262 g3s_point p2;
263
264 g3_add_delta_vec(p,last_p,delta_j);
265 g3_add_delta_vec(p2,p,get_dy_vec(HEIGHT(i+1,j+1)));
266
267 draw_cell(canvas, Viewer_eye, i, j, save_row[j], save_row[j+1], p2, last_p2, mine_tiles_drawn);
268
269 last_p = p;
270 save_row[j] = last_p2;
271 last_p2 = p2;
272
273 }
274
275 vm_vec_negate(delta_j); //don't have a delta sub...
276
277 g3_add_delta_vec(save_p_high,save_p_high,delta_i);
278 last_p = save_p_high;
279 g3_add_delta_vec(last_p2,last_p,get_dy_vec(HEIGHT(i+1,high_j)));
280
281 for (j=high_j-1;j>=viewer_j;j--) {
282 g3s_point p2;
283
284 g3_add_delta_vec(p,last_p,delta_j);
285 g3_add_delta_vec(p2,p,get_dy_vec(HEIGHT(i+1,j)));
286
287 draw_cell(canvas, Viewer_eye, i, j, save_row[j], save_row[j+1], last_p2, p2, mine_tiles_drawn);
288
289 last_p = p;
290 save_row[j+1] = last_p2;
291 last_p2 = p2;
292
293 }
294
295 save_row[j+1] = last_p2;
296
297 vm_vec_negate(delta_j); //restore sign of j
298
299 }
300
301 //now do i from other end
302
303 vm_vec_negate(delta_i); //going the other way now...
304
305 //@@start_point.x += (high_i-low_i)*GRID_SCALE;
306 vm_vec_scale_add2(start_point,surface_orient.rvec,(high_i-low_i)*GRID_SCALE);
307 g3_rotate_point(last_p,start_point);
308 save_p_low = last_p;
309
310 for (j=low_j;j<=high_j;j++) {
311 g3_add_delta_vec(save_row[j],last_p,get_dy_vec(HEIGHT(high_i,j)));
312 if (j==high_j)
313 save_p_high = last_p;
314 else
315 g3_add_delta_vec(last_p,last_p,delta_j);
316 }
317
318 for (i=high_i-1;i>=viewer_i;i--) {
319
320 g3_add_delta_vec(save_p_low,save_p_low,delta_i);
321 last_p = save_p_low;
322 g3_add_delta_vec(last_p2,last_p,get_dy_vec(HEIGHT(i,low_j)));
323
324 for (j=low_j;j<viewer_j;j++) {
325 g3s_point p2;
326
327 g3_add_delta_vec(p,last_p,delta_j);
328 g3_add_delta_vec(p2,p,get_dy_vec(HEIGHT(i,j+1)));
329
330 draw_cell(canvas, Viewer_eye, i, j, last_p2, p2, save_row[j+1], save_row[j], mine_tiles_drawn);
331
332 last_p = p;
333 save_row[j] = last_p2;
334 last_p2 = p2;
335
336 }
337
338 vm_vec_negate(delta_j); //don't have a delta sub...
339
340 g3_add_delta_vec(save_p_high,save_p_high,delta_i);
341 last_p = save_p_high;
342 g3_add_delta_vec(last_p2,last_p,get_dy_vec(HEIGHT(i,high_j)));
343
344 for (j=high_j-1;j>=viewer_j;j--) {
345 g3s_point p2;
346
347 g3_add_delta_vec(p,last_p,delta_j);
348 g3_add_delta_vec(p2,p,get_dy_vec(HEIGHT(i,j)));
349
350 draw_cell(canvas, Viewer_eye, i, j, p2, last_p2, save_row[j+1], save_row[j], mine_tiles_drawn);
351
352 last_p = p;
353 save_row[j+1] = last_p2;
354 last_p2 = p2;
355
356 }
357
358 save_row[j+1] = last_p2;
359
360 vm_vec_negate(delta_j); //restore sign of j
361
362 }
363
364 }
365
366 namespace dcx {
367
368 void free_height_array()
369 {
370 height_array.reset();
371 }
372
373 }
374
375 void load_terrain(const char *filename)
376 {
377 grs_bitmap height_bitmap;
378 int i,j;
379 ubyte h,min_h,max_h;
380
381 if (const auto iff_error{iff_read_bitmap(filename, height_bitmap, NULL)}; iff_error != iff_status_code::no_error)
382 {
383 Error("File %s - IFF error: %s",filename,iff_errormsg(iff_error));
384 }
385 grid_w = height_bitmap.bm_w;
386 grid_h = height_bitmap.bm_h;
387
388 Assert(grid_w <= GRID_MAX_SIZE);
389 Assert(grid_h <= GRID_MAX_SIZE);
390
391 height_array.reset(height_bitmap.get_bitmap_data());
392
393 max_h=0; min_h=255;
394 for (i=0;i<grid_w;i++)
395 for (j=0;j<grid_h;j++) {
396
397 h = HEIGHT(i,j);
398
399 if (h > max_h)
400 max_h = h;
401
402 if (h < min_h)
403 min_h = h;
404 }
405
406 for (i=0;i<grid_w;i++)
407 for (j=0;j<grid_h;j++)
408 HEIGHT(i,j) -= min_h;
409
410
411 // d_free(height_bitmap.bm_data);
412
413 terrain_bm = terrain_bitmap;
414 light_array = build_light_table(grid_w, grid_h);
415 }
416
417 namespace dcx {
418
419 namespace {
420
421 static vms_vector get_pnt(int i, int j)
422 {
423 // added on 02/20/99 by adb to prevent overflow
424 if (i >= grid_h) i = grid_h - 1;
425 if (i == grid_h - 1 && j >= grid_w) j = grid_w - 1;
426 // end additions by adb
427 return {
428 .x = GRID_SCALE * i,
429 .y = HEIGHT(i, j) * HEIGHT_SCALE,
430 .z = GRID_SCALE * j,
431 };
432 }
433
434 constexpr vms_vector light{0x2e14,0xe8f5,0x5eb8};
435
436 static fix get_face_light(const vms_vector &p0,const vms_vector &p1,const vms_vector &p2)
437 {
438 const auto norm{vm_vec_normal(p0, p1, p2)};
439 return -vm_vec_build_dot(norm,light);
440 }
441
442 static fix get_avg_light(int i,int j)
443 {
444 fix sum;
445 int f;
446
447 const auto pp = get_pnt(i, j);
448 const std::array<vms_vector, 6> p{{
449 get_pnt(i - 1, j),
450 get_pnt(i, j - 1),
451 get_pnt(i + 1, j - 1),
452 get_pnt(i + 1, j),
453 get_pnt(i, j + 1),
454 get_pnt(i - 1, j + 1),
455 }};
456
457 for (f=0,sum=0;f<6;f++)
458 sum += get_face_light(pp,p[f],p[(f+1)%5]);
459
460 return sum/6;
461 }
462
463 static std::unique_ptr<uint8_t[]> build_light_table(const std::size_t grid_w, const std::size_t grid_h)
464 {
465 std::size_t alloc = grid_w*grid_h;
466 std::unique_ptr<uint8_t[]> light_array{std::make_unique<uint8_t[]>(alloc)};
467 int i,j;
468 fix min_l = INT32_MAX, max_l = 0;
469 for (i=1;i<grid_w;i++)
470 for (j=1;j<grid_h;j++) {
471 const auto l{get_avg_light(i, j)};
472 if (l > max_l)
473 max_l = l;
474 if (l < min_l)
475 min_l = l;
476 }
477
478 for (i=1;i<grid_w;i++)
479 for (j=1;j<grid_h;j++) {
480 const auto l{get_avg_light(i, j)};
481
482 if (min_l == max_l) {
483 LIGHT(i,j) = l>>8;
484 continue;
485 }
486
487 auto l2{fixdiv((l - min_l), (max_l - min_l))};
488
489 if (l2==f1_0)
490 l2--;
491
492 LIGHT(i,j) = l2>>8;
493
494 }
495 return light_array;
496 }
497
498 }
499
500 void free_light_table()
501 {
502 light_array.reset();
503 }
504
505 }