Debian Codegraph @

Descent3/terrainrender.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/terrainrender.cpp $
21 * $Revision: 242 $
22 * $Date: 9/06/01 12:15p $
23 * $Author: Matt $
24 *
25 * Terrain rendering code
26 *
27 * $Log: /DescentIII/Main/terrainrender.cpp $
28 *
29 * 242 9/06/01 12:15p Matt
30 * Added bounds checking for postrender list.
31 *
32 * 241 4/19/00 5:30p Matt
33 * From Duane for 1.4
34 * Mac sky changes
35 *
36 * 240 3/20/00 12:21p Matt
37 * Merge of Duane's post-1.3 changes.
38 * Caching optimization.
39 *
40 * 239 10/21/99 9:29p Jeff
41 * B.A. Macintosh code merge
42 *
43 * 238 10/21/99 5:27p Gwar
44 * fixed a pesky crash
45 *
46 * 237 10/21/99 4:33p Gwar
47 * objects weren't displaying in NEWEDITOR release build
48 *
49 * 236 10/12/99 10:55a Gwar
50 * added DrawTerrainOutline for NEWEDITOR
51 *
52 * 235 10/12/99 10:19a Gwar
53 * adding terrain support to NEWEDITOR
54 *
55 * 234 7/28/99 3:40p Kevin
56 * Macintosh!
57 *
58 * 233 5/13/99 3:42p Ardussi
59 * changes for compiling on the Mac
60 *
61 * 232 5/07/99 7:22p Jason
62 * changes for cards with low videomemory
63 *
64 * 230 5/05/99 6:09p 3dsmax
65 * checked in dumb hack for now
66 *
67 * 229 4/27/99 3:36p Jason
68 * fixed terrain occlusion bug
69 *
70 * 228 4/23/99 7:18p Jason
71 * fixed some multiplayer issues with observer mode
72 *
73 * 227 4/22/99 12:05p Jason
74 * fixed some more direct3d issues
75 *
76 * 226 4/21/99 11:05a Kevin
77 * new ps_rand and ps_srand to replace rand & srand
78 *
79 * 225 4/17/99 6:15p Samir
80 * replaced gr.h with grdefs.h and fixed resulting compile bugs.
81 *
82 * 224 4/16/99 11:33p Jeff
83 * ifdef out some terrain triangle draw code, needs software renderer (for
84 * linux)
85 *
86 * 223 4/16/99 12:37a Matt
87 * Made Windows & Linux versions use the same qsort calls.
88 *
89 * 222 4/15/99 1:43a Jeff
90 * changes for linux compile
91 *
92 * 221 4/14/99 3:57a Jeff
93 * fixed case mismatch in #includes
94 *
95 * 220 4/03/99 2:20p Jason
96 * added better heat damage effect
97 *
98 * 219 4/01/99 1:19p Jason
99 * draw band even with textured sky
100 *
101 * 218 3/31/99 11:41a Jason
102 * added support for attached thick lightning
103 *
104 * 217 3/01/99 2:39p Jason
105 * fixed star and halo rendering
106 *
107 * 216 2/26/99 3:26p Jason
108 * made satellites clip properly
109 *
110 * 215 2/19/99 4:26p Jason
111 * more work on Katmai support
112 *
113 * 214 2/19/99 12:04p Jason
114 * took out sky band
115 *
116 * 213 2/17/99 1:59p Jason
117 * fixed some postrender issues
118 *
119 * 212 2/17/99 1:05p Jason
120 * revamped object/face/terrain selection code
121 *
122 * 211 2/16/99 12:39a Matt
123 * Removed render_objs from terrain_segment and created parallel array
124 *
125 * 210 2/09/99 12:10p Jason
126 * rewriting indoor engine
127 *
128 * 209 2/08/99 5:07p Jason
129 * orthogonalize sky rotation matrix
130 *
131 * 208 2/05/99 3:37p Jason
132 * added/fixed features for the designers
133 *
134 * 207 2/04/99 12:17p Kevin
135 * Small fix for release builds
136 *
137 * 206 2/04/99 12:13p Jason
138 * fixed release array
139 *
140 * 205 2/01/99 10:51a Jason
141 * fixed outlines on terrain
142 *
143 * 204 1/29/99 11:29a Jason
144 * added misc bug fixes/features
145 *
146 * 203 1/24/99 5:43p Jason
147 * changes for terrain tiling
148 *
149 * 202 1/24/99 2:11a Jason
150 * fixed dumb texture UV problem
151 *
152 * 201 1/22/99 3:59p Jason
153 * added 256x256 textures to help with terrain skies
154 *
155 * 200 1/21/99 11:17a Jason
156 * fixed terrain rendering problem at the edges of the map
157 *
158 * 199 1/20/99 3:50p Jason
159 * fixed automap problem
160 *
161 * 197 1/20/99 2:48p Jason
162 * more terrain speedups
163 *
164 * 196 1/20/99 1:40p Jason
165 * terrain speedups
166 *
167 * 195 1/20/99 10:50a Jason
168 * added new terrain
169 *
170 * 194 12/29/98 6:12p Jason
171 * stupid fix for level designers
172 *
173 * 193 12/17/98 2:18p Jason
174 * fixed mip mapping problem
175 *
176 * 192 12/11/98 2:43p Jason
177 * tweaked snow
178 *
179 * 191 12/09/98 1:09p Jason
180 * second draft of automap
181 *
182 * 190 11/18/98 5:10p Jason
183 * fixed z buffer snow
184 *
185 * 189 11/18/98 4:30p Jason
186 * added some functionality to terrain fog
187 *
188 * 188 11/18/98 2:54p Jason
189 * added snow effect
190 *
191 * 187 11/06/98 5:42p Jason
192 * took out terrain casting
193 *
194 * 186 10/22/98 4:02p Jason
195 * made lightmaps draw even in nolightmap mode
196 *
197 * 185 10/22/98 10:21a Jason
198 * fixed wrap/clamp problem
199 *
200 * 184 10/21/98 9:28p Jason
201 * Made no lightmaps work globally
202 *
203 * 183 10/21/98 8:40p Jason
204 * testing for Mark
205 *
206 * 182 10/21/98 6:54p Jason
207 * changes for shitty direct3d cards
208 *
209 * 181 10/19/98 6:30p Jeff
210 * changes made for detail variables. Put in preset values. Preset
211 * options. Removed terrain_cast from detail. Put new callbacks in
212 * UIListBox and UISlider
213 *
214 * 180 10/16/98 1:54p Kevin
215 * Changes for Demo Beta 4
216 *
217 * 179 10/07/98 5:17p Jason
218 * fixed lighting on the terrain
219 *
220 * 178 10/03/98 11:21p Matt
221 * Added system to seperately control outline mode for mine, terrain, sky,
222 * & objects
223 *
224 * 177 10/02/98 5:31p Jason
225 * tweaks for statelimited stuff
226 *
227 * 176 10/02/98 12:26p Jason
228 * fixed fogging of indoor rooms
229 *
230 * 175 9/29/98 3:00p Jason
231 * fixed state limited bug with lightmaps
232 *
233 * 174 9/29/98 2:02p Jason
234 * fixed external rooms rendering even when they shouldn't
235 *
236 * 173 9/25/98 9:25p Jason
237 * did some graphics optimizations
238 *
239 * 172 9/25/98 11:58a Jason
240 * various rendering optimizations
241 *
242 * 171 9/24/98 12:57p Jason
243 * more state limited optimizations
244 *
245 * 170 9/23/98 11:16a Jason
246 * fixed some lightmap problems
247 *
248 * 169 9/22/98 11:46a Jason
249 * made terrain lightmap rendering more state friendly
250 *
251 * 168 9/17/98 6:51p Jason
252 * fixed streaking vectors
253 *
254 * 167 9/17/98 3:03p Jason
255 * added lightning and invul hit effects
256 *
257 * 166 9/11/98 5:09p Jason
258 * made terrain state-change friendly
259 *
260 * 165 8/25/98 6:34p Craig
261 * Include terrain outline func in non-editor debug versions.
262 *
263 * 164 8/25/98 1:50p Jason
264 * made outlines draw in ndebug mode, instead of editor mode
265 *
266 * 163 8/19/98 5:48p Jason
267 * made robots have at least a mininum amount of lighting
268 *
269 * 162 8/19/98 2:19p Jeff
270 * moved detail globals to a struct
271 *
272 * 161 8/17/98 5:27p Jason
273 * fixed dynamic lighting for objects above the terrain ceiling
274 *
275 * 160 8/14/98 4:00p Jason
276 * added specular objects outside
277 *
278 * 159 8/06/98 11:00a Jason
279 * added blurred stars
280 *
281 * 158 8/04/98 2:55p Jason
282 * added motion blurred stars
283 *
284 * 157 7/24/98 4:49p Jason
285 * made terrain casting work again
286 *
287 * 156 7/20/98 12:04p Jason
288 * added per level satellite lighting
289 *
290 * 155 6/24/98 7:03p Jason
291 * fixed omega and external room sorting problem
292 *
293 * 154 6/24/98 3:56p Jason
294 * added code for new omega cannon
295 *
296 * 153 6/16/98 2:56p Jason
297 * fixed terrain occlusion bug with objects
298 *
299 * 152 6/15/98 4:00p Jason
300 * replaced monochromatic polymodel lighting with rgb lighting
301 *
302 * 151 6/12/98 5:15p Jason
303 * added dynamic volume light system
304 *
305 * 150 6/10/98 12:23p Jason
306 * added new skies
307 *
308 * 149 6/04/98 6:45p Jason
309 * made objects use the terrain occlusion system
310 *
311 * 148 5/26/98 6:54p Jason
312 * viseffect optimizations
313 *
314 * 147 5/25/98 3:46p Jason
315 * added better light glows
316 *
317 * 146 5/14/98 2:17p Jason
318 * more changes to be memory friendly
319 *
320 * 145 5/14/98 12:56p Jason
321 * changes to help lower memory usage
322 *
323 * 144 5/11/98 7:03p Jason
324 * fixed terrain bug with microwave effect
325 *
326 * 143 5/06/98 6:18p Jason
327 * improved terrain raycasting
328 *
329 * 142 5/05/98 6:27p Chris
330 * Added BBoxs and spheres to external rooms (in mode 2)
331 *
332 * 141 5/05/98 5:28p Jason
333 * more terrain speedups
334 *
335 * 140 5/05/98 4:38p Jason
336 * added raycasting test stuff
337 *
338 * 139 5/04/98 8:04p Jason
339 * made wireframe sky view
340 *
341 * 138 5/01/98 5:46p Jason
342 * fixed external room popping bug
343 *
344 * 137 5/01/98 3:51p Jason
345 * sped up terrain rendering by precaching all megacell lod bitmaps
346 *
347 * 136 5/01/98 12:42p Jason
348 * slight framerate enhancement
349 *
350 * 135 4/30/98 6:45p Jason
351 * more changes for weather
352 *
353 * 134 4/30/98 12:22p Jason
354 * did some lo-res model optimizations
355 *
356 * 133 4/29/98 3:27p Jason
357 * added weather engine
358 *
359 * 132 4/29/98 11:38a Jason
360 * added some weather effects (first pass)
361 *
362 * 131 4/23/98 12:21p Jason
363 * fixed rendering bug if satellite values were out of range
364 *
365 * 130 4/19/98 5:00p Jason
366 * added cool napalm effect, plus made object effects dynamically
367 * allocated
368 *
369 * 129 4/17/98 3:57p Jason
370 * added microwave effect
371 *
372 * 128 4/10/98 6:04p Jason
373 * fixed some lighting issues
374 *
375 * 127 4/03/98 1:36p Jason
376 * saved some memory
377 *
378 * 126 4/01/98 11:26a Jeff
379 * Added an editor bool to skip Terrain object rendering
380 *
381 * 125 3/26/98 10:54a Jason
382 * tweaked some stuff for size independant terrain
383 *
384 * 124 3/17/98 5:02p Jason
385 * sped up rendering the terrain from the mine by a large amount
386 *
387 * 123 3/09/98 4:54p Jason
388 * don't render objects that can't been seen through a portal
389 *
390 * 122 3/06/98 2:33p Jason
391 * fixed potential memory overwrite bug
392 *
393 * 121 3/06/98 2:17p Jason
394 * sped up terrain by making lod'd triangles quads
395 *
396 * 120 2/25/98 2:05p Jason
397 * did FOV and object visibility changes
398 *
399 * 119 2/17/98 2:22p Jason
400 * fixed some potential z sorting problems
401 *
402 * 118 2/12/98 1:32p Jason
403 * got mipmapping working
404 *
405 * 117 2/10/98 5:42p Jason
406 * fixed object rendering with RT_ROOM type
407 *
408 * 116 2/10/98 10:28a Matt
409 * Got rid of compile warning
410 *
411 * 115 2/09/98 7:31p Matt
412 * Changed object rendering code to not look at Viewer_object to determine
413 * whether to render the object
414 *
415 * 114 2/05/98 6:02p Jason
416 * fixed bug my previous rev introduced
417 *
418 * 113 2/05/98 4:41p Jason
419 * fixed off by one bug
420 *
421 * 112 2/05/98 12:35p Jason
422 * fixed screen clearing problem with gouraud sky
423 *
424 * 111 2/04/98 6:58p Matt
425 * Fixed viseffect rendering on terrain.
426 *
427 * 110 2/04/98 6:09p Matt
428 * Changed object room number to indicate a terrain cell via a flag. Got
429 * rid of the object flag which used to indicate terrain.
430 *
431 * 109 2/02/98 3:19p Jason
432 * made stars draw with different colors
433 *
434 * 108 1/30/98 12:12p Jason
435 * fixed LOD bug. This gives us twice the framerate in hilly areas!!!
436 *
437 * 107 1/29/98 5:50p Matt
438 * Changed old camera object type to be viewer object (for editor), and
439 * now camera objects will just be for game cameras.
440 *
441 * 106 1/28/98 4:06p Jason
442 * made stars not darken so much on blur
443 *
444 * 105 1/28/98 3:55p Jason
445 * added stars after the textured sky
446 *
447 * 104 1/23/98 6:25p Jason
448 * Got spray weapons working
449 *
450 * 103 1/20/98 2:17p Jason
451 * fixed weird sorting problem with objects and viseffects
452 *
453 * 102 1/20/98 12:10p Jason
454 * implemented vis effect system
455 *
456 * 101 1/16/98 8:06p Jason
457 * added selectable halos and atmosphere blends to satellites
458 *
459 * 100 1/16/98 3:05p Jason
460 * fixed LOD cracking at terrain edges
461 *
462 * 99 1/16/98 2:47p Jason
463 * tweaked some things for speed reasons
464 *
465 * 98 1/16/98 1:04p Jason
466 * don't do update environment
467 *
468 * 97 1/15/98 6:32p Jason
469 * added v wrapping for sky textures
470 *
471 * 96 1/15/98 12:42p Jason
472 * big fix for terrain sky rendering
473 *
474 * 95 1/12/98 3:34p Jason
475 * sped up indoor rendering by clipping faces against portals
476 *
477 * 94 1/07/98 4:19p Jason
478 * added dome to terrain sky
479 *
480 * 93 1/06/98 1:29p Matt
481 * Cleaned up interfaces to rendering routines, deleted unused code, etc.
482 *
483 * 92 1/02/98 6:08p Jason
484 * fixed bug with alphaed moons and halos
485 *
486 * 90 12/30/97 5:45p Jason
487 * added toggleable fog
488 *
489 * 89 12/29/97 5:44p Samir
490 * Took out references to grViewport and old 2d library.
491 *
492 * 88 12/19/97 5:58p Jason
493 * fixed out-of-bounds bug
494 *
495 * 87 12/19/97 5:38p Jason
496 * fixed errors with 2d/3d integration
497 *
498 * 86 12/19/97 12:20p Jason
499 * changes for better 2d/3d system integration
500 *
501 * 85 12/18/97 3:29p Jason
502 * fixed some lightmap wrapping issues
503 *
504 * 84 12/17/97 5:26p Jason
505 * added support for selectable wraparound sky
506 *
507 * 83 12/17/97 4:08p Jason
508 * took out fog (again)
509 *
510 * 82 12/10/97 6:54p Jason
511 * toned down star blur effect
512 *
513 * 81 12/09/97 7:13p Jason
514 * added support for blurred lines in hardware
515 *
516 * 80 12/01/97 10:52a Jason
517 * separated satellite and star rendering flags
518 *
519 * 79 11/21/97 5:33p Jason
520 * fixed far clip bug
521 *
522 * 78 11/19/97 5:23p Jason
523 * added terrain dynamic lighting
524 *
525 * 77 11/17/97 8:33p Jason
526 * fixed bug with object selection introduced by last rev
527 *
528 * 76 11/17/97 6:13p Mark
529 * allowed object 0 to be selected
530 *
531 * 75 11/10/97 12:21p Jason
532 * implemented different object visibility
533 *
534 * 74 11/10/97 12:00p Jason
535 * fixed view scaling bug in g3_DrawRotatedBitmap
536 *
537 * 73 10/30/97 4:38p Jason
538 * special cased highest-lod in DrawTerrainTrianglesHardware
539 *
540 * 72 10/29/97 12:37p Jason
541 * fixed tjoint problems
542 *
543 * 71 10/28/97 2:06p Jason
544 * check for backfaces early
545 *
546 * 70 10/17/97 1:23p Jason
547 * took out references to bitmap8bit.h
548 *
549 * 69 10/13/97 5:06p Chris
550 * Made it compile in the main project (outline mode only in editor)
551 *
552 * 68 10/10/97 6:31p Jason
553 * added extra level of detail level
554 *
555 * 67 10/09/97 11:11a Jason
556 * turned fog back on for now
557 *
558 * 66 10/08/97 4:34p Jason
559 * fixed line drawing problems
560 *
561 * 65 10/06/97 6:39p Jason
562 * got terrain objects working with lightmaps/radiosity
563 *
564 * 64 10/02/97 6:45p Jason
565 * optimized terrain rotation
566 *
567 * 63 10/02/97 6:21p Jason
568 * checked in for matt
569 *
570 *
571 * 62 10/02/97 3:48p Matt
572 * Don't render room (via room object) if terrain renderer was called from
573 * room renderer
574 *
575 * 61 10/02/97 2:45p Jason
576 * got invisible terrain cells working
577 *
578 * 60 10/01/97 7:51p Matt
579 * Added code for external rooms
580 *
581 * 59 9/30/97 4:20p Jason
582 * took out terrain object light
583 *
584 * 58 9/29/97 5:40p Jason
585 * patched circular linked list bug
586 *
587 * 57 9/21/97 7:57p Jason
588 * separated hardware/software rendering pipes, plus got lightmaps working
589 * with tmap2's
590 *
591 * 56 9/21/97 7:17p Jason
592 * Made terrain lightmaps work again
593 *
594 * 55 9/18/97 1:24p Matt
595 * Changed terrain object rendering to keep its data locally, instead of
596 * in the object struct
597 *
598 * 54 9/17/97 5:08p Jason
599 * took out fog (maybe just temporary)
600 *
601 *
602 * 53 9/17/97 12:02p Jason
603 * ripped out segment engine
604 *
605 * 52 9/12/97 7:06p Jason
606 * fixed bug with MAX_RENDER_ROOMS being too low
607 *
608 * 51 9/12/97 5:17p Jason
609 * fixed problem where buildings could be lower than terrain, thus
610 * generating error
611 *
612 * 50 9/10/97 5:17p Jason
613 * more lighting improvements
614 *
615 * 49 9/09/97 6:17p Jason
616 * fixed bug with color model and dynamic objects
617 *
618 * 48 9/09/97 6:15p Jason
619 * Added groovy dynamic terrain lighting system
620 *
621 * 47 9/09/97 1:12p Jason
622 * fixed color model bug
623 *
624 * 46 9/09/97 11:44a Jason
625 * changed the way alpha works with the renderer
626 *
627 * 45 9/08/97 4:13p Jason
628 * added colored lighting for the terrain
629 *
630 * 44 9/05/97 4:38p Jason
631 * incremental updates
632 *
633 * 43 9/02/97 11:07a Jason
634 * added alpha component to uvl struct
635 *
636 * 42 8/29/97 11:41a Jason
637 * made textures not clamp if renderering the mine view
638 *
639 * 41 8/26/97 10:23a Jason
640 * added some asserts, plus clamping for terrain textures
641 *
642 * 40 8/20/97 6:18p Jason
643 * fixed memory corruption bug
644 *
645 * 39 8/20/97 4:22p Matt
646 * Took out unneeded extern
647 *
648 * 38 8/19/97 5:12p Jason
649 * (hopefully) fixed the polygon object popping problem
650 *
651 * 37 8/07/97 5:06p Jason
652 * added new tiling system for terrain
653 *
654 * 36 8/07/97 3:19p Jason
655 * replaced old terrain tmap system with new one
656 *
657 * 35 8/07/97 11:47a Jason
658 * got tmap2s rotatable by themselves
659 *
660 * 34 8/06/97 5:00p Jason
661 * ripped out lightmaps pending a better lighting system for terrain
662 *
663 * 33 8/06/97 11:43a Jason
664 * made lightmaps work correctly in the terrain
665 *
666 * 32 8/01/97 5:46p Jason
667 * did some housekeeping
668 *
669 * 31 7/31/97 3:28p Jason
670 * fixed bug where memory would get corrupted when drawing newstyle
671 * gouraud objects
672 *
673 * 30 7/29/97 5:33p Jason
674 * precompute the edge rotation list
675 *
676 * 29 7/29/97 10:28a Jason
677 * added RenderOBject abstraction layer
678 *
679 * 28 7/28/97 5:23p Jason
680 * added features to lod engine - namely dynamic texture generation
681 *
682 * 27 7/28/97 1:14p Chris
683 * Added support for sub-object visability. Plus, debris.
684 *
685 * 26 7/25/97 5:25p Jason
686 * fixed multiple bugs in terrain LOD engine
687 *
688 * 25 7/23/97 6:27p Jason
689 * added code to support terrain simplification
690 *
691 * 24 7/21/97 1:59p Jason
692 * added user adjustable fog parameters
693 *
694 * 23 7/20/97 8:37p Jason
695 * fixed some fog problems
696 *
697 * 22 7/20/97 7:37p Jason
698 * added new sky
699 *
700 * 21 7/20/97 3:40p Jason
701 * got room/terrain links working (at least in the terrain)
702 *
703 * 20 7/18/97 7:25p Jason
704 * do object drawing rather better
705 *
706 * 19 7/17/97 3:00p Jason
707 * changed "moons" to be called satellites since we're going to have suns
708 * and other things in orbit
709 *
710 *
711 * 18 7/16/97 5:20p Jason
712 * implemented selection of moons with mouse click
713 *
714 * 17 7/16/97 4:18p Matt
715 * Changes to work with changed 3d draw functions
716 *
717 * 16 7/15/97 7:40p Jason
718 * made fogplane obscenely close
719 *
720 * 15 7/15/97 6:44p Mark
721 *
722 * 14 7/15/97 5:32p Jason
723 * got simple static lighting working with objects
724 *
725 * 13 7/15/97 4:57p Mark
726 * Corrected usage of UseHardware flag
727 *
728 * 12 7/15/97 4:54p Jason
729 * made water not move its friends
730 *
731 * 11 7/15/97 4:18p Matt
732 * Don't do water undulation on tiles that have tmap2's on them
733 *
734 * 10 7/15/97 4:11p Jason
735 * added focus selection to terrain objects
736 *
737 * 9 7/15/97 3:33p Jason
738 * fixed some object sorting problems
739 *
740 * 8 7/15/97 3:09p Jason
741 * fixed dumb bug that I introduced in my last rev
742 *
743 * 7 7/15/97 2:59p Matt
744 * Added assert
745 *
746 * 39 6/24/97 4:24p Jason
747 * changes for y only terrain
748 *
749 * 38 6/24/97 12:45p Jason
750 * checked in for safety
751 *
752 * 37 6/17/97 11:35a Jason
753 * checked in for safety
754 *
755 * 36 6/16/97 2:34p Jason
756 * added 3dfx support
757 *
758 * 35 6/12/97 12:26p Jason
759 * fixed dumb rotational bug
760 *
761 * 34 5/22/97 5:16p Jason
762 * added tmap2 capability to the terrain
763 *
764 * 33 5/21/97 3:40p Jason
765 * added terrain editing stuff
766 *
767 * 32 5/19/97 5:10p Jason
768 * changes for our new abstracted renderer code
769 *
770 * 31 5/16/97 4:53p Jason
771 * made terrain clear the screen only when called from inside a mine
772 *
773 * 30 5/15/97 4:37p Jason
774 * fixed some terrain bugs
775 *
776 * 29 5/15/97 3:28a Jason
777 * tried to get subwindow clearing to work with terrain - but it won't
778 * until samir fixes lock_clear
779 *
780 * 28 5/13/97 11:34a Samir
781 * Changed some NDEBUGs to ifdef EDITORs
782 *
783 * 27 5/11/97 1:31p Matt
784 * Added code to only render terrain that might be visible when terrain
785 * called from the mine renderer.
786 *
787 * $NoKeywords: $
788 */
789 #ifdef NEWEDITOR
790 #include "neweditor\globals.h"
791 void RenderMine(int viewer_roomnum, int flag_automap, int called_from_terrain, bool render_all, bool outline, bool flat,
792 prim *prim);
793 #endif
794 #include "terrain.h"
795 #include "grdefs.h"
796 #include "3d.h"
797 #include "pstypes.h"
798 #include "pserror.h"
799 #include "renderer.h"
800 #include "gametexture.h"
801 #include "descent.h"
802 #include "render.h"
803 #include "game.h"
804 #include "ddio.h"
805 #include "polymodel.h"
806 #include "lighting.h"
807 #include "vecmat.h"
808 #include "renderobject.h"
809 #include "findintersection.h"
810 #include "weapon.h"
811 #include "weather.h"
812 #include "viseffect.h"
813 #ifdef EDITOR
814 #include "editor\d3edit.h"
815 #endif
816 #include "fireball.h"
817 #include <stdlib.h>
818 #include "config.h"
819 #include "gameloop.h"
820 #include "postrender.h"
821 #include "psrand.h"
822 #include "player.h"
823
824 #include <algorithm>
825
826 #define TERRAIN_PERSPECTIVE_TEXTURE_DEPTH 1 * TERRAIN_SIZE
827 #define LOD_ROW_SIZE (MAX_LOD_SIZE + 1)
828 int DrawTerrainTrianglesSoftware(int index, int bm_handle, int upper_left, int lower_right);
829 int DrawTerrainTrianglesHardware(int index, int bm_handle, int upper_left, int lower_right);
830 int DrawTerrainTrianglesHardwareNoLight(int index, int bm_handle, int upper_left, int lower_right);
831 void DrawTerrainLightmapsHardware(int index, int upper_left, int lower_right);
832 void DrawSky(vector *veye, matrix *vorient);
833 function_mode View_mode;
834 int ManageFramerate = 0;
835 int MinAllowableFramerate = 15;
836 uint8_t Fast_terrain = 1;
837 float Far_fog_border;
838 vector Terrain_viewer_eye;
839 uint8_t Terrain_from_mine = 0;
840 uint8_t Show_invisible_terrain = 0;
841 int Terrain_objects_drawn = 0;
842 vector Last_frame_stars[MAX_STARS];
843 float Terrain_texture_distance = DEFAULT_TEXTURE_DISTANCE;
844 int Check_terrain_portal = 0;
845 static vector Temp_sky_vectors[MAX_HORIZON_PIECES][6];
846 static vector Temp_sky_vectors_unrotated[MAX_HORIZON_PIECES][6];
847 // Last time terrain was rendered
848 float Last_terrain_render_time = -1;
849 // Sets UV's based on 90 degree rotations
850 float TerrainUSpeedup[4][LOD_ROW_SIZE * LOD_ROW_SIZE];
851 float TerrainVSpeedup[4][LOD_ROW_SIZE * LOD_ROW_SIZE];
852 #if (!defined(RELEASE) || defined(NEWEDITOR))
853 // for building a render list for each terrain cell
854 int render_next[MAX_OBJECTS];
855 #endif
856 void DrawPlayerOnWireframe();
857 float Clip_scale_left, Clip_scale_top, Clip_scale_right, Clip_scale_bot;
858 #ifdef NEWEDITOR
859 bool Rendering_main_view = true;
860 #endif
861 void InitTerrainRenderSpeedups() {
862 // Figure out a table of values for rotated uv points
863 for (int y = 0; y <= MAX_LOD_SIZE; y++) {
864 for (int x = 0; x <= MAX_LOD_SIZE; x++) {
865 TerrainUSpeedup[0][(y * LOD_ROW_SIZE) + x] = (float)x / (float)MAX_LOD_SIZE;
866 TerrainVSpeedup[0][(y * LOD_ROW_SIZE) + x] = (float)y / (float)MAX_LOD_SIZE;
867 TerrainUSpeedup[1][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)y / (float)MAX_LOD_SIZE);
868 TerrainVSpeedup[1][(y * LOD_ROW_SIZE) + x] = (float)x / (float)MAX_LOD_SIZE;
869 TerrainUSpeedup[2][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)x / (float)MAX_LOD_SIZE);
870 TerrainVSpeedup[2][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)y / (float)MAX_LOD_SIZE);
871 TerrainUSpeedup[3][(y * LOD_ROW_SIZE) + x] = ((float)y / (float)MAX_LOD_SIZE);
872 TerrainVSpeedup[3][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)x / (float)MAX_LOD_SIZE);
873 }
874 }
875 }
876 // codes a point for visibility in the window passed to RenderTerrain()
877 uint8_t CodeTerrainPoint(g3Point *p) {
878 uint8_t cc = 0;
879 if (p->p3_sx > Clip_scale_right)
880 cc |= CC_OFF_RIGHT;
881 if (p->p3_sx < Clip_scale_left)
882 cc |= CC_OFF_LEFT;
883 if (p->p3_sy > Clip_scale_bot)
884 cc |= CC_OFF_BOT;
885 if (p->p3_sy < Clip_scale_top)
886 cc |= CC_OFF_TOP;
887 return cc;
888 }
889 // Returns true if light can hit this segment/heighth bit
890 int IsTerrainDynamicChecked(int seg, int bit) {
891 if (seg < 0 || seg >= (TERRAIN_WIDTH * TERRAIN_DEPTH))
892 return 1;
893 if (bit >= 8)
894 return 1;
895 uint8_t val = Terrain_dynamic_table[seg] & (1 << bit);
896 if (val)
897 return 1;
898 return 0;
899 }
900 // Gets the dynamic light value for this position
901 float GetTerrainDynamicScalar(vector *pos, int seg) {
902 float cube_values[10];
903 int y_increment = MAX_TERRAIN_HEIGHT / 8;
904 int y_int = pos->y / y_increment;
905 int x_int = pos->x / TERRAIN_SIZE;
906 int z_int = pos->z / TERRAIN_SIZE;
907
908 float x_norm = (pos->x / TERRAIN_SIZE) - x_int;
909 float z_norm = (pos->z / TERRAIN_SIZE) - z_int;
910 float y_norm = (pos->y / y_increment) - y_int;
911 if (y_norm < 0) {
912 y_norm = 0;
913 y_int = 0;
914 }
915 if (y_norm > 1) {
916 y_norm = 1.0;
917 y_int = 7;
918 }
919 if (x_norm < 0 || x_norm > 1 || z_norm < 0 || z_norm > 1)
920 return .5;
921 float left_norm, right_norm, top_norm, bottom_norm, scalar;
922
923 cube_values[0] = IsTerrainDynamicChecked(seg, y_int);
924 cube_values[1] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH, y_int);
925 cube_values[2] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH + 1, y_int);
926 cube_values[3] = IsTerrainDynamicChecked(seg + 1, y_int);
927 cube_values[4] = IsTerrainDynamicChecked(seg, y_int + 1);
928 cube_values[5] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH, y_int + 1);
929 cube_values[6] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH + 1, y_int + 1);
930 cube_values[7] = IsTerrainDynamicChecked(seg + 1, y_int + 1);
931 left_norm = ((1 - z_norm) * cube_values[0]) + (z_norm * cube_values[1]);
932 right_norm = ((1 - z_norm) * cube_values[3]) + (z_norm * cube_values[2]);
933 bottom_norm = ((1 - x_norm) * left_norm) + (x_norm * right_norm);
934 left_norm = ((1 - z_norm) * cube_values[4]) + (z_norm * cube_values[5]);
935 right_norm = ((1 - z_norm) * cube_values[7]) + (z_norm * cube_values[6]);
936 top_norm = ((1 - x_norm) * left_norm) + (x_norm * right_norm);
937 scalar = ((1 - y_norm) * bottom_norm) + (y_norm * top_norm);
938 ASSERT(scalar >= 0 && scalar <= 1);
939 return scalar;
940 }
941 // Takes a min,max vector and makes a surrounding cube from it
942 void MakePointsFromMinMax(vector *corners, vector *minp, vector *maxp);
943 struct obj_sort_item {
944 int objnum;
945 float dist;
946 int vis_effect;
947 };
948 // Compare function for room face sort
949 static int obj_sort_func(const obj_sort_item *a, const obj_sort_item *b) {
950 if (a->dist < b->dist)
951 return -1;
952 else if (a->dist > b->dist)
953 return 1;
954 else
955 return 0;
956 }
957 // Returns true if the object is outside of our terrain portal
958 int ObjectOutOfPortal(object *obj) {
959 g3Point pnt1, pnt2;
960 uint8_t anded = 0xff;
961 g3_RotatePoint(&pnt1, &obj->min_xyz);
962 if (pnt1.p3_codes & CC_BEHIND)
963 return 0;
964 g3_ProjectPoint(&pnt1);
965 anded &= CodeTerrainPoint(&pnt1);
966 g3_RotatePoint(&pnt2, &obj->max_xyz);
967 if (pnt2.p3_codes & CC_BEHIND)
968 return 0;
969 g3_ProjectPoint(&pnt2);
970 anded &= CodeTerrainPoint(&pnt2);
971 if (anded)
972 return 1;
973 return 0;
974 }
975 obj_sort_item objs_to_render[MAX_OBJECTS + MAX_VIS_EFFECTS];
976 obj_sort_item rooms_to_render[MAX_ROOMS];
977 // Checks to see if this object can even be seen from our current viewpoint
978 // By shooting rays to it
979 // Returns true if any of the rays hit
980 int ShootRaysToObject(object *obj) {
981 vector corners[8];
982 if (obj->type == OBJ_ROOM) {
983 room *rp = &Rooms[obj->id];
984 MakePointsFromMinMax(corners, &rp->min_xyz, &rp->max_xyz);
985 } else {
986 MakePointsFromMinMax(corners, &obj->min_xyz, &obj->max_xyz);
987 }
988 fvi_info hit_info;
989 fvi_query fq;
990 fq.p0 = &Viewer_object->pos;
991 fq.startroom = Viewer_object->roomnum;
992 fq.rad = 0.0f;
993 fq.flags = FQ_NO_RELINK | FQ_EXTERNAL_ROOMS_AS_SPHERE | FQ_IGNORE_EXTERNAL_ROOMS;
994 fq.thisobjnum = Viewer_object - Objects;
995 fq.ignore_obj_list = NULL;
996
997 for (int i = 0; i < 8; i++) {
998 fq.p1 = &corners[i];
999 int fate = fvi_FindIntersection(&fq, &hit_info);
1000 /*g3Point pnt1,pnt2;
1001 vector fpnt = *fq.p0 + 3.0 * Viewer_object->orient.fvec;
1002 g3_RotatePoint (&pnt1,&fpnt);
1003 g3_RotatePoint (&pnt2,&hit_info.hit_pnt);
1004 g3_DrawLine ((GR_RGB(255,255,255)),&pnt1,&pnt2);*/
1005 if (fate == HIT_NONE)
1006 return 1;
1007 }
1008 return 0;
1009 }
1010 // Returns true if the external room is in the view cone
1011 // Else returns false
1012 bool ExternalRoomVisible(room *rp, vector *center, float *zdist) {
1013 ASSERT(rp->flags & RF_EXTERNAL);
1014 g3Point pnt;
1015 uint8_t ccode;
1016
1017 vector corners[8];
1018 g3_RotatePoint(&pnt, center);
1019 *zdist = pnt.p3_z;
1020 MakePointsFromMinMax(corners, &rp->min_xyz, &rp->max_xyz);
1021 uint8_t andbyte = 0xff;
1022 for (int t = 0; t < 8; t++) {
1023 g3_RotatePoint(&pnt, &corners[t]);
1024 ccode = g3_CodePoint(&pnt);
1025 if (!ccode)
1026 return true;
1027 andbyte &= ccode;
1028 }
1029 if (andbyte)
1030 return false;
1031 return true;
1032 }
1033 // Render all the rooms out on the terrain for this frame
1034 void RenderTerrainRooms() {
1035 object *obj;
1036 #ifdef EDITOR
1037 if (!Terrain_render_ext_room_objs)
1038 return;
1039 #endif
1040
1041 int room_count = 0;
1042 float zdist;
1043 int use_occlusion = 0;
1044 int src_occlusion_index;
1045 int i;
1046 if (Terrain_from_mine)
1047 return;
1048 if ((Terrain_checksum + 1) == Terrain_occlusion_checksum) {
1049 use_occlusion = 1;
1050 int oz = (Viewer_object->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
1051 int ox = (Viewer_object->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
1052 if (oz < 0 || oz >= OCCLUSION_SIZE || ox < 0 || ox >= OCCLUSION_SIZE)
1053 use_occlusion = 0;
1054 src_occlusion_index = oz * OCCLUSION_SIZE + ox;
1055 }
1056 for (i = 0; i <= Highest_object_index; i++) {
1057 obj = &Objects[i];
1058 if (obj->type != OBJ_ROOM)
1059 continue;
1060
1061 if (obj->flags & OF_DEAD)
1062 continue;
1063 if (obj->render_type == RT_NONE)
1064 continue;
1065 if (!OBJECT_OUTSIDE(obj))
1066 continue;
1067 float size = obj->size;
1068
1069 if (use_occlusion) {
1070 int y1 = (obj->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
1071 int x1 = (obj->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
1072 int dest_occlusion_index = (y1 * OCCLUSION_SIZE);
1073 dest_occlusion_index += x1;
1074 int occ_byte = dest_occlusion_index / 8;
1075 int occ_bit = dest_occlusion_index % 8;
1076 if (obj->pos.y < MAX_TERRAIN_HEIGHT) {
1077 if (!(Terrain_occlusion_map[src_occlusion_index][occ_byte] & (1 << occ_bit)))
1078 continue;
1079 }
1080 }
1081 if (!ExternalRoomVisible(&Rooms[obj->id], &obj->pos, &zdist))
1082 continue;
1083 if (!IsPointVisible(&obj->pos, size, &zdist))
1084 continue;
1085
1086 if (Check_terrain_portal && ObjectOutOfPortal(obj))
1087 continue;
1088 /*if (!Terrain_from_mine)
1089 {
1090 if (!ShootRaysToObject (obj))
1091 continue;
1092 }*/
1093 rooms_to_render[room_count].vis_effect = 0;
1094 rooms_to_render[room_count].objnum = obj - Objects;
1095 rooms_to_render[room_count].dist = zdist;
1096 room_count++;
1097 }
1098 // Sort and draw rooms
1099 qsort(rooms_to_render, room_count, sizeof(*rooms_to_render), (int (*)(const void *, const void *))obj_sort_func);
1100 for (i = room_count - 1; i >= 0; i--) {
1101 int objnum = rooms_to_render[i].objnum;
1102 object *obj = &Objects[objnum];
1103 #ifndef NEWEDITOR
1104 RenderMine(obj->id, 0, 1);
1105 #else
1106 RenderMine(obj->id, 0, 1, 1, 0, 0, NULL);
1107 #endif
1108 // Draw a surrounding sphere
1109 #if (defined(_DEBUG) && !defined(NEWEDITOR))
1110 if (Game_show_sphere == 2)
1111 DrawDebugInfo(obj);
1112 #endif
1113 }
1114 }
1115 // Renders every visible terrain object
1116 void RenderAllTerrainObjects() {
1117 object *obj;
1118 int snows[500];
1119 int num_snows = 0;
1120 int obj_count = 0;
1121 float zdist;
1122 int use_occlusion = 0;
1123 int src_occlusion_index;
1124 int i;
1125 if ((Terrain_checksum + 1) == Terrain_occlusion_checksum) {
1126 use_occlusion = 1;
1127 int oz = (Viewer_object->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
1128 int ox = (Viewer_object->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
1129 if (oz < 0 || oz >= OCCLUSION_SIZE || ox < 0 || ox >= OCCLUSION_SIZE)
1130 use_occlusion = 0;
1131 src_occlusion_index = oz * OCCLUSION_SIZE + ox;
1132 }
1133 for (i = 0; i <= Highest_object_index; i++) {
1134 obj = &Objects[i];
1135 if (obj == Viewer_object)
1136 continue;
1137 // Don't draw piggybacked objects
1138 if (Viewer_object->type == OBJ_OBSERVER && i == Players[Viewer_object->id].piggy_objnum)
1139 continue;
1140 if (obj->type == OBJ_ROOM)
1141 continue;
1142
1143 if (obj->type == OBJ_NONE)
1144 continue;
1145 if (obj->flags & OF_DEAD)
1146 continue;
1147 if (obj->render_type == RT_NONE)
1148 continue;
1149 if (!OBJECT_OUTSIDE(obj))
1150 continue;
1151 float size = obj->size;
1152
1153 // Special case weapons with streamers
1154 if (obj->type == OBJ_WEAPON && (Weapons[obj->id].flags & WF_STREAMER))
1155 size = Weapons[obj->id].phys_info.velocity.z;
1156 if (use_occlusion) {
1157 int y1 = (obj->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
1158 int x1 = (obj->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
1159 int dest_occlusion_index = (y1 * OCCLUSION_SIZE);
1160 dest_occlusion_index += x1;
1161 int occ_byte = dest_occlusion_index / 8;
1162 int occ_bit = dest_occlusion_index % 8;
1163 if (obj->pos.y + obj->size < MAX_TERRAIN_HEIGHT) {
1164 if (!(Terrain_occlusion_map[src_occlusion_index][occ_byte] & (1 << occ_bit)))
1165 continue;
1166 }
1167 }
1168 if (obj->type == OBJ_WEAPON && Weapons[obj->id].flags & WF_ELECTRICAL) {
1169 // Automatically render all electrical objects
1170 zdist = 0;
1171 } else {
1172 if (!IsPointVisible(&obj->pos, size, &zdist))
1173 continue;
1174
1175 if (Check_terrain_portal && ObjectOutOfPortal(obj))
1176 continue;
1177 }
1178
1179 if (UseHardware) {
1180 if (Num_postrenders < MAX_POSTRENDERS) {
1181 Postrender_list[Num_postrenders].type = PRT_OBJECT;
1182 Postrender_list[Num_postrenders].z = zdist;
1183 Postrender_list[Num_postrenders++].objnum = obj - Objects;
1184 }
1185 } else {
1186 objs_to_render[obj_count].vis_effect = 0;
1187 objs_to_render[obj_count].objnum = obj - Objects;
1188 objs_to_render[obj_count].dist = zdist;
1189 obj_count++;
1190 }
1191 }
1192 #ifndef NEWEDITOR
1193 for (i = 0; i <= Highest_vis_effect_index; i++) {
1194 vis_effect *vis = &VisEffects[i];
1195
1196 if (vis->type == VIS_NONE)
1197 continue;
1198 if (vis->flags & VF_DEAD)
1199 continue;
1200 if (!ROOMNUM_OUTSIDE(vis->roomnum))
1201 continue;
1202 // Special case snow
1203 if (vis->id == SNOWFLAKE_INDEX) {
1204 snows[num_snows] = vis - VisEffects;
1205 num_snows++;
1206 } else {
1207 if ((vis->flags & VF_WINDSHIELD_EFFECT) || IsPointVisible(&vis->pos, vis->size, &zdist)) {
1208 if (vis->flags & VF_WINDSHIELD_EFFECT)
1209 zdist = 0;
1210 if (UseHardware) {
1211 if (Num_postrenders < MAX_POSTRENDERS) {
1212 Postrender_list[Num_postrenders].type = PRT_VISEFFECT;
1213 Postrender_list[Num_postrenders].z = zdist;
1214 Postrender_list[Num_postrenders++].objnum = vis - VisEffects;
1215 }
1216 } else {
1217 objs_to_render[obj_count].vis_effect = 1;
1218 objs_to_render[obj_count].objnum = vis - VisEffects;
1219 objs_to_render[obj_count].dist = zdist;
1220 obj_count++;
1221 }
1222 }
1223 }
1224 }
1225 #endif
1226
1227 // Sort objects
1228 qsort(objs_to_render, obj_count, sizeof(*objs_to_render), (int (*)(const void *, const void *))obj_sort_func);
1229 // Render the objects
1230 for (i = obj_count - 1; i >= 0; i--) {
1231 int vis_effect = objs_to_render[i].vis_effect;
1232 int objnum = objs_to_render[i].objnum;
1233 if (vis_effect)
1234 DrawVisEffect(&VisEffects[objnum]);
1235 else
1236 RenderObject(&Objects[objnum]);
1237 }
1238 // Render snows
1239 rend_SetZBufferWriteMask(0);
1240 for (i = 0; i < num_snows; i++)
1241 DrawVisEffect(&VisEffects[snows[i]]);
1242 rend_SetZBufferWriteMask(1);
1243 rend_SetZBufferState(1);
1244 rend_SetWrapType(WT_WRAP);
1245 }
1246 #define FOG_LAYER_HEIGHT (TERRAIN_HEIGHT_INCREMENT * 30.5f)
1247 // Draws a flat fog layer
1248 void DrawFogLayer() {
1249 vector worldvec[4];
1250 g3Point pnt[4], *pntlist[6];
1251 rend_SetFlatColor(GR_RGB(132, 132, 255));
1252 rend_SetAlphaValue(64);
1253 rend_SetAlphaType(AT_CONSTANT);
1254 rend_SetTextureType(TT_FLAT);
1255 rend_SetLighting(LS_NONE);
1256 worldvec[0].x = 0;
1257 worldvec[0].y = FOG_LAYER_HEIGHT;
1258 worldvec[0].z = 0;
1259 worldvec[1].x = 0;
1260 worldvec[1].y = FOG_LAYER_HEIGHT;
1261 worldvec[1].z = TERRAIN_DEPTH * TERRAIN_SIZE;
1262 worldvec[2].x = TERRAIN_WIDTH * TERRAIN_SIZE;
1263 worldvec[2].y = FOG_LAYER_HEIGHT;
1264 worldvec[2].z = TERRAIN_DEPTH * TERRAIN_SIZE;
1265 worldvec[3].x = TERRAIN_WIDTH * TERRAIN_SIZE;
1266 worldvec[3].y = FOG_LAYER_HEIGHT;
1267 worldvec[3].z = 0;
1268 for (int i = 0; i < 4; i++) {
1269 g3_RotatePoint(&pnt[i], &worldvec[i]);
1270 pntlist[i] = &pnt[i];
1271 }
1272 g3_DrawPoly(4, pntlist, 0);
1273 }
1274 #define CLOUD_LAYER_HEIGHT (TERRAIN_HEIGHT_INCREMENT * 320.0f)
1275 // Draws a flat fog layer
1276 void DrawCloudLayer() {
1277 vector worldvec[4];
1278 g3Point pnt[4], *pntlist[6];
1279 rend_SetFlatColor(GR_RGB(192, 192, 255));
1280 rend_SetAlphaValue(32);
1281 rend_SetAlphaType(AT_CONSTANT);
1282 rend_SetTextureType(TT_FLAT);
1283 rend_SetLighting(LS_NONE);
1284 worldvec[0].x = -(TERRAIN_SIZE * 100);
1285 worldvec[0].y = CLOUD_LAYER_HEIGHT;
1286 worldvec[0].z = (TERRAIN_SIZE * (100 + TERRAIN_DEPTH));
1287
1288 worldvec[1].x = -(TERRAIN_SIZE * 100);
1289 worldvec[1].y = CLOUD_LAYER_HEIGHT;
1290 worldvec[1].z = -(TERRAIN_SIZE * (100));
1291
1292 worldvec[2].x = (TERRAIN_SIZE * (100 + TERRAIN_WIDTH));
1293 worldvec[2].y = CLOUD_LAYER_HEIGHT;
1294 worldvec[2].z = -(TERRAIN_SIZE * (100));
1295
1296 worldvec[3].x = (TERRAIN_SIZE * (100 + TERRAIN_WIDTH));
1297 worldvec[3].y = CLOUD_LAYER_HEIGHT;
1298 worldvec[3].z = (TERRAIN_SIZE * (100 + TERRAIN_DEPTH));
1299 for (int i = 0; i < 4; i++) {
1300 g3_RotatePoint(&pnt[i], &worldvec[i]);
1301 pntlist[i] = &pnt[i];
1302 }
1303 g3_DrawPoly(4, pntlist, 0);
1304 }
1305 // left,top,right,bot are optional parameters. Omiting them (or setting them to -1) will
1306 // render to the whole screen. Passing valid values will only render tiles visible in the
1307 // specified window (though it won't clip those tiles to the window)
1308 void RenderTerrain(uint8_t from_mine, int left, int top, int right, int bot) {
1309 static int first = 1;
1310 if (first) {
1311 InitTerrainRenderSpeedups();
1312 first = 0;
1313 }
1314
1315 // Get the size of the current render window
1316 int render_width, render_height;
1317 rend_GetProjectionParameters(&render_width, &render_height);
1318 float w2 = ((float)render_width - 1) / 2.0f;
1319 float h2 = ((float)render_height - 1) / 2.0f;
1320
1321 // Set up vars for (psuedo-)clipping window
1322 if (left < 0) {
1323 Check_terrain_portal = 0;
1324 left = 0;
1325 } else {
1326 int w = right - left;
1327 int h = bot - top;
1328
1329 // If the portal takes up more than 50% the screen space then we don't check against it
1330 float threshold = (render_width * render_height) * 0.5f;
1331 if (w * h > threshold) {
1332 Check_terrain_portal = 0;
1333 } else {
1334 Check_terrain_portal = 1;
1335 }
1336 }
1337
1338 if (top < 0) {
1339 top = 0;
1340 }
1341 if ((right == -1) || right > render_width)
1342 right = render_width - 1;
1343 if ((bot == -1) || bot > render_height)
1344 bot = render_height - 1;
1345 Clip_scale_left = left;
1346 Clip_scale_right = right;
1347 Clip_scale_top = top;
1348 Clip_scale_bot = bot;
1349 if (!Terrain_sky.textured) {
1350 rend_FillRect(Terrain_sky.sky_color, left, top, right + 1, bot + 1);
1351 }
1352
1353 rend_SetFlatColor(Terrain_sky.sky_color);
1354 View_mode = GetFunctionMode();
1355
1356 // Set this so we don't do reentrant rendering between terrain/mine
1357 Terrain_from_mine = from_mine;
1358
1359 #ifndef NEWEDITOR
1360 // See if we're supposed change the fog plane distance based on our framerate
1361 if (View_mode != EDITOR_MODE && ManageFramerate) {
1362 float fps = GetFPS();
1363 if (fps < MinAllowableFramerate) {
1364 if (Detail_settings.Terrain_render_distance > TERRAIN_SIZE) {
1365 Detail_settings.Terrain_render_distance -= (float)(TERRAIN_SIZE / 4);
1366 }
1367 } else if (fps > MinAllowableFramerate + 1) {
1368 if (Detail_settings.Terrain_render_distance < 60 * TERRAIN_SIZE) {
1369 Detail_settings.Terrain_render_distance += (float)(TERRAIN_SIZE / 4);
1370 }
1371 }
1372 }
1373 #endif
1374
1375 #ifndef NEWEDITOR
1376 const float kTerrainRenderDistance = Detail_settings.Terrain_render_distance;
1377 #else
1378 const float kTerrainRenderDistance = 1200.0f;
1379 #endif
1380 VisibleTerrainZ = kTerrainRenderDistance * Matrix_scale.z;
1381 Far_fog_border = VisibleTerrainZ;
1382
1383 // Set up our z wall
1384 g3_SetFarClipZ(VisibleTerrainZ);
1385
1386 // Get the viewer position & orientation
1387 vector viewer_eye;
1388 matrix viewer_orient;
1389 g3_GetViewPosition(&viewer_eye);
1390 g3_GetViewMatrix(&viewer_orient);
1391
1392 // Get all of the cells visible to us
1393 int nt = GetVisibleTerrain(&viewer_eye, &viewer_orient);
1394
1395 // Set this to really far away so our sky can render
1396 g3_SetFarClipZ(60000);
1397 rend_SetFogState(0);
1398
1399 // Draw the sky
1400 DrawSky(&viewer_eye, &viewer_orient);
1401
1402 //// Set up our z wall
1403 rend_SetZBufferState(1);
1404 rend_SetZBufferWriteMask(1);
1405 g3_SetFarClipZ(VisibleTerrainZ);
1406
1407 #ifndef NEWEDITOR
1408 if ((Terrain_sky.flags & TF_FOG) && (UseHardware || (!UseHardware && Lighting_on))) {
1409 rend_SetZValues(0, VisibleTerrainZ);
1410 rend_SetFogState(1);
1411 rend_SetFogBorders(VisibleTerrainZ * Terrain_sky.fog_scalar, Far_fog_border);
1412 rend_SetFogColor(Terrain_sky.fog_color);
1413 } else
1414 #endif
1415 {
1416 rend_SetZValues(0, 5000);
1417 }
1418
1419 // And display!
1420 if (nt > 0) {
1421 DisplayTerrainList(nt);
1422 }
1423
1424 // Draw rooms
1425 RenderTerrainRooms();
1426
1427 // Show objects
1428 if (nt < 1 || UseHardware) {
1429 RenderAllTerrainObjects();
1430 }
1431
1432 mprintf_at(2, 5, 0, "Objs Drawn=%5d", Terrain_objects_drawn);
1433 Last_terrain_render_time = Gametime;
1434 }
1435
1436 // Draws a segment of lightning that is always facing you
1437 // Vectors are in world coords
1438 void DrawLightningSegment(vector *from, vector *to) {
1439 vector src_vecs[2], world_vecs[6];
1440 g3Point rot_src_pnts[2], world_points[6], *pntlist[6];
1441 static float alphas[] = {0.3f, 1.0f, 0.3f, 0.3f, 1.0f, 0.3f};
1442 src_vecs[0] = *from;
1443 src_vecs[1] = *to;
1444
1445 g3_RotatePoint(&rot_src_pnts[0], &src_vecs[0]);
1446 g3_RotatePoint(&rot_src_pnts[1], &src_vecs[1]);
1447 if (rot_src_pnts[0].p3_codes & rot_src_pnts[1].p3_codes)
1448 return; // Don't draw because both points are off screen
1449 vector rvec = Viewer_object->orient.rvec * 10;
1450
1451 // Put all points so that they face the viewer
1452 world_vecs[0] = src_vecs[0] - rvec;
1453 world_vecs[1] = src_vecs[0];
1454 world_vecs[2] = src_vecs[0] + rvec;
1455 world_vecs[3] = src_vecs[1] + rvec;
1456 world_vecs[4] = src_vecs[1];
1457 world_vecs[5] = src_vecs[1] - rvec;
1458 for (int i = 0; i < 6; i++) {
1459 g3_RotatePoint(&world_points[i], &world_vecs[i]);
1460 world_points[i].p3_flags |= PF_RGBA;
1461 world_points[i].p3_r = .2f;
1462 world_points[i].p3_g = .4f;
1463 world_points[i].p3_b = 1.0f;
1464 world_points[i].p3_a = alphas[i];
1465 pntlist[i] = &world_points[i];
1466 }
1467 rend_SetTextureType(TT_FLAT);
1468 rend_SetLighting(LS_GOURAUD);
1469 rend_SetAlphaType(AT_SATURATE_VERTEX);
1470 rend_SetColorModel(CM_RGB);
1471 g3_ProjectPoint(&world_points[1]);
1472 g3_ProjectPoint(&world_points[4]);
1473 rend_DrawSpecialLine(&world_points[1], &world_points[4]);
1474 g3_DrawPoly(6, pntlist, 0);
1475 }
1476 #define PUSH_LIGHTNING_TREE(f, l, sp) \
1477 { \
1478 froms[si] = f; \
1479 stack_level[si] = l; \
1480 splits[si] = sp; \
1481 si++; \
1482 }
1483 #define POP_LIGHTNING_TREE() \
1484 { \
1485 si--; \
1486 cur_from = froms[si]; \
1487 level = stack_level[si]; \
1488 cur_splits = splits[si]; \
1489 }
1490 // Draws an entire strip of lightning
1491 void DrawLightning(void) {
1492 angvec player_angs;
1493 matrix mat;
1494 vector froms[50];
1495 int si = 0, level;
1496 int stack_level[50];
1497 int splits[50];
1498 int cur_splits = 0;
1499 int new_heading;
1500 float scalar;
1501 scalar = ((ps_rand() % 1000) - 500) / 500.0;
1502 scalar *= 15000;
1503 vm_ExtractAnglesFromMatrix(&player_angs, &Viewer_object->orient);
1504 new_heading = (player_angs.h + (int)scalar) % 65536;
1505 vm_AnglesToMatrix(&mat, 0, new_heading, 0);
1506 // Put the starting point way up in the air
1507 float ylimit = (-(Viewer_object->pos.y * 2)) + (ps_rand() % 400);
1508 vector cur_from = Viewer_object->pos + (mat.fvec * 4000);
1509 vector new_vec;
1510 cur_from.y += 800.0f;
1511 cur_from.y += (ps_rand() % 100);
1512 // Set some states
1513
1514 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1515 rend_SetAlphaValue(.5 * 255);
1516 rend_SetLighting(LS_NONE);
1517 int bm_handle;
1518
1519 // See if we should drawn an origin bitmap
1520 if (ps_rand() % 3) {
1521 // Pick an origin bitmap
1522 if (ps_rand() % 2)
1523 bm_handle = Fireballs[LIGHTNING_ORIGIN_INDEXA].bm_handle;
1524 else
1525 bm_handle = Fireballs[LIGHTNING_ORIGIN_INDEXB].bm_handle;
1526 // Draw the origin bitmap
1527 int size = 300 + (ps_rand() % 200);
1528 g3_DrawRotatedBitmap(&cur_from, 0, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
1529 }
1530 PUSH_LIGHTNING_TREE(cur_from, 0, 0)
1531 while (si > 0) {
1532 POP_LIGHTNING_TREE()
1533 ASSERT(level < 50);
1534 float x_adjust = ((ps_rand() % 200) - 100) / 100.0;
1535 float y_adjust = .3 + ((ps_rand() % 100) / 100.0);
1536
1537 new_vec = cur_from;
1538 new_vec += x_adjust * (mat.rvec * 70);
1539 new_vec -= y_adjust * (mat.uvec * 100);
1540 DrawLightningSegment(&cur_from, &new_vec);
1541 if (cur_from.y < ylimit) // We're close to the ground, so just bail!
1542 continue;
1543
1544 if ((ps_rand() % ((level * level * 20) + 8)) == 0 && cur_splits < 2) {
1545 // Make this branch split
1546 PUSH_LIGHTNING_TREE(new_vec, level, cur_splits + 1)
1547 PUSH_LIGHTNING_TREE(new_vec, level, cur_splits + 1)
1548 } else {
1549 PUSH_LIGHTNING_TREE(new_vec, level, cur_splits)
1550 }
1551 }
1552 }
1553 // Draws a lightning sky
1554 void DrawLightningSky(void) {
1555 int t, k, tw;
1556 float r, g, b;
1557
1558 g3Point pnt[4], *pntlist[6];
1559
1560 rend_SetTextureType(TT_FLAT);
1561 rend_SetColorModel(CM_RGB);
1562 rend_SetLighting(LS_GOURAUD);
1563 rend_SetAlphaType(AT_SATURATE_VERTEX);
1564
1565 // figure out colors for sky
1566 r = .8f;
1567 g = .8f;
1568 b = 1.0;
1569 // Draw top part
1570 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1571 tw = (t + 1) % MAX_HORIZON_PIECES;
1572 pnt[0].p3_vec = Temp_sky_vectors[t][0];
1573 pnt[0].p3_vecPreRot = Temp_sky_vectors_unrotated[t][0];
1574 pnt[1].p3_vec = Temp_sky_vectors[tw][1];
1575 pnt[1].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][1];
1576 pnt[2].p3_vec = Temp_sky_vectors[t][1];
1577 pnt[2].p3_vecPreRot = Temp_sky_vectors_unrotated[t][1];
1578
1579 for (k = 0; k < 3; k++) {
1580 g3Point *p = &pnt[k];
1581 p->p3_flags = PF_RGBA | PF_ORIGPOINT;
1582 g3_CodePoint(p);
1583 p->p3_a = .3f;
1584 p->p3_r = r;
1585 p->p3_g = g;
1586 p->p3_b = b;
1587 }
1588 pntlist[0] = &pnt[0];
1589 pntlist[1] = &pnt[1];
1590 pntlist[2] = &pnt[2];
1591 g3_DrawPoly(3, pntlist, 0);
1592 }
1593 // Draw bottom part
1594 for (int i = 1; i < 5; i++) {
1595 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1596 tw = (t + 1) % MAX_HORIZON_PIECES;
1597 pnt[0].p3_vec = Temp_sky_vectors[t][i];
1598 pnt[0].p3_vecPreRot = Temp_sky_vectors_unrotated[t][i];
1599 pnt[1].p3_vec = Temp_sky_vectors[tw][i];
1600 pnt[1].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][i];
1601 pnt[2].p3_vec = Temp_sky_vectors[tw][i + 1];
1602 pnt[2].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][i + 1];
1603 pnt[3].p3_vec = Temp_sky_vectors[t][i + 1];
1604 pnt[3].p3_vecPreRot = Temp_sky_vectors_unrotated[t][i + 1];
1605
1606 for (k = 0; k < 4; k++) {
1607 g3Point *p = &pnt[k];
1608 p->p3_flags = PF_RGBA | PF_ORIGPOINT;
1609 g3_CodePoint(p);
1610 p->p3_a = .3f;
1611 p->p3_r = r;
1612 p->p3_g = g;
1613 p->p3_b = b;
1614 pntlist[k] = p;
1615 }
1616 g3_DrawPoly(4, pntlist, 0);
1617 }
1618 }
1619 }
1620 // Draws the gouraud sky
1621 void DrawGouraudSky(void) {
1622 int t, k, tw;
1623 float sr, sg, sb, hr, hg, hb;
1624
1625 g3Point pnt[4], *pntlist[6];
1626 g3UVL uvls[10];
1627 if (Terrain_sky.sky_color == Terrain_sky.horizon_color)
1628 return; // No sense in drawing anything
1629 rend_SetTextureType(TT_FLAT);
1630 rend_SetColorModel(CM_RGB);
1631 rend_SetAlphaType(AT_ALWAYS);
1632 // figure out colors for sky
1633 {
1634 sr = (float)GR_COLOR_RED(Terrain_sky.sky_color) / 255.0;
1635 sg = (float)GR_COLOR_GREEN(Terrain_sky.sky_color) / 255.0;
1636 sb = (float)GR_COLOR_BLUE(Terrain_sky.sky_color) / 255.0;
1637 hr = (float)GR_COLOR_RED(Terrain_sky.horizon_color) / 255.0;
1638 hg = (float)GR_COLOR_GREEN(Terrain_sky.horizon_color) / 255.0;
1639 hb = (float)GR_COLOR_BLUE(Terrain_sky.horizon_color) / 255.0;
1640 }
1641 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1642 tw = (t + 1) % MAX_HORIZON_PIECES;
1643 pnt[0].p3_vec = Temp_sky_vectors[t][4];
1644 pnt[0].p3_vecPreRot = Temp_sky_vectors_unrotated[t][4];
1645 uvls[0].r = sr;
1646 uvls[0].g = sg;
1647 uvls[0].b = sb;
1648
1649 pnt[1].p3_vec = Temp_sky_vectors[tw][4];
1650 pnt[1].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][4];
1651 uvls[1].r = sr;
1652 uvls[1].g = sg;
1653 uvls[1].b = sb;
1654
1655 pnt[2].p3_vec = Temp_sky_vectors[tw][5];
1656 pnt[2].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][5];
1657 uvls[2].r = hr;
1658 uvls[2].g = hg;
1659 uvls[2].b = hb;
1660
1661 pnt[3].p3_vec = Temp_sky_vectors[t][5];
1662 pnt[3].p3_vecPreRot = Temp_sky_vectors_unrotated[t][5];
1663 uvls[3].r = hr;
1664 uvls[3].g = hg;
1665 uvls[3].b = hb;
1666
1667 for (k = 0; k < 4; k++) {
1668 g3Point *p = &pnt[k];
1669 p->p3_flags = PF_RGBA | PF_ORIGPOINT;
1670 g3_CodePoint(p);
1671 pntlist[k] = p;
1672 p->p3_uvl = uvls[k];
1673 }
1674 g3_DrawPoly(4, pntlist, 0);
1675 }
1676 }
1677 // Draws the sky textures
1678 void DrawTexturedSky(void) {
1679 int t, k, tw;
1680 float sr, sg, sb, hr, hg, hb;
1681
1682 // Change terrain sky if needed
1683 int dome_bm = GetTextureBitmap(Terrain_sky.dome_texture, 0);
1684
1685 g3Point pnt[6], *pntlist[6];
1686 g3UVL uvls[10];
1687
1688 rend_SetWrapType(WT_WRAP);
1689 rend_SetTextureType(TT_PERSPECTIVE);
1690 rend_SetColorModel(CM_MONO);
1691 rend_SetAlphaType(ATF_TEXTURE);
1692 g3_SetTriangulationTest(1);
1693
1694 // Draw top part
1695 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1696 tw = (t + 1) % MAX_HORIZON_PIECES;
1697 pnt[0].p3_vec = Temp_sky_vectors[t][0];
1698 pnt[0].p3_vecPreRot = Temp_sky_vectors_unrotated[t][0];
1699 uvls[0].u = Terrain_sky.horizon_u[t][0];
1700 uvls[0].v = Terrain_sky.horizon_v[t][0];
1701
1702 pnt[1].p3_vec = Temp_sky_vectors[tw][1];
1703 pnt[1].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][1];
1704 uvls[1].u = Terrain_sky.horizon_u[tw][1];
1705 uvls[1].v = Terrain_sky.horizon_v[tw][1];
1706
1707 pnt[2].p3_vec = Temp_sky_vectors[t][1];
1708 pnt[2].p3_vecPreRot = Temp_sky_vectors_unrotated[t][1];
1709 uvls[2].u = Terrain_sky.horizon_u[t][1];
1710 uvls[2].v = Terrain_sky.horizon_v[t][1];
1711
1712 for (k = 0; k < 3; k++) {
1713 g3Point *p = &pnt[k];
1714 p->p3_flags = PF_UV | PF_L | PF_ORIGPOINT;
1715 g3_CodePoint(p);
1716 p->p3_uvl = uvls[k];
1717 p->p3_l = 1;
1718 }
1719
1720 #if (defined(EDITOR) || defined(NEWEDITOR))
1721 ddgr_color oldcolor;
1722 if (TSearch_on) {
1723 rend_SetPixel(GR_RGB(0, 255, 0), TSearch_x, TSearch_y);
1724 oldcolor = rend_GetPixel(TSearch_x, TSearch_y); // will be different in 15/16-bit color
1725 }
1726 #endif
1727
1728 pntlist[0] = &pnt[0];
1729 pntlist[1] = &pnt[1];
1730 pntlist[2] = &pnt[2];
1731 g3_DrawPoly(3, pntlist, dome_bm);
1732
1733 #if (defined(EDITOR) || defined(NEWEDITOR))
1734 if (TSearch_on) {
1735 ddgr_color newcolor = rend_GetPixel(TSearch_x, TSearch_y);
1736 if (newcolor != oldcolor) {
1737 TSearch_seg = t;
1738 TSearch_found_type = TSEARCH_FOUND_SKY_DOME;
1739 }
1740 }
1741 #endif
1742 }
1743
1744 // Draw bottom part
1745 for (int i = 1; i < 4; i++) {
1746 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1747 tw = (t + 1) % MAX_HORIZON_PIECES;
1748 pnt[0].p3_vec = Temp_sky_vectors[t][i];
1749 pnt[0].p3_vecPreRot = Temp_sky_vectors_unrotated[t][i];
1750 uvls[0].u = Terrain_sky.horizon_u[t][i];
1751 uvls[0].v = Terrain_sky.horizon_v[t][i];
1752
1753 pnt[1].p3_vec = Temp_sky_vectors[tw][i];
1754 pnt[1].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][i];
1755 uvls[1].u = Terrain_sky.horizon_u[tw][i];
1756 uvls[1].v = Terrain_sky.horizon_v[tw][i];
1757
1758 pnt[2].p3_vec = Temp_sky_vectors[tw][i + 1];
1759 pnt[2].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][i + 1];
1760 uvls[2].u = Terrain_sky.horizon_u[tw][i + 1];
1761 uvls[2].v = Terrain_sky.horizon_v[tw][i + 1];
1762
1763 pnt[3].p3_vec = Temp_sky_vectors[t][i + 1];
1764 pnt[3].p3_vecPreRot = Temp_sky_vectors_unrotated[t][i + 1];
1765 uvls[3].u = Terrain_sky.horizon_u[t][i + 1];
1766 uvls[3].v = Terrain_sky.horizon_v[t][i + 1];
1767
1768 for (k = 0; k < 4; k++) {
1769 g3Point *p = &pnt[k];
1770 p->p3_flags = PF_UV | PF_L | PF_ORIGPOINT;
1771 g3_CodePoint(p);
1772 pntlist[k] = p;
1773 p->p3_uvl = uvls[k];
1774 p->p3_l = 1;
1775 }
1776 #if (defined(EDITOR) || defined(NEWEDITOR))
1777 ddgr_color oldcolor;
1778 if (TSearch_on) {
1779 rend_SetPixel(GR_RGB(0, 255, 0), TSearch_x, TSearch_y);
1780 oldcolor = rend_GetPixel(TSearch_x, TSearch_y); // will be different in 15/16-bit color
1781 }
1782 #endif
1783
1784 g3_DrawPoly(4, pntlist, dome_bm);
1785 #if (defined(EDITOR) || defined(NEWEDITOR))
1786 if (TSearch_on) {
1787 ddgr_color newcolor = rend_GetPixel(TSearch_x, TSearch_y);
1788 if (newcolor != oldcolor) {
1789 TSearch_seg = t;
1790 TSearch_found_type = TSEARCH_FOUND_SKY_DOME;
1791 }
1792 }
1793 #endif
1794 }
1795 }
1796 // Now draw band
1797 rend_SetTextureType(TT_FLAT);
1798 rend_SetColorModel(CM_RGB);
1799 rend_SetAlphaType(AT_ALWAYS);
1800
1801 // figure out colors for sky
1802 {
1803 sr = (float)GR_COLOR_RED(Terrain_sky.sky_color) / 255.0;
1804 sg = (float)GR_COLOR_GREEN(Terrain_sky.sky_color) / 255.0;
1805 sb = (float)GR_COLOR_BLUE(Terrain_sky.sky_color) / 255.0;
1806 hr = (float)GR_COLOR_RED(Terrain_sky.horizon_color) / 255.0;
1807 hg = (float)GR_COLOR_GREEN(Terrain_sky.horizon_color) / 255.0;
1808 hb = (float)GR_COLOR_BLUE(Terrain_sky.horizon_color) / 255.0;
1809 }
1810
1811 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1812 tw = (t + 1) % MAX_HORIZON_PIECES;
1813 pnt[0].p3_vec = Temp_sky_vectors[t][4];
1814 pnt[0].p3_vecPreRot = Temp_sky_vectors_unrotated[t][4];
1815 uvls[0].r = sr;
1816 uvls[0].g = sg;
1817 uvls[0].b = sb;
1818
1819 pnt[1].p3_vec = Temp_sky_vectors[tw][4];
1820 pnt[1].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][4];
1821 uvls[1].r = sr;
1822 uvls[1].g = sg;
1823 uvls[1].b = sb;
1824
1825 pnt[2].p3_vec = Temp_sky_vectors[tw][5];
1826 pnt[2].p3_vecPreRot = Temp_sky_vectors_unrotated[tw][5];
1827 uvls[2].r = hr;
1828 uvls[2].g = hg;
1829 uvls[2].b = hb;
1830
1831 pnt[3].p3_vec = Temp_sky_vectors[t][5];
1832 pnt[3].p3_vecPreRot = Temp_sky_vectors_unrotated[t][5];
1833 uvls[3].r = hr;
1834 uvls[3].g = hg;
1835 uvls[3].b = hb;
1836
1837 for (k = 0; k < 4; k++) {
1838 g3Point *p = &pnt[k];
1839 p->p3_flags = PF_RGBA | PF_ORIGPOINT;
1840 g3_CodePoint(p);
1841 pntlist[k] = p;
1842 p->p3_uvl = uvls[k];
1843 }
1844 g3_DrawPoly(4, pntlist, 0);
1845 }
1846 g3_SetTriangulationTest(0);
1847 rend_SetWrapType(WT_WRAP);
1848 }
1849
1850 // Draws the sky textures
1851 void DrawWireframeSky(void) {
1852 int t, k, tw, i;
1853 g3Point pnt[6];
1854
1855 // Draw top part
1856 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1857 tw = (t + 1) % MAX_HORIZON_PIECES;
1858 pnt[0].p3_vec = Temp_sky_vectors[t][0];
1859 pnt[1].p3_vec = Temp_sky_vectors[tw][1];
1860 pnt[2].p3_vec = Temp_sky_vectors[t][1];
1861
1862 for (k = 0; k < 3; k++) {
1863 g3_CodePoint(&pnt[k]);
1864 pnt[k].p3_flags = 0;
1865 }
1866
1867 for (k = 0; k < 3; k++)
1868 g3_DrawLine(GR_RGB(255, 255, 255), &pnt[k], &pnt[(k + 1) % 3]);
1869 }
1870 // Draw bottom parts
1871 for (i = 1; i < 5; i++) {
1872 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
1873 tw = (t + 1) % MAX_HORIZON_PIECES;
1874 pnt[0].p3_vec = Temp_sky_vectors[t][i];
1875 pnt[1].p3_vec = Temp_sky_vectors[tw][i];
1876 pnt[2].p3_vec = Temp_sky_vectors[tw][i + 1];
1877 pnt[3].p3_vec = Temp_sky_vectors[t][i + 1];
1878 for (k = 0; k < 4; k++) {
1879 g3_CodePoint(&pnt[k]);
1880 pnt[k].p3_flags = 0;
1881 }
1882
1883 for (k = 0; k < 4; k++)
1884 g3_DrawLine(GR_RGB(255, 255, 255), &pnt[k], &pnt[(k + 1) % 4]);
1885 }
1886 }
1887 }
1888 // Draws the atmosphere over a satellite
1889 void DrawAtmosphereBlend(vector *pos, angle rotAngle, float w, float h, int bm, float r, float g, float b) {
1890 g3Point pnt;
1891 if (g3_RotatePoint(&pnt, pos) & CC_BEHIND)
1892 return;
1893
1894 // create the rotation matrix
1895 matrix rotMatrix;
1896 vm_AnglesToMatrix(&rotMatrix, 0, 0, rotAngle);
1897
1898 // get the view matrix
1899 matrix viewToWorld;
1900 g3_GetUnscaledMatrix(&viewToWorld);
1901 viewToWorld = ~viewToWorld;
1902
1903 // combine the matrices into one
1904 matrix rotationToWorld = rotMatrix * viewToWorld;
1905
1906 // setup the rotation vectors
1907 vector rotVectors[4];
1908 rotVectors[0].x = -w;
1909 rotVectors[0].y = h;
1910 rotVectors[1].x = w;
1911 rotVectors[1].y = h;
1912 rotVectors[2].x = w;
1913 rotVectors[2].y = -h;
1914 rotVectors[3].x = -w;
1915 rotVectors[3].y = -h;
1916
1917 // rotate the points
1918 g3Point rotPoints[8], *pntList[8];
1919 int i;
1920 for (i = 0; i < 4; ++i) {
1921 rotVectors[i].z = 0.0f;
1922 rotVectors[i] = (rotVectors[i] * rotationToWorld) + (*pos);
1923
1924 // setup the point
1925 g3_RotatePoint(&rotPoints[i], &rotVectors[i]);
1926 rotPoints[i].p3_flags |= PF_UV | PF_RGBA;
1927 rotPoints[i].p3_r = r;
1928 rotPoints[i].p3_g = g;
1929 rotPoints[i].p3_b = b;
1930 rotPoints[i].p3_a = 0.4f;
1931
1932 pntList[i] = &rotPoints[i];
1933 }
1934
1935 rotPoints[0].p3_u = 0.0f;
1936 rotPoints[0].p3_v = 0.0f;
1937 rotPoints[1].p3_u = 1.0f;
1938 rotPoints[1].p3_v = 0.0f;
1939 rotPoints[2].p3_u = 1.0f;
1940 rotPoints[2].p3_v = 1.0f;
1941 rotPoints[3].p3_u = 0.0f;
1942 rotPoints[3].p3_v = 1.0f;
1943
1944 // and draw!!
1945 rend_SetLighting(LS_NONE);
1946 rend_SetFlatColor(Terrain_sky.sky_color);
1947 rend_SetAlphaType(AT_TEXTURE_VERTEX);
1948 rend_SetTextureType(TT_FLAT);
1949 g3_DrawPoly(4, pntList, bm);
1950 }
1951
1952 // Draws our pretty stars
1953 void DrawStars(matrix *vorient) {
1954 rend_SetLighting(LS_NONE);
1955 rend_SetTextureType(TT_FLAT);
1956 rend_SetOverlayType(OT_NONE);
1957 rend_SetColorModel(CM_MONO);
1958 rend_SetAlphaType(AT_VERTEX);
1959 rend_SetZBufferState(0);
1960 g3_SetFarClipZ(6000000);
1961 vector tempvec;
1962 if (Rendering_main_view && Terrain_sky.flags & TF_ROTATE_STARS && Terrain_sky.rotate_rate > 0) {
1963 matrix mat;
1964 vm_AnglesToMatrix(&mat, 0, Terrain_sky.rotate_rate * Frametime * (65536.0 / 360.0), 0);
1965 vm_Orthogonalize(&mat);
1966 for (int i = 0; i < MAX_STARS; i++) {
1967 vm_MatrixMulVector(&tempvec, &Terrain_sky.star_vectors[i], &mat);
1968 Terrain_sky.star_vectors[i] = tempvec;
1969 }
1970 }
1971 for (int i = 0; i < MAX_STARS; i++) {
1972 g3Point starpnt, lastpnt;
1973 vector streak_vec;
1974 float mag;
1975 // Rotate star
1976 tempvec = Terrain_sky.star_vectors[i];
1977 vm_MatrixMulVector(&starpnt.p3_vec, &tempvec, vorient);
1978 starpnt.p3_flags = PF_RGBA;
1979 // Get streaking line from last frame
1980 if (Rendering_main_view) {
1981 streak_vec = Last_frame_stars[i] - starpnt.p3_vec;
1982 mag = vm_GetMagnitudeFast(&streak_vec);
1983 } else {
1984 mag = 0.0f;
1985 }
1986
1987 if (Rendering_main_view && mag > 9000.0) {
1988 streak_vec /= mag;
1989 float norm = (mag / 90000);
1990 if (norm > 1)
1991 norm = 1.0;
1992 float revnorm = 1.0 - (norm * 4);
1993 if (revnorm < 0)
1994 revnorm = 0;
1995 float color_norm = (.6 + (revnorm * .4));
1996
1997 lastpnt.p3_vec = starpnt.p3_vec + ((norm * .75 * 90000) * streak_vec);
1998 lastpnt.p3_flags = PF_RGBA;
1999 lastpnt.p3_a = 0;
2000 starpnt.p3_a = color_norm;
2001
2002 g3_CodePoint(&starpnt);
2003 g3_CodePoint(&lastpnt);
2004 rend_SetFlatColor(Terrain_sky.star_color[i]);
2005 g3_DrawSpecialLine(&starpnt, &lastpnt);
2006 } else {
2007 starpnt.p3_flags = PF_RGBA;
2008 if (!g3_CodePoint(&starpnt)) // only draw if this point is on screen
2009 {
2010 starpnt.p3_a = 1.0;
2011
2012 g3_ProjectPoint(&starpnt);
2013 lastpnt = starpnt;
2014 lastpnt.p3_sx++;
2015 rend_SetFlatColor(Terrain_sky.star_color[i]);
2016 rend_DrawSpecialLine(&starpnt, &lastpnt);
2017 // rend_SetPixel (Terrain_sky.star_color[i],starpnt.p3_sx,starpnt.p3_sy);
2018 }
2019 }
2020 if (Rendering_main_view)
2021 Last_frame_stars[i] = starpnt.p3_vec;
2022 }
2023 rend_SetZBufferState(1);
2024 g3_SetFarClipZ(60000);
2025 }
2026 // Draw the suns,moons,stars, horizon, etc
2027 void DrawSky(vector *veye, matrix *vorient) {
2028 int i, t;
2029
2030 vector tempvec;
2031
2032 rend_SetLighting(LS_GOURAUD);
2033 rend_SetZBufferState(0);
2034 rend_SetZBufferWriteMask(0);
2035
2036 // If the sky is rotating, update the horizon vectors accordingly
2037 if (Rendering_main_view && Terrain_sky.flags & TF_ROTATE_SKY && Terrain_sky.rotate_rate > 0) {
2038 matrix mat;
2039 vm_AnglesToMatrix(&mat, 0, Terrain_sky.rotate_rate * Frametime * (65536.0 / 360.0), 0);
2040 vm_Orthogonalize(&mat);
2041 for (i = 0; i < 6; i++) {
2042 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
2043 vector rot_vec;
2044 tempvec = Terrain_sky.horizon_vectors[t][i];
2045 vm_MatrixMulVector(&rot_vec, &tempvec, &mat);
2046 Terrain_sky.horizon_vectors[t][i] = rot_vec;
2047 }
2048 }
2049 }
2050
2051 for (i = 0; i < 6; i++) {
2052 for (t = 0; t < MAX_HORIZON_PIECES; t++) {
2053 tempvec = Terrain_sky.horizon_vectors[t][i];
2054 tempvec.y -= veye->y * 0.5f;
2055 vm_MatrixMulVector(&Temp_sky_vectors[t][i], &tempvec, vorient);
2056
2057 tempvec = Terrain_sky.horizon_vectors[t][i];
2058 tempvec.x += veye->x;
2059 tempvec.z += veye->z;
2060 tempvec.y += veye->y * 0.5f;
2061 Temp_sky_vectors_unrotated[t][i] = tempvec;
2062 }
2063 }
2064 float sr = (float)GR_COLOR_RED(Terrain_sky.sky_color) / 255.0;
2065 float sg = (float)GR_COLOR_GREEN(Terrain_sky.sky_color) / 255.0;
2066 float sb = (float)GR_COLOR_BLUE(Terrain_sky.sky_color) / 255.0;
2067 if (!Terrain_sky.textured) {
2068 DrawGouraudSky();
2069 } else {
2070 DrawTexturedSky();
2071 }
2072
2073 if (Terrain_sky.flags & TF_STARS) {
2074 DrawStars(vorient);
2075 }
2076
2077 if (Terrain_sky.flags & TF_SATELLITES) {
2078 rend_SetWrapType(WT_CLAMP);
2079 rend_SetColorModel(CM_MONO);
2080
2081 // do satellites
2082 for (i = 0; i < Terrain_sky.num_satellites; i++) {
2083 int bm_handle = GetTextureBitmap(Terrain_sky.satellite_texture[i], 0);
2084 vector subvec = Terrain_sky.satellite_vectors[i] - *veye;
2085 float size = Terrain_sky.satellite_size[i];
2086
2087 // Get position, angle of satellite
2088 vm_NormalizeVector(&subvec);
2089 tempvec = *veye + (subvec * (Terrain_sky.radius * 3));
2090
2091 #ifndef NEWEDITOR
2092 texture *tex = &GameTextures[Terrain_sky.satellite_texture[i]];
2093 #else
2094 ned_texture_info *tex = &GameTextures[Terrain_sky.satellite_texture[i]];
2095 #endif
2096 float str = Terrain_sky.satellite_r[i];
2097 float stg = Terrain_sky.satellite_g[i];
2098 float stb = Terrain_sky.satellite_b[i];
2099 float maxc = std::max({str, stg, stb});
2100 float r, g, b;
2101 if (maxc > 1.0) {
2102 r = str / maxc;
2103 g = stg / maxc;
2104 b = stb / maxc;
2105 } else {
2106 r = str;
2107 g = stg;
2108 b = stb;
2109 }
2110 #ifndef NEWEDITOR
2111 // Draw halo
2112 if (Terrain_sky.satellite_flags[i] & TSF_HALO) {
2113 rend_SetZBufferWriteMask(0);
2114 DrawColoredRing(&tempvec, r, g, b, .4f, 0, size * 1.2, .3f, 0, 0);
2115 rend_SetZBufferWriteMask(1);
2116 }
2117 #endif
2118 // Draw satellite
2119 if (tex->flags & TF_SATURATE)
2120 rend_SetAlphaType(AT_SATURATE_TEXTURE);
2121 else
2122 rend_SetAlphaType(AT_CONSTANT + AT_TEXTURE);
2123 rend_SetLighting(LS_NONE);
2124 rend_SetAlphaValue(tex->alpha * 255);
2125 // Check to see if the user clicked on a satellite
2126 #if (defined(EDITOR) || defined(NEWEDITOR))
2127 ddgr_color oldcolor;
2128 if (TSearch_on) {
2129 rend_SetPixel(GR_RGB(0, 255, 0), TSearch_x, TSearch_y);
2130 oldcolor = rend_GetPixel(TSearch_x, TSearch_y); // will be different in 15/16-bit color
2131 }
2132 #endif
2133
2134 g3_SetTriangulationTest(1);
2135 g3_DrawPlanarRotatedBitmap(&tempvec, &subvec, 0, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0),
2136 bm_handle);
2137 g3_SetTriangulationTest(0);
2138
2139 // Draw atmosphere blend
2140 if (UseHardware) {
2141 if (Terrain_sky.satellite_flags[i] & TSF_ATMOSPHERE) {
2142 angvec angs;
2143 vm_ExtractAnglesFromMatrix(&angs, vorient);
2144 DrawAtmosphereBlend(&tempvec, angs.b, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle, sr,
2145 sg, sb);
2146 }
2147 }
2148
2149 #if (defined(EDITOR) || defined(NEWEDITOR))
2150 if (TSearch_on) {
2151 if (rend_GetPixel(TSearch_x, TSearch_y) != oldcolor) {
2152 TSearch_seg = i;
2153 TSearch_found_type = TSEARCH_FOUND_SATELLITE;
2154 }
2155 }
2156 #endif
2157 }
2158 }
2159 #ifndef NEWEDITOR
2160 if ((Weather.flags & WEATHER_FLAGS_LIGHTNING) && Weather.lightning_sequence == 2) {
2161 DrawLightning();
2162 Weather.lightning_sequence = 0;
2163 }
2164
2165 if ((Weather.flags & WEATHER_FLAGS_LIGHTNING) && Weather.lightning_sequence == 1) {
2166 DrawLightningSky();
2167 Weather.lightning_sequence = 2;
2168 }
2169 #endif
2170 // DrawCloudLayer();
2171 #if (!defined(RELEASE) || defined(NEWEDITOR))
2172 if (OUTLINE_ON(OM_SKY))
2173 DrawWireframeSky();
2174 #endif
2175
2176 rend_SetAlphaValue(255);
2177 rend_SetZBufferState(1);
2178 }
2179
2180 int AddRenderObjectToTerrainSeg(int n, int objnum);
2181 // Checks to see if we should render objects and also sets which order the objects
2182 // should be rendered in
2183 void SortTerrainObjectsForRendering(int cellcount) {
2184 int i, segnum;
2185 object *obj;
2186 #if (!defined(RELEASE) || defined(NEWEDITOR))
2187 for (i = 0; i < cellcount; i++)
2188 Terrain_seg_render_objs[Terrain_list[i].segment] = -1;
2189 #endif
2190
2191 // Go through each object and do trivial rejection
2192 for (i = 0; i <= Highest_object_index; i++) {
2193 #if (!defined(RELEASE) || defined(NEWEDITOR))
2194 render_next[i] = -1;
2195 #endif
2196 obj = &Objects[i];
2197 if (obj->type == OBJ_NONE)
2198 continue;
2199 if (obj->render_type == RT_NONE)
2200 continue;
2201 if (!OBJECT_OUTSIDE(obj))
2202 continue;
2203 // Ok, we know that we can see this point. If its segment is in our list
2204 // then make this object draw right after this segment is drawn. Otherwise
2205 // just draw it
2206 segnum = CELLNUM(obj->roomnum);
2207 ASSERT(segnum >= 0 && segnum <= (TERRAIN_WIDTH * TERRAIN_DEPTH));
2208 if (Terrain_rotate_list[segnum] == TS_FrameCount) {
2209 AddRenderObjectToTerrainSeg(segnum, i);
2210 } else
2211 AddRenderObjectToTerrainSeg(Terrain_list[cellcount - 1].segment, i);
2212 }
2213 }
2214 #if (defined(EDITOR) || defined(NEWEDITOR))
2215 #define CROSS_WIDTH 8.0
2216 #define CROSS_HEIGHT 8.0
2217 void TerrainDrawCurrentVert(int tcell) {
2218 if (TerrainSelected[tcell]) {
2219 g3Point pnt;
2220 pnt.p3_flags = 0;
2221 pnt.p3_vec = World_point_buffer[tcell].p3_vec;
2222 g3_ProjectPoint(&pnt); // make sure projected
2223
2224 rend_SetFlatColor(GR_RGB(0, 250, 0));
2225 rend_DrawLine(pnt.p3_sx - CROSS_WIDTH, pnt.p3_sy, pnt.p3_sx, pnt.p3_sy - CROSS_HEIGHT);
2226 rend_DrawLine(pnt.p3_sx, pnt.p3_sy - CROSS_HEIGHT, pnt.p3_sx + CROSS_WIDTH, pnt.p3_sy);
2227 rend_DrawLine(pnt.p3_sx + CROSS_WIDTH, pnt.p3_sy, pnt.p3_sx, pnt.p3_sy + CROSS_HEIGHT);
2228 rend_DrawLine(pnt.p3_sx, pnt.p3_sy + CROSS_HEIGHT, pnt.p3_sx - CROSS_WIDTH, pnt.p3_sy);
2229 }
2230 }
2231 #endif
2232 #if (!defined(RELEASE) || defined(NEWEDITOR))
2233 __inline void DrawTerrainOutline(int tcell, int nverts, g3Point **pointlist)
2234 {
2235 int i;
2236 g3Point tpnt[256];
2237 g3Point *tpnt_list[256];
2238 for (i = 0; i < nverts; i++) {
2239 tpnt[i] = *pointlist[i];
2240 tpnt_list[i] = &tpnt[i];
2241 }
2242 #if (defined(EDITOR) || defined(NEWEDITOR))
2243 if (TerrainSelected[tcell]) {
2244 for (i = 0; i < nverts - 1; i++)
2245 g3_DrawLine(GR_RGB(255, 255, 255), tpnt_list[i], tpnt_list[i + 1]);
2246 g3_DrawLine(GR_RGB(255, 255, 255), tpnt_list[i], tpnt_list[0]);
2247 } else
2248 #endif
2249 {
2250 for (i = 0; i < nverts - 1; i++)
2251 g3_DrawLine(GR_RGB(128, 128, 128), tpnt_list[i], tpnt_list[i + 1]);
2252 g3_DrawLine(GR_RGB(128, 128, 128), tpnt_list[i], tpnt_list[0]);
2253 }
2254 }
2255 #endif
2256 int TerrainSortingFunction(const terrain_render_info *a, terrain_render_info *b) { return b->z - a->z; }
2257 void SortTerrainList(int cellcount) {
2258 int i, t, k;
2259 int lod, simplemul;
2260 int n[4];
2261
2262 for (i = 0; i < cellcount; i++) {
2263 t = Terrain_list[i].segment;
2264 lod = Terrain_list[i].lod;
2265 #if (!defined(RELEASE) || defined(NEWEDITOR))
2266 Terrain_seg_render_objs[t] = -1;
2267 #endif
2268
2269 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
2270 Terrain_list[i].z = 0.0f;
2271 n[0] = t;
2272 n[1] = t + (TERRAIN_WIDTH * simplemul);
2273 n[2] = t + (TERRAIN_WIDTH * simplemul) + simplemul;
2274 n[3] = t + simplemul;
2275 for (k = 0; k < 4; k++)
2276 if (n[k] <= 65535)
2277 Terrain_list[i].z += World_point_buffer[n[k]].p3_vec.z;
2278 Terrain_list[i].z /= 4;
2279 }
2280 // Sort the faces
2281 qsort(Terrain_list, cellcount, sizeof(*Terrain_list), (int (*)(const void *, const void *))TerrainSortingFunction);
2282 }
2283 // Returns the number of points to rotate, plus the actual numbers of the points
2284 // are returned in the "n" array
2285 int BuildEdgeLists(int *n, int tlist_index) {
2286 int lod, simplemul;
2287 int t, k;
2288 int smul_x, smul_z; // for tracing the very edge of the terrain
2289
2290 // Now match up all edges of the differing levels of detail
2291 int cx, cz;
2292 int transcount;
2293 int offset;
2294 int maplod;
2295 int start = 0;
2296 int seg;
2297 int answer;
2298 int edgecount = 0;
2299 int edge1, edge2;
2300 float delta, cury;
2301 int max_edge_size = 1 << (MAX_TERRAIN_LOD - 1);
2302
2303 t = Terrain_list[tlist_index].segment;
2304 lod = Terrain_list[tlist_index].lod;
2305 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
2306 ASSERT(simplemul <= max_edge_size);
2307 cx = t % TERRAIN_WIDTH;
2308 cz = t / TERRAIN_WIDTH;
2309 if (cx == TERRAIN_WIDTH - simplemul) {
2310 if (lod == MAX_TERRAIN_LOD - 1)
2311 return 0;
2312
2313 smul_x = simplemul - 1;
2314 } else
2315 smul_x = simplemul;
2316 if (cz == TERRAIN_DEPTH - simplemul) {
2317 if (lod == MAX_TERRAIN_LOD - 1)
2318 return 0;
2319
2320 smul_z = simplemul - 1;
2321 } else
2322 smul_z = simplemul;
2323 if (lod != MAX_TERRAIN_LOD - 1) {
2324 // Bottom edge
2325 // | |
2326 // 2-------1
2327
2328 Terrain_list[tlist_index].bottom_edge = 0;
2329 Terrain_list[tlist_index].bottom_count = 0;
2330 transcount = 0;
2331 if (cz != 0) {
2332 edge1 = t + smul_x;
2333 edge2 = t;
2334 delta = (Terrain_seg[edge2].mody - Terrain_seg[edge1].mody) / smul_x;
2335 cury = Terrain_seg[edge1].mody;
2336
2337 offset = ((cz - 1) * TERRAIN_WIDTH) + cx + smul_x;
2338 for (k = 0; k < smul_x; k++, cury += delta) {
2339 maplod = TerrainJoinMap[offset - k];
2340 answer = TerrainEdgeTest[maplod][k + simplemul - smul_x];
2341 if (answer || k == 0) {
2342 seg = t + smul_x - k;
2343 n[transcount + start] = seg;
2344 Terrain_seg[seg].mody = cury;
2345
2346 Terrain_list[tlist_index].bottom_edge |= (1 << k);
2347 Terrain_list[tlist_index].bottom_count++;
2348
2349 transcount++;
2350 }
2351 }
2352 } else {
2353 Terrain_list[tlist_index].bottom_edge |= 1;
2354 Terrain_list[tlist_index].bottom_count++;
2355 seg = t + smul_x;
2356 n[transcount + start] = seg;
2357 transcount++;
2358 }
2359
2360 start += transcount;
2361 // Right edge
2362 //---1
2363 // |
2364 // |
2365 //-- 2
2366
2367 Terrain_list[tlist_index].right_edge = 0;
2368 Terrain_list[tlist_index].right_count = 0;
2369 transcount = 0;
2370 offset = ((cz + smul_z) * TERRAIN_WIDTH) + cx + smul_x;
2371 edge1 = t + smul_x + (smul_z * TERRAIN_WIDTH);
2372 edge2 = t + smul_x;
2373 delta = (Terrain_seg[edge2].mody - Terrain_seg[edge1].mody) / smul_z;
2374 cury = Terrain_seg[edge1].mody;
2375 for (k = 0; k < smul_z; k++, cury += delta) {
2376 maplod = TerrainJoinMap[offset - (k * TERRAIN_WIDTH)];
2377 answer = TerrainEdgeTest[maplod][k + simplemul - smul_z];
2378 if (answer || k == 0) {
2379 seg = t + smul_x + ((smul_z - k) * TERRAIN_WIDTH);
2380 n[transcount + start] = seg;
2381 Terrain_seg[seg].mody = cury;
2382 Terrain_list[tlist_index].right_edge |= (1 << k);
2383 Terrain_list[tlist_index].right_count++;
2384 transcount++;
2385 }
2386 }
2387 start += transcount;
2388 // Top edge
2389 // 1--------2
2390 // | |
2391
2392 Terrain_list[tlist_index].top_edge = 0;
2393 Terrain_list[tlist_index].top_count = 0;
2394 transcount = 0;
2395 offset = cx + ((cz + smul_z) * TERRAIN_WIDTH);
2396 edge1 = t + (smul_z * TERRAIN_WIDTH);
2397 edge2 = t + smul_x + (smul_z * TERRAIN_WIDTH);
2398 delta = (Terrain_seg[edge2].mody - Terrain_seg[edge1].mody) / smul_x;
2399 cury = Terrain_seg[edge1].mody;
2400 for (k = 0; k < smul_x; k++, cury += delta) {
2401 maplod = TerrainJoinMap[offset + k];
2402 answer = TerrainEdgeTest[maplod][k];
2403 if (answer || k == 0) {
2404 seg = (t + k) + (smul_z * TERRAIN_WIDTH);
2405 n[transcount + start] = seg;
2406 Terrain_seg[seg].mody = cury;
2407 Terrain_list[tlist_index].top_edge |= (1 << k);
2408 Terrain_list[tlist_index].top_count++;
2409 transcount++;
2410 }
2411 }
2412 start += transcount;
2413 // left edge
2414 // 2----
2415 // |
2416 // |
2417 // 1----
2418 Terrain_list[tlist_index].left_edge = 0;
2419 Terrain_list[tlist_index].left_count = 0;
2420 transcount = 0;
2421 if (cx != 0) {
2422 offset = (cz * TERRAIN_WIDTH) + cx - 1;
2423 edge1 = t;
2424 edge2 = t + (smul_z * TERRAIN_WIDTH);
2425 delta = (Terrain_seg[edge2].mody - Terrain_seg[edge1].mody) / smul_z;
2426 cury = Terrain_seg[edge1].mody;
2427 for (k = 0; k < smul_z; k++, cury += delta) {
2428 maplod = TerrainJoinMap[offset + (k * TERRAIN_WIDTH)];
2429 answer = TerrainEdgeTest[maplod][k];
2430 if (answer || k == 0) {
2431 seg = t + (k * TERRAIN_WIDTH);
2432 n[transcount + start] = seg;
2433 Terrain_seg[seg].mody = cury;
2434 Terrain_list[tlist_index].left_edge |= (1 << k);
2435 Terrain_list[tlist_index].left_count++;
2436 transcount++;
2437 }
2438 }
2439 } else {
2440 seg = t;
2441 n[transcount + start] = seg;
2442 Terrain_list[tlist_index].left_edge |= 1;
2443 Terrain_list[tlist_index].left_count++;
2444 transcount++;
2445 }
2446
2447 edgecount = start + transcount;
2448 } else {
2449 n[0] = t;
2450 n[1] = t + TERRAIN_WIDTH;
2451 n[2] = t + TERRAIN_WIDTH + 1;
2452 n[3] = t + 1;
2453 Terrain_list[tlist_index].left_edge = 1;
2454 Terrain_list[tlist_index].bottom_edge = 1;
2455 Terrain_list[tlist_index].right_edge = 1;
2456 Terrain_list[tlist_index].top_edge = 1;
2457 edgecount = 4;
2458 }
2459 return edgecount;
2460 }
2461 // This function rotates all the points that we can see.
2462 // If a cell is a low-res cell, then we rotate all the points down each edge
2463 // of the cell to make sure any higher res blocks that touch the cell don't
2464 // appear with cracks
2465 vector Terrain_alter_vec = {19, -19, 19};
2466 void RotateTerrainList(int cellcount, bool from_automap) {
2467 int lod, simplemul, edgecount;
2468 int i, n[200], t, k, cx, cz;
2469 vector camlight = Terrain_sky.lightsource;
2470 vm_NormalizeVector(&camlight);
2471 // Reset all modified y values for the corners of each cell
2472 for (i = 0; i < cellcount; i++) {
2473 int ax, az;
2474 t = Terrain_list[i].segment;
2475 lod = Terrain_list[i].lod;
2476 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
2477 cx = t & (TERRAIN_WIDTH - 1);
2478 cz = t >> 8;
2479
2480 ax = az = simplemul;
2481
2482 if (cx + ax >= TERRAIN_WIDTH)
2483 ax = (TERRAIN_WIDTH - 1) - cx;
2484 if (cz + az >= TERRAIN_DEPTH)
2485 az = (TERRAIN_DEPTH - 1) - cz;
2486
2487 n[0] = t;
2488 n[1] = t + (TERRAIN_WIDTH * az);
2489 n[2] = t + (az * TERRAIN_WIDTH) + ax;
2490 n[3] = t + ax;
2491 // This could be in a loop, but I unrolled it for speed
2492 Terrain_seg[n[0]].mody = Terrain_seg[n[0]].y;
2493 Terrain_seg[n[1]].mody = Terrain_seg[n[1]].y;
2494 Terrain_seg[n[2]].mody = Terrain_seg[n[2]].y;
2495 Terrain_seg[n[3]].mody = Terrain_seg[n[3]].y;
2496 if (StateLimited || from_automap) // Setup for sorting later
2497 {
2498 int unique_id;
2499 unique_id = Terrain_tex_seg[Terrain_seg[t].texseg_index].tex_index;
2500 State_elements[i].facenum = i;
2501 State_elements[i].sort_key = unique_id + (Terrain_seg[t].lm_quad << 24);
2502 }
2503 }
2504 for (i = 0; i < cellcount; i++) {
2505 edgecount = BuildEdgeLists(n, i);
2506 for (k = 0; k < edgecount; k++) {
2507 if (Terrain_rotate_list[n[k]] != TS_FrameCount) {
2508 Terrain_rotate_list[n[k]] = TS_FrameCount;
2509 GlobalTransCount++;
2510
2511 cx = n[k] % TERRAIN_WIDTH;
2512 cz = n[k] / TERRAIN_WIDTH;
2513
2514 World_point_buffer[n[k]].p3_flags = PF_UV | PF_UV2;
2515 if (Terrain_seg[n[k]].mody == Terrain_seg[n[k]].y) {
2516 GetPreRotatedPoint(&World_point_buffer[n[k]], cx, cz, Terrain_seg[n[k]].ypos);
2517 } else {
2518 GetSpecialRotatedPoint(&World_point_buffer[n[k]], cx, cz, Terrain_seg[n[k]].mody);
2519 }
2520
2521 if (Viewer_object->effect_info && (Viewer_object->effect_info->type_flags & EF_DEFORM)) {
2522 float val = (((ps_rand() % 1000) - 500.0f) / 500.0f) * Viewer_object->effect_info->deform_time;
2523 vector jitterVec = Terrain_alter_vec * (Viewer_object->effect_info->deform_range * val);
2524 World_point_buffer[n[k]].p3_vec += jitterVec;
2525 World_point_buffer[n[k]].p3_vecPreRot += jitterVec;
2526 }
2527 g3_CodePoint(&World_point_buffer[n[k]]);
2528 }
2529 }
2530 }
2531 }
2532
2533 // Puts a 1 in upperleft,lowerright if those triangles are visible
2534 void TerrainCellVisible(int index, int *upper_left, int *lower_right) {
2535 int seg = Terrain_list[index].segment;
2536 int lod = Terrain_list[index].lod;
2537 int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
2538 int cx, cz, smul_x, smul_z;
2539
2540 vector tempv;
2541 vector *corner[4];
2542
2543 cx = seg % TERRAIN_WIDTH;
2544 cz = seg / TERRAIN_WIDTH;
2545 if (cx + simplemul == TERRAIN_WIDTH)
2546 smul_x = simplemul - 1;
2547 else
2548 smul_x = simplemul;
2549 if (cz + simplemul == TERRAIN_DEPTH)
2550 smul_z = simplemul - 1;
2551 else
2552 smul_z = simplemul;
2553
2554 // Note - this is upper left and proceeds clockwise
2555 corner[0] = &World_point_buffer[seg + (TERRAIN_WIDTH * smul_z)].p3_vec;
2556 corner[1] = &World_point_buffer[seg + (TERRAIN_WIDTH * smul_z) + smul_x].p3_vec;
2557 corner[2] = &World_point_buffer[seg].p3_vec;
2558 corner[3] = &World_point_buffer[seg + smul_x].p3_vec;
2559
2560 vm_GetPerp(&tempv, corner[0], corner[1], corner[2]);
2561 if ((tempv * *corner[1]) < 0)
2562 *upper_left = 1;
2563 else
2564 *upper_left = 0;
2565
2566 // Now do lower right
2567 vm_GetPerp(&tempv, corner[2], corner[1], corner[3]);
2568 if ((tempv * *corner[1]) < 0)
2569 *lower_right = 1;
2570 else
2571 *lower_right = 0;
2572 }
2573
2574 void DisplayTerrainList(int cellcount, bool from_automap) {
2575 int total = 0, on, t, i, lod, simplemul;
2576 int bm_handle;
2577 bool draw_lightmap = false;
2578 int savecell;
2579 int obj_to_draw;
2580 Terrain_objects_drawn = 0;
2581 rend_SetWrapType(WT_WRAP);
2582 if (!UseHardware)
2583 rend_SetColorModel(CM_MONO);
2584 else {
2585 rend_SetColorModel(CM_RGB);
2586 rend_SetTextureType(TT_LINEAR);
2587 rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
2588 rend_SetLighting(LS_NONE);
2589 if (!StateLimited || UseMultitexture)
2590 draw_lightmap = true;
2591 }
2592 RotateTerrainList(cellcount, from_automap);
2593 if (!UseHardware) {
2594 SortTerrainList(cellcount);
2595 SortTerrainObjectsForRendering(cellcount);
2596 }
2597 // If state limited, sort by texture
2598 if (StateLimited || from_automap)
2599 SortStates(State_elements, cellcount);
2600 if (from_automap) {
2601 savecell = cellcount;
2602 cellcount = 0;
2603 }
2604
2605 for (i = 0; i < cellcount; i++) {
2606 int cx, cz;
2607 int seg_to_render;
2608 if (StateLimited)
2609 seg_to_render = State_elements[i].facenum;
2610 else
2611 seg_to_render = i;
2612 t = Terrain_list[seg_to_render].segment;
2613 lod = Terrain_list[seg_to_render].lod;
2614 simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
2615
2616 cx = t % TERRAIN_WIDTH;
2617 cz = t / TERRAIN_WIDTH;
2618
2619 if (cx < TERRAIN_WIDTH - simplemul && cz < TERRAIN_DEPTH - simplemul || lod != (MAX_TERRAIN_LOD - 1)) {
2620 int ul, lr; // upper_left,lower_right
2621 if (Terrain_seg[t].flags & TF_INVISIBLE)
2622 if (!Show_invisible_terrain)
2623 goto draw_objects; // bad! No gotos! -JL
2624 // Check to see if these triangles are visible if they're the smallest lod
2625 TerrainCellVisible(seg_to_render, &ul, &lr);
2626
2627 total += (ul + lr);
2628 if (ul == 0 && lr == 0)
2629 goto draw_objects;
2630
2631 bm_handle = GetTextureBitmap(Terrain_tex_seg[Terrain_seg[t].texseg_index].tex_index, 0);
2632
2633 if (UseHardware) {
2634 if (draw_lightmap)
2635 on = DrawTerrainTrianglesHardware(seg_to_render, bm_handle, ul, lr);
2636 else
2637 on = DrawTerrainTrianglesHardwareNoLight(seg_to_render, bm_handle, ul, lr);
2638
2639 } else
2640 on = DrawTerrainTrianglesSoftware(seg_to_render, bm_handle, ul, lr);
2641 }
2642
2643 draw_objects:;
2644 // Now draw any objects in this segment
2645 #if (!defined(RELEASE) || defined(NEWEDITOR))
2646 if (!UseHardware) {
2647 obj_to_draw = Terrain_seg_render_objs[t];
2648
2649 while (obj_to_draw != -1) {
2650
2651 if (Objects[obj_to_draw].type != OBJ_ROOM)
2652 RenderObject(&Objects[obj_to_draw]);
2653 obj_to_draw = render_next[obj_to_draw];
2654 }
2655 Terrain_seg_render_objs[t] = -1;
2656 }
2657 #endif
2658 }
2659 #if (defined(EDITOR) || defined(NEWEDITOR))
2660 if (!UseHardware) {
2661 #if (!defined(RELEASE) || defined(NEWEDITOR))
2662 for (i = 0; i < cellcount; i++) {
2663 t = Terrain_list[i].segment;
2664 Terrain_seg_render_objs[t] = -1;
2665 }
2666 #endif
2667
2668 if ((View_mode == EDITOR_MODE) && OUTLINE_ON(OM_TERRAIN)) {
2669 for (i = 0; i < cellcount; i++) {
2670 t = Terrain_list[i].segment;
2671 if (TerrainSelected[t] && Terrain_rotate_list[t] == TS_FrameCount)
2672 TerrainDrawCurrentVert(t);
2673 }
2674 }
2675 }
2676 #endif
2677 // Draw lightmaps if this is state limited
2678 if ((UseHardware && !draw_lightmap) || from_automap) {
2679 if (from_automap) {
2680 rend_SetAlphaType(AT_CONSTANT);
2681 cellcount = savecell;
2682 } else
2683 rend_SetAlphaType(AT_LIGHTMAP_BLEND);
2684 rend_SetAlphaValue(255);
2685 rend_SetLighting(LS_NONE);
2686 rend_SetOverlayType(OT_NONE);
2687 rend_SetTextureType(TT_PERSPECTIVE);
2688 rend_SetWrapType(WT_WRAP);
2689 rend_SetZBias(-.5f);
2690
2691 for (i = 0; i < cellcount; i++) {
2692 int ul, lr;
2693 int seg_to_render;
2694 seg_to_render = State_elements[i].facenum;
2695 TerrainCellVisible(seg_to_render, &ul, &lr);
2696 if (ul == 0 && lr == 0)
2697 continue;
2698 DrawTerrainLightmapsHardware(seg_to_render, ul, lr);
2699 }
2700 rend_SetZBias(0);
2701 }
2702
2703 rend_SetOverlayType(OT_NONE);
2704 rend_SetWrapType(WT_WRAP);
2705
2706 mprintf_at(2, 1, 0, "%5d cells", cellcount);
2707 mprintf_at(2, 2, 0, "%5d trans", GlobalTransCount);
2708 mprintf_at(2, 3, 0, "Tdepth=%5d", TotalDepth);
2709 }
2710 // Arrays for drawing
2711 static int src[256];
2712 static g3Point base[256];
2713 static g3Point *slist[256];
2714 // Draws the 2 triangles of the Terrainlist[index] (software)
2715 int DrawTerrainTrianglesSoftware(int index, int bm_handle, int upper_left, int lower_right) {
2716 /*
2717 #if !defined(POSIX)
2718 int i,tlist[4],close=0,lit=0;
2719 float closest_z=9999;
2720 int color;
2721 int n=Terrain_list[index].segment;
2722 int lod=Terrain_list[index].lod;
2723
2724 terrain_segment *tseg=&Terrain_seg[n];
2725 terrain_tex_segment *texseg=&Terrain_tex_seg[tseg->texseg_index];
2726 int rotation=texseg->rotation & 0x0F;
2727 int tile=texseg->rotation >> 4;
2728 int simplemul=1<<((MAX_TERRAIN_LOD-1)-lod);
2729 int cx,cz,smul_x,smul_z;
2730 #if (defined(EDITOR) || defined(NEWEDITOR))
2731 ddgr_color oldcolor;
2732 #endif
2733 cx=n%TERRAIN_WIDTH;
2734 cz=n/TERRAIN_WIDTH;
2735 int subx=cx % MAX_LOD_SIZE;
2736 int subz=(MAX_LOD_SIZE-1)-((cz+(simplemul-1)) % MAX_LOD_SIZE);
2737 if (cx+simplemul==TERRAIN_WIDTH)
2738 smul_x=simplemul-1;
2739 else
2740 smul_x=simplemul;
2741 if (cz+simplemul==TERRAIN_DEPTH)
2742 smul_z=simplemul-1;
2743 else
2744 smul_z=simplemul;
2745
2746 // Note - this is upper left and proceeds lockwise
2747 tlist[0]=n+(TERRAIN_WIDTH*smul_z);
2748 tlist[1]=n+(TERRAIN_WIDTH*smul_z)+(smul_x);
2749 tlist[2]=n+(smul_x);
2750 tlist[3]=n;
2751 rend_SetOverlayType (OT_NONE);
2752 for (close=0,i=0;i<4;i++)
2753 {
2754 base[i]=*((g3Point *)&World_point_buffer[tlist[i]]);
2755 base[i].p3_flags|=(PF_L|PF_RGBA);
2756 base[i].p3_l=Ubyte_to_float[Terrain_seg[tlist[i]].l];
2757
2758 // only do perspective if all the points are inside our range
2759 if (!UseHardware)
2760 {
2761 if (base[i].p3_vec.z<TERRAIN_PERSPECTIVE_TEXTURE_DEPTH)
2762 close=1;
2763 if (base[i].p3_vec.z<closest_z)
2764 closest_z=base[i].p3_vec.z;
2765 }
2766 }
2767 base[0].p3_u=TerrainUSpeedup[rotation][subz*LOD_ROW_SIZE+subx]*tile;
2768 base[0].p3_v=TerrainVSpeedup[rotation][subz*LOD_ROW_SIZE+subx]*tile;
2769 base[1].p3_u=TerrainUSpeedup[rotation][subz*LOD_ROW_SIZE+subx+simplemul]*tile;
2770 base[1].p3_v=TerrainVSpeedup[rotation][subz*LOD_ROW_SIZE+subx+simplemul]*tile;
2771 base[2].p3_u=TerrainUSpeedup[rotation][((subz+simplemul)*LOD_ROW_SIZE)+subx+simplemul]*tile;
2772 base[2].p3_v=TerrainVSpeedup[rotation][((subz+simplemul)*LOD_ROW_SIZE)+subx+simplemul]*tile;
2773 base[3].p3_u=TerrainUSpeedup[rotation][((subz+simplemul)*LOD_ROW_SIZE)+subx]*tile;
2774 base[3].p3_v=TerrainVSpeedup[rotation][((subz+simplemul)*LOD_ROW_SIZE)+subx]*tile;
2775
2776 rend_SetLighting (Lighting_on?LS_GOURAUD:LS_NONE);
2777
2778 #if (defined(EDITOR) || defined(NEWEDITOR))
2779 if (TSearch_on)
2780 {
2781 rend_SetPixel(GR_RGB(0,255,0),TSearch_x,TSearch_y);
2782 oldcolor = rend_GetPixel(TSearch_x,TSearch_y); //will be different in
2783 15/16-bit color
2784 }
2785 #endif
2786
2787 // Make sure the triangle faces us and if so draw
2788 // Upper left triangle
2789 if (!upper_left)
2790 goto draw_lower_right;
2791 src[0]=0;
2792 src[1]=1;
2793 src[2]=3;
2794
2795 for (lit=0,i=0;i<3;i++)
2796 {
2797 if (base[src[i]].p3_z<=Far_fog_border)
2798 lit=1;
2799
2800 slist[i]=&base[src[i]];
2801 }
2802
2803 if (!lit && Lighting_on)
2804 {
2805 rend_SetTextureType(TT_FLAT);
2806 rend_SetFlatColor (0);
2807 g3_DrawPoly(3,slist,0);
2808 }
2809 else
2810 {
2811 // If we're past our texturing distance, flat shade!
2812 if (closest_z>Terrain_texture_distance)
2813 {
2814 rend_SetTextureType (TT_FLAT);
2815 int lightval=Ubyte_to_float [tseg->l]*(MAX_TEXTURE_SHADES-1);
2816 int pix=*bm_data(bm_handle,0);
2817 int fadepixel=(TexShadeTable16[lightval][pix>>8])+TexShadeTable8[lightval][pix & 0xFF];
2818 color=GR_16_TO_COLOR (fadepixel);
2819 rend_SetFlatColor (color);
2820 g3_DrawPoly(3,slist,0);
2821 }
2822 else
2823 {
2824 if (close)
2825 rend_SetTextureType (TT_PERSPECTIVE);
2826 else
2827 rend_SetTextureType (TT_LINEAR);
2828
2829 g3_DrawPoly(3,slist,bm_handle);
2830 }
2831 }
2832 #if (!defined(RELEASE) || defined(NEWEDITOR))
2833 if (OUTLINE_ON(OM_TERRAIN))
2834 DrawTerrainOutline(n,3, slist);
2835 #endif
2836 // Now do lower right triangle
2837 draw_lower_right:
2838 if (!lower_right)
2839 return 0;
2840 src[0]=3;
2841 src[1]=1;
2842 src[2]=2;
2843 for (lit=0,i=0;i<3;i++)
2844 {
2845 if (base[src[i]].p3_z<=Far_fog_border)
2846 lit=1;
2847
2848 slist[i]=&base[src[i]];
2849 }
2850 if (!lit && Lighting_on)
2851 {
2852 rend_SetTextureType(TT_FLAT);
2853 rend_SetFlatColor (0);
2854 g3_DrawPoly(3,slist,0);
2855 }
2856 else
2857 {
2858 // If we're past our texturing distance, flat shade!
2859 if (closest_z>Terrain_texture_distance)
2860 {
2861 rend_SetTextureType (TT_FLAT);
2862 int lightval=Ubyte_to_float[tseg->l]*(MAX_TEXTURE_SHADES-1);
2863 int pix=*bm_data(bm_handle,0);
2864 int fadepixel=(TexShadeTable16[lightval][pix>>8])+TexShadeTable8[lightval][pix & 0xFF];
2865 color=GR_16_TO_COLOR (fadepixel);
2866 rend_SetFlatColor (color);
2867 g3_DrawPoly(3,slist,0);
2868 }
2869 else
2870 {
2871 if (close)
2872 rend_SetTextureType (TT_PERSPECTIVE);
2873 else
2874 rend_SetTextureType (TT_LINEAR);
2875 }
2876
2877 g3_DrawPoly(3,slist,bm_handle);
2878 }
2879
2880 #if (!defined(RELEASE) || defined(NEWEDITOR))
2881 if (OUTLINE_ON(OM_TERRAIN))
2882 DrawTerrainOutline(n,3, slist);
2883 #endif
2884 #if (defined(EDITOR) || defined(NEWEDITOR))
2885 if (TSearch_on)
2886 {
2887 if (rend_GetPixel(TSearch_x,TSearch_y) != oldcolor)
2888 {
2889 TSearch_seg = n;
2890 TSearch_found_type=TSEARCH_FOUND_TERRAIN;
2891 }
2892 }
2893 #endif
2894 #endif//__LINUX__
2895 */
2896 return 0;
2897 }
2898 // Draws the 2 triangles of the Terrainlist[index] (hardware)
2899 int DrawTerrainTrianglesHardware(int index, int bm_handle, int upper_left, int lower_right) {
2900 int i;
2901 int cur_seg;
2902 int n = Terrain_list[index].segment;
2903 int lod = Terrain_list[index].lod;
2904 int bottom_start, left_start, right_start;
2905 int point_count = 0;
2906 int points_this_triangle = 0;
2907
2908 terrain_segment *tseg = &Terrain_seg[n];
2909 terrain_tex_segment *texseg = &Terrain_tex_seg[tseg->texseg_index];
2910 int rotator = texseg->rotation & 0x0F;
2911 int tile = texseg->rotation >> 4;
2912 int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
2913 int cx, cz, smul_x, smul_z;
2914 cx = n % TERRAIN_WIDTH;
2915 cz = n / TERRAIN_WIDTH;
2916
2917 // Get lightmap coordinates
2918 float lightmap_u = (cx % 128) / 128.0;
2919 float lightmap_v = (128 - ((cz % 128) + simplemul)) / 128.0;
2920 float uvadjust;
2921 int draw_big_square = 0;
2922 int subx = cx % MAX_LOD_SIZE;
2923 int subz = (MAX_LOD_SIZE - 1) - ((cz + (simplemul - 1)) % MAX_LOD_SIZE);
2924 bool solid_square = 1;
2925 int testt = 0, testr = 0, testb = 0, testl = 0;
2926 // Check to make sure we don't access memory that is off the map
2927 if (cx + simplemul == TERRAIN_WIDTH) {
2928 smul_x = simplemul - 1;
2929 solid_square = 0;
2930 } else
2931 smul_x = simplemul;
2932 if (cz + simplemul == TERRAIN_DEPTH) {
2933 solid_square = 0;
2934 smul_z = simplemul - 1;
2935 } else
2936 smul_z = simplemul;
2937 // Build a list of points for our polygon. We must do it this way to
2938 // prevent tjoint cracking
2939 // Do simpler operation if at highest level of detail
2940 if (lod == (MAX_TERRAIN_LOD - 1)) {
2941 uvadjust = (simplemul / 128.0);
2942 cur_seg = n + (TERRAIN_WIDTH * smul_z);
2943 base[0] = World_point_buffer[cur_seg];
2944
2945 cur_seg = n + (TERRAIN_WIDTH * smul_z) + smul_x;
2946 base[1] = World_point_buffer[cur_seg];
2947
2948 cur_seg = n + smul_x;
2949 base[2] = World_point_buffer[cur_seg];
2950
2951 base[3] = World_point_buffer[n];
2952
2953 base[0].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx];
2954 base[0].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx];
2955 base[1].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx + 1];
2956 base[1].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx + 1];
2957 base[2].p3_u = tile * TerrainUSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx + 1];
2958 base[2].p3_v = tile * TerrainVSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx + 1];
2959 base[3].p3_u = tile * TerrainUSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx];
2960 base[3].p3_v = tile * TerrainVSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx];
2961
2962 base[0].p3_u2 = lightmap_u;
2963 base[0].p3_v2 = lightmap_v;
2964 base[1].p3_u2 = lightmap_u + uvadjust;
2965 base[1].p3_v2 = lightmap_v;
2966 base[2].p3_u2 = lightmap_u + uvadjust;
2967 base[2].p3_v2 = lightmap_v + uvadjust;
2968
2969 base[3].p3_u2 = lightmap_u;
2970 base[3].p3_v2 = lightmap_v + uvadjust;
2971 } else {
2972 uvadjust = (simplemul / 128.0) / simplemul;
2973 float uvmul = uvadjust * simplemul;
2974 if (solid_square) {
2975 right_start = Terrain_list[index].top_count;
2976 bottom_start = right_start + Terrain_list[index].right_count;
2977 left_start = bottom_start + Terrain_list[index].bottom_count;
2978 point_count = left_start + Terrain_list[index].left_count;
2979
2980 for (i = 0; i < simplemul; i++) {
2981 // Top edge
2982 if (Terrain_list[index].top_edge & (1 << i)) {
2983 cur_seg = n + i + (TERRAIN_WIDTH * smul_z);
2984 base[testt] = World_point_buffer[cur_seg];
2985
2986 base[testt].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
2987 base[testt].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
2988
2989 base[testt].p3_u2 = lightmap_u + (i * uvadjust);
2990 base[testt].p3_v2 = lightmap_v;
2991 slist[testt] = &base[testt];
2992 testt++;
2993 }
2994 // Right edge
2995 if (Terrain_list[index].right_edge & (1 << i)) {
2996 cur_seg = n + (TERRAIN_WIDTH * (smul_z - i)) + smul_x;
2997 base[right_start + testr] = World_point_buffer[cur_seg];
2998
2999 base[right_start + testr].p3_u =
3000 tile * TerrainUSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + simplemul];
3001 base[right_start + testr].p3_v =
3002 tile * TerrainVSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + simplemul];
3003
3004 base[right_start + testr].p3_u2 = lightmap_u + uvmul;
3005 base[right_start + testr].p3_v2 = lightmap_v + (i * uvadjust);
3006 slist[right_start + testr] = &base[right_start + testr];
3007 testr++;
3008 }
3009 // Bottom edge
3010 if (Terrain_list[index].bottom_edge & (1 << i)) {
3011 cur_seg = n + (smul_x - i);
3012 base[bottom_start + testb] = World_point_buffer[cur_seg];
3013
3014 base[bottom_start + testb].p3_u =
3015 tile * TerrainUSpeedup[rotator][((subz + simplemul) * LOD_ROW_SIZE) + (subx + simplemul) - i];
3016 base[bottom_start + testb].p3_v =
3017 tile * TerrainVSpeedup[rotator][((subz + simplemul) * LOD_ROW_SIZE) + (subx + simplemul) - i];
3018 base[bottom_start + testb].p3_u2 = lightmap_u + uvmul - (i * uvadjust);
3019 base[bottom_start + testb].p3_v2 = lightmap_v + uvmul;
3020 slist[bottom_start + testb] = &base[bottom_start + testb];
3021 testb++;
3022 }
3023 // left edge
3024 if (Terrain_list[index].left_edge & (1 << i)) {
3025 cur_seg = n + (TERRAIN_WIDTH * i);
3026 base[left_start + testl] = World_point_buffer[cur_seg];
3027
3028 base[left_start + testl].p3_u =
3029 tile * TerrainUSpeedup[rotator][((subz + simplemul - i) * LOD_ROW_SIZE) + subx];
3030 base[left_start + testl].p3_v =
3031 tile * TerrainVSpeedup[rotator][((subz + simplemul - i) * LOD_ROW_SIZE) + subx];
3032
3033 base[left_start + testl].p3_u2 = lightmap_u;
3034 base[left_start + testl].p3_v2 = lightmap_v + uvmul - (i * uvadjust);
3035 slist[left_start + testl] = &base[left_start + testl];
3036 testl++;
3037 }
3038 }
3039 } else {
3040 right_start = Terrain_list[index].top_count;
3041 bottom_start = right_start + Terrain_list[index].right_count;
3042 left_start = bottom_start + Terrain_list[index].bottom_count;
3043 point_count = left_start + Terrain_list[index].left_count;
3044
3045 for (i = 0; i < smul_x; i++) {
3046 // top edge
3047 if (Terrain_list[index].top_edge & (1 << i)) {
3048 cur_seg = n + i + (TERRAIN_WIDTH * smul_z);
3049 base[testt] = World_point_buffer[cur_seg];
3050
3051 base[testt].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
3052 base[testt].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE];
3053
3054 base[testt].p3_u2 = lightmap_u + (i * uvadjust);
3055 base[testt].p3_v2 = lightmap_v;
3056 slist[testt] = &base[testt];
3057 testt++;
3058 }
3059 // Bottom edge
3060 if (Terrain_list[index].bottom_edge & (1 << i)) {
3061 cur_seg = n + (smul_x - i);
3062 base[bottom_start + testb] = World_point_buffer[cur_seg];
3063
3064 base[bottom_start + testb].p3_u =
3065 tile * TerrainUSpeedup[rotator][((subz + smul_z) * LOD_ROW_SIZE) + subx + smul_x - i];
3066 base[bottom_start + testb].p3_v =
3067 tile * TerrainVSpeedup[rotator][((subz + smul_z) * LOD_ROW_SIZE) + subx + smul_x - i];
3068 base[bottom_start + testb].p3_u2 = lightmap_u + uvmul - (i * uvadjust);
3069 base[bottom_start + testb].p3_v2 = lightmap_v + uvmul;
3070 slist[bottom_start + testb] = &base[bottom_start + testb];
3071 testb++;
3072 }
3073 }
3074
3075 for (i = 0; i < smul_z; i++) {
3076 // Right edge
3077 if (Terrain_list[index].right_edge & (1 << i)) {
3078 cur_seg = n + (TERRAIN_WIDTH * (smul_z - i)) + smul_x;
3079 base[right_start + testr] = World_point_buffer[cur_seg];
3080
3081 base[right_start + testr].p3_u = tile * TerrainUSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + smul_x];
3082 base[right_start + testr].p3_v = tile * TerrainVSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + smul_x];
3083
3084 base[right_start + testr].p3_u2 = lightmap_u + uvmul;
3085 base[right_start + testr].p3_v2 = lightmap_v + (i * uvadjust);
3086 slist[right_start + testr] = &base[right_start + testr];
3087 testr++;
3088 }
3089 // left edge
3090 if (Terrain_list[index].left_edge & (1 << i)) {
3091 cur_seg = n + (TERRAIN_WIDTH * i);
3092 base[left_start + testl] = World_point_buffer[cur_seg];
3093
3094 base[left_start + testl].p3_u = tile * TerrainUSpeedup[rotator][((subz + smul_z - i) * LOD_ROW_SIZE) + subx];
3095 base[left_start + testl].p3_v = tile * TerrainVSpeedup[rotator][((subz + smul_z - i) * LOD_ROW_SIZE) + subx];
3096
3097 base[left_start + testl].p3_u2 = lightmap_u;
3098 base[left_start + testl].p3_v2 = lightmap_v + uvmul - (i * uvadjust);
3099 slist[left_start + testl] = &base[left_start + testl];
3100 testl++;
3101 }
3102 }
3103 }
3104 }
3105
3106 rend_SetOverlayType(OT_BLEND);
3107 rend_SetOverlayMap(TerrainLightmaps[tseg->lm_quad]);
3108
3109 // Make sure the triangle faces us and if so draw
3110 // Upper left triangle
3111 if (lod != (MAX_TERRAIN_LOD - 1))
3112 draw_big_square = 1;
3113 if (!upper_left && !draw_big_square)
3114 goto draw_lower_right;
3115
3116 if (lod == (MAX_TERRAIN_LOD - 1)) {
3117 slist[0] = &base[0];
3118 slist[1] = &base[1];
3119 slist[2] = &base[3];
3120 points_this_triangle = 3;
3121 } else {
3122 points_this_triangle = point_count;
3123 }
3124
3125 g3_DrawPoly(points_this_triangle, slist, bm_handle);
3126 #if (!defined(RELEASE) || defined(NEWEDITOR))
3127 if (OUTLINE_ON(OM_TERRAIN))
3128 DrawTerrainOutline(n, points_this_triangle, slist);
3129 #endif
3130 // If we're LOD'd, we've already drawn our 1 polygon. Return!
3131 if (draw_big_square)
3132 return 0;
3133
3134 // Now do lower right triangle
3135 draw_lower_right:
3136 if (!lower_right)
3137 return 0;
3138 slist[0] = &base[3];
3139 slist[1] = &base[1];
3140 slist[2] = &base[2];
3141 points_this_triangle = 3;
3142
3143 g3_DrawPoly(points_this_triangle, slist, bm_handle);
3144
3145 #if (!defined(RELEASE) || defined(NEWEDITOR))
3146 if (OUTLINE_ON(OM_TERRAIN))
3147 DrawTerrainOutline(n, points_this_triangle, slist);
3148 #endif
3149 return 0;
3150 }
3151 // Draws the 2 triangles of the Terrainlist[index] (hardware)
3152 int DrawTerrainTrianglesHardwareNoLight(int index, int bm_handle, int upper_left, int lower_right) {
3153 int i;
3154 int cur_seg;
3155 int n = Terrain_list[index].segment;
3156 int lod = Terrain_list[index].lod;
3157 int bottom_start, left_start, right_start;
3158 int point_count = 0;
3159 int points_this_triangle = 0;
3160
3161 terrain_segment *tseg = &Terrain_seg[n];
3162 terrain_tex_segment *texseg = &Terrain_tex_seg[tseg->texseg_index];
3163 int rotator = texseg->rotation & 0x0F;
3164 int tile = texseg->rotation >> 4;
3165 int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
3166 int cx, cz, smul_x, smul_z;
3167 cx = n % TERRAIN_WIDTH;
3168 cz = n / TERRAIN_WIDTH;
3169
3170 int draw_big_square = 0;
3171 int subx = cx % MAX_LOD_SIZE;
3172 int subz = (MAX_LOD_SIZE - 1) - ((cz + (simplemul - 1)) % MAX_LOD_SIZE);
3173 bool solid_square = 1;
3174 int testt = 0, testr = 0, testb = 0, testl = 0;
3175 // Check to make sure we don't access memory that is off the map
3176 if (cx + simplemul == TERRAIN_WIDTH) {
3177 smul_x = simplemul - 1;
3178 solid_square = 0;
3179 } else
3180 smul_x = simplemul;
3181 if (cz + simplemul == TERRAIN_DEPTH) {
3182 solid_square = 0;
3183 smul_z = simplemul - 1;
3184 } else
3185 smul_z = simplemul;
3186 // Build a list of points for our polygon. We must do it this way to
3187 // prevent tjoint cracking
3188 // Do simpler operation if at highest level of detail
3189 if (lod == (MAX_TERRAIN_LOD - 1)) {
3190 cur_seg = n + (TERRAIN_WIDTH * smul_z);
3191 base[0] = World_point_buffer[cur_seg];
3192
3193 cur_seg = n + (TERRAIN_WIDTH * smul_z) + smul_x;
3194 base[1] = World_point_buffer[cur_seg];
3195
3196 cur_seg = n + smul_x;
3197 base[2] = World_point_buffer[cur_seg];
3198
3199 base[3] = World_point_buffer[n];
3200
3201 base[0].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx];
3202 base[0].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx];
3203 base[1].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx + 1];
3204 base[1].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx + 1];
3205 base[2].p3_u = tile * TerrainUSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx + 1];
3206 base[2].p3_v = tile * TerrainVSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx + 1];
3207 base[3].p3_u = tile * TerrainUSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx];
3208 base[3].p3_v = tile * TerrainVSpeedup[rotator][(subz + 1) * LOD_ROW_SIZE + subx];
3209 } else {
3210 if (solid_square) {
3211 right_start = Terrain_list[index].top_count;
3212 bottom_start = right_start + Terrain_list[index].right_count;
3213 left_start = bottom_start + Terrain_list[index].bottom_count;
3214 point_count = left_start + Terrain_list[index].left_count;
3215
3216 for (i = 0; i < simplemul; i++) {
3217 // Top edge
3218 if (Terrain_list[index].top_edge & (1 << i)) {
3219 cur_seg = n + i + (TERRAIN_WIDTH * smul_z);
3220 base[testt] = World_point_buffer[cur_seg];
3221
3222 base[testt].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
3223 base[testt].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
3224
3225 slist[testt] = &base[testt];
3226 testt++;
3227 }
3228 // Right edge
3229 if (Terrain_list[index].right_edge & (1 << i)) {
3230 cur_seg = n + (TERRAIN_WIDTH * (smul_z - i)) + smul_x;
3231 base[right_start + testr] = World_point_buffer[cur_seg];
3232
3233 base[right_start + testr].p3_u =
3234 tile * TerrainUSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + simplemul];
3235 base[right_start + testr].p3_v =
3236 tile * TerrainVSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + simplemul];
3237
3238 slist[right_start + testr] = &base[right_start + testr];
3239 testr++;
3240 }
3241 // Bottom edge
3242 if (Terrain_list[index].bottom_edge & (1 << i)) {
3243 cur_seg = n + (smul_x - i);
3244 base[bottom_start + testb] = World_point_buffer[cur_seg];
3245
3246 base[bottom_start + testb].p3_u =
3247 tile * TerrainUSpeedup[rotator][((subz + simplemul) * LOD_ROW_SIZE) + subx + simplemul - i];
3248 base[bottom_start + testb].p3_v =
3249 tile * TerrainVSpeedup[rotator][((subz + simplemul) * LOD_ROW_SIZE) + subx + simplemul - i];
3250 slist[bottom_start + testb] = &base[bottom_start + testb];
3251 testb++;
3252 }
3253 // left edge
3254 if (Terrain_list[index].left_edge & (1 << i)) {
3255 cur_seg = n + (TERRAIN_WIDTH * i);
3256 base[left_start + testl] = World_point_buffer[cur_seg];
3257
3258 base[left_start + testl].p3_u =
3259 tile * TerrainUSpeedup[rotator][((subz + simplemul - i) * LOD_ROW_SIZE) + subx];
3260 base[left_start + testl].p3_v =
3261 tile * TerrainVSpeedup[rotator][((subz + simplemul - i) * LOD_ROW_SIZE) + subx];
3262
3263 slist[left_start + testl] = &base[left_start + testl];
3264 testl++;
3265 }
3266 }
3267 } else {
3268 right_start = Terrain_list[index].top_count;
3269 bottom_start = right_start + Terrain_list[index].right_count;
3270 left_start = bottom_start + Terrain_list[index].bottom_count;
3271 point_count = left_start + Terrain_list[index].left_count;
3272
3273 for (i = 0; i < smul_x; i++) {
3274 // top edge
3275 if (Terrain_list[index].top_edge & (1 << i)) {
3276 cur_seg = n + i + (TERRAIN_WIDTH * smul_z);
3277 base[testt] = World_point_buffer[cur_seg];
3278
3279 base[testt].p3_u = tile * TerrainUSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
3280 base[testt].p3_v = tile * TerrainVSpeedup[rotator][subz * LOD_ROW_SIZE + subx + i];
3281
3282 slist[testt] = &base[testt];
3283 testt++;
3284 }
3285 // Bottom edge
3286 if (Terrain_list[index].bottom_edge & (1 << i)) {
3287 cur_seg = n + (smul_x - i);
3288 base[bottom_start + testb] = World_point_buffer[cur_seg];
3289
3290 base[bottom_start + testb].p3_u =
3291 tile * TerrainUSpeedup[rotator][((subz + simplemul) * LOD_ROW_SIZE) + subx + simplemul - i];
3292 base[bottom_start + testb].p3_v =
3293 tile * TerrainVSpeedup[rotator][((subz + simplemul) * LOD_ROW_SIZE) + subx + simplemul - i];
3294
3295 slist[bottom_start + testb] = &base[bottom_start + testb];
3296 testb++;
3297 }
3298 }
3299
3300 for (i = 0; i < smul_z; i++) {
3301 // Right edge
3302 if (Terrain_list[index].right_edge & (1 << i)) {
3303 cur_seg = n + (TERRAIN_WIDTH * (smul_z - i)) + smul_x;
3304 base[right_start + testr] = World_point_buffer[cur_seg];
3305
3306 base[right_start + testr].p3_u =
3307 tile * TerrainUSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + simplemul];
3308 base[right_start + testr].p3_v =
3309 tile * TerrainVSpeedup[rotator][((subz + i) * LOD_ROW_SIZE) + subx + simplemul];
3310
3311 slist[right_start + testr] = &base[right_start + testr];
3312 testr++;
3313 }
3314 // left edge
3315 if (Terrain_list[index].left_edge & (1 << i)) {
3316 cur_seg = n + (TERRAIN_WIDTH * i);
3317 base[left_start + testl] = World_point_buffer[cur_seg];
3318
3319 base[left_start + testl].p3_u =
3320 tile * TerrainUSpeedup[rotator][((subz + simplemul - i) * LOD_ROW_SIZE) + subx];
3321 base[left_start + testl].p3_v =
3322 tile * TerrainVSpeedup[rotator][((subz + simplemul - i) * LOD_ROW_SIZE) + subx];
3323 slist[left_start + testl] = &base[left_start + testl];
3324 testl++;
3325 }
3326 }
3327 }
3328 }
3329 #if (defined(EDITOR) || defined(NEWEDITOR))
3330 ddgr_color oldcolor;
3331 if (TSearch_on) {
3332 rend_SetPixel(GR_RGB(0, 255, 0), TSearch_x, TSearch_y);
3333 oldcolor = rend_GetPixel(TSearch_x, TSearch_y); // will be different in 15/16-bit color
3334 }
3335 #endif
3336
3337 rend_SetOverlayType(OT_NONE);
3338
3339 // Make sure the triangle faces us and if so draw
3340 // Upper left triangle
3341 if (lod != (MAX_TERRAIN_LOD - 1))
3342 draw_big_square = 1;
3343 if (!upper_left && !draw_big_square)
3344 goto draw_lower_right;
3345
3346 if (lod == (MAX_TERRAIN_LOD - 1)) {
3347 slist[0] = &base[0];
3348 slist[1] = &base[1];
3349 slist[2] = &base[3];
3350 points_this_triangle = 3;
3351 } else {
3352 points_this_triangle = point_count;
3353 }
3354
3355 g3_DrawPoly(points_this_triangle, slist, bm_handle);
3356 #if (!defined(RELEASE) || defined(NEWEDITOR))
3357 if (OUTLINE_ON(OM_TERRAIN))
3358 DrawTerrainOutline(n, points_this_triangle, slist);
3359 #endif
3360 // If we're LOD'd, we've already drawn our 1 polygon. Return!
3361 if (draw_big_square)
3362 return 0;
3363
3364 // Now do lower right triangle
3365 draw_lower_right:
3366 if (!lower_right)
3367 return 0;
3368 slist[0] = &base[3];
3369 slist[1] = &base[1];
3370 slist[2] = &base[2];
3371 points_this_triangle = 3;
3372
3373 g3_DrawPoly(points_this_triangle, slist, bm_handle);
3374 #if (defined(EDITOR) || defined(NEWEDITOR))
3375 if (TSearch_on) {
3376 if (rend_GetPixel(TSearch_x, TSearch_y) != oldcolor) {
3377 TSearch_seg = n;
3378 TSearch_found_type = TSEARCH_FOUND_TERRAIN;
3379 }
3380 }
3381 #endif
3382 #if (!defined(RELEASE) || defined(NEWEDITOR))
3383 if (OUTLINE_ON(OM_TERRAIN))
3384 DrawTerrainOutline(n, points_this_triangle, slist);
3385 #endif
3386 return 0;
3387 }
3388 // Draws the 2 triangles of the Terrainlist[index] (hardware)
3389 void DrawTerrainLightmapsHardware(int index, int upper_left, int lower_right) {
3390 int i;
3391 int cur_seg;
3392 int n = Terrain_list[index].segment;
3393 int lod = Terrain_list[index].lod;
3394 int bottom_start, left_start, right_start;
3395 int point_count = 0;
3396 int points_this_triangle = 0;
3397
3398 terrain_segment *tseg = &Terrain_seg[n];
3399 int simplemul = 1 << ((MAX_TERRAIN_LOD - 1) - lod);
3400 int cx, cz, smul_x, smul_z;
3401 cx = n % TERRAIN_WIDTH;
3402 cz = n / TERRAIN_WIDTH;
3403
3404 // Get lightmap coordinates
3405 float lightmap_u = (cx % 128) / 128.0;
3406 float lightmap_v = (128 - ((cz % 128) + simplemul)) / 128.0;
3407 float uvadjust;
3408 int draw_big_square = 0;
3409 bool solid_square = 1;
3410 int testt = 0, testr = 0, testb = 0, testl = 0;
3411 // Check to make sure we don't access memory that is off the map
3412 if (cx + simplemul == TERRAIN_WIDTH) {
3413 smul_x = simplemul - 1;
3414 solid_square = 0;
3415 } else
3416 smul_x = simplemul;
3417 if (cz + simplemul == TERRAIN_DEPTH) {
3418 solid_square = 0;
3419 smul_z = simplemul - 1;
3420 } else
3421 smul_z = simplemul;
3422 // Build a list of points for our polygon. We must do it this way to
3423 // prevent tjoint cracking
3424 // Do simpler operation if at highest level of detail
3425 if (lod == (MAX_TERRAIN_LOD - 1)) {
3426 uvadjust = (simplemul / 128.0);
3427 cur_seg = n + (TERRAIN_WIDTH * smul_z);
3428 base[0] = World_point_buffer[cur_seg];
3429
3430 cur_seg = n + (TERRAIN_WIDTH * smul_z) + smul_x;
3431 base[1] = World_point_buffer[cur_seg];
3432
3433 cur_seg = n + smul_x;
3434 base[2] = World_point_buffer[cur_seg];
3435
3436 base[3] = World_point_buffer[n];
3437
3438 base[0].p3_u = lightmap_u;
3439 base[0].p3_v = lightmap_v;
3440 base[1].p3_u = lightmap_u + uvadjust;
3441 base[1].p3_v = lightmap_v;
3442 base[2].p3_u = lightmap_u + uvadjust;
3443 base[2].p3_v = lightmap_v + uvadjust;
3444
3445 base[3].p3_u = lightmap_u;
3446 base[3].p3_v = lightmap_v + uvadjust;
3447 } else {
3448 uvadjust = (simplemul / 128.0) / simplemul;
3449 float uvmul = uvadjust * simplemul;
3450 if (solid_square) {
3451 right_start = Terrain_list[index].top_count;
3452 bottom_start = right_start + Terrain_list[index].right_count;
3453 left_start = bottom_start + Terrain_list[index].bottom_count;
3454 point_count = left_start + Terrain_list[index].left_count;
3455
3456 for (i = 0; i < simplemul; i++) {
3457 // Top edge
3458 if (Terrain_list[index].top_edge & (1 << i)) {
3459 cur_seg = n + i + (TERRAIN_WIDTH * smul_z);
3460 base[testt] = World_point_buffer[cur_seg];
3461
3462 base[testt].p3_u = lightmap_u + (i * uvadjust);
3463 base[testt].p3_v = lightmap_v;
3464 slist[testt] = &base[testt];
3465 testt++;
3466 }
3467 // Right edge
3468 if (Terrain_list[index].right_edge & (1 << i)) {
3469 cur_seg = n + (TERRAIN_WIDTH * (smul_z - i)) + smul_x;
3470 base[right_start + testr] = World_point_buffer[cur_seg];
3471
3472 base[right_start + testr].p3_u = lightmap_u + uvmul;
3473 base[right_start + testr].p3_v = lightmap_v + (i * uvadjust);
3474 slist[right_start + testr] = &base[right_start + testr];
3475 testr++;
3476 }
3477 // Bottom edge
3478 if (Terrain_list[index].bottom_edge & (1 << i)) {
3479 cur_seg = n + (smul_x - i);
3480 base[bottom_start + testb] = World_point_buffer[cur_seg];
3481
3482 base[bottom_start + testb].p3_u = lightmap_u + uvmul - (i * uvadjust);
3483 base[bottom_start + testb].p3_v = lightmap_v + uvmul;
3484 slist[bottom_start + testb] = &base[bottom_start + testb];
3485 testb++;
3486 }
3487 // left edge
3488 if (Terrain_list[index].left_edge & (1 << i)) {
3489 cur_seg = n + (TERRAIN_WIDTH * i);
3490 base[left_start + testl] = World_point_buffer[cur_seg];
3491
3492 base[left_start + testl].p3_u = lightmap_u;
3493 base[left_start + testl].p3_v = lightmap_v + uvmul - (i * uvadjust);
3494 slist[left_start + testl] = &base[left_start + testl];
3495 testl++;
3496 }
3497 }
3498 } else {
3499 right_start = Terrain_list[index].top_count;
3500 bottom_start = right_start + Terrain_list[index].right_count;
3501 left_start = bottom_start + Terrain_list[index].bottom_count;
3502 point_count = left_start + Terrain_list[index].left_count;
3503
3504 for (i = 0; i < smul_x; i++) {
3505 // top edge
3506 if (Terrain_list[index].top_edge & (1 << i)) {
3507 cur_seg = n + i + (TERRAIN_WIDTH * smul_z);
3508 base[testt] = World_point_buffer[cur_seg];
3509
3510 base[testt].p3_u = lightmap_u + (i * uvadjust);
3511 base[testt].p3_v = lightmap_v;
3512 slist[testt] = &base[testt];
3513 testt++;
3514 }
3515 // Bottom edge
3516 if (Terrain_list[index].bottom_edge & (1 << i)) {
3517 cur_seg = n + (smul_x - i);
3518 base[bottom_start + testb] = World_point_buffer[cur_seg];
3519
3520 base[bottom_start + testb].p3_u = lightmap_u + uvmul - (i * uvadjust);
3521 base[bottom_start + testb].p3_v = lightmap_v + uvmul;
3522 slist[bottom_start + testb] = &base[bottom_start + testb];
3523 testb++;
3524 }
3525 }
3526
3527 for (i = 0; i < smul_z; i++) {
3528 // Right edge
3529 if (Terrain_list[index].right_edge & (1 << i)) {
3530 cur_seg = n + (TERRAIN_WIDTH * (smul_z - i)) + smul_x;
3531 base[right_start + testr] = World_point_buffer[cur_seg];
3532
3533 base[right_start + testr].p3_u = lightmap_u + uvmul;
3534 base[right_start + testr].p3_v = lightmap_v + (i * uvadjust);
3535 slist[right_start + testr] = &base[right_start + testr];
3536 testr++;
3537 }
3538 // left edge
3539 if (Terrain_list[index].left_edge & (1 << i)) {
3540 cur_seg = n + (TERRAIN_WIDTH * i);
3541 base[left_start + testl] = World_point_buffer[cur_seg];
3542
3543 base[left_start + testl].p3_u = lightmap_u;
3544 base[left_start + testl].p3_v = lightmap_v + uvmul - (i * uvadjust);
3545 slist[left_start + testl] = &base[left_start + testl];
3546 testl++;
3547 }
3548 }
3549 }
3550 }
3551
3552 // Make sure the triangle faces us and if so draw
3553 // Upper left triangle
3554 if (lod != (MAX_TERRAIN_LOD - 1))
3555 draw_big_square = 1;
3556 if (!upper_left && !draw_big_square)
3557 goto draw_lower_right;
3558
3559 if (lod == (MAX_TERRAIN_LOD - 1)) {
3560 slist[0] = &base[0];
3561 slist[1] = &base[1];
3562 slist[2] = &base[3];
3563 points_this_triangle = 3;
3564 } else {
3565 points_this_triangle = point_count;
3566 }
3567
3568 g3_DrawPoly(points_this_triangle, slist, TerrainLightmaps[tseg->lm_quad], MAP_TYPE_LIGHTMAP);
3569 #if (!defined(RELEASE) || defined(NEWEDITOR))
3570 if (OUTLINE_ON(OM_TERRAIN))
3571 DrawTerrainOutline(n, points_this_triangle, slist);
3572 #endif
3573 // If we're LOD'd, we've already drawn our 1 polygon. Return!
3574 if (draw_big_square)
3575 return;
3576
3577 // Now do lower right triangle
3578 draw_lower_right:
3579 if (!lower_right)
3580 return;
3581 slist[0] = &base[3];
3582 slist[1] = &base[1];
3583 slist[2] = &base[2];
3584 points_this_triangle = 3;
3585
3586 g3_DrawPoly(points_this_triangle, slist, TerrainLightmaps[tseg->lm_quad], MAP_TYPE_LIGHTMAP);
3587
3588 #if (!defined(RELEASE) || defined(NEWEDITOR))
3589 if (OUTLINE_ON(OM_TERRAIN))
3590 DrawTerrainOutline(n, points_this_triangle, slist);
3591 #endif
3592 }
3593 // Adds object obj to terrain segment n.
3594 // This object will be rendered immediately following the rendering of this
3595 // terrain segment
3596 int AddRenderObjectToTerrainSeg(int n, int objnum) {
3597 // Uses a linked list to keep track of what objects are in this segment
3598 #if (!defined(RELEASE) || defined(NEWEDITOR))
3599 if (Terrain_seg_render_objs[n] == objnum)
3600 return 0;
3601 // New object points at first in list
3602 render_next[objnum] = Terrain_seg_render_objs[n];
3603 // New object becomes first in list
3604 Terrain_seg_render_objs[n] = objnum;
3605 #endif
3606
3607 return (0);
3608 }