/
MapRenderer3D.cpp
2914 lines (2559 loc) · 81.8 KB
/
MapRenderer3D.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
/*******************************************************************
* SLADE - It's a Doom Editor
* Copyright (C) 2008-2014 Simon Judd
*
* Email: sirjuddington@gmail.com
* Web: http://slade.mancubus.net
* Filename: MapRenderer3D.cpp
* Description: MapRenderer3D class - handles all rendering related
* stuff for 3d mode
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*******************************************************************/
/*******************************************************************
* INCLUDES
*******************************************************************/
#include "Main.h"
#include "WxStuff.h"
#include "MapRenderer3D.h"
#include "SLADEMap.h"
#include "MathStuff.h"
#include "MapEditorWindow.h"
#include "ColourConfiguration.h"
#include "ResourceManager.h"
#include "MainWindow.h"
#include "OpenGL.h"
#include <SFML/System.hpp>
/*******************************************************************
* VARIABLES
*******************************************************************/
CVAR(Float, render_max_dist, 2000, CVAR_SAVE)
CVAR(Float, render_max_thing_dist, 2000, CVAR_SAVE)
CVAR(Int, render_thing_icon_size, 16, CVAR_SAVE)
CVAR(Bool, render_fog_quality, true, CVAR_SAVE)
CVAR(Bool, render_max_dist_adaptive, false, CVAR_SAVE)
CVAR(Int, render_adaptive_ms, 15, CVAR_SAVE)
CVAR(Bool, render_3d_sky, true, CVAR_SAVE)
CVAR(Int, render_3d_things, 1, CVAR_SAVE)
CVAR(Int, render_3d_things_style, 1, CVAR_SAVE)
CVAR(Int, render_3d_hilight, 1, CVAR_SAVE)
CVAR(Float, render_3d_brightness, 1, CVAR_SAVE)
CVAR(Float, render_fog_distance, 1500, CVAR_SAVE)
CVAR(Bool, render_fog_new_formula, true, CVAR_SAVE)
/*******************************************************************
* EXTERNAL VARIABLES
*******************************************************************/
EXTERN_CVAR(Bool, flats_use_vbo)
EXTERN_CVAR(Bool, use_zeth_icons)
/*******************************************************************
* MAPRENDERER3D CLASS FUNCTIONS
*******************************************************************/
/* MapRenderer3D::MapRenderer3D
* MapRenderer3D class constructor
*******************************************************************/
MapRenderer3D::MapRenderer3D(SLADEMap* map)
{
// Init variables
this->map = map;
this->fog = true;
this->fullbright = false;
this->gravity = 0.5;
this->vbo_ceilings = 0;
this->vbo_floors = 0;
this->vbo_walls = 0;
this->skytex1 = "SKY1";
this->quads = NULL;
this->flats = NULL;
this->tex_last = NULL;
this->n_quads = 0;
this->n_flats = 0;
this->flat_last = 0;
this->render_hilight = true;
this->render_selection = true;
// Build skybox circle
buildSkyCircle();
// Init other
init();
// Listen to stuff
listenTo(thePaletteChooser);
listenTo(theResourceManager);
}
/* MapRenderer3D::~MapRenderer3D
* MapRenderer3D class destructor
*******************************************************************/
MapRenderer3D::~MapRenderer3D()
{
if (quads) delete quads;
if (flats) delete flats;
if (vbo_ceilings > 0) glDeleteBuffers(1, &vbo_ceilings);
if (vbo_floors > 0) glDeleteBuffers(1, &vbo_floors);
if (vbo_walls > 0) glDeleteBuffers(1, &vbo_walls);
}
/* MapRenderer3D::init
* Initialises the 3d renderer
*******************************************************************/
bool MapRenderer3D::init()
{
// Check to enable zdoom udmf extensions
if (S_CMPNOCASE(theGameConfiguration->udmfNamespace(), "zdoom") && map->currentFormat() == MAP_UDMF)
udmf_zdoom = true;
// Init camera
bbox_t bbox = map->getMapBBox();
cam_position.set(bbox.min.x + ((bbox.max.x - bbox.min.x)*0.5), bbox.min.y + ((bbox.max.y - bbox.min.y)*0.5), 64);
cam_direction.set(0,1);
cam_pitch = 0;
cameraUpdateVectors();
refresh();
return true;
}
/* MapRenderer3D::refresh
* Clears VBOs and cached data
*******************************************************************/
void MapRenderer3D::refresh()
{
// Clear any existing map data
dist_sectors.clear();
if (quads)
{
delete quads;
quads = NULL;
}
if (flats)
{
delete flats;
flats = NULL;
}
// Clear VBOs
if (vbo_floors != 0)
{
glDeleteBuffers(1, &vbo_floors);
glDeleteBuffers(1, &vbo_ceilings);
vbo_floors = vbo_ceilings = 0;
}
floors.clear();
ceilings.clear();
// Set sky texture
gc_mapinfo_t minf = theGameConfiguration->mapInfo(map->mapName());
skytex1 = minf.sky1;
skytex2 = minf.sky2;
skycol_top.a = 0;
//wxLogMessage("sky1: %s, sky2: %s", skytex1, skytex2);
}
/* MapRenderer3D::clearData
* Clears all cached rendering data
*******************************************************************/
void MapRenderer3D::clearData()
{
// Clear map structures
lines.clear();
things.clear();
floors.clear();
ceilings.clear();
// Clear everything else
refresh();
}
/* MapRenderer3D::buildSkyCircle
* Generates the array of points (circular) used for the sky
*******************************************************************/
void MapRenderer3D::buildSkyCircle()
{
double rot = 0;
for (unsigned a = 0; a < 32; a++)
{
sky_circle[a].set(sin(rot), -cos(rot));
rot -= (3.1415926535897932384626433832795 * 2) / 32.0;
}
}
/* MapRenderer3D::getQuad
* Returns the wall quad for wall selection [item]
*******************************************************************/
MapRenderer3D::quad_3d_t* MapRenderer3D::getQuad(selection_3d_t item)
{
// Check item type
if (item.type != MapEditor::SEL_SIDE_BOTTOM &&
item.type != MapEditor::SEL_SIDE_MIDDLE &&
item.type != MapEditor::SEL_SIDE_TOP)
return NULL;
// Get side
MapSide* side = map->getSide(item.index);
if (!side)
return NULL;
// Find matching quad
int lindex = side->getParentLine()->getIndex();
for (unsigned a = 0; a < lines[lindex].quads.size(); a++)
{
quad_3d_t* quad = &lines[lindex].quads[a];
// Check side
if (side == side->getParentLine()->s1() && quad->flags & BACK)
continue;
if (side == side->getParentLine()->s2() && (quad->flags & BACK) == 0)
continue;
// Check part
if (item.type == MapEditor::SEL_SIDE_BOTTOM)
{
if (quad->flags & LOWER)
return quad;
}
if (item.type == MapEditor::SEL_SIDE_TOP)
{
if (quad->flags & UPPER)
return quad;
}
if (item.type == MapEditor::SEL_SIDE_MIDDLE)
{
if ((quad->flags & UPPER) == 0 && (quad->flags & LOWER) == 0)
return quad;
}
}
// Not found
return NULL;
}
/* MapRenderer3D::getFlat
* Returns the flat for sector flat selection [item]
*******************************************************************/
MapRenderer3D::flat_3d_t* MapRenderer3D::getFlat(selection_3d_t item)
{
// Check index
if ((unsigned)item.index >= floors.size())
return NULL;
// Floor
if (item.type == MapEditor::SEL_FLOOR)
return &floors[item.index];
// Ceiling
else if (item.type == MapEditor::SEL_CEILING)
return &ceilings[item.index];
// Wrong type
else
return NULL;
}
/* MapRenderer3D::cameraMove
* Moves the camera the direction it is facing by [distance]. If [z]
* is false it will only be moved along x/y axes
*******************************************************************/
void MapRenderer3D::cameraMove(double distance, bool z)
{
// Move along direction vector
if (z)
{
cam_position.x += cam_dir3d.x * distance;
cam_position.y += cam_dir3d.y * distance;
cam_position.z += cam_dir3d.z * distance;
}
else
{
cam_position.x += cam_direction.x * distance;
cam_position.y += cam_direction.y * distance;
}
}
/* MapRenderer3D::cameraTurn
* Rotates the camera by [angle] around the z axis
*******************************************************************/
void MapRenderer3D::cameraTurn(double angle)
{
// Find rotated view point
fpoint2_t cp2d(cam_position.x, cam_position.y);
fpoint2_t nd = MathStuff::rotatePoint(cp2d, cp2d + cam_direction, angle);
// Update direction
cam_direction.x = nd.x - cam_position.x;
cam_direction.y = nd.y - cam_position.y;
// Update vectors
cameraUpdateVectors();
}
/* MapRenderer3D::cameraMoveUp
* Moves the camera along the z axis by [distance]
*******************************************************************/
void MapRenderer3D::cameraMoveUp(double distance)
{
cam_position.z += distance;
}
/* MapRenderer3D::cameraStrafe
* Moves the camera along the strafe axis by [distance]
*******************************************************************/
void MapRenderer3D::cameraStrafe(double distance)
{
// Move along strafe vector
cam_position.x += cam_strafe.x * distance;
cam_position.y += cam_strafe.y * distance;
}
/* MapRenderer3D::cameraPitch
* Rotates the camera view around the strafe axis by [amount]
*******************************************************************/
void MapRenderer3D::cameraPitch(double amount)
{
// Pitch camera
cam_pitch += amount;
// Clamp
double rad90 = PI*0.5;
if (cam_pitch > rad90)
cam_pitch = rad90;
if (cam_pitch < -rad90)
cam_pitch = -rad90;
// Update vectors
cameraUpdateVectors();
}
/* MapRenderer3D::cameraUpdateVectors
* Updates the strafe and direction vectors for the camera
*******************************************************************/
void MapRenderer3D::cameraUpdateVectors()
{
// Normalize direction
cam_direction.normalize();
// Calculate strafe vector
cam_strafe = fpoint3_t(cam_direction.x, cam_direction.y, 0).cross(fpoint3_t(0, 0, 1));
cam_strafe = cam_strafe.normalize();
// Calculate 3d direction vector
cam_dir3d = MathStuff::rotateVector3D(fpoint3_t(cam_direction.x, cam_direction.y, 0), cam_strafe, cam_pitch);
cam_dir3d = cam_dir3d.normalize();
}
/* MapRenderer3D::cameraSet
* Sets the camera position to [position], facing [direction]
*******************************************************************/
void MapRenderer3D::cameraSet(fpoint3_t position, fpoint2_t direction)
{
// Set camera position/direction
cam_position = position;
cam_direction = direction;
cam_pitch = 0;
// Update camera vectors
cameraUpdateVectors();
}
/* MapRenderer3D::cameraSetPosition
* Moves the camera to [position]
*******************************************************************/
void MapRenderer3D::cameraSetPosition(fpoint3_t position)
{
cam_position = position;
}
/* MapRenderer3D::cameraApplyGravity
* Applies gravity to the camera
*******************************************************************/
void MapRenderer3D::cameraApplyGravity(double mult)
{
// Get current sector
int sector = map->sectorAt(cam_position.x, cam_position.y);
if (sector < 0)
return;
// Get target height
int view_height = (map->currentFormat() == MAP_DOOM64) ? 56 : 41;
int fheight = map->getSector(sector)->floorHeightAt(cam_position.x, cam_position.y) + view_height;
int cheight = map->getSector(sector)->ceilingHeightAt(cam_position.x, cam_position.y);
if (fheight > cheight - 4)
fheight = cheight - 4;
if (cam_position.z > fheight)
{
double diff = cam_position.z - fheight;
cam_position.z -= (diff*0.3*mult);
if (cam_position.z < fheight)
cam_position.z = fheight;
}
else if (cam_position.z < fheight)
{
double diff = fheight - cam_position.z;
cam_position.z += (diff*0.5*mult);
if (cam_position.z > fheight)
cam_position.z = fheight;
}
}
/* MapRenderer3D::setupView
* Sets up the OpenGL view/projection for rendering
*******************************************************************/
void MapRenderer3D::setupView(int width, int height)
{
// Calculate aspect ratio
float aspect = (1.6f / 1.333333f) * ((float)width / (float)height);
float fovy = 2 * MathStuff::radToDeg(atan(tan(MathStuff::degToRad(90) / 2) / aspect));
// Setup projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float max = render_max_dist * 1.5f;
if (max < 100) max = 20000;
gluPerspective(fovy, aspect, 0.5f, max);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Calculate up vector
fpoint3_t up = cam_strafe.cross(cam_dir3d).normalize();
// Setup camera view
gluLookAt(cam_position.x, cam_position.y, cam_position.z,
cam_position.x+cam_dir3d.x, cam_position.y+cam_dir3d.y, cam_position.z+cam_dir3d.z,
up.x, up.y, up.z);
}
/* MapRenderer3D::setLight
* Sets the OpenGL colour for rendering an object using [colour]
* and [light] level
*******************************************************************/
void MapRenderer3D::setLight(rgba_t& colour, uint8_t light, float alpha)
{
// Force 255 light in fullbright mode
if (fullbright)
light = 255;
// Apply brightness
light = MathStuff::clamp(light * render_3d_brightness, 0, 255);
// If we have a non-coloured light, darken it a bit to
// closer resemble the software renderer light level
float mult = (float)light / 255.0f;
mult *= (mult * 1.3f);
glColor4f(colour.fr()*mult, colour.fg()*mult, colour.fb()*mult, colour.fa()*alpha);
}
/* MapRenderer3D::setFog
* Sets the OpenGL fog for rendering an object using [fogcol]
*******************************************************************/
void MapRenderer3D::setFog(rgba_t &fogcol, uint8_t light)
{
if (!fog)
return;
GLfloat fogColor[3]= {fogcol.fr(), fogcol.fg(), fogcol.fb()};
glEnable(GL_FOG);
glFogfv(GL_FOG_COLOR, fogColor);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_DENSITY, 1.0f);
glFogf(GL_FOG_START, 0.0f);
// check if fog color is default
if ((fogColor[0] == 0 && fogColor[1] == 0 && fogColor[2] == 0) || !render_fog_new_formula)
{
float lm = light/170.0f;
glFogf(GL_FOG_END, (lm * lm * 3000.0f));
}
else
glFogf(GL_FOG_END, render_fog_distance);
if (render_fog_quality)
glHint(GL_FOG_HINT, GL_NICEST);
else
glHint(GL_FOG_HINT, GL_FASTEST);
}
/* MapRenderer3D::renderMap
* Renders the map in 3d
*******************************************************************/
void MapRenderer3D::renderMap()
{
// Setup GL stuff
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_ALPHA_TEST);
glDepthMask(GL_TRUE);
glAlphaFunc(GL_GREATER, 0.0f);
// Init
tex_last = NULL;
// Init VBO stuff
if (OpenGL::vboSupport())
{
// Check if any polygon vertex data has changed (in this case we need to refresh the entire vbo)
bool vbo_updated = false;
for (unsigned a = 0; a < map->nSectors(); a++)
{
Polygon2D* poly = map->getSector(a)->getPolygon();
if (poly && poly->vboUpdate() > 1)
{
updateFlatsVBO();
vbo_updated = true;
break;
}
}
// Create VBO if necessary
if (!vbo_updated && vbo_floors == 0)
updateFlatsVBO();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
// Create flat arrays if needed
if (floors.size() != map->nSectors())
{
floors.resize(map->nSectors());
ceilings.resize(map->nSectors());
}
// Create lines array if empty
if (lines.size() != map->nLines())
lines.resize(map->nLines());
// Create things array if empty
if (things.size() != map->nThings())
things.resize(map->nThings());
// Quick distance vis check
sf::Clock clock;
quickVisDiscard();
// Build lists of quads and flats to render
checkVisibleFlats();
checkVisibleQuads();
// Render sky
if (render_3d_sky)
renderSky();
OpenGL::setColour(COL_WHITE);
// Render walls
renderWalls();
// Render flats
renderFlats();
// Render things
if (render_3d_things > 0)
renderThings();
// Render transparent stuff
renderTransparentWalls();
// Check elapsed time
if (render_max_dist_adaptive)
{
long ms = clock.getElapsedTime().asMilliseconds();
if (ms > render_adaptive_ms)
{
render_max_dist = render_max_dist - 100;
if (render_max_dist < 1000)
render_max_dist = 1000;
}
else if (ms < render_adaptive_ms-5)
{
render_max_dist = render_max_dist + 100;
if (render_max_dist > 20000)
render_max_dist = 20000;
}
}
// Cleanup gl state
glDisable(GL_ALPHA_TEST);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_FOG);
}
/* MapRenderer3D::renderSkySlice
* Renders a cylindrical 'slice' of the sky between [top] and
* [bottom] on the z axis
*******************************************************************/
void MapRenderer3D::renderSkySlice(float top, float bottom, float atop, float abottom, float size, float tx, float ty)
{
float tc_x = 0.0f;
float tc_y1 = (-top + 1.0f) * (ty * 0.5f);
float tc_y2 = (-bottom + 1.0f) * (ty * 0.5f);
glBegin(GL_QUADS);
// Go through circular points
for (unsigned a = 0; a < 31; a++)
{
// Top
glColor4f(1.0f, 1.0f, 1.0f, atop);
glTexCoord2f(tc_x+tx, tc_y1); glVertex3f(cam_position.x + (sky_circle[a+1].x * size), cam_position.y - (sky_circle[a+1].y * size), cam_position.z + (top * size));
glTexCoord2f(tc_x, tc_y1); glVertex3f(cam_position.x + (sky_circle[a].x * size), cam_position.y - (sky_circle[a].y * size), cam_position.z + (top * size));
// Bottom
glColor4f(1.0f, 1.0f, 1.0f, abottom);
glTexCoord2f(tc_x, tc_y2); glVertex3f(cam_position.x + (sky_circle[a].x * size), cam_position.y - (sky_circle[a].y * size), cam_position.z + (bottom * size));
glTexCoord2f(tc_x+tx, tc_y2); glVertex3f(cam_position.x + (sky_circle[a+1].x * size), cam_position.y - (sky_circle[a+1].y * size), cam_position.z + (bottom * size));
tc_x += tx;
}
// Link last point -> first
// Top
glColor4f(1.0f, 1.0f, 1.0f, atop);
glTexCoord2f(tc_x+tx, tc_y1); glVertex3f(cam_position.x + (sky_circle[0].x * size), cam_position.y - (sky_circle[0].y * size), cam_position.z + (top * size));
glTexCoord2f(tc_x, tc_y1); glVertex3f(cam_position.x + (sky_circle[31].x * size), cam_position.y - (sky_circle[31].y * size), cam_position.z + (top * size));
// Bottom
glColor4f(1.0f, 1.0f, 1.0f, abottom);
glTexCoord2f(tc_x, tc_y2); glVertex3f(cam_position.x + (sky_circle[31].x * size), cam_position.y - (sky_circle[31].y * size), cam_position.z + (bottom * size));
glTexCoord2f(tc_x+tx, tc_y2); glVertex3f(cam_position.x + (sky_circle[0].x * size), cam_position.y - (sky_circle[0].y * size), cam_position.z + (bottom * size));
glEnd();
}
/* MapRenderer3D::renderSky
* Renders the sky
*******************************************************************/
void MapRenderer3D::renderSky()
{
OpenGL::setColour(COL_WHITE);
glDisable(GL_CULL_FACE);
glDisable(GL_FOG);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glEnable(GL_TEXTURE_2D);
GLTexture* sky = NULL;
// Center skybox a bit below the camera view
glPushMatrix();
glTranslatef(0.0f, 0.0f, -10.0f);
// Get sky texture
if (!skytex2.IsEmpty())
sky = theMapEditor->textureManager().getTexture(skytex2, false);
else
sky = theMapEditor->textureManager().getTexture(skytex1, false);
if (sky)
{
// Bind texture
sky->bind();
// Get average colour if needed
if (skycol_top.a == 0)
{
int theight = sky->getHeight() * 0.4;
skycol_top = sky->averageColour(rect_t(0, 0, sky->getWidth(), theight));
skycol_bottom = sky->averageColour(rect_t(0, sky->getHeight() - theight, sky->getWidth(), sky->getHeight()));
}
// Render top cap
float size = 64.0f;
glDisable(GL_TEXTURE_2D);
OpenGL::setColour(skycol_top, false);
glBegin(GL_QUADS);
glVertex3f(cam_position.x-(size*10), cam_position.y-(size*10), cam_position.z+size);
glVertex3f(cam_position.x-(size*10), cam_position.y+(size*10), cam_position.z+size);
glVertex3f(cam_position.x+(size*10), cam_position.y+(size*10), cam_position.z+size);
glVertex3f(cam_position.x+(size*10), cam_position.y-(size*10), cam_position.z+size);
glEnd();
// Render bottom cap
OpenGL::setColour(skycol_bottom, false);
glBegin(GL_QUADS);
glVertex3f(cam_position.x-(size*10), cam_position.y-(size*10), cam_position.z-size);
glVertex3f(cam_position.x-(size*10), cam_position.y+(size*10), cam_position.z-size);
glVertex3f(cam_position.x+(size*10), cam_position.y+(size*10), cam_position.z-size);
glVertex3f(cam_position.x+(size*10), cam_position.y-(size*10), cam_position.z-size);
glEnd();
// Render skybox sides
glDisable(GL_ALPHA_TEST);
glEnable(GL_TEXTURE_2D);
// Check for odd sky sizes
float tx = 0.125f;
float ty = 2.0f;
if (sky->getWidth() > 256)
tx = 0.125f / ((float)sky->getWidth() / 256.0f);
if (sky->getHeight() > 128)
ty = 1.0f;
renderSkySlice(1.0f, 0.5f, 0.0f, 1.0f, size, tx, ty); // Top
renderSkySlice(0.5f, -0.5f, 1.0f, 1.0f, size, tx, ty); // Middle
renderSkySlice(-0.5f, -1.0f, 1.0f, 0.0f, size, tx, ty); // Bottom
}
glPopMatrix();
glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_ALPHA_TEST);
// Render all sky quads
glDisable(GL_TEXTURE_2D);
for (unsigned a = 0; a < n_quads; a++)
{
// Ignore if not sky
if ((quads[a]->flags & SKY) == 0)
continue;
// Render quad
renderQuad(quads[a]);
quads[a] = quads[n_quads-1];
n_quads--;
a--;
}
// Render all sky flats
flat_last = 0;
for (unsigned a = 0; a < n_flats; a++)
{
// Ignore if not sky
if ((flats[a]->flags & SKY) == 0)
continue;
// Render quad
renderFlat(flats[a]);
flats[a] = flats[n_flats-1];
n_flats--;
a--;
}
glEnable(GL_TEXTURE_2D);
}
/* MapRenderer3D::updateFlatTexCoords
* Updates the vertex texture coordinates of all polygons for sector
* [index]
*******************************************************************/
void MapRenderer3D::updateFlatTexCoords(unsigned index, bool floor)
{
// Check index
if (index >= map->nSectors())
return;
// Get sector
MapSector* sector = map->getSector(index);
// Get scaling/offset info
double ox = 0;
double oy = 0;
double sx = floor ? floors[index].texture->getScaleX() : ceilings[index].texture->getScaleX();
double sy = floor ? floors[index].texture->getScaleY() : ceilings[index].texture->getScaleY();
double rot = 0;
// Check for UDMF + ZDoom extensions
if (theMapEditor->currentMapDesc().format == MAP_UDMF && S_CMPNOCASE(theGameConfiguration->udmfNamespace(), "zdoom"))
{
if (floor)
{
ox = sector->floatProperty("xpanningfloor");
oy = sector->floatProperty("ypanningfloor");
sx *= (1.0 / sector->floatProperty("xscalefloor"));
sy *= (1.0 / sector->floatProperty("yscalefloor"));
rot = sector->floatProperty("rotationfloor");
}
else
{
ox = sector->floatProperty("xpanningceiling");
oy = sector->floatProperty("ypanningceiling");
sx *= (1.0 / sector->floatProperty("xscaleceiling"));
sy *= (1.0 / sector->floatProperty("yscaleceiling"));
rot = sector->floatProperty("rotationceiling");
}
}
// Update polygon texture coordinates
if (floor)
sector->getPolygon()->setTexture(floors[index].texture);
else
sector->getPolygon()->setTexture(ceilings[index].texture);
sector->getPolygon()->updateTextureCoords(sx, sy, ox, oy, rot);
}
/* MapRenderer3D::updateSector
* Updates cached rendering data for sector [index]
*******************************************************************/
void MapRenderer3D::updateSector(unsigned index)
{
// Check index
if (index >= map->nSectors())
return;
// Update floor
MapSector* sector = map->getSector(index);
floors[index].sector = sector;
floors[index].texture = theMapEditor->textureManager().getFlat(sector->getFloorTex(), theGameConfiguration->mixTexFlats());
floors[index].colour = sector->getColour(1, true);
floors[index].fogcolour = sector->getFogColour();
floors[index].light = sector->getLight(1);
floors[index].flags = 0;
floors[index].plane = sector->getFloorPlane();
if (sector->getFloorTex() == theGameConfiguration->skyFlat())
floors[index].flags |= SKY;
// Update floor VBO
if (OpenGL::vboSupport())
{
updateFlatTexCoords(index, true);
glBindBuffer(GL_ARRAY_BUFFER, vbo_floors);
Polygon2D::setupVBOPointers();
sector->getPolygon()->setZ(floors[index].plane);
sector->getPolygon()->updateVBOData();
}
// Update ceiling
ceilings[index].sector = sector;
ceilings[index].texture = theMapEditor->textureManager().getFlat(sector->getCeilingTex(), theGameConfiguration->mixTexFlats());
ceilings[index].colour = sector->getColour(2, true);
ceilings[index].fogcolour = sector->getFogColour();
ceilings[index].light = sector->getLight(2);
ceilings[index].flags = CEIL;
ceilings[index].plane = sector->getCeilingPlane();
if (sector->getCeilingTex() == theGameConfiguration->skyFlat())
ceilings[index].flags |= SKY;
// Update ceiling VBO
if (OpenGL::vboSupport())
{
updateFlatTexCoords(index, false);
glBindBuffer(GL_ARRAY_BUFFER, vbo_ceilings);
Polygon2D::setupVBOPointers();
sector->getPolygon()->setZ(ceilings[index].plane);
sector->getPolygon()->updateVBOData();
}
// Finish up
floors[index].updated_time = theApp->runTimer();
ceilings[index].updated_time = theApp->runTimer();
if (OpenGL::vboSupport())
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
sector->getPolygon()->setZ(0);
}
}
/* MapRenderer3D::renderFlat
* Renders [flat]
*******************************************************************/
void MapRenderer3D::renderFlat(flat_3d_t* flat)
{
// Skip if no sector (for whatever reason)
if (!flat->sector)
return;
// Setup special rendering options
float alpha = flat->alpha;
if (flat->flags & SKY && render_3d_sky)
{
alpha = 0;
glDisable(GL_ALPHA_TEST);
}
// Setup colour/light
setLight(flat->colour, flat->light, alpha);
// Setup fog colour
setFog(flat->fogcolour, flat->light);
// Render flat
if (OpenGL::vboSupport() && flats_use_vbo)
{
// Setup for floor or ceiling
if (flat->flags & CEIL)
{
if (flat_last != 2)
{
glCullFace(GL_BACK);
glBindBuffer(GL_ARRAY_BUFFER, vbo_ceilings);
Polygon2D::setupVBOPointers();
flat_last = 2;
}
}
else
{
if (flat_last != 1)
{
glCullFace(GL_FRONT);
glBindBuffer(GL_ARRAY_BUFFER, vbo_floors);
Polygon2D::setupVBOPointers();
flat_last = 1;
}
}
// Render
flat->sector->getPolygon()->renderVBO(false);
}
else
{
glPushMatrix();
// Setup for floor or ceiling
if (flat->flags & CEIL)
{
glCullFace(GL_BACK);
glTranslated(0, 0, flat->sector->getCeilingHeight());
}
else
{
glCullFace(GL_FRONT);
glTranslated(0, 0, flat->sector->getFloorHeight());
}
// Render
flat->sector->getPolygon()->render();
glPopMatrix();
}
// Reset settings
if (flat->flags & SKY && render_3d_sky)
glEnable(GL_ALPHA_TEST);
}
/* MapRenderer3D::renderFlats
* Renders all currently visible flats
*******************************************************************/
void MapRenderer3D::renderFlats()
{
// Check for map
if (!map)
return;
// Init textures
glEnable(GL_TEXTURE_2D);
// Render all visible flats, ordered by texture
unsigned a = 0;
flat_last = 0;
while (n_flats > 0)
{
tex_last = NULL;
a = 0;
while (a < n_flats)
{
// Check texture
if (!tex_last && flats[a]->texture)
{
tex_last = flats[a]->texture;
flats[a]->texture->bind();
}
if (flats[a]->texture != tex_last)
{
a++;
continue;
}
// Render flat
renderFlat(flats[a]);
flats[a] = flats[n_flats-1];
n_flats--;
}
}
// Reset gl stuff
glDisable(GL_TEXTURE_2D);
if (OpenGL::vboSupport())
{
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}