-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_backend.c
1921 lines (1774 loc) · 53.8 KB
/
gl_backend.c
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
#include "quakedef.h"
#include "cl_collision.h"
cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
cvar_t gl_mesh_prefer_short_elements = {0, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
qboolean v_flipped_state = false;
r_viewport_t gl_viewport;
matrix4x4_t gl_modelmatrix;
matrix4x4_t gl_viewmatrix;
matrix4x4_t gl_modelviewmatrix;
matrix4x4_t gl_projectionmatrix;
matrix4x4_t gl_modelviewprojectionmatrix;
float gl_modelview16f[16];
float gl_modelviewprojection16f[16];
qboolean gl_modelmatrixchanged;
int gl_maxdrawrangeelementsvertices;
int gl_maxdrawrangeelementsindices;
#ifdef DEBUGGL
int errornumber = 0;
void GL_PrintError(int errornumber, char *filename, int linenumber)
{
switch(errornumber)
{
#ifdef GL_INVALID_ENUM
case GL_INVALID_ENUM:
Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_INVALID_VALUE
case GL_INVALID_VALUE:
Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_INVALID_OPERATION
case GL_INVALID_OPERATION:
Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_STACK_OVERFLOW
case GL_STACK_OVERFLOW:
Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_STACK_UNDERFLOW
case GL_STACK_UNDERFLOW:
Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_OUT_OF_MEMORY
case GL_OUT_OF_MEMORY:
Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_TABLE_TOO_LARGE
case GL_TABLE_TOO_LARGE:
Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
break;
#endif
#ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
break;
#endif
default:
Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
break;
}
}
#endif
#define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
void SCR_ScreenShot_f (void);
typedef struct gl_bufferobjectinfo_s
{
int target;
int object;
size_t size;
char name[MAX_QPATH];
}
gl_bufferobjectinfo_t;
typedef struct gltextureunit_s
{
const void *pointer_texcoord;
size_t pointer_texcoord_offset;
int pointer_texcoord_buffer;
int t2d, t3d, tcubemap, trectangle;
int arrayenabled;
unsigned int arraycomponents;
int rgbscale, alphascale;
int combine;
int combinergb, combinealpha;
// texmatrixenabled exists only to avoid unnecessary texmatrix compares
int texmatrixenabled;
matrix4x4_t matrix;
}
gltextureunit_t;
typedef struct gl_state_s
{
int cullface;
int cullfaceenable;
int blendfunc1;
int blendfunc2;
int blend;
GLboolean depthmask;
int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
int depthtest;
float depthrange[2];
float polygonoffset[2];
int alphatest;
int scissortest;
unsigned int unit;
unsigned int clientunit;
gltextureunit_t units[MAX_TEXTUREUNITS];
float color4f[4];
int lockrange_first;
int lockrange_count;
int vertexbufferobject;
int elementbufferobject;
qboolean pointer_color_enabled;
const void *pointer_vertex;
const void *pointer_color;
size_t pointer_vertex_offset;
size_t pointer_color_offset;
int pointer_vertex_buffer;
int pointer_color_buffer;
memexpandablearray_t bufferobjectinfoarray;
qboolean active;
}
gl_state_t;
static gl_state_t gl_state;
/*
note: here's strip order for a terrain row:
0--1--2--3--4
|\ |\ |\ |\ |
| \| \| \| \|
A--B--C--D--E
clockwise
A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
*elements++ = i + row;
*elements++ = i;
*elements++ = i + row + 1;
*elements++ = i;
*elements++ = i + 1;
*elements++ = i + row + 1;
for (y = 0;y < rows - 1;y++)
{
for (x = 0;x < columns - 1;x++)
{
i = y * rows + x;
*elements++ = i + columns;
*elements++ = i;
*elements++ = i + columns + 1;
*elements++ = i;
*elements++ = i + 1;
*elements++ = i + columns + 1;
}
}
alternative:
0--1--2--3--4
| /| /|\ | /|
|/ |/ | \|/ |
A--B--C--D--E
counterclockwise
for (y = 0;y < rows - 1;y++)
{
for (x = 0;x < columns - 1;x++)
{
i = y * rows + x;
*elements++ = i;
*elements++ = i + columns;
*elements++ = i + columns + 1;
*elements++ = i + columns;
*elements++ = i + columns + 1;
*elements++ = i + 1;
}
}
*/
int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
int quadelement3i[QUADELEMENTS_MAXQUADS*6];
unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
void GL_VBOStats_f(void)
{
GL_Mesh_ListVBOs(true);
}
static void GL_Backend_ResetState(void);
static void gl_backend_start(void)
{
memset(&gl_state, 0, sizeof(gl_state));
Mem_ExpandableArray_NewArray(&gl_state.bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
Con_DPrintf("OpenGL backend started.\n");
CHECKGLERROR
GL_Backend_ResetState();
}
static void gl_backend_shutdown(void)
{
Con_DPrint("OpenGL Backend shutting down\n");
Mem_ExpandableArray_FreeArray(&gl_state.bufferobjectinfoarray);
memset(&gl_state, 0, sizeof(gl_state));
}
static void gl_backend_newmap(void)
{
}
void gl_backend_init(void)
{
int i;
for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
{
polygonelement3s[i * 3 + 0] = 0;
polygonelement3s[i * 3 + 1] = i + 1;
polygonelement3s[i * 3 + 2] = i + 2;
}
// elements for rendering a series of quads as triangles
for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
{
quadelement3s[i * 6 + 0] = i * 4;
quadelement3s[i * 6 + 1] = i * 4 + 1;
quadelement3s[i * 6 + 2] = i * 4 + 2;
quadelement3s[i * 6 + 3] = i * 4;
quadelement3s[i * 6 + 4] = i * 4 + 2;
quadelement3s[i * 6 + 5] = i * 4 + 3;
}
for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
polygonelement3i[i] = polygonelement3s[i];
for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
quadelement3i[i] = quadelement3s[i];
Cvar_RegisterVariable(&r_render);
Cvar_RegisterVariable(&r_renderview);
Cvar_RegisterVariable(&r_waterwarp);
Cvar_RegisterVariable(&gl_polyblend);
Cvar_RegisterVariable(&v_flipped);
Cvar_RegisterVariable(&gl_dither);
Cvar_RegisterVariable(&gl_vbo);
Cvar_RegisterVariable(&gl_paranoid);
Cvar_RegisterVariable(&gl_printcheckerror);
Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
Cvar_RegisterVariable(&gl_mesh_testarrayelement);
Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
}
void GL_SetMirrorState(qboolean state);
void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
{
vec4_t temp;
float iw;
Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
iw = 1.0f / out[3];
out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
}
static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
{
float q[4];
float d;
float clipPlane[4], v3[3], v4[3];
float normal[3];
// This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
VectorSet(normal, normalx, normaly, normalz);
Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
VectorScale(normal, dist, v3);
Matrix4x4_Transform(&v->viewmatrix, v3, v4);
// FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
clipPlane[3] = -DotProduct(v4, clipPlane);
#if 0
{
// testing code for comparing results
float clipPlane2[4];
VectorCopy4(clipPlane, clipPlane2);
R_EntityMatrix(&identitymatrix);
VectorSet(q, normal[0], normal[1], normal[2], -dist);
qglClipPlane(GL_CLIP_PLANE0, q);
qglGetClipPlane(GL_CLIP_PLANE0, q);
VectorCopy4(q, clipPlane);
}
#endif
// Calculate the clip-space corner point opposite the clipping plane
// as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
// transform it into camera space by multiplying it
// by the inverse of the projection matrix
q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
q[2] = -1.0f;
q[3] = (1.0f + m[10]) / m[14];
// Calculate the scaled plane vector
d = 2.0f / DotProduct4(clipPlane, q);
// Replace the third row of the projection matrix
m[2] = clipPlane[0] * d;
m[6] = clipPlane[1] * d;
m[10] = clipPlane[2] * d + 1.0f;
m[14] = clipPlane[3] * d;
}
void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
{
float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
float m[16];
memset(v, 0, sizeof(*v));
v->type = R_VIEWPORTTYPE_ORTHO;
v->cameramatrix = *cameramatrix;
v->x = x;
v->y = y;
v->z = 0;
v->width = width;
v->height = height;
v->depth = 1;
memset(m, 0, sizeof(m));
m[0] = 2/(right - left);
m[5] = 2/(top - bottom);
m[10] = -2/(zFar - zNear);
m[12] = - (right + left)/(right - left);
m[13] = - (top + bottom)/(top - bottom);
m[14] = - (zFar + zNear)/(zFar - zNear);
m[15] = 1;
v->screentodepth[0] = -farclip / (farclip - nearclip);
v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
if (nearplane)
R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
#if 0
{
vec4_t test1;
vec4_t test2;
Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
R_Viewport_TransformToScreen(v, test1, test2);
Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
}
#endif
}
void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
{
matrix4x4_t tempmatrix, basematrix;
float m[16];
memset(v, 0, sizeof(*v));
if(v_flipped.integer)
frustumx = -frustumx;
v->type = R_VIEWPORTTYPE_PERSPECTIVE;
v->cameramatrix = *cameramatrix;
v->x = x;
v->y = y;
v->z = 0;
v->width = width;
v->height = height;
v->depth = 1;
memset(m, 0, sizeof(m));
m[0] = 1.0 / frustumx;
m[5] = 1.0 / frustumy;
m[10] = -(farclip + nearclip) / (farclip - nearclip);
m[11] = -1;
m[14] = -2 * nearclip * farclip / (farclip - nearclip);
v->screentodepth[0] = -farclip / (farclip - nearclip);
v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
if (nearplane)
R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
}
void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
{
matrix4x4_t tempmatrix, basematrix;
const float nudge = 1.0 - 1.0 / (1<<23);
float m[16];
memset(v, 0, sizeof(*v));
if(v_flipped.integer)
frustumx = -frustumx;
v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
v->cameramatrix = *cameramatrix;
v->x = x;
v->y = y;
v->z = 0;
v->width = width;
v->height = height;
v->depth = 1;
memset(m, 0, sizeof(m));
m[ 0] = 1.0 / frustumx;
m[ 5] = 1.0 / frustumy;
m[10] = -nudge;
m[11] = -1;
m[14] = -2 * nearclip * nudge;
v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
v->screentodepth[1] = m[14] * -0.5;
Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
if (nearplane)
R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
}
float cubeviewmatrix[6][16] =
{
// standard cubemap projections
{ // +X
0, 0,-1, 0,
0,-1, 0, 0,
-1, 0, 0, 0,
0, 0, 0, 1,
},
{ // -X
0, 0, 1, 0,
0,-1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 1,
},
{ // +Y
1, 0, 0, 0,
0, 0,-1, 0,
0, 1, 0, 0,
0, 0, 0, 1,
},
{ // -Y
1, 0, 0, 0,
0, 0, 1, 0,
0,-1, 0, 0,
0, 0, 0, 1,
},
{ // +Z
1, 0, 0, 0,
0,-1, 0, 0,
0, 0,-1, 0,
0, 0, 0, 1,
},
{ // -Z
-1, 0, 0, 0,
0,-1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
},
};
float rectviewmatrix[6][16] =
{
// sign-preserving cubemap projections
{ // +X
0, 0,-1, 0,
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 1,
},
{ // -X
0, 0, 1, 0,
0, 1, 0, 0,
1, 0, 0, 0,
0, 0, 0, 1,
},
{ // +Y
1, 0, 0, 0,
0, 0,-1, 0,
0, 1, 0, 0,
0, 0, 0, 1,
},
{ // -Y
1, 0, 0, 0,
0, 0, 1, 0,
0, 1, 0, 0,
0, 0, 0, 1,
},
{ // +Z
1, 0, 0, 0,
0, 1, 0, 0,
0, 0,-1, 0,
0, 0, 0, 1,
},
{ // -Z
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
},
};
void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
{
matrix4x4_t tempmatrix, basematrix;
float m[16];
memset(v, 0, sizeof(*v));
v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
v->cameramatrix = *cameramatrix;
v->width = size;
v->height = size;
v->depth = 1;
memset(m, 0, sizeof(m));
m[0] = m[5] = 1.0f;
m[10] = -(farclip + nearclip) / (farclip - nearclip);
m[11] = -1;
m[14] = -2 * nearclip * farclip / (farclip - nearclip);
Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
if (nearplane)
R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
}
void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
{
matrix4x4_t tempmatrix, basematrix;
float m[16];
memset(v, 0, sizeof(*v));
v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
v->cameramatrix = *cameramatrix;
v->x = (side & 1) * size;
v->y = (side >> 1) * size;
v->width = size;
v->height = size;
v->depth = 1;
memset(m, 0, sizeof(m));
m[0] = m[5] = 1.0f * ((float)size - border) / size;
m[10] = -(farclip + nearclip) / (farclip - nearclip);
m[11] = -1;
m[14] = -2 * nearclip * farclip / (farclip - nearclip);
Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
if (nearplane)
R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
}
void R_SetViewport(const r_viewport_t *v)
{
float m[16];
gl_viewport = *v;
CHECKGLERROR
qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
// FIXME: v_flipped_state is evil, this probably breaks somewhere
GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
// copy over the matrices to our state
gl_viewmatrix = v->viewmatrix;
gl_projectionmatrix = v->projectmatrix;
switch(vid.renderpath)
{
case RENDERPATH_GL20:
case RENDERPATH_CGGL:
// break;
case RENDERPATH_GL13:
case RENDERPATH_GL11:
// Load the projection matrix into OpenGL
qglMatrixMode(GL_PROJECTION);CHECKGLERROR
Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
qglLoadMatrixf(m);CHECKGLERROR
qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
break;
}
// force an update of the derived matrices
gl_modelmatrixchanged = true;
R_EntityMatrix(&gl_modelmatrix);
}
void R_GetViewport(r_viewport_t *v)
{
*v = gl_viewport;
}
static void GL_BindVBO(int bufferobject)
{
if (gl_state.vertexbufferobject != bufferobject)
{
gl_state.vertexbufferobject = bufferobject;
CHECKGLERROR
qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
CHECKGLERROR
}
}
static void GL_BindEBO(int bufferobject)
{
if (gl_state.elementbufferobject != bufferobject)
{
gl_state.elementbufferobject = bufferobject;
CHECKGLERROR
qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
CHECKGLERROR
}
}
static void GL_Backend_ResetState(void)
{
unsigned int i;
gl_state.active = true;
gl_state.depthtest = true;
gl_state.alphatest = false;
gl_state.blendfunc1 = GL_ONE;
gl_state.blendfunc2 = GL_ZERO;
gl_state.blend = false;
gl_state.depthmask = GL_TRUE;
gl_state.colormask = 15;
gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
gl_state.lockrange_first = 0;
gl_state.lockrange_count = 0;
gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
gl_state.cullfaceenable = true;
gl_state.polygonoffset[0] = 0;
gl_state.polygonoffset[1] = 0;
CHECKGLERROR
qglColorMask(1, 1, 1, 1);
qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
qglDisable(GL_ALPHA_TEST);CHECKGLERROR
qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
qglDisable(GL_BLEND);CHECKGLERROR
qglCullFace(gl_state.cullface);CHECKGLERROR
qglEnable(GL_CULL_FACE);CHECKGLERROR
qglDepthFunc(GL_LEQUAL);CHECKGLERROR
qglEnable(GL_DEPTH_TEST);CHECKGLERROR
qglDepthMask(gl_state.depthmask);CHECKGLERROR
qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
if (vid.support.arb_vertex_buffer_object)
{
qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
}
if (vid.support.ext_framebuffer_object)
{
qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
GL_Color(0, 0, 0, 0);
GL_Color(1, 1, 1, 1);
gl_state.unit = MAX_TEXTUREUNITS;
gl_state.clientunit = MAX_TEXTUREUNITS;
switch(vid.renderpath)
{
case RENDERPATH_GL20:
case RENDERPATH_CGGL:
for (i = 0;i < vid.teximageunits;i++)
{
GL_ActiveTexture(i);
qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
if (vid.support.ext_texture_3d)
{
qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
}
if (vid.support.arb_texture_cube_map)
{
qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
}
if (vid.support.arb_texture_rectangle)
{
qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
}
}
for (i = 0;i < vid.texarrayunits;i++)
{
GL_ClientActiveTexture(i);
GL_BindVBO(0);
qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
}
CHECKGLERROR
break;
case RENDERPATH_GL13:
case RENDERPATH_GL11:
for (i = 0;i < vid.texunits;i++)
{
GL_ActiveTexture(i);
GL_ClientActiveTexture(i);
qglDisable(GL_TEXTURE_2D);CHECKGLERROR
qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
if (vid.support.ext_texture_3d)
{
qglDisable(GL_TEXTURE_3D);CHECKGLERROR
qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
}
if (vid.support.arb_texture_cube_map)
{
qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
}
if (vid.support.arb_texture_rectangle)
{
qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
}
GL_BindVBO(0);
qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
qglMatrixMode(GL_TEXTURE);CHECKGLERROR
qglLoadIdentity();CHECKGLERROR
qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
}
CHECKGLERROR
break;
}
}
void GL_ActiveTexture(unsigned int num)
{
if (gl_state.unit != num)
{
gl_state.unit = num;
if (qglActiveTexture)
{
CHECKGLERROR
qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
CHECKGLERROR
}
}
}
void GL_ClientActiveTexture(unsigned int num)
{
if (gl_state.clientunit != num)
{
gl_state.clientunit = num;
if (qglActiveTexture)
{
CHECKGLERROR
qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
CHECKGLERROR
}
}
}
void GL_BlendFunc(int blendfunc1, int blendfunc2)
{
if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
{
CHECKGLERROR
qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
if (gl_state.blendfunc2 == GL_ZERO)
{
if (gl_state.blendfunc1 == GL_ONE)
{
if (gl_state.blend)
{
gl_state.blend = 0;
qglDisable(GL_BLEND);CHECKGLERROR
}
}
else
{
if (!gl_state.blend)
{
gl_state.blend = 1;
qglEnable(GL_BLEND);CHECKGLERROR
}
}
}
else
{
if (!gl_state.blend)
{
gl_state.blend = 1;
qglEnable(GL_BLEND);CHECKGLERROR
}
}
}
}
void GL_DepthMask(int state)
{
if (gl_state.depthmask != state)
{
CHECKGLERROR
qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
}
}
void GL_DepthTest(int state)
{
if (gl_state.depthtest != state)
{
gl_state.depthtest = state;
CHECKGLERROR
if (gl_state.depthtest)
{
qglEnable(GL_DEPTH_TEST);CHECKGLERROR
}
else
{
qglDisable(GL_DEPTH_TEST);CHECKGLERROR
}
}
}
void GL_DepthRange(float nearfrac, float farfrac)
{
if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
{
gl_state.depthrange[0] = nearfrac;
gl_state.depthrange[1] = farfrac;
qglDepthRange(nearfrac, farfrac);
}
}
void GL_PolygonOffset(float planeoffset, float depthoffset)
{
if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
{
gl_state.polygonoffset[0] = planeoffset;
gl_state.polygonoffset[1] = depthoffset;
qglPolygonOffset(planeoffset, depthoffset);
}
}
void GL_SetMirrorState(qboolean state)
{
if(!state != !v_flipped_state)
{
// change cull face mode!
if(gl_state.cullface == GL_BACK)
qglCullFace((gl_state.cullface = GL_FRONT));
else if(gl_state.cullface == GL_FRONT)
qglCullFace((gl_state.cullface = GL_BACK));
}
v_flipped_state = state;
}
void GL_CullFace(int state)
{
CHECKGLERROR
if(v_flipped_state)
{
if(state == GL_FRONT)
state = GL_BACK;
else if(state == GL_BACK)
state = GL_FRONT;
}
if (state != GL_NONE)
{
if (!gl_state.cullfaceenable)
{
gl_state.cullfaceenable = true;
qglEnable(GL_CULL_FACE);CHECKGLERROR
}
if (gl_state.cullface != state)
{
gl_state.cullface = state;
qglCullFace(gl_state.cullface);CHECKGLERROR
}
}
else
{
if (gl_state.cullfaceenable)
{
gl_state.cullfaceenable = false;
qglDisable(GL_CULL_FACE);CHECKGLERROR
}
}
}
void GL_AlphaTest(int state)
{
if (gl_state.alphatest != state)
{
gl_state.alphatest = state;
CHECKGLERROR
if (gl_state.alphatest)
{
qglEnable(GL_ALPHA_TEST);CHECKGLERROR
}
else
{
qglDisable(GL_ALPHA_TEST);CHECKGLERROR
}
}
}
void GL_ColorMask(int r, int g, int b, int a)
{
int state = r*8 + g*4 + b*2 + a*1;
if (gl_state.colormask != state)
{
gl_state.colormask = state;
CHECKGLERROR
qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
}
}
void GL_Color(float cr, float cg, float cb, float ca)
{
if (gl_state.pointer_color_enabled || gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
{
gl_state.color4f[0] = cr;
gl_state.color4f[1] = cg;
gl_state.color4f[2] = cb;
gl_state.color4f[3] = ca;
CHECKGLERROR
qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
CHECKGLERROR
}
}
void GL_Scissor (int x, int y, int width, int height)
{
CHECKGLERROR
qglScissor(x, y,width,height);
CHECKGLERROR