/
ogl.cpp
1962 lines (1799 loc) · 54 KB
/
ogl.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
/*
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
/*
*
* Graphics support functions for OpenGL.
*
*/
#include "dxxsconf.h"
#include <stdexcept>
#include <tuple>
#ifdef _WIN32
#include <windows.h>
#include <stddef.h>
#endif
#if defined(__APPLE__) && defined(__MACH__)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#if DXX_USE_OGLES
#include <GLES/gl.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#endif
#include <string.h>
#include <math.h>
#include <stdio.h>
#include "3d.h"
#include "piggy.h"
#include "common/3d/globvars.h"
#include "dxxerror.h"
#include "texmap.h"
#include "palette.h"
#include "rle.h"
#include "console.h"
#include "config.h"
#include "u_mem.h"
#include "segment.h"
#include "textures.h"
#include "texmerge.h"
#include "effects.h"
#include "weapon.h"
#include "powerup.h"
#include "laser.h"
#include "player.h"
#include "robot.h"
#include "gamefont.h"
#include "byteutil.h"
#include "internal.h"
#include "gauges.h"
#include "playsave.h"
#include "object.h"
#include "args.h"
#include "compiler-range_for.h"
#include "d_range.h"
#include "d_zip.h"
#include "partial_range.h"
#include <algorithm>
#include <memory>
#include <utility>
using std::max;
//change to 1 for lots of spew.
#if 0
#define glmprintf(A) con_printf A
#else
#define glmprintf(A)
#endif
#ifndef M_PI
#define M_PI 3.14159
#endif
namespace {
template <unsigned G>
struct enable_ogl_client_state
{
enable_ogl_client_state() noexcept
{
glEnableClientState(G);
}
~enable_ogl_client_state() noexcept
{
glDisableClientState(G);
}
};
template <typename T, unsigned... Gs>
using ogl_client_states = std::tuple<T, enable_ogl_client_state<Gs>...>;
template <typename T, std::size_t N1, std::size_t N2>
union flatten_array
{
std::array<T, N1 * N2> flat;
std::array<std::array<T, N1>, N2> nested;
static_assert(sizeof(flat) == sizeof(nested), "array padding error");
};
}
#if defined(_WIN32) || (defined(__APPLE__) && defined(__MACH__)) || defined(__sun__) || defined(macintosh)
#define cosf(a) cos(a)
#define sinf(a) sin(a)
#endif
namespace dcx {
static std::unique_ptr<GLubyte[]> texbuf;
unsigned last_width=~0u,last_height=~0u;
int GL_TEXTURE_2D_enabled=-1;
static int r_texcount = 0, r_cachedtexcount = 0;
#if DXX_USE_OGLES
static int ogl_rgba_internalformat = GL_RGBA;
static int ogl_rgb_internalformat = GL_RGB;
#else
static int ogl_rgba_internalformat = GL_RGBA8;
static int ogl_rgb_internalformat = GL_RGB8;
#endif
static std::unique_ptr<GLfloat[]> sphere_va, circle_va, disk_va;
static std::array<std::unique_ptr<GLfloat[]>, 3> secondary_lva;
static int r_polyc,r_tpolyc,r_bitmapc,r_ubitbltc;
#define f2glf(x) (f2fl(x))
#define OGL_BINDTEXTURE(a) glBindTexture(GL_TEXTURE_2D, a);
/* I assume this ought to be >= MAX_BITMAP_FILES in piggy.h? */
static std::array<ogl_texture, 20000> ogl_texture_list;
static int ogl_texture_list_cur;
/* some function prototypes */
#define GL_TEXTURE0_ARB 0x84C0
static int ogl_loadtexture(const palette_array_t &, const uint8_t *data, int dxo, int dyo, ogl_texture &tex, int bm_flags, int data_format, int texfilt, bool texanis, bool edgepad) __attribute_nonnull();
static void ogl_freetexture(ogl_texture &gltexture);
static void ogl_loadbmtexture(grs_bitmap &bm, bool edgepad)
{
ogl_loadbmtexture_f(bm, CGameCfg.TexFilt, CGameCfg.TexAnisotropy, edgepad);
}
}
#if DXX_USE_OGLES
// Replacement for gluPerspective
static void perspective(double fovy, double aspect, double zNear, double zFar)
{
double xmin, xmax, ymin, ymax;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ymax = zNear * tan(fovy * M_PI / 360.0);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDepthMask(GL_TRUE);
}
#endif
static void ogl_init_texture_stats(ogl_texture &t)
{
t.prio=0.3;//default prio
t.numrend=0;
}
void ogl_init_texture(ogl_texture &t, int w, int h, int flags)
{
t.handle = 0;
#if !DXX_USE_OGLES
if (flags & OGL_FLAG_NOCOLOR)
{
// use GL_INTENSITY instead of GL_RGB
if (flags & OGL_FLAG_ALPHA)
{
if (CGameArg.DbgGlIntensity4Ok)
{
t.internalformat = GL_INTENSITY4;
t.format = GL_LUMINANCE;
}
else if (CGameArg.DbgGlLuminance4Alpha4Ok)
{
t.internalformat = GL_LUMINANCE4_ALPHA4;
t.format = GL_LUMINANCE_ALPHA;
}
else if (CGameArg.DbgGlRGBA2Ok)
{
t.internalformat = GL_RGBA2;
t.format = GL_RGBA;
}
else
{
t.internalformat = ogl_rgba_internalformat;
t.format = GL_RGBA;
}
}
else
{
// there are certainly smaller formats we could use here, but nothing needs it ATM.
t.internalformat = ogl_rgb_internalformat;
t.format = GL_RGB;
}
}
else
{
#endif
if (flags & OGL_FLAG_ALPHA)
{
t.internalformat = ogl_rgba_internalformat;
t.format = GL_RGBA;
}
else
{
t.internalformat = ogl_rgb_internalformat;
t.format = GL_RGB;
}
#if !DXX_USE_OGLES
}
#endif
t.wrapstate = -1;
t.lw = t.w = w;
t.h = h;
ogl_init_texture_stats(t);
}
static void ogl_reset_texture(ogl_texture &t)
{
ogl_init_texture(t, 0, 0, 0);
}
static void ogl_reset_texture_stats_internal(void){
range_for (auto &i, ogl_texture_list)
if (i.handle>0)
ogl_init_texture_stats(i);
}
void ogl_init_texture_list_internal(void){
ogl_texture_list_cur=0;
range_for (auto &i, ogl_texture_list)
ogl_reset_texture(i);
}
void ogl_smash_texture_list_internal(void){
sphere_va.reset();
circle_va.reset();
disk_va.reset();
secondary_lva = {};
range_for (auto &i, ogl_texture_list)
{
if (i.handle>0){
glDeleteTextures( 1, &i.handle );
i.handle=0;
}
i.wrapstate = -1;
}
}
ogl_texture* ogl_get_free_texture(void){
for (unsigned i = ogl_texture_list.size(); i--;)
{
if (ogl_texture_list[ogl_texture_list_cur].handle<=0 && ogl_texture_list[ogl_texture_list_cur].w==0)
return &ogl_texture_list[ogl_texture_list_cur];
if (++ogl_texture_list_cur >= ogl_texture_list.size())
ogl_texture_list_cur=0;
}
Error("OGL: texture list full!\n");
}
static void ogl_texture_stats(void)
{
int used = 0, usedother = 0, usedidx = 0, usedrgb = 0, usedrgba = 0;
int databytes = 0, truebytes = 0, datatexel = 0, truetexel = 0;
int prio0=0,prio1=0,prio2=0,prio3=0,prioh=0;
GLint idx, r, g, b, a, dbl, depth;
int res, colorsize, depthsize;
range_for (auto &i, ogl_texture_list)
{
if (i.handle>0){
used++;
datatexel+=i.w*i.h;
truetexel+=i.tw*i.th;
databytes+=i.bytesu;
truebytes+=i.bytes;
if (i.prio<0.299)prio0++;
else if (i.prio<0.399)prio1++;
else if (i.prio<0.499)prio2++;
else if (i.prio<0.599)prio3++;
else prioh++;
if (i.format == GL_RGBA)
usedrgba++;
else if (i.format == GL_RGB)
usedrgb++;
#if !DXX_USE_OGLES
else if (i.format == GL_COLOR_INDEX)
usedidx++;
#endif
else
usedother++;
}
}
res = SWIDTH * SHEIGHT;
#if !DXX_USE_OGLES
glGetIntegerv(GL_INDEX_BITS, &idx);
#else
idx=16;
#endif
glGetIntegerv(GL_RED_BITS, &r);
glGetIntegerv(GL_GREEN_BITS, &g);
glGetIntegerv(GL_BLUE_BITS, &b);
glGetIntegerv(GL_ALPHA_BITS, &a);
#if !DXX_USE_OGLES
glGetIntegerv(GL_DOUBLEBUFFER, &dbl);
#else
dbl=1;
#endif
dbl += 1;
glGetIntegerv(GL_DEPTH_BITS, &depth);
gr_set_default_canvas();
auto &canvas = *grd_curcanv;
const auto &game_font = *GAME_FONT;
gr_set_fontcolor(canvas, BM_XRGB(255, 255, 255), -1);
colorsize = (idx * res * dbl) / 8;
depthsize = res * depth / 8;
const auto &&fspacx2 = FSPACX(2);
const auto &&fspacy1 = FSPACY(1);
const auto &&line_spacing = LINE_SPACING(game_font, game_font);
gr_printf(canvas, game_font, fspacx2, fspacy1, "%i flat %i tex %i bitmaps", r_polyc, r_tpolyc, r_bitmapc);
gr_printf(canvas, game_font, fspacx2, fspacy1 + line_spacing, "%i(%i,%i,%i,%i) %iK(%iK wasted) (%i postcachedtex)", used, usedrgba, usedrgb, usedidx, usedother, truebytes / 1024, (truebytes - databytes) / 1024, r_texcount - r_cachedtexcount);
gr_printf(canvas, game_font, fspacx2, fspacy1 + (line_spacing * 2), "%ibpp(r%i,g%i,b%i,a%i)x%i=%iK depth%i=%iK", idx, r, g, b, a, dbl, colorsize / 1024, depth, depthsize / 1024);
gr_printf(canvas, game_font, fspacx2, fspacy1 + (line_spacing * 3), "total=%iK", (colorsize + depthsize + truebytes) / 1024);
}
static void ogl_bindbmtex(grs_bitmap &bm, bool edgepad){
if (bm.gltexture==NULL || bm.gltexture->handle<=0)
ogl_loadbmtexture(bm, edgepad);
OGL_BINDTEXTURE(bm.gltexture->handle);
bm.gltexture->numrend++;
}
//gltexture MUST be bound first
static void ogl_texwrap(ogl_texture *const gltexture, const int state)
{
if (gltexture->wrapstate != state || gltexture->numrend < 1)
{
gltexture->wrapstate = state;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, state);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, state);
}
}
//crude texture precaching
//handles: powerups, walls, weapons, polymodels, etc.
//it is done with the horrid do_special_effects kludge so that sides that have to be texmerged and have animated textures will be correctly cached.
//similarly, with the objects(esp weapons), we could just go through and cache em all instead, but that would get ones that might not even be on the level
//TODO: doors
void ogl_cache_polymodel_textures(const unsigned model_num)
{
auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
if (model_num >= Polygon_models.size())
return;
const auto &po = Polygon_models[model_num];
unsigned i = po.first_texture;
const unsigned last_texture = i + po.n_textures;
for (; i != last_texture; ++i)
{
auto &objbitmap = ObjBitmaps[ObjBitmapPtrs[i]];
PIGGY_PAGE_IN(objbitmap);
ogl_loadbmtexture(GameBitmaps[objbitmap.index], 1);
}
}
static void ogl_cache_vclip_textures(const vclip &vc)
{
range_for (auto &i, partial_const_range(vc.frames, vc.num_frames))
{
PIGGY_PAGE_IN(i);
ogl_loadbmtexture(GameBitmaps[i.index], 0);
}
}
static void ogl_cache_vclipn_textures(const d_vclip_array &Vclip, const unsigned i)
{
if (i < Vclip.size())
ogl_cache_vclip_textures(Vclip[i]);
}
static void ogl_cache_weapon_textures(const d_vclip_array &Vclip, const weapon_info_array &Weapon_info, const unsigned weapon_type)
{
if (weapon_type >= Weapon_info.size())
return;
const auto &w = Weapon_info[weapon_type];
ogl_cache_vclipn_textures(Vclip, w.flash_vclip);
ogl_cache_vclipn_textures(Vclip, w.robot_hit_vclip);
ogl_cache_vclipn_textures(Vclip, w.wall_hit_vclip);
if (w.render_type == WEAPON_RENDER_VCLIP)
ogl_cache_vclipn_textures(Vclip, w.weapon_vclip);
else if (w.render_type == WEAPON_RENDER_POLYMODEL)
{
ogl_cache_polymodel_textures(w.model_num);
ogl_cache_polymodel_textures(w.model_num_inner);
}
}
namespace dsx {
void ogl_cache_level_textures(void)
{
auto &Effects = LevelUniqueEffectsClipState.Effects;
auto &Objects = LevelUniqueObjectState.Objects;
auto &vcobjptridx = Objects.vcptridx;
int max_efx=0,ef;
ogl_reset_texture_stats_internal();//loading a new lev should reset textures
range_for (auto &ec, partial_const_range(Effects, Num_effects))
{
ogl_cache_vclipn_textures(Vclip, ec.dest_vclip);
if ((ec.changing_wall_texture == -1) && (ec.changing_object_texture==-1) )
continue;
if (ec.vc.num_frames>max_efx)
max_efx=ec.vc.num_frames;
}
glmprintf((CON_DEBUG, "max_efx:%i", max_efx));
for (ef=0;ef<max_efx;ef++){
range_for (eclip &ec, partial_range(Effects, Num_effects))
{
if ((ec.changing_wall_texture == -1) && (ec.changing_object_texture==-1) )
continue;
ec.time_left=-1;
}
do_special_effects();
range_for (const unique_segment &seg, vcsegptr)
{
range_for (auto &side, seg.sides)
{
const auto tmap1 = side.tmap_num;
const auto tmap2 = side.tmap_num2;
if (tmap1<0 || tmap1>=NumTextures){
glmprintf((CON_DEBUG, "ogl_cache_level_textures %p %p %i %i", seg.get_unchecked_pointer(), &side, tmap1, NumTextures));
// tmap1=0;
continue;
}
PIGGY_PAGE_IN(Textures[tmap1]);
grs_bitmap *bm = &GameBitmaps[Textures[tmap1].index];
if (tmap2 != 0){
PIGGY_PAGE_IN(Textures[tmap2&0x3FFF]);
auto &bm2 = GameBitmaps[Textures[tmap2&0x3FFF].index];
if (CGameArg.DbgUseOldTextureMerge || bm2.get_flag_mask(BM_FLAG_SUPER_TRANSPARENT))
bm = &texmerge_get_cached_bitmap( tmap1, tmap2 );
else {
ogl_loadbmtexture(bm2, 1);
}
}
ogl_loadbmtexture(*bm, 0);
}
}
glmprintf((CON_DEBUG, "finished ef:%i", ef));
}
reset_special_effects();
init_special_effects();
{
auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
// always have lasers, concs, flares. Always shows player appearance, and at least concs are always available to disappear.
ogl_cache_weapon_textures(Vclip, Weapon_info, Primary_weapon_to_weapon_info[primary_weapon_index_t::LASER_INDEX]);
ogl_cache_weapon_textures(Vclip, Weapon_info, Secondary_weapon_to_weapon_info[CONCUSSION_INDEX]);
ogl_cache_weapon_textures(Vclip, Weapon_info, weapon_id_type::FLARE_ID);
ogl_cache_vclipn_textures(Vclip, VCLIP_PLAYER_APPEARANCE);
ogl_cache_vclipn_textures(Vclip, VCLIP_POWERUP_DISAPPEARANCE);
ogl_cache_polymodel_textures(Player_ship->model_num);
ogl_cache_vclipn_textures(Vclip, Player_ship->expl_vclip_num);
range_for (const auto &&objp, vcobjptridx)
{
if (objp->type == OBJ_POWERUP && objp->render_type==RT_POWERUP)
{
ogl_cache_vclipn_textures(Vclip, objp->rtype.vclip_info.vclip_num);
const auto id = get_powerup_id(objp);
primary_weapon_index_t p;
secondary_weapon_index_t s;
int w;
if (
(
(
(id == POW_VULCAN_WEAPON && (p = primary_weapon_index_t::VULCAN_INDEX, true)) ||
(id == POW_SPREADFIRE_WEAPON && (p = primary_weapon_index_t::SPREADFIRE_INDEX, true)) ||
(id == POW_PLASMA_WEAPON && (p = primary_weapon_index_t::PLASMA_INDEX, true)) ||
(id == POW_FUSION_WEAPON && (p = primary_weapon_index_t::FUSION_INDEX, true))
) && (w = Primary_weapon_to_weapon_info[p], true)
) ||
(
(
(id == POW_PROXIMITY_WEAPON && (s = secondary_weapon_index_t::PROXIMITY_INDEX, true)) ||
((id == POW_HOMING_AMMO_1 || id == POW_HOMING_AMMO_4) && (s = secondary_weapon_index_t::HOMING_INDEX, true)) ||
(id == POW_SMARTBOMB_WEAPON && (s = secondary_weapon_index_t::SMART_INDEX, true)) ||
(id == POW_MEGA_WEAPON && (s = secondary_weapon_index_t::MEGA_INDEX, true))
) && (w = Secondary_weapon_to_weapon_info[s], true)
)
)
{
ogl_cache_weapon_textures(Vclip, Weapon_info, w);
}
}
else if (objp->type != OBJ_NONE && objp->render_type==RT_POLYOBJ)
{
if (objp->type == OBJ_ROBOT)
{
auto &ri = Robot_info[get_robot_id(objp)];
ogl_cache_vclipn_textures(Vclip, ri.exp1_vclip_num);
ogl_cache_vclipn_textures(Vclip, ri.exp2_vclip_num);
ogl_cache_weapon_textures(Vclip, Weapon_info, ri.weapon_type);
}
if (objp->rtype.pobj_info.tmap_override != -1)
{
auto &t = Textures[objp->rtype.pobj_info.tmap_override];
PIGGY_PAGE_IN(t);
ogl_loadbmtexture(GameBitmaps[t.index], 1);
}
else
ogl_cache_polymodel_textures(objp->rtype.pobj_info.model_num);
}
}
}
glmprintf((CON_DEBUG, "finished caching"));
r_cachedtexcount = r_texcount;
}
}
namespace dcx {
void g3_draw_line(grs_canvas &canvas, const g3s_point &p0, const g3s_point &p1, const uint8_t c)
{
GLfloat color_r, color_g, color_b;
GLfloat color_array[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY> cs;
OGL_DISABLE(TEXTURE_2D);
color_r = PAL2Tr(c);
color_g = PAL2Tg(c);
color_b = PAL2Tb(c);
color_array[0] = color_array[4] = color_r;
color_array[1] = color_array[5] = color_g;
color_array[2] = color_array[6] = color_b;
color_array[3] = color_array[7] = 1.0;
std::array<GLfloat, 6> vertices = {{
f2glf(p0.p3_vec.x), f2glf(p0.p3_vec.y), -f2glf(p0.p3_vec.z),
f2glf(p1.p3_vec.x), f2glf(p1.p3_vec.y), -f2glf(p1.p3_vec.z)
}};
glVertexPointer(3, GL_FLOAT, 0, vertices.data());
glColorPointer(4, GL_FLOAT, 0, color_array);
glDrawArrays(GL_LINES, 0, 2);
}
static void ogl_drawcircle(const unsigned nsides, const unsigned type, GLfloat *const vertices)
{
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(type, 0, nsides);
glDisableClientState(GL_VERTEX_ARRAY);
}
static std::unique_ptr<GLfloat[]> circle_array_init(const unsigned nsides)
{
auto vertices = std::make_unique<GLfloat[]>(nsides * 2);
for (unsigned i = 0; i < nsides; i++)
{
const float ang = 2.0 * M_PI * i / nsides;
vertices[i * 2] = cosf(ang);
vertices[i * 2 + 1] = sinf(ang);
}
return vertices;
}
static std::unique_ptr<GLfloat[]> circle_array_init_2(const unsigned nsides, const float xsc, const float xo, const float ysc, const float yo)
{
auto vertices = std::make_unique<GLfloat[]>(nsides * 2);
for (unsigned i = 0; i < nsides; i++)
{
const float ang = 2.0 * M_PI * i / nsides;
vertices[i * 2] = cosf(ang) * xsc + xo;
vertices[i * 2 + 1] = sinf(ang) * ysc + yo;
}
return vertices;
}
}
void ogl_draw_vertex_reticle(int cross,int primary,int secondary,int color,int alpha,int size_offs)
{
int size=270+(size_offs*20);
float scale = (static_cast<float>(SWIDTH)/SHEIGHT);
const std::array<float, 4> ret_rgba{{
static_cast<float>(PAL2Tr(color)),
static_cast<float>(PAL2Tg(color)),
static_cast<float>(PAL2Tb(color)),
static_cast<float>(1.0 - (static_cast<float>(alpha) / (static_cast<float>(GR_FADE_LEVELS))))
}}, ret_dark_rgba{{
ret_rgba[0] / 2,
ret_rgba[1] / 2,
ret_rgba[2] / 2,
ret_rgba[3] / 2
}};
std::array<GLfloat, 16 * 4> dark_lca, bright_lca;
for (uint_fast32_t i = 0; i != dark_lca.size(); i += 4)
{
bright_lca[i] = ret_rgba[0];
dark_lca[i] = ret_dark_rgba[0];
bright_lca[i+1] = ret_rgba[1];
dark_lca[i+1] = ret_dark_rgba[1];
bright_lca[i+2] = ret_rgba[2];
dark_lca[i+2] = ret_dark_rgba[2];
bright_lca[i+3] = ret_rgba[3];
dark_lca[i+3] = ret_dark_rgba[3];
}
glPushMatrix();
glTranslatef((grd_curcanv->cv_bitmap.bm_w/2+grd_curcanv->cv_bitmap.bm_x)/static_cast<float>(last_width),1.0-(grd_curcanv->cv_bitmap.bm_h/2+grd_curcanv->cv_bitmap.bm_y)/static_cast<float>(last_height),0);
{
float gl1, gl2, gl3;
if (scale >= 1)
{
size/=scale;
gl2 = f2glf(size*scale);
gl1 = f2glf(size);
gl3 = gl1;
}
else
{
size*=scale;
gl1 = f2glf(size/scale);
gl2 = f2glf(size);
gl3 = gl2;
}
glScalef(gl1, gl2, gl3);
}
glLineWidth(linedotscale*2);
OGL_DISABLE(TEXTURE_2D);
glDisable(GL_CULL_FACE);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
//cross
std::array<GLfloat, 8 * 4> cross_lca;
GLfloat *cross_lca_ptr;
if (cross)
{
for (uint_fast32_t i = 0; i != cross_lca.size(); i += 8)
{
cross_lca[i] = ret_dark_rgba[0];
cross_lca[i+1] = ret_dark_rgba[1];
cross_lca[i+2] = ret_dark_rgba[2];
cross_lca[i+3] = ret_dark_rgba[3];
cross_lca[i+4] = ret_rgba[0];
cross_lca[i+5] = ret_rgba[1];
cross_lca[i+6] = ret_rgba[2];
cross_lca[i+7] = ret_rgba[3];
}
cross_lca_ptr = cross_lca.data();
}
else
{
cross_lca_ptr = dark_lca.data();
}
glColorPointer(4, GL_FLOAT, 0, cross_lca_ptr);
static const std::array<GLfloat, 8 * 2> cross_lva{{
-4.0, 2.0, -2.0, 0, -3.0, -4.0, -2.0, -3.0, 4.0, 2.0, 2.0, 0, 3.0, -4.0, 2.0, -3.0,
}};
glVertexPointer(2, GL_FLOAT, 0, cross_lva.data());
glDrawArrays(GL_LINES, 0, 8);
std::array<GLfloat, 4 * 4> primary_lca0;
GLfloat *lca0_data;
//left primary bar
if(primary == 0)
lca0_data = dark_lca.data();
else
{
primary_lca0[0] = primary_lca0[4] = ret_rgba[0];
primary_lca0[1] = primary_lca0[5] = ret_rgba[1];
primary_lca0[2] = primary_lca0[6] = ret_rgba[2];
primary_lca0[3] = primary_lca0[7] = ret_rgba[3];
primary_lca0[8] = primary_lca0[12] = ret_dark_rgba[0];
primary_lca0[9] = primary_lca0[13] = ret_dark_rgba[1];
primary_lca0[10] = primary_lca0[14] = ret_dark_rgba[2];
primary_lca0[11] = primary_lca0[15] = ret_dark_rgba[3];
lca0_data = primary_lca0.data();
}
glColorPointer(4, GL_FLOAT, 0, lca0_data);
static const std::array<GLfloat, 4 * 2> primary_lva0{{
-5.5, -5.0, -6.5, -7.5, -10.0, -7.0, -10.0, -8.7
}};
static const std::array<GLfloat, 4 * 2> primary_lva1{{
-10.0, -7.0, -10.0, -8.7, -15.0, -8.5, -15.0, -9.5
}};
static const std::array<GLfloat, 4 * 2> primary_lva2{{
5.5, -5.0, 6.5, -7.5, 10.0, -7.0, 10.0, -8.7
}};
static const std::array<GLfloat, 4 * 2> primary_lva3{{
10.0, -7.0, 10.0, -8.7, 15.0, -8.5, 15.0, -9.5
}};
glVertexPointer(2, GL_FLOAT, 0, primary_lva0.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
std::array<GLfloat, 4 * 4> primary_lca1;
GLfloat *lca1_data;
if(primary != 2)
lca1_data = dark_lca.data();
else
{
primary_lca1[8] = primary_lca1[12] = ret_rgba[0];
primary_lca1[9] = primary_lca1[13] = ret_rgba[1];
primary_lca1[10] = primary_lca1[14] = ret_rgba[2];
primary_lca1[11] = primary_lca1[15] = ret_rgba[3];
primary_lca1[0] = primary_lca1[4] = ret_dark_rgba[0];
primary_lca1[1] = primary_lca1[5] = ret_dark_rgba[1];
primary_lca1[2] = primary_lca1[6] = ret_dark_rgba[2];
primary_lca1[3] = primary_lca1[7] = ret_dark_rgba[3];
lca1_data = primary_lca1.data();
}
glColorPointer(4, GL_FLOAT, 0, lca1_data);
glVertexPointer(2, GL_FLOAT, 0, primary_lva1.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//right primary bar
glColorPointer(4, GL_FLOAT, 0, lca0_data);
glVertexPointer(2, GL_FLOAT, 0, primary_lva2.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glColorPointer(4, GL_FLOAT, 0, lca1_data);
glVertexPointer(2, GL_FLOAT, 0, primary_lva3.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
GLfloat *secondary_lva_ptr;
if (secondary<=2){
//left secondary
glColorPointer(4, GL_FLOAT, 0, (secondary != 1 ? dark_lca : bright_lca).data());
if(!secondary_lva[0])
secondary_lva[0] = circle_array_init_2(16, 2.0, -10.0, 2.0, -2.0);
ogl_drawcircle(16, GL_LINE_LOOP, secondary_lva[0].get());
//right secondary
glColorPointer(4, GL_FLOAT, 0, (secondary != 2 ? dark_lca : bright_lca).data());
if(!secondary_lva[1])
secondary_lva[1] = circle_array_init_2(16, 2.0, 10.0, 2.0, -2.0);
secondary_lva_ptr = secondary_lva[1].get();
}
else {
//bottom/middle secondary
glColorPointer(4, GL_FLOAT, 0, (secondary != 4 ? dark_lca : bright_lca).data());
if(!secondary_lva[2])
secondary_lva[2] = circle_array_init_2(16, 2.0, 0.0, 2.0, -8.0);
secondary_lva_ptr = secondary_lva[2].get();
}
ogl_drawcircle(16, GL_LINE_LOOP, secondary_lva_ptr);
//glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
glLineWidth(linedotscale);
}
namespace dcx {
/*
* Stars on heaven in exit sequence, automap objects
*/
void g3_draw_sphere(grs_canvas &canvas, cg3s_point &pnt, fix rad, const uint8_t c)
{
int i;
const float scale = (static_cast<float>(canvas.cv_bitmap.bm_w) / canvas.cv_bitmap.bm_h);
std::array<GLfloat, 20 * 4> color_array;
for (i = 0; i < 20*4; i += 4)
{
color_array[i] = CPAL2Tr(c);
color_array[i+1] = CPAL2Tg(c);
color_array[i+2] = CPAL2Tb(c);
color_array[i+3] = 1.0;
}
OGL_DISABLE(TEXTURE_2D);
glDisable(GL_CULL_FACE);
glPushMatrix();
glTranslatef(f2glf(pnt.p3_vec.x),f2glf(pnt.p3_vec.y),-f2glf(pnt.p3_vec.z));
GLfloat gl1, gl2;
if (scale >= 1)
{
rad/=scale;
gl1 = f2glf(rad);
gl2 = f2glf(rad*scale);
}
else
{
rad*=scale;
gl1 = f2glf(rad/scale);
gl2 = f2glf(rad);
}
glScalef(gl1, gl2, f2glf(rad));
if(!sphere_va)
sphere_va = circle_array_init(20);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 0, color_array.data());
ogl_drawcircle(20, GL_TRIANGLE_FAN, sphere_va.get());
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
}
int gr_ucircle(grs_canvas &canvas, const fix xc1, const fix yc1, const fix r1, const uint8_t c)
{
int nsides;
OGL_DISABLE(TEXTURE_2D);
glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), (canvas.cv_fade_level >= GR_FADE_OFF)?1.0:1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
glPushMatrix();
glTranslatef(
(f2fl(xc1) + canvas.cv_bitmap.bm_x + 0.5) / static_cast<float>(last_width),
1.0 - (f2fl(yc1) + canvas.cv_bitmap.bm_y + 0.5) / static_cast<float>(last_height),0);
glScalef(f2fl(r1) / last_width, f2fl(r1) / last_height, 1.0);
nsides = 10 + 2 * static_cast<int>(M_PI * f2fl(r1) / 19);
if(!circle_va)
circle_va = circle_array_init(nsides);
ogl_drawcircle(nsides, GL_LINE_LOOP, circle_va.get());
glPopMatrix();
return 0;
}
int gr_disk(grs_canvas &canvas, const fix x, const fix y, const fix r, const uint8_t c)
{
int nsides;
OGL_DISABLE(TEXTURE_2D);
glColor4f(CPAL2Tr(c), CPAL2Tg(c), CPAL2Tb(c), (canvas.cv_fade_level >= GR_FADE_OFF) ? 1.0 : 1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
glPushMatrix();
glTranslatef(
(f2fl(x) + canvas.cv_bitmap.bm_x + 0.5) / static_cast<float>(last_width),
1.0 - (f2fl(y) + canvas.cv_bitmap.bm_y + 0.5) / static_cast<float>(last_height),0);
glScalef(f2fl(r) / last_width, f2fl(r) / last_height, 1.0);
nsides = 10 + 2 * static_cast<int>(M_PI * f2fl(r) / 19);
if(!disk_va)
disk_va = circle_array_init(nsides);
ogl_drawcircle(nsides, GL_TRIANGLE_FAN, disk_va.get());
glPopMatrix();
return 0;
}
/*
* Draw flat-shaded Polygon (Lasers, Drone-arms, Driller-ears)
*/
void _g3_draw_poly(grs_canvas &canvas, const uint_fast32_t nv, cg3s_point *const *const pointlist, const uint8_t palette_color_index)
{
struct vfloat
{
GLfloat x, y, z;
};
static_assert(sizeof(vfloat) == sizeof(GLfloat) * 3, "vfloat size wrong");
struct cfloat
{
GLfloat r, g, b, a;
};
static_assert(sizeof(cfloat) == sizeof(GLfloat) * 4, "cfloat size wrong");
RAIIdmem<GLfloat[]> color_array;
auto &&vertices = std::make_unique<GLfloat[]>(nv * 3);
MALLOC(color_array, GLfloat[], nv*4);
r_polyc++;
ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY> cs;
OGL_DISABLE(TEXTURE_2D);
const float color_r = PAL2Tr(palette_color_index), color_g = PAL2Tg(palette_color_index), color_b = PAL2Tb(palette_color_index);
const float color_a = (canvas.cv_fade_level >= GR_FADE_OFF)
? 1.0
: 1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0);
vfloat *const varray = reinterpret_cast<vfloat *>(vertices.get());
cfloat *const carray = reinterpret_cast<cfloat *>(color_array.get());
for (unsigned c=0; c < nv; ++c)
{
carray[c].r = color_r;
carray[c].g = color_g;
carray[c].b = color_b;
carray[c].a = color_a;
auto &p = pointlist[c]->p3_vec;
varray[c].x = f2glf(p.x);
varray[c].y = f2glf(p.y);
varray[c].z = -f2glf(p.z);
}
glVertexPointer(3, GL_FLOAT, 0, varray);
glColorPointer(4, GL_FLOAT, 0, color_array.get());
glDrawArrays(GL_TRIANGLE_FAN, 0, nv);
}
/*
* Everything texturemapped (walls, robots, ship)
*/
void _g3_draw_tmap(grs_canvas &canvas, const unsigned nv, cg3s_point *const *const pointlist, const g3s_uvl *const uvl_list, const g3s_lrgb *const light_rgb, grs_bitmap &bm)
{
GLfloat color_alpha = 1.0;
ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY> cs;
if (tmap_drawer_ptr == draw_tmap) {
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
OGL_ENABLE(TEXTURE_2D);
ogl_bindbmtex(bm, 0);
ogl_texwrap(bm.gltexture, GL_REPEAT);
r_tpolyc++;
color_alpha = (canvas.cv_fade_level >= GR_FADE_OFF) ? 1.0 : (1.0 - static_cast<float>(canvas.cv_fade_level) / (static_cast<float>(GR_FADE_LEVELS) - 1.0));
} else if (tmap_drawer_ptr == draw_tmap_flat) {
OGL_DISABLE(TEXTURE_2D);
/* for cloaked state faces */
color_alpha = 1.0 - (canvas.cv_fade_level / static_cast<GLfloat>(NUM_LIGHTING_LEVELS));
} else {
glmprintf((CON_DEBUG, "g3_draw_tmap: unhandled tmap_drawer"));
return;
}
flatten_array<GLfloat, 3, MAX_POINTS_PER_POLY> vertices;
flatten_array<GLfloat, 4, MAX_POINTS_PER_POLY> color_array;
flatten_array<GLfloat, 2, MAX_POINTS_PER_POLY> texcoord_array;
for (auto &&[point, light, uvl, vert, color, texcoord] : zip(
unchecked_partial_range(pointlist, nv),
unchecked_partial_range(light_rgb, nv),
unchecked_partial_range(uvl_list, nv),
unchecked_partial_range(vertices.nested.data(), nv),
unchecked_partial_range(color_array.nested.data(), nv),
partial_range(texcoord_array.nested, nv)
)
)
{
vert[0] = f2glf(point->p3_vec.x);
vert[1] = f2glf(point->p3_vec.y);
vert[2] = -f2glf(point->p3_vec.z);
color[3] = color_alpha;
if (tmap_drawer_ptr == draw_tmap_flat) {
color[0] = color[1] = color[2] = 0;
}
else
{
if (bm.get_flag_mask(BM_FLAG_NO_LIGHTING))
{
color[0] = color[1] = color[2] = 1.0;
}
else
{
color[0] = f2glf(light.r);
color[1] = f2glf(light.g);
color[2] = f2glf(light.b);
}
texcoord[0] = f2glf(uvl.u);
texcoord[1] = f2glf(uvl.v);
}
}
glVertexPointer(3, GL_FLOAT, 0, vertices.flat.data());
glColorPointer(4, GL_FLOAT, 0, color_array.flat.data());
if (tmap_drawer_ptr == draw_tmap) {
glTexCoordPointer(2, GL_FLOAT, 0, texcoord_array.flat.data());
}
glDrawArrays(GL_TRIANGLE_FAN, 0, nv);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
/*
* Everything texturemapped with secondary texture (walls with secondary texture)
*/
void _g3_draw_tmap_2(grs_canvas &canvas, const unsigned nv, const g3s_point *const *const pointlist, const g3s_uvl *uvl_list, const g3s_lrgb *light_rgb, grs_bitmap &bmbot, grs_bitmap &bm, const unsigned orient)
{
int index2, index3;
RAIIdmem<GLfloat[]> vertices, color_array, texcoord_array;
MALLOC(vertices, GLfloat[], nv*3);
MALLOC(color_array, GLfloat[], nv*4);
MALLOC(texcoord_array, GLfloat[], nv*2);
_g3_draw_tmap(canvas, nv, pointlist, uvl_list, light_rgb, bmbot);//draw the bottom texture first.. could be optimized with multitexturing..
ogl_client_states<int, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY> cs;