-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl_texmgr.c
More file actions
2282 lines (1916 loc) · 53.8 KB
/
Copy pathgl_texmgr.c
File metadata and controls
2282 lines (1916 loc) · 53.8 KB
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
/*
Copyright (C) 1996-1997 Id Software, Inc.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// gl_texmgr.c -- texture manager. manages opengl texture images (adapted from fitzquake)
#include "quakedef.h"
cvar_t gl_picmip = {"gl_picmip", "0", CVAR_NONE};
cvar_t gl_warpimagesize = {"gl_warpimagesize", "256", CVAR_ARCHIVE}; // was 512, for water warp
cvar_t gl_compression = {"gl_compression", "1", CVAR_ARCHIVE};
cvar_t gl_swapinterval = {"gl_swapinterval", "1", CVAR_ARCHIVE};
gltexture_t *notexture;
gltexture_t *nulltexture;
int indexed_bytes = 1;
int rgba_bytes = 4;
int lightmap_bytes = 4;
#define MAXGLMODES 6
typedef struct
{
int magfilter;
int minfilter;
char *name;
} glmode_t;
glmode_t modes[MAXGLMODES] = {
{GL_NEAREST, GL_NEAREST, "GL_NEAREST"},
{GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST, "GL_NEAREST_MIPMAP_NEAREST"},
{GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR, "GL_NEAREST_MIPMAP_LINEAR"},
{GL_LINEAR, GL_LINEAR, "GL_LINEAR"},
{GL_LINEAR, GL_LINEAR_MIPMAP_NEAREST, "GL_LINEAR_MIPMAP_NEAREST"},
{GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, "GL_LINEAR_MIPMAP_LINEAR"},
};
int gl_texturemode = 0; // nearest
int gl_filter_min = GL_NEAREST; // was GL_LINEAR
int gl_filter_mag = GL_NEAREST;
float gl_hardware_max_anisotropy = 1; // just in case
float gl_texture_anisotropy = 1;
GLint gl_hardware_max_size = 1024; // just in case
int warpimage_size = 256; // fitzquake has 512, for water warp
#define MAX_GLTEXTURES 4096 // orig was 1024, prev 2048
gltexture_t *active_gltextures, *free_gltextures;
int numgltextures;
static GLuint currenttexture[3] = {GL_UNUSED_TEXTURE, GL_UNUSED_TEXTURE, GL_UNUSED_TEXTURE}; // to avoid unnecessary texture sets
static GLenum currenttarget = GL_TEXTURE0_ARB;
unsigned int d_8to24table_original[256]; //standard unmodifyed palette
unsigned int d_8to24table_opaque[256]; //standard palette with alpha 255 for all colors
unsigned int d_8to24table[256]; //standard palette, 255 is transparent
unsigned int d_8to24table_fullbright[256]; //fullbright palette, 0-223 are black (for additive blending)
unsigned int d_8to24table_fullbright_holey[256]; //fullbright palette, for holey textures (fence)
unsigned int d_8to24table_nobright[256]; //nobright palette, 224-255 are black (for additive blending)
unsigned int d_8to24table_nobright_holey[256]; //nobright palette, for holey textures (fence)
unsigned int d_8to24table_conchars[256]; //conchars palette, 0 and 255 are transparent
unsigned int is_fullbright[256/32];
char *gl_vendor;
char *gl_renderer;
char *gl_version;
char *gl_extensions;
qboolean fullsbardraw = false;
qboolean isIntel = false; // intel video workaround
qboolean gl_texture_NPOT = false; //ericw
qboolean gl_texture_compression = false; // EER1
qboolean gl_swap_control = false;
int gl_stencilbits;
int gl_depthbits;
int gl_alphabits;
int gl_redbits;
int gl_greenbits;
int gl_bluebits;
/*
================================================
OpenGL Stuff
================================================
*/
/*
===============
GL_CheckExtension
===============
*/
void GL_CheckExtension_Multitexture (void)
{
qboolean ARBmultitexture = false;
int units;
//
// Multitexture
//
ARBmultitexture = strstr (gl_extensions, "GL_ARB_multitexture") != NULL;
if (ARBmultitexture)
{
// Check how many texture units there actually are
glGetIntegerv (GL_MAX_TEXTURE_UNITS_ARB, &units);
qglMultiTexCoord2f = (void *) qglGetProcAddress ("glMultiTexCoord2fARB");
qglActiveTexture = (void *) qglGetProcAddress ("glActiveTextureARB");
qglClientActiveTexture = (void *) qglGetProcAddress ("glClientActiveTextureARB");
if (units < 3)
{
Sys_Error ("Only %i TMU available, but this engine requires minimum 3 TMUs", units);
}
else if (!qglMultiTexCoord2f || !qglActiveTexture || !qglClientActiveTexture)
{
Sys_Error ("Multitexture not supported (qglGetProcAddress failed)");
}
else
{
Con_Printf ("Found GL_ARB_multitexture\n");
Con_Printf (" %i TMUs on hardware\n", units);
}
}
else
{
Sys_Error ("Multitexture not supported (extension not found)");
}
}
void GL_CheckExtension_EnvCombine (void)
{
qboolean EXTcombine, ARBcombine;
//
// Texture combine environment mode
//
ARBcombine = strstr (gl_extensions, "GL_ARB_texture_env_combine") != NULL;
EXTcombine = strstr (gl_extensions, "GL_EXT_texture_env_combine") != NULL;
if (ARBcombine || EXTcombine)
{
Con_Printf ("Found GL_%s_texture_env_combine\n", ARBcombine ? "ARB" : "EXT");
}
else
{
Sys_Error ("Texture combine environment not supported (extension not found)");
}
}
void GL_CheckExtension_EnvAdd (void)
{
qboolean EXTadd, ARBadd;
//
// Texture add environment mode
//
ARBadd = strstr (gl_extensions, "GL_ARB_texture_env_add") != NULL;
EXTadd = strstr (gl_extensions, "GL_EXT_texture_env_add") != NULL;
if (ARBadd || EXTadd)
{
Con_Printf ("Found GL_%s_texture_env_add\n", ARBadd ? "ARB" : "EXT");
}
else
{
Sys_Error ("Texture add environment not supported (extension not found)");
}
}
void GL_CheckExtension_NPoT (void)
{
qboolean npot;
//
// Texture non power of two
//
npot = strstr (gl_extensions, "GL_ARB_texture_non_power_of_two") != NULL;
if (COM_CheckParm("-notexturenpot"))
{
Con_Warning ("Texture non power of two disabled at command line\n");
}
else if (npot)
{
Con_Printf ("Found GL_ARB_texture_non_power_of_two\n");
gl_texture_NPOT = true;
}
else
{
Con_Warning ("Texture non power of two not supported (extension not found)\n");
}
}
void GL_CheckExtension_TextureCompression (void)
{
qboolean ARBcompression, EXTcompression;
//
// Texture compression mode
//
ARBcompression = strstr (gl_extensions, "GL_ARB_texture_compression") != NULL;
EXTcompression = strstr (gl_extensions, "GL_EXT_texture_compression_s3tc") != NULL;
if (COM_CheckParm("-nocompression"))
{
Con_Warning ("Texture compression disabled at command line\n");
}
else if (ARBcompression || EXTcompression)
{
qglCompressedTexImage2D = (void *) qglGetProcAddress ("glCompressedTexImage2D");
if (!qglCompressedTexImage2D)
{
Con_Warning ("Texture compression not supported (qglGetProcAddress failed)\n");
}
else
{
if (ARBcompression)
{
// query for the available compression formats
GLint num = 0;
glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num);
if (num)
{
GLint formats[num];
glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats);
qboolean DXT1ext = false;
qboolean DXT5ext = false;
for (int i = 0; i < num; i++)
{
if (formats[i] == GL_COMPRESSED_RGB_S3TC_DXT1_EXT)
DXT1ext = true;
if (formats[i] == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)
DXT5ext = true;
}
if (DXT1ext && DXT5ext)
gl_texture_compression = true;
}
}
if (EXTcompression)
gl_texture_compression = true;
if (gl_texture_compression)
Con_Printf ("Found GL_%s_texture_compression%s\n", ARBcompression ? "ARB" : "EXT", ARBcompression ? "" : "_s3tc");
else
Con_Warning ("Texture compression not supported (compression formats unavailable)\n");
}
}
else
{
Con_Warning ("Texture compression not supported (extension not found)\n");
}
}
void GL_CheckExtension_FramebufferObject (void)
{
qboolean ARBobject, EXTobject;
//
// Texture framebuffer object (generate mipmap)
//
ARBobject = strstr (gl_extensions, "GL_ARB_framebuffer_object") != NULL;
EXTobject = strstr (gl_extensions, "GL_EXT_framebuffer_object") != NULL;
if (COM_CheckParm("-nogenmipmap"))
{
Con_Warning ("Texture generate mipmap disabled at command line\n");
}
else if (ARBobject || EXTobject)
{
if (ARBobject)
qglGenerateMipmap = (void *) qglGetProcAddress ("glGenerateMipmap");
else if (EXTobject)
qglGenerateMipmap = (void *) qglGetProcAddress ("glGenerateMipmapEXT");
if (qglGenerateMipmap)
{
Con_Printf ("Found GL_%s_framebuffer_object\n", ARBobject ? "ARB" : "EXT");
Con_Printf ("Found glGenerateMipmap%s\n", ARBobject ? "" : "EXT");
}
else
Con_Warning ("Texture generate mipmap not supported (qglGetProcAddress failed)\n");
}
else
{
Con_Warning ("Texture generate mipmap not supported (extension not found)\n");
}
}
void GL_CheckExtension_Anisotropy (void)
{
qboolean anisotropy;
//
// Anisotropic filtering
//
anisotropy = strstr (gl_extensions, "GL_EXT_texture_filter_anisotropic") != NULL;
if (COM_CheckParm("-noanisotropy"))
{
Con_Warning ("Anisotropic filtering disabled at command line\n");
}
else if (anisotropy)
{
float test1, test2;
GLuint tex;
// test to make sure we really have control over it
// 1.0 and 2.0 should always be legal values
glGenTextures (1, &tex);
glBindTexture (GL_TEXTURE_2D, tex);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
glGetTexParameterfv (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &test1);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.0f);
glGetTexParameterfv (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, &test2);
glDeleteTextures (1, &tex);
if (test1 == 1 && test2 == 2)
Con_Printf ("Found GL_EXT_texture_filter_anisotropic\n");
else
Con_Warning ("Anisotropic filtering locked by driver. Current driver setting is %f\n", test1);
// get max value either way, so the menu and stuff know it
glGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_hardware_max_anisotropy);
}
else
{
Con_Warning ("Anisotropic filtering not supported (extension not found)\n");
}
}
#if defined __APPLE__ && defined __MACH__
GLint gl_swapintervalstate = 0;
#endif
void GL_CheckExtension_VSync (void)
{
qboolean SWAPcontrol;
//
// Swap control
//
#ifdef _WIN32
SWAPcontrol = strstr (gl_extensions, SWAPCONTROLSTRING) != NULL;
#elif defined GLX_GLXEXT_PROTOTYPES
SWAPcontrol = strstr (glx_extensions, SWAPCONTROLSTRING) != NULL;
#elif defined __APPLE__ && defined __MACH__
SWAPcontrol = true;
#endif
if (COM_CheckParm("-novsync"))
{
Con_Warning ("Vertical sync disabled at command line\n");
}
else if (SWAPcontrol)
{
#if defined __APPLE__ && defined __MACH__
GLint state;
CGLError glerr = CGLGetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &state);
if (glerr == kCGLNoError) {
Con_Printf ("%s sync to vertical retrace\n", (state == 1) ? "Enabled" : "Disabled");
gl_swapintervalstate = state;
gl_swap_control = true;
} else {
Con_Warning ("Unable to get CGL swap interval\n");
}
#else
qglSwapInterval = (void *) qglGetProcAddress (SWAPINTERVALFUNC);
if (qglSwapInterval)
{
if (!qglSwapInterval(0))
Con_Warning ("Vertical sync not supported (%s failed)\n", SWAPINTERVALFUNC);
else
{
Con_Printf ("Found %s\n", SWAPCONTROLSTRING);
gl_swap_control = true;
}
}
else
Con_Warning ("Vertical sync not supported (qglGetProcAddress failed)\n");
#endif
}
else
Con_Warning ("Vertical sync not supported (extension not found)\n");
}
/*
===============
GL_CheckSwapInterval -- check vsync after vid_restart
===============
*/
void GL_CheckSwapInterval (void)
{
if (gl_swap_control)
{
#if defined __APPLE__ && defined __MACH__
GLint state = gl_swapintervalstate;
CGLError glerr = CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &state);
if (glerr == kCGLNoError) {
Con_Printf ("%s sync to vertical retrace\n", (state == 1) ? "Enabled" : "Disabled");
} else {
Con_Warning ("Unable to set CGL swap interval\n");
}
#else
if (!qglSwapInterval((gl_swapinterval.value) ? 1 : 0))
Con_Printf ("GL_SwapInterval: failed on %s\n", SWAPINTERVALFUNC);
#endif
}
}
/*
===============
GL_SwapInterval
===============
*/
void GL_SwapInterval (void)
{
if (gl_swap_control)
{
#if defined __APPLE__ && defined __MACH__
const GLint state = (gl_swapinterval.value) ? 1 : 0;
if (state == gl_swapintervalstate)
return;
CGLError glerr = CGLSetParameter(CGLGetCurrentContext(), kCGLCPSwapInterval, &state);
if (glerr == kCGLNoError) {
Con_Printf ("%s CGL swap interval\n", (state == 1) ? "Enabled" : "Disabled");
gl_swapintervalstate = state;
} else {
Con_Warning ("Unable to set CGL swap interval\n");
}
#else
if (!qglSwapInterval((gl_swapinterval.value) ? 1 : 0))
Con_Printf ("GL_SwapInterval: failed on %s\n", SWAPINTERVALFUNC);
#endif
}
}
void GL_CheckMultithreadedGL (void)
{
if (has_smp)
{
#if defined __APPLE__ && defined __MACH__
CGLError glerr = CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
if (glerr == kCGLNoError) {
Con_Printf("Enabled multi-threaded GL engine\n");
}
#endif
}
}
/*
===============
GL_CheckExtensions
===============
*/
void GL_CheckExtensions (void)
{
//
// poll max size from hardware
//
glGetIntegerv (GL_MAX_TEXTURE_SIZE, &gl_hardware_max_size);
Con_Printf ("Maximum texture size %i\n", gl_hardware_max_size);
// by default we sets maxsize as hardware maxsize
GL_CheckExtension_Multitexture ();
GL_CheckExtension_EnvCombine ();
GL_CheckExtension_EnvAdd ();
GL_CheckExtension_NPoT ();
GL_CheckExtension_TextureCompression ();
GL_CheckExtension_FramebufferObject ();
GL_CheckExtension_Anisotropy ();
GL_CheckExtension_VSync ();
}
/*
===============
GL_MakeNiceExtensionsList -- johnfitz
===============
*/
char *GL_MakeNiceExtensionsList (char *in)
{
char *copy, *token, *out;
int i, count;
if (!in)
return Z_Strdup ("(none)");
// each space will be replaced by 4 chars, so count the spaces before we malloc
for (i = 0, count = 1; i < (int)strlen(in); i++)
if (in[i] == ' ')
count++;
out = Z_Malloc (strlen(in) + count*3 + 1); // usually about 1-2k
out[0] = 0;
copy = Z_Strdup (in);
for (token = strtok(copy, " "); token; token = strtok(NULL, " "))
{
strcat(out, "\n ");
strcat(out, token);
}
Z_Free (copy);
return out;
}
void GL_Info (qboolean all)
{
static char *gl_extensions_nice = NULL;
#ifdef GLX_GLXEXT_PROTOTYPES
static char *glx_extensions_nice = NULL;
#endif
if (!gl_extensions_nice)
gl_extensions_nice = GL_MakeNiceExtensionsList (gl_extensions);
#ifdef GLX_GLXEXT_PROTOTYPES
if (!glx_extensions_nice)
glx_extensions_nice = GL_MakeNiceExtensionsList (glx_extensions);
#endif
Con_SafePrintf ("GL_VENDOR: %s\n", gl_vendor);
Con_SafePrintf ("GL_RENDERER: %s\n", gl_renderer);
Con_SafePrintf ("GL_VERSION: %s\n", gl_version);
if (all)
{
Con_SafePrintf ("GL_EXTENSIONS: %s\n", gl_extensions_nice);
#ifdef GLX_GLXEXT_PROTOTYPES
Con_SafePrintf ("GLX_EXTENSIONS: %s\n", glx_extensions_nice);
#endif
}
}
/*
===============
GL_Info_f
===============
*/
void GL_Info_f (void)
{
char *command;
if (Cmd_Argc() < 2)
{
Con_Printf("commands: ");
Con_Printf("short, all, pf\n");
return;
}
command = Cmd_Argv (1);
if (strcasecmp(command, "short") == 0)
{
GL_Info (false);
return;
}
if (strcasecmp(command, "all") == 0)
{
GL_Info (true);
return;
}
if (strcasecmp(command, "pf") == 0)
{
GL_GetPixelFormatInfo ();
return;
}
}
//=============================================================================
/*
================
GL_Set2D
Setup as if the screen was 320*200
================
*/
void GL_Set2D (void)
{
//
// set up viewpoint
//
glViewport (glx, gly, glwidth, glheight);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, glwidth, glheight, 0, -99999, 99999);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
//
// set drawing parms
//
glDisable (GL_DEPTH_TEST);
glDisable (GL_CULL_FACE);
glDisable (GL_BLEND);
glEnable (GL_ALPHA_TEST);
glColor4f (1,1,1,1);
}
/*
===============
GL_SetupState
does all the stuff from GL_Init that needs to be done every time a new GL render context is created
GL_Init will still do the stuff that only needs to be done once
===============
*/
void GL_SetupState (void)
{
glClearColor (0.15,0.15,0.15,0); // originally was 1,0,0,0
// glCullFace (GL_FRONT);
glCullFace (GL_BACK); // johnfitz -- glquake used CCW with backwards culling -- let's do it right
glFrontFace (GL_CW); // johnfitz -- glquake used CCW with backwards culling -- let's do it right
glEnable (GL_TEXTURE_2D);
glEnable (GL_ALPHA_TEST);
glAlphaFunc (GL_GREATER, 0.666);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glShadeModel (GL_FLAT);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glDepthRange (0, 1); // moved here because gl_ztrick is gone.
glDepthFunc (GL_LEQUAL); // moved here because gl_ztrick is gone.
// set up global fog
glFogi (GL_FOG_MODE, GL_EXP2);
glHint (GL_FOG_HINT, GL_NICEST); /* per pixel */
}
void GL_GetPixelFormatInfo (void)
{
// read stencil bits
glGetIntegerv (GL_STENCIL_BITS, &gl_stencilbits);
// read depth bits
glGetIntegerv (GL_DEPTH_BITS, &gl_depthbits);
// read color bits
glGetIntegerv (GL_RED_BITS, &gl_redbits);
glGetIntegerv (GL_GREEN_BITS, &gl_greenbits);
glGetIntegerv (GL_BLUE_BITS, &gl_bluebits);
// read alpha bits
glGetIntegerv (GL_ALPHA_BITS, &gl_alphabits);
Con_Printf ("Pixel format\n"
" RGBA (%d/%d/%d/%d bits)\n"
" Z-buffer (%d bits)\n"
" stencil (%d bits)\n",
gl_redbits, gl_greenbits, gl_bluebits, gl_alphabits,
gl_depthbits,
gl_stencilbits);
}
void GL_GetGLInfo (void)
{
// gl_info
gl_vendor = (char *)glGetString (GL_VENDOR);
gl_renderer = (char *)glGetString (GL_RENDERER);
gl_version = (char *)glGetString (GL_VERSION);
gl_extensions = (char *)glGetString (GL_EXTENSIONS);
}
/*
===============
GL_Init
===============
*/
void GL_Init (void)
{
GL_GetGLInfo ();
// gl_info
Con_SafePrintf ("GL_VENDOR: %s\n", gl_vendor);
Con_SafePrintf ("GL_RENDERER: %s\n", gl_renderer);
Con_SafePrintf ("GL_VERSION: %s\n", gl_version);
GL_CheckExtensions ();
GL_CheckMultithreadedGL ();
Cvar_RegisterVariableCallback (&gl_swapinterval, GL_SwapInterval);
Cmd_AddCommand ("gl_info", GL_Info_f);
if (!strcmp(gl_vendor, "Intel")) // intel video workaround
{
Con_Printf ("Intel Display Adapter detected\n");
isIntel = true;
}
Con_Printf ("OpenGL initialized\n");
}
/*
================
GL_BindTexture
================
*/
void GL_BindTexture (gltexture_t *texture)
{
if (!texture)
texture = nulltexture;
if (texture->texnum != currenttexture[currenttarget - GL_TEXTURE0_ARB])
{
currenttexture[currenttarget - GL_TEXTURE0_ARB] = texture->texnum;
glBindTexture (GL_TEXTURE_2D, texture->texnum);
}
}
/*
================
GL_DeleteTexture -- ericw
Wrapper around glDeleteTextures that also clears the given texture number
from our per-TMU cached texture binding table.
================
*/
void GL_DeleteTexture (gltexture_t *texture)
{
glDeleteTextures (1, &texture->texnum);
if (texture->texnum == currenttexture[0]) currenttexture[0] = GL_UNUSED_TEXTURE;
if (texture->texnum == currenttexture[1]) currenttexture[1] = GL_UNUSED_TEXTURE;
if (texture->texnum == currenttexture[2]) currenttexture[2] = GL_UNUSED_TEXTURE;
texture->texnum = 0;
}
/*
================
GL_SelectTexture
================
*/
void GL_SelectTexture (GLenum target)
{
// if (target == currenttarget)
// return;
qglActiveTexture (target);
currenttarget = target;
}
void GL_SelectTMU0 (void)
{
if (GL_TEXTURE0_ARB == currenttarget)
return;
glDisable (GL_TEXTURE_2D);
GL_SelectTexture (GL_TEXTURE0_ARB);
}
void GL_SelectTMU1 (void)
{
if (GL_TEXTURE1_ARB == currenttarget)
return;
GL_SelectTexture (GL_TEXTURE1_ARB);
glEnable (GL_TEXTURE_2D);
}
void GL_SelectTMU2 (void)
{
if (GL_TEXTURE2_ARB == currenttarget)
return;
GL_SelectTexture (GL_TEXTURE2_ARB);
glEnable (GL_TEXTURE_2D);
}
void GL_SelectTMU3 (void)
{
if (GL_TEXTURE3_ARB == currenttarget)
return;
GL_SelectTexture (GL_TEXTURE3_ARB);
glEnable (GL_TEXTURE_2D);
}
//=============================================================================
/*
===============
GL_SetFilterModes
===============
*/
void GL_SetFilterModes (gltexture_t *glt)
{
if (glt->flags & TEXPREF_NEAREST)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
else if (glt->flags & TEXPREF_LINEAR)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
else if (glt->flags & TEXPREF_MIPMAP)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, modes[gl_texturemode].magfilter);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, modes[gl_texturemode].minfilter);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, gl_texture_anisotropy);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, modes[gl_texturemode].magfilter);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, modes[gl_texturemode].magfilter);
}
}
/*
===============
GL_TextureMode_f
===============
*/
void GL_TextureMode_f (void)
{
int i;
char *arg;
gltexture_t *glt;
if (Cmd_Argc() == 1)
{
for (i=0 ; i< MAXGLMODES ; i++)
{
if (gl_filter_min == modes[i].minfilter)
{
Con_Printf ("gl_texturemode is %s (%d)\n", modes[i].name, i + 1);
return;
}
}
}
for (i=0 ; i< MAXGLMODES ; i++)
{
arg = Cmd_Argv(1);
if (!strcasecmp (modes[i].name, arg ) || (isdigit(*arg) && atoi(arg) - 1 == i))
break;
}
if (i == MAXGLMODES)
{
Con_Printf ("bad filter name, available are:\n");
for (i=0 ; i< MAXGLMODES ; i++)
Con_Printf ("%s (%d)\n", modes[i].name, i + 1);
return;
}
gl_texturemode = i;
gl_filter_min = modes[gl_texturemode].minfilter;
gl_filter_mag = modes[gl_texturemode].magfilter;
// change all the existing texture objects
for (glt=active_gltextures; glt; glt=glt->next)
{
GL_BindTexture (glt);
GL_SetFilterModes (glt);
}
Sbar_Changed (); // sbar graphics need to be redrawn with new filter mode
}
/*
===============
GL_Texture_Anisotropy_f
===============
*/
void GL_Texture_Anisotropy_f (void)
{
gltexture_t *glt;
if (Cmd_Argc() == 1)
{
Con_Printf ("gl_texture_anisotropy is %f, hardware limit is %f\n", gl_texture_anisotropy, gl_hardware_max_anisotropy);
return;
}
gl_texture_anisotropy = CLAMP(1.0f, atof (Cmd_Argv(1)), gl_hardware_max_anisotropy);
for (glt=active_gltextures; glt; glt=glt->next)
{
GL_BindTexture (glt);
GL_SetFilterModes (glt);
}
}
//=============================================================================
/*
================
TexMgr_NewGame
================
*/
void TexMgr_NewGame (void)
{
TexMgr_FreeTextures (0, TEXPREF_PERSIST); // deletes all textures where TEXPREF_PERSIST is unset
TexMgr_LoadPalette ();
}
/*
===============
TexMgr_UploadWarpImage
called during init,
choose correct warpimage size and reload existing warpimage textures if needed
===============
*/
void TexMgr_UploadWarpImage (void)
{
int mark;
gltexture_t *glt;
byte *dummy;
//
// find the new correct size
//
if ((int)gl_warpimagesize.value < 32)
Cvar_SetValueNoCallback ("gl_warpimagesize", 32);
//
// make sure warpimage size is a power of two
//
warpimage_size = TexMgr_SafeTextureSize((int)gl_warpimagesize.value);
while (warpimage_size*2 > vid.width) // *2 for glow
warpimage_size >>= 1;
while (warpimage_size > vid.height)
warpimage_size >>= 1;
if (warpimage_size != gl_warpimagesize.value)
Cvar_SetValueNoCallback ("gl_warpimagesize", warpimage_size);
// ericw -- removed early exit if (warpimage_size == oldsize).
// after reloads textures to source width/height, which might not match oldsize.
//
// resize the textures in opengl
//
mark = Hunk_LowMark ();
dummy = Hunk_Alloc (warpimage_size*warpimage_size*4);
for (glt=active_gltextures; glt; glt=glt->next)
{
if (glt->flags & TEXPREF_WARPIMAGE)
{
GL_BindTexture (glt);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, warpimage_size, warpimage_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, dummy);