-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathterrainrender.cpp
2778 lines (2566 loc) · 95.7 KB
/
terrainrender.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF OUTRAGE
ENTERTAINMENT, INC. ("OUTRAGE"). OUTRAGE, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1996-2000 OUTRAGE ENTERTAINMENT, INC. ALL RIGHTS RESERVED.
*/
#ifdef NEWEDITOR
#include "neweditor\globals.h"
void RenderMine(int viewer_roomnum, int flag_automap, int called_from_terrain, bool render_all, bool outline, bool flat,
prim *prim);
#endif
#include "terrain.h"
#include "grdefs.h"
#include "3d.h"
#include "pstypes.h"
#include "pserror.h"
#include "renderer.h"
#include "gametexture.h"
#include "descent.h"
#include "render.h"
#include "game.h"
#include "texture.h"
#include "ddio.h"
#include "polymodel.h"
#include "lighting.h"
#include "vecmat.h"
#include "renderobject.h"
#include "FindIntersection.h"
#include "weapon.h"
#include "weather.h"
#include "viseffect.h"
#ifdef EDITOR
#include "editor\d3edit.h"
#endif
#include "fireball.h"
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "gameloop.h"
#include "postrender.h"
#include "macros.h"
#include "psrand.h"
#include "player.h"
#define TERRAIN_PERSPECTIVE_TEXTURE_DEPTH 1 * TERRAIN_SIZE
#define LOD_ROW_SIZE (MAX_LOD_SIZE + 1)
int DrawTerrainTrianglesSoftware(int index, int bm_handle, int upper_left, int lower_right);
int DrawTerrainTrianglesHardware(int index, int bm_handle, int upper_left, int lower_right);
int DrawTerrainTrianglesHardwareNoLight(int index, int bm_handle, int upper_left, int lower_right);
void DrawTerrainLightmapsHardware(int index, int upper_left, int lower_right);
void DrawSky(vector *veye, matrix *vorient);
function_mode View_mode;
int ManageFramerate = 0;
int MinAllowableFramerate = 15;
ubyte Fast_terrain = 1;
float Far_fog_border;
vector Terrain_viewer_eye;
ubyte Terrain_from_mine = 0;
ubyte Show_invisible_terrain = 0;
int Terrain_objects_drawn = 0;
vector Last_frame_stars[MAX_STARS];
float Terrain_texture_distance = DEFAULT_TEXTURE_DISTANCE;
int Check_terrain_portal = 0;
static vector Temp_sky_vectors[MAX_HORIZON_PIECES][6];
// Last time terrain was rendered
float Last_terrain_render_time = -1;
// Sets UV's based on 90 degree rotations
float TerrainUSpeedup[4][LOD_ROW_SIZE * LOD_ROW_SIZE];
float TerrainVSpeedup[4][LOD_ROW_SIZE * LOD_ROW_SIZE];
#if (!defined(RELEASE) || defined(NEWEDITOR))
// for building a render list for each terrain cell
int render_next[MAX_OBJECTS];
#endif
void DrawPlayerOnWireframe();
float Clip_scale_left, Clip_scale_top, Clip_scale_right, Clip_scale_bot;
#ifdef NEWEDITOR
bool Rendering_main_view = true;
#endif
void InitTerrainRenderSpeedups() {
// Figure out a table of values for rotated uv points
for (int y = 0; y <= MAX_LOD_SIZE; y++) {
for (int x = 0; x <= MAX_LOD_SIZE; x++) {
TerrainUSpeedup[0][(y * LOD_ROW_SIZE) + x] = (float)x / (float)MAX_LOD_SIZE;
TerrainVSpeedup[0][(y * LOD_ROW_SIZE) + x] = (float)y / (float)MAX_LOD_SIZE;
TerrainUSpeedup[1][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)y / (float)MAX_LOD_SIZE);
TerrainVSpeedup[1][(y * LOD_ROW_SIZE) + x] = (float)x / (float)MAX_LOD_SIZE;
TerrainUSpeedup[2][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)x / (float)MAX_LOD_SIZE);
TerrainVSpeedup[2][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)y / (float)MAX_LOD_SIZE);
TerrainUSpeedup[3][(y * LOD_ROW_SIZE) + x] = ((float)y / (float)MAX_LOD_SIZE);
TerrainVSpeedup[3][(y * LOD_ROW_SIZE) + x] = 1.0 - ((float)x / (float)MAX_LOD_SIZE);
}
}
}
// codes a point for visibility in the window passed to RenderTerrain()
ubyte CodeTerrainPoint(g3Point *p) {
ubyte cc = 0;
if (p->p3_sx > Clip_scale_right)
cc |= CC_OFF_RIGHT;
if (p->p3_sx < Clip_scale_left)
cc |= CC_OFF_LEFT;
if (p->p3_sy > Clip_scale_bot)
cc |= CC_OFF_BOT;
if (p->p3_sy < Clip_scale_top)
cc |= CC_OFF_TOP;
return cc;
}
// Returns true if light can hit this segment/heighth bit
int IsTerrainDynamicChecked(int seg, int bit) {
if (seg < 0 || seg >= (TERRAIN_WIDTH * TERRAIN_DEPTH))
return 1;
if (bit >= 8)
return 1;
ubyte val = Terrain_dynamic_table[seg] & (1 << bit);
if (val)
return 1;
return 0;
}
// Gets the dynamic light value for this position
float GetTerrainDynamicScalar(vector *pos, int seg) {
float cube_values[10];
int y_increment = MAX_TERRAIN_HEIGHT / 8;
int y_int = pos->y / y_increment;
int x_int = pos->x / TERRAIN_SIZE;
int z_int = pos->z / TERRAIN_SIZE;
float x_norm = (pos->x / TERRAIN_SIZE) - x_int;
float z_norm = (pos->z / TERRAIN_SIZE) - z_int;
float y_norm = (pos->y / y_increment) - y_int;
if (y_norm < 0) {
y_norm = 0;
y_int = 0;
}
if (y_norm > 1) {
y_norm = 1.0;
y_int = 7;
}
if (x_norm < 0 || x_norm > 1 || z_norm < 0 || z_norm > 1)
return .5;
float left_norm, right_norm, top_norm, bottom_norm, scalar;
cube_values[0] = IsTerrainDynamicChecked(seg, y_int);
cube_values[1] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH, y_int);
cube_values[2] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH + 1, y_int);
cube_values[3] = IsTerrainDynamicChecked(seg + 1, y_int);
cube_values[4] = IsTerrainDynamicChecked(seg, y_int + 1);
cube_values[5] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH, y_int + 1);
cube_values[6] = IsTerrainDynamicChecked(seg + TERRAIN_WIDTH + 1, y_int + 1);
cube_values[7] = IsTerrainDynamicChecked(seg + 1, y_int + 1);
left_norm = ((1 - z_norm) * cube_values[0]) + (z_norm * cube_values[1]);
right_norm = ((1 - z_norm) * cube_values[3]) + (z_norm * cube_values[2]);
bottom_norm = ((1 - x_norm) * left_norm) + (x_norm * right_norm);
left_norm = ((1 - z_norm) * cube_values[4]) + (z_norm * cube_values[5]);
right_norm = ((1 - z_norm) * cube_values[7]) + (z_norm * cube_values[6]);
top_norm = ((1 - x_norm) * left_norm) + (x_norm * right_norm);
scalar = ((1 - y_norm) * bottom_norm) + (y_norm * top_norm);
ASSERT(scalar >= 0 && scalar <= 1);
return scalar;
}
// Takes a min,max vector and makes a surrounding cube from it
void MakePointsFromMinMax(vector *corners, vector *minp, vector *maxp);
typedef struct {
int objnum;
float dist;
int vis_effect;
} obj_sort_item;
// Compare function for room face sort
static int obj_sort_func(const obj_sort_item *a, const obj_sort_item *b) {
if (a->dist < b->dist)
return -1;
else if (a->dist > b->dist)
return 1;
else
return 0;
}
// Returns true if the object is outside of our terrain portal
int ObjectOutOfPortal(object *obj) {
g3Point pnt1, pnt2;
ubyte anded = 0xff;
g3_RotatePoint(&pnt1, &obj->min_xyz);
if (pnt1.p3_codes & CC_BEHIND)
return 0;
g3_ProjectPoint(&pnt1);
anded &= CodeTerrainPoint(&pnt1);
g3_RotatePoint(&pnt2, &obj->max_xyz);
if (pnt2.p3_codes & CC_BEHIND)
return 0;
g3_ProjectPoint(&pnt2);
anded &= CodeTerrainPoint(&pnt2);
if (anded)
return 1;
return 0;
}
obj_sort_item objs_to_render[MAX_OBJECTS + MAX_VIS_EFFECTS];
obj_sort_item rooms_to_render[MAX_ROOMS];
// Checks to see if this object can even be seen from our current viewpoint
// By shooting rays to it
// Returns true if any of the rays hit
int ShootRaysToObject(object *obj) {
vector corners[8];
if (obj->type == OBJ_ROOM) {
room *rp = &Rooms[obj->id];
MakePointsFromMinMax(corners, &rp->min_xyz, &rp->max_xyz);
} else {
MakePointsFromMinMax(corners, &obj->min_xyz, &obj->max_xyz);
}
fvi_info hit_info;
fvi_query fq;
fq.p0 = &Viewer_object->pos;
fq.startroom = Viewer_object->roomnum;
fq.rad = 0.0f;
fq.flags = FQ_NO_RELINK | FQ_EXTERNAL_ROOMS_AS_SPHERE | FQ_IGNORE_EXTERNAL_ROOMS;
fq.thisobjnum = Viewer_object - Objects;
fq.ignore_obj_list = NULL;
for (int i = 0; i < 8; i++) {
fq.p1 = &corners[i];
int fate = fvi_FindIntersection(&fq, &hit_info);
/*g3Point pnt1,pnt2;
vector fpnt = *fq.p0 + 3.0 * Viewer_object->orient.fvec;
g3_RotatePoint (&pnt1,&fpnt);
g3_RotatePoint (&pnt2,&hit_info.hit_pnt);
g3_DrawLine ((GR_RGB(255,255,255)),&pnt1,&pnt2);*/
if (fate == HIT_NONE)
return 1;
}
return 0;
}
// Returns true if the external room is in the view cone
// Else returns false
bool ExternalRoomVisible(room *rp, vector *center, float *zdist) {
ASSERT(rp->flags & RF_EXTERNAL);
g3Point pnt;
ubyte ccode;
vector corners[8];
g3_RotatePoint(&pnt, center);
*zdist = pnt.p3_z;
MakePointsFromMinMax(corners, &rp->min_xyz, &rp->max_xyz);
ubyte and = 0xff;
for (int t = 0; t < 8; t++) {
g3_RotatePoint(&pnt, &corners[t]);
ccode = g3_CodePoint(&pnt);
if (!ccode)
return true;
and &= ccode;
}
if (and)
return false;
return true;
}
// Render all the rooms out on the terrain for this frame
void RenderTerrainRooms() {
object *obj;
#ifdef EDITOR
if (!Terrain_render_ext_room_objs)
return;
#endif
int room_count = 0;
float zdist;
int use_occlusion = 0;
int src_occlusion_index;
int i;
if (Terrain_from_mine)
return;
if ((Terrain_checksum + 1) == Terrain_occlusion_checksum) {
use_occlusion = 1;
int oz = (Viewer_object->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
int ox = (Viewer_object->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
if (oz < 0 || oz >= OCCLUSION_SIZE || ox < 0 || ox >= OCCLUSION_SIZE)
use_occlusion = 0;
src_occlusion_index = oz * OCCLUSION_SIZE + ox;
}
for (i = 0; i <= Highest_object_index; i++) {
obj = &Objects[i];
if (obj->type != OBJ_ROOM)
continue;
if (obj->flags & OF_DEAD)
continue;
if (obj->render_type == RT_NONE)
continue;
if (!OBJECT_OUTSIDE(obj))
continue;
float size = obj->size;
if (use_occlusion) {
int y1 = (obj->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
int x1 = (obj->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
int dest_occlusion_index = (y1 * OCCLUSION_SIZE);
dest_occlusion_index += x1;
int occ_byte = dest_occlusion_index / 8;
int occ_bit = dest_occlusion_index % 8;
if (obj->pos.y < MAX_TERRAIN_HEIGHT) {
if (!(Terrain_occlusion_map[src_occlusion_index][occ_byte] & (1 << occ_bit)))
continue;
}
}
if (!ExternalRoomVisible(&Rooms[obj->id], &obj->pos, &zdist))
continue;
if (!IsPointVisible(&obj->pos, size, &zdist))
continue;
if (Check_terrain_portal && ObjectOutOfPortal(obj))
continue;
/*if (!Terrain_from_mine)
{
if (!ShootRaysToObject (obj))
continue;
}*/
rooms_to_render[room_count].vis_effect = 0;
rooms_to_render[room_count].objnum = obj - Objects;
rooms_to_render[room_count].dist = zdist;
room_count++;
}
// Sort and draw rooms
qsort(rooms_to_render, room_count, sizeof(*rooms_to_render), (int (*)(const void *, const void *))obj_sort_func);
for (i = room_count - 1; i >= 0; i--) {
int objnum = rooms_to_render[i].objnum;
object *obj = &Objects[objnum];
#ifndef NEWEDITOR
RenderMine(obj->id, 0, 1);
#else
RenderMine(obj->id, 0, 1, 1, 0, 0, NULL);
#endif
// Draw a surrounding sphere
#if (defined(_DEBUG) && !defined(NEWEDITOR))
if (Game_show_sphere == 2)
DrawDebugInfo(obj);
#endif
}
}
// Renders every visible terrain object
void RenderAllTerrainObjects() {
object *obj;
int snows[500];
int num_snows = 0;
int obj_count = 0;
float zdist;
int use_occlusion = 0;
int src_occlusion_index;
int i;
if ((Terrain_checksum + 1) == Terrain_occlusion_checksum) {
use_occlusion = 1;
int oz = (Viewer_object->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
int ox = (Viewer_object->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
if (oz < 0 || oz >= OCCLUSION_SIZE || ox < 0 || ox >= OCCLUSION_SIZE)
use_occlusion = 0;
src_occlusion_index = oz * OCCLUSION_SIZE + ox;
}
for (i = 0; i <= Highest_object_index; i++) {
obj = &Objects[i];
if (obj == Viewer_object)
continue;
// Don't draw piggybacked objects
if (Viewer_object->type == OBJ_OBSERVER && i == Players[Viewer_object->id].piggy_objnum)
continue;
if (obj->type == OBJ_ROOM)
continue;
if (obj->type == OBJ_NONE)
continue;
if (obj->flags & OF_DEAD)
continue;
if (obj->render_type == RT_NONE)
continue;
if (!OBJECT_OUTSIDE(obj))
continue;
float size = obj->size;
// Special case weapons with streamers
if (obj->type == OBJ_WEAPON && (Weapons[obj->id].flags & WF_STREAMER))
size = Weapons[obj->id].phys_info.velocity.z;
if (use_occlusion) {
int y1 = (obj->pos.z / TERRAIN_SIZE) / OCCLUSION_SIZE;
int x1 = (obj->pos.x / TERRAIN_SIZE) / OCCLUSION_SIZE;
int dest_occlusion_index = (y1 * OCCLUSION_SIZE);
dest_occlusion_index += x1;
int occ_byte = dest_occlusion_index / 8;
int occ_bit = dest_occlusion_index % 8;
if (obj->pos.y + obj->size < MAX_TERRAIN_HEIGHT) {
if (!(Terrain_occlusion_map[src_occlusion_index][occ_byte] & (1 << occ_bit)))
continue;
}
}
if (obj->type == OBJ_WEAPON && Weapons[obj->id].flags & WF_ELECTRICAL) {
// Automatically render all electrical objects
zdist = 0;
} else {
if (!IsPointVisible(&obj->pos, size, &zdist))
continue;
if (Check_terrain_portal && ObjectOutOfPortal(obj))
continue;
}
if (UseHardware) {
Postrender_list[Num_postrenders].type = PRT_OBJECT;
Postrender_list[Num_postrenders].z = zdist;
Postrender_list[Num_postrenders++].objnum = obj - Objects;
} else {
objs_to_render[obj_count].vis_effect = 0;
objs_to_render[obj_count].objnum = obj - Objects;
objs_to_render[obj_count].dist = zdist;
obj_count++;
}
}
#ifndef NEWEDITOR
for (i = 0; i <= Highest_vis_effect_index; i++) {
vis_effect *vis = &VisEffects[i];
if (vis->type == VIS_NONE)
continue;
if (vis->flags & VF_DEAD)
continue;
if (!ROOMNUM_OUTSIDE(vis->roomnum))
continue;
// Special case snow
if (vis->id == SNOWFLAKE_INDEX) {
snows[num_snows] = vis - VisEffects;
num_snows++;
} else {
if ((vis->flags & VF_WINDSHIELD_EFFECT) || IsPointVisible(&vis->pos, vis->size, &zdist)) {
if (vis->flags & VF_WINDSHIELD_EFFECT)
zdist = 0;
if (UseHardware) {
Postrender_list[Num_postrenders].type = PRT_VISEFFECT;
Postrender_list[Num_postrenders].z = zdist;
Postrender_list[Num_postrenders++].objnum = vis - VisEffects;
} else {
objs_to_render[obj_count].vis_effect = 1;
objs_to_render[obj_count].objnum = vis - VisEffects;
objs_to_render[obj_count].dist = zdist;
obj_count++;
}
}
}
}
#endif
// Sort objects
qsort(objs_to_render, obj_count, sizeof(*objs_to_render), (int (*)(const void *, const void *))obj_sort_func);
// Render the objects
for (i = obj_count - 1; i >= 0; i--) {
int vis_effect = objs_to_render[i].vis_effect;
int objnum = objs_to_render[i].objnum;
if (vis_effect)
DrawVisEffect(&VisEffects[objnum]);
else
RenderObject(&Objects[objnum]);
}
// Render snows
rend_SetZBufferWriteMask(0);
for (i = 0; i < num_snows; i++)
DrawVisEffect(&VisEffects[snows[i]]);
rend_SetZBufferWriteMask(1);
rend_SetZBufferState(1);
rend_SetWrapType(WT_WRAP);
}
#define FOG_LAYER_HEIGHT (TERRAIN_HEIGHT_INCREMENT * 30.5f)
// Draws a flat fog layer
void DrawFogLayer() {
vector worldvec[4];
g3Point pnt[4], *pntlist[6];
rend_SetFlatColor(GR_RGB(132, 132, 255));
rend_SetAlphaValue(64);
rend_SetAlphaType(AT_CONSTANT);
rend_SetTextureType(TT_FLAT);
rend_SetLighting(LS_NONE);
worldvec[0].x = 0;
worldvec[0].y = FOG_LAYER_HEIGHT;
worldvec[0].z = 0;
worldvec[1].x = 0;
worldvec[1].y = FOG_LAYER_HEIGHT;
worldvec[1].z = TERRAIN_DEPTH * TERRAIN_SIZE;
worldvec[2].x = TERRAIN_WIDTH * TERRAIN_SIZE;
worldvec[2].y = FOG_LAYER_HEIGHT;
worldvec[2].z = TERRAIN_DEPTH * TERRAIN_SIZE;
worldvec[3].x = TERRAIN_WIDTH * TERRAIN_SIZE;
worldvec[3].y = FOG_LAYER_HEIGHT;
worldvec[3].z = 0;
for (int i = 0; i < 4; i++) {
g3_RotatePoint(&pnt[i], &worldvec[i]);
pntlist[i] = &pnt[i];
}
g3_DrawPoly(4, pntlist, 0);
}
#define CLOUD_LAYER_HEIGHT (TERRAIN_HEIGHT_INCREMENT * 320.0f)
// Draws a flat fog layer
void DrawCloudLayer() {
vector worldvec[4];
g3Point pnt[4], *pntlist[6];
rend_SetFlatColor(GR_RGB(192, 192, 255));
rend_SetAlphaValue(32);
rend_SetAlphaType(AT_CONSTANT);
rend_SetTextureType(TT_FLAT);
rend_SetLighting(LS_NONE);
worldvec[0].x = -(TERRAIN_SIZE * 100);
worldvec[0].y = CLOUD_LAYER_HEIGHT;
worldvec[0].z = (TERRAIN_SIZE * (100 + TERRAIN_DEPTH));
worldvec[1].x = -(TERRAIN_SIZE * 100);
worldvec[1].y = CLOUD_LAYER_HEIGHT;
worldvec[1].z = -(TERRAIN_SIZE * (100));
worldvec[2].x = (TERRAIN_SIZE * (100 + TERRAIN_WIDTH));
worldvec[2].y = CLOUD_LAYER_HEIGHT;
worldvec[2].z = -(TERRAIN_SIZE * (100));
worldvec[3].x = (TERRAIN_SIZE * (100 + TERRAIN_WIDTH));
worldvec[3].y = CLOUD_LAYER_HEIGHT;
worldvec[3].z = (TERRAIN_SIZE * (100 + TERRAIN_DEPTH));
for (int i = 0; i < 4; i++) {
g3_RotatePoint(&pnt[i], &worldvec[i]);
pntlist[i] = &pnt[i];
}
g3_DrawPoly(4, pntlist, 0);
}
// left,top,right,bot are optional parameters. Omiting them (or setting them to -1) will
// render to the whole screen. Passing valid values will only render tiles visible in the
// specified window (though it won't clip those tiles to the window)
void RenderTerrain(ubyte from_mine, int left, int top, int right, int bot) {
int nt;
int render_width, render_height;
float w2, h2;
vector viewer_eye;
matrix viewer_orient;
static first = 1;
if (first) {
InitTerrainRenderSpeedups();
first = 0;
}
// Get the size of the current render window
rend_GetProjectionParameters(&render_width, &render_height);
w2 = ((float)render_width - 1) / 2.0;
h2 = ((float)render_height - 1) / 2.0;
// Set up vars for (psuedo-)clipping window
if (left < 0) {
Check_terrain_portal = 0;
left = 0;
} else {
int w = right - left;
int h = bot - top;
// If the portal takes up more than 50% the screen space then we don't check against it
float threshold = (render_width * render_height) * .5;
if (w * h > threshold)
Check_terrain_portal = 0;
else
Check_terrain_portal = 1;
}
if (top < 0)
top = 0;
if ((right == -1) || right > render_width)
right = render_width - 1;
if ((bot == -1) || bot > render_height)
bot = render_height - 1;
Clip_scale_left = left;
Clip_scale_right = right;
Clip_scale_top = top;
Clip_scale_bot = bot;
if (!Terrain_sky.textured) {
rend_FillRect(Terrain_sky.sky_color, left, top, right + 1, bot + 1);
}
rend_SetFlatColor(Terrain_sky.sky_color);
View_mode = GetFunctionMode();
// Get the viewer position & orientation
g3_GetViewPosition(&viewer_eye);
g3_GetViewMatrix(&viewer_orient);
// Set this so we don't do reentrant rendering between terrain/mine
Terrain_from_mine = from_mine;
#ifndef NEWEDITOR
// See if we're supposed change the fog plane distance based on our framerate
if (View_mode != EDITOR_MODE && ManageFramerate) {
float fps = GetFPS();
if (fps < MinAllowableFramerate) {
// if (Detail_settings.Terrain_render_distance>20*TERRAIN_SIZE)
// DAJ Detail_settings.Terrain_render_distance-=(float)(TERRAIN_SIZE/10);
if (Detail_settings.Terrain_render_distance > TERRAIN_SIZE)
Detail_settings.Terrain_render_distance -= (float)(TERRAIN_SIZE / 4);
} else if (fps > MinAllowableFramerate + 1) {
if (Detail_settings.Terrain_render_distance < 60 * TERRAIN_SIZE)
// DAJ Detail_settings.Terrain_render_distance+=(float)(TERRAIN_SIZE/10);
Detail_settings.Terrain_render_distance += (float)(TERRAIN_SIZE / 4);
}
}
#endif
#ifndef NEWEDITOR
VisibleTerrainZ = (Detail_settings.Terrain_render_distance) * Matrix_scale.z;
#else
VisibleTerrainZ = (1200.0f) * Matrix_scale.z;
#endif
Far_fog_border = VisibleTerrainZ;
// Set up our z wall
g3_SetFarClipZ(VisibleTerrainZ);
// Get all of the cells visible to us
nt = GetVisibleTerrain(&viewer_eye, &viewer_orient);
// Set this to really far away so our sky can render
g3_SetFarClipZ(60000);
rend_SetFogState(0);
// Draw the sky
DrawSky(&viewer_eye, &viewer_orient);
//// Set up our z wall
rend_SetZBufferState(1);
rend_SetZBufferWriteMask(1);
g3_SetFarClipZ(VisibleTerrainZ);
// rend_SetZValues (0,VisibleTerrainZ);
#ifndef NEWEDITOR
if ((Terrain_sky.flags & TF_FOG) && (UseHardware || (!UseHardware && Lighting_on))) {
rend_SetZValues(0, VisibleTerrainZ);
rend_SetFogState(1);
rend_SetFogBorders(VisibleTerrainZ * Terrain_sky.fog_scalar, Far_fog_border);
rend_SetFogColor(Terrain_sky.fog_color);
} else
#endif
rend_SetZValues(0, 5000);
// And display!
if (nt > 0)
DisplayTerrainList(nt);
// Draw rooms
RenderTerrainRooms();
// Show objects
if (nt < 1 || UseHardware) {
RenderAllTerrainObjects();
// rend_SetFogState (0);
// DrawFogLayer();
}
// rend_SetZValues (0,3000);
// g3_SetFarClipZ (3000);
mprintf_at((2, 5, 0, "Objs Drawn=%5d", Terrain_objects_drawn));
Last_terrain_render_time = Gametime;
}
// Draws a segment of lightning that is always facing you
// Vectors are in world coords
void DrawLightningSegment(vector *from, vector *to) {
vector src_vecs[2], world_vecs[6];
g3Point rot_src_pnts[2], world_points[6], *pntlist[6];
static alphas[] = {.3f, 1.0, .3f, .3f, 1.0, .3f};
src_vecs[0] = *from;
src_vecs[1] = *to;
g3_RotatePoint(&rot_src_pnts[0], &src_vecs[0]);
g3_RotatePoint(&rot_src_pnts[1], &src_vecs[1]);
if (rot_src_pnts[0].p3_codes & rot_src_pnts[1].p3_codes)
return; // Don't draw because both points are off screen
vector rvec = Viewer_object->orient.rvec * 10;
// Put all points so that they face the viewer
world_vecs[0] = src_vecs[0] - rvec;
world_vecs[1] = src_vecs[0];
world_vecs[2] = src_vecs[0] + rvec;
world_vecs[3] = src_vecs[1] + rvec;
world_vecs[4] = src_vecs[1];
world_vecs[5] = src_vecs[1] - rvec;
for (int i = 0; i < 6; i++) {
g3_RotatePoint(&world_points[i], &world_vecs[i]);
world_points[i].p3_flags = PF_RGBA;
world_points[i].p3_r = .2f;
world_points[i].p3_g = .4f;
world_points[i].p3_b = 1.0f;
world_points[i].p3_a = alphas[i];
pntlist[i] = &world_points[i];
}
rend_SetTextureType(TT_FLAT);
rend_SetLighting(LS_GOURAUD);
rend_SetAlphaType(AT_SATURATE_VERTEX);
rend_SetColorModel(CM_RGB);
g3_ProjectPoint(&world_points[1]);
g3_ProjectPoint(&world_points[4]);
rend_DrawSpecialLine(&world_points[1], &world_points[4]);
g3_DrawPoly(6, pntlist, 0);
}
#define PUSH_LIGHTNING_TREE(f, l, sp) \
{ \
froms[si] = f; \
stack_level[si] = l; \
splits[si] = sp; \
si++; \
}
#define POP_LIGHTNING_TREE() \
{ \
si--; \
cur_from = froms[si]; \
level = stack_level[si]; \
cur_splits = splits[si]; \
}
// Draws an entire strip of lightning
void DrawLightning() {
angvec player_angs;
matrix mat;
vector froms[50];
int si = 0, level;
int stack_level[50];
int splits[50];
int cur_splits = 0;
int new_heading;
float scalar;
scalar = ((ps_rand() % 1000) - 500) / 500.0;
scalar *= 15000;
vm_ExtractAnglesFromMatrix(&player_angs, &Viewer_object->orient);
new_heading = (player_angs.h + (int)scalar) % 65536;
vm_AnglesToMatrix(&mat, 0, new_heading, 0);
// Put the starting point way up in the air
float ylimit = (-(Viewer_object->pos.y * 2)) + (ps_rand() % 400);
vector cur_from = Viewer_object->pos + (mat.fvec * 4000);
vector new_vec;
cur_from.y += 800.0f;
cur_from.y += (ps_rand() % 100);
// Set some states
rend_SetAlphaType(AT_SATURATE_TEXTURE);
rend_SetAlphaValue(.5 * 255);
rend_SetLighting(LS_NONE);
int bm_handle;
// See if we should drawn an origin bitmap
if (ps_rand() % 3) {
// Pick an origin bitmap
if (ps_rand() % 2)
bm_handle = Fireballs[LIGHTNING_ORIGIN_INDEXA].bm_handle;
else
bm_handle = Fireballs[LIGHTNING_ORIGIN_INDEXB].bm_handle;
// Draw the origin bitmap
int size = 300 + (ps_rand() % 200);
g3_DrawRotatedBitmap(&cur_from, 0, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
}
PUSH_LIGHTNING_TREE(cur_from, 0, 0)
while (si > 0) {
POP_LIGHTNING_TREE()
ASSERT(level < 50);
float x_adjust = ((ps_rand() % 200) - 100) / 100.0;
float y_adjust = .3 + ((ps_rand() % 100) / 100.0);
new_vec = cur_from;
new_vec += x_adjust * (mat.rvec * 70);
new_vec -= y_adjust * (mat.uvec * 100);
DrawLightningSegment(&cur_from, &new_vec);
if (cur_from.y < ylimit) // We're close to the ground, so just bail!
continue;
if ((ps_rand() % ((level * level * 20) + 8)) == 0 && cur_splits < 2) {
// Make this branch split
PUSH_LIGHTNING_TREE(new_vec, level, cur_splits + 1)
PUSH_LIGHTNING_TREE(new_vec, level, cur_splits + 1)
} else {
PUSH_LIGHTNING_TREE(new_vec, level, cur_splits)
}
}
}
// Draws a lightning sky
void DrawLightningSky() {
int t, k, tw;
float r, g, b;
g3Point pnt[4], *pntlist[6];
rend_SetTextureType(TT_FLAT);
rend_SetColorModel(CM_RGB);
rend_SetLighting(LS_GOURAUD);
rend_SetAlphaType(AT_SATURATE_VERTEX);
// figure out colors for sky
r = .8f;
g = .8f;
b = 1.0;
// Draw top part
for (t = 0; t < MAX_HORIZON_PIECES; t++) {
tw = (t + 1) % MAX_HORIZON_PIECES;
pnt[0].p3_vec = Temp_sky_vectors[t][0];
pnt[1].p3_vec = Temp_sky_vectors[tw][1];
pnt[2].p3_vec = Temp_sky_vectors[t][1];
for (k = 0; k < 3; k++) {
g3Point *p = &pnt[k];
p->p3_flags = PF_RGBA;
g3_CodePoint(p);
p->p3_a = .3f;
p->p3_r = r;
p->p3_g = g;
p->p3_b = b;
}
pntlist[0] = &pnt[0];
pntlist[1] = &pnt[1];
pntlist[2] = &pnt[2];
g3_DrawPoly(3, pntlist, 0);
}
// Draw bottom part
for (int i = 1; i < 5; i++) {
for (t = 0; t < MAX_HORIZON_PIECES; t++) {
tw = (t + 1) % MAX_HORIZON_PIECES;
pnt[0].p3_vec = Temp_sky_vectors[t][i];
pnt[1].p3_vec = Temp_sky_vectors[tw][i];
pnt[2].p3_vec = Temp_sky_vectors[tw][i + 1];
pnt[3].p3_vec = Temp_sky_vectors[t][i + 1];
for (k = 0; k < 4; k++) {
g3Point *p = &pnt[k];
p->p3_flags = PF_RGBA;
g3_CodePoint(p);
p->p3_a = .3f;
p->p3_r = r;
p->p3_g = g;
p->p3_b = b;
pntlist[k] = p;
}
g3_DrawPoly(4, pntlist, 0);
}
}
}
// Draws the gouraud sky
void DrawGouraudSky() {
int t, k, tw;
float sr, sg, sb, hr, hg, hb;
g3Point pnt[4], *pntlist[6];
g3UVL uvls[10];
if (Terrain_sky.sky_color == Terrain_sky.horizon_color)
return; // No sense in drawing anything
rend_SetTextureType(TT_FLAT);
rend_SetColorModel(CM_RGB);
rend_SetAlphaType(AT_ALWAYS);
// figure out colors for sky
sr = (float)GR_COLOR_RED(Terrain_sky.sky_color) / 255.0;
sg = (float)GR_COLOR_GREEN(Terrain_sky.sky_color) / 255.0;
sb = (float)GR_COLOR_BLUE(Terrain_sky.sky_color) / 255.0;
hr = (float)GR_COLOR_RED(Terrain_sky.horizon_color) / 255.0;
hg = (float)GR_COLOR_GREEN(Terrain_sky.horizon_color) / 255.0;
hb = (float)GR_COLOR_BLUE(Terrain_sky.horizon_color) / 255.0;
for (t = 0; t < MAX_HORIZON_PIECES; t++) {
tw = (t + 1) % MAX_HORIZON_PIECES;
pnt[0].p3_vec = Temp_sky_vectors[t][4];
uvls[0].r = sr;
uvls[0].g = sg;
uvls[0].b = sb;
pnt[1].p3_vec = Temp_sky_vectors[tw][4];
uvls[1].r = sr;
uvls[1].g = sg;
uvls[1].b = sb;
pnt[2].p3_vec = Temp_sky_vectors[tw][5];
uvls[2].r = hr;
uvls[2].g = hg;
uvls[2].b = hb;
pnt[3].p3_vec = Temp_sky_vectors[t][5];
uvls[3].r = hr;
uvls[3].g = hg;
uvls[3].b = hb;
for (k = 0; k < 4; k++) {
g3Point *p = &pnt[k];
p->p3_flags = PF_RGBA;
g3_CodePoint(p);
pntlist[k] = p;
p->p3_uvl = uvls[k];
}
g3_DrawPoly(4, pntlist, 0);
}
}
// Draws the sky textures
void DrawTexturedSky() {
int t, k, tw;
float sr, sg, sb, hr, hg, hb;
// Change terrain sky if needed
int dome_bm = GetTextureBitmap(Terrain_sky.dome_texture, 0);
g3Point pnt[6], *pntlist[6];
g3UVL uvls[10];
rend_SetWrapType(WT_WRAP);
rend_SetTextureType(TT_PERSPECTIVE);
rend_SetColorModel(CM_MONO);
rend_SetAlphaType(ATF_TEXTURE);
g3_SetTriangulationTest(1);
// Draw top part
for (t = 0; t < MAX_HORIZON_PIECES; t++) {
tw = (t + 1) % MAX_HORIZON_PIECES;
pnt[0].p3_vec = Temp_sky_vectors[t][0];
uvls[0].u = Terrain_sky.horizon_u[t][0];
uvls[0].v = Terrain_sky.horizon_v[t][0];
pnt[1].p3_vec = Temp_sky_vectors[tw][1];
uvls[1].u = Terrain_sky.horizon_u[tw][1];
uvls[1].v = Terrain_sky.horizon_v[tw][1];
pnt[2].p3_vec = Temp_sky_vectors[t][1];
uvls[2].u = Terrain_sky.horizon_u[t][1];
uvls[2].v = Terrain_sky.horizon_v[t][1];
for (k = 0; k < 3; k++) {
g3Point *p = &pnt[k];
p->p3_flags = PF_UV + PF_L;
g3_CodePoint(p);
p->p3_uvl = uvls[k];
p->p3_l = 1;
}
#if (defined(EDITOR) || defined(NEWEDITOR))
ddgr_color oldcolor;
if (TSearch_on) {
rend_SetPixel(GR_RGB(0, 255, 0), TSearch_x, TSearch_y);
oldcolor = rend_GetPixel(TSearch_x, TSearch_y); // will be different in 15/16-bit color
}
#endif
pntlist[0] = &pnt[0];
pntlist[1] = &pnt[1];
pntlist[2] = &pnt[2];
g3_DrawPoly(3, pntlist, dome_bm);
#if (defined(EDITOR) || defined(NEWEDITOR))
if (TSearch_on) {
ddgr_color newcolor = rend_GetPixel(TSearch_x, TSearch_y);
if (newcolor != oldcolor) {
TSearch_seg = t;
TSearch_found_type = TSEARCH_FOUND_SKY_DOME;
}
}
#endif
}
// Draw bottom part
for (int i = 1; i < 4; i++) {
for (t = 0; t < MAX_HORIZON_PIECES; t++) {
tw = (t + 1) % MAX_HORIZON_PIECES;
pnt[0].p3_vec = Temp_sky_vectors[t][i];
uvls[0].u = Terrain_sky.horizon_u[t][i];
uvls[0].v = Terrain_sky.horizon_v[t][i];
pnt[1].p3_vec = Temp_sky_vectors[tw][i];
uvls[1].u = Terrain_sky.horizon_u[tw][i];
uvls[1].v = Terrain_sky.horizon_v[tw][i];
pnt[2].p3_vec = Temp_sky_vectors[tw][i + 1];
uvls[2].u = Terrain_sky.horizon_u[tw][i + 1];
uvls[2].v = Terrain_sky.horizon_v[tw][i + 1];
pnt[3].p3_vec = Temp_sky_vectors[t][i + 1];
uvls[3].u = Terrain_sky.horizon_u[t][i + 1];
uvls[3].v = Terrain_sky.horizon_v[t][i + 1];
for (k = 0; k < 4; k++) {
g3Point *p = &pnt[k];
p->p3_flags = PF_UV + PF_L;
g3_CodePoint(p);
pntlist[k] = p;
p->p3_uvl = uvls[k];
p->p3_l = 1;
}
#if (defined(EDITOR) || defined(NEWEDITOR))
ddgr_color oldcolor;
if (TSearch_on) {
rend_SetPixel(GR_RGB(0, 255, 0), TSearch_x, TSearch_y);
oldcolor = rend_GetPixel(TSearch_x, TSearch_y); // will be different in 15/16-bit color
}
#endif
g3_DrawPoly(4, pntlist, dome_bm);
#if (defined(EDITOR) || defined(NEWEDITOR))
if (TSearch_on) {
ddgr_color newcolor = rend_GetPixel(TSearch_x, TSearch_y);
if (newcolor != oldcolor) {
TSearch_seg = t;
TSearch_found_type = TSEARCH_FOUND_SKY_DOME;
}
}
#endif
}
}
// Now draw band
rend_SetTextureType(TT_FLAT);
rend_SetColorModel(CM_RGB);
rend_SetAlphaType(AT_ALWAYS);
// figure out colors for sky
sr = (float)GR_COLOR_RED(Terrain_sky.sky_color) / 255.0;
sg = (float)GR_COLOR_GREEN(Terrain_sky.sky_color) / 255.0;
sb = (float)GR_COLOR_BLUE(Terrain_sky.sky_color) / 255.0;
hr = (float)GR_COLOR_RED(Terrain_sky.horizon_color) / 255.0;
hg = (float)GR_COLOR_GREEN(Terrain_sky.horizon_color) / 255.0;
hb = (float)GR_COLOR_BLUE(Terrain_sky.horizon_color) / 255.0;
for (t = 0; t < MAX_HORIZON_PIECES; t++) {
tw = (t + 1) % MAX_HORIZON_PIECES;
pnt[0].p3_vec = Temp_sky_vectors[t][4];
uvls[0].r = sr;
uvls[0].g = sg;
uvls[0].b = sb;
pnt[1].p3_vec = Temp_sky_vectors[tw][4];
uvls[1].r = sr;
uvls[1].g = sg;
uvls[1].b = sb;