-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtr_surface.cpp
More file actions
1530 lines (1229 loc) · 44.2 KB
/
tr_surface.cpp
File metadata and controls
1530 lines (1229 loc) · 44.2 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) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
This file is part of Daemon source code.
Daemon source code 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.
Daemon source code 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 Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_surface.c
#include "tr_local.h"
#include "gl_shader.h"
#include "Material.h"
/*
==============================================================================
THIS ENTIRE FILE IS BACK END!
backEnd.currentEntity will be valid.
Tess_Begin has already been called for the surface's shader.
The modelview matrix will be set.
It is safe to actually issue drawing commands here if you don't want to
use the shader system.
==============================================================================
*/
static transform_t bones[ MAX_BONES ];
/*
==============
Tess_EndBegin
Flush the buffered polygons and prepare to receive more with the same parameters
==============
*/
void Tess_EndBegin()
{
Tess_End();
Tess_Begin( tess.stageIteratorFunc, tess.surfaceShader, tess.skipTangents,
tess.lightmapNum, tess.fogNum, tess.bspSurface );
}
/*
==============
Tess_CheckVBOAndIBO
Bind the buffers and flush any data that came from a different buffer.
For a multidraw-using static VBO surface, it performs an analogous function to Tess_CheckOverflow,
with the assumption that only one multidraw primitive will be used.
==============
*/
static void Tess_CheckVBOAndIBO( VBO_t *vbo, IBO_t *ibo )
{
if ( glState.currentVBO != vbo || glState.currentIBO != ibo || tess.multiDrawPrimitives >= MAX_MULTIDRAW_PRIMITIVES )
{
Tess_EndBegin();
R_BindVBO( vbo );
R_BindIBO( ibo );
}
}
/*
==============
Tess_CheckOverflow
Used for non-VBO surfaces. Check that tess.verts and tess.indexes have sufficient room for the
data that is about to be written and ensure that the default VBO and IBO are bound. If there
is old data from a different surface or if there is too much data already, flush it.
==============
*/
void Tess_CheckOverflow( int verts, int indexes )
{
// FIXME: need to check if a vbo is bound, otherwise we fail on startup
if ( glState.currentVBO != nullptr && glState.currentIBO != nullptr )
{
Tess_CheckVBOAndIBO( tess.vbo, tess.ibo );
}
if ( tess.numVertexes + verts < SHADER_MAX_VERTEXES && tess.numIndexes + indexes < SHADER_MAX_INDEXES )
{
return;
}
GLIMP_LOGCOMMENT( "--- Tess_CheckOverflow(%i + %i vertices, %i + %i triangles ) ---",
tess.numVertexes, verts,( tess.numIndexes / 3 ), indexes );
Tess_End();
if ( verts >= SHADER_MAX_VERTEXES )
{
Sys::Drop( "Tess_CheckOverflow: verts > max (%d > %d)", verts, SHADER_MAX_VERTEXES );
}
if ( indexes >= SHADER_MAX_INDEXES )
{
Sys::Drop( "Tess_CheckOverflow: indexes > max (%d > %d)", indexes, SHADER_MAX_INDEXES );
}
Tess_Begin( tess.stageIteratorFunc, tess.surfaceShader, tess.skipTangents,
tess.lightmapNum, tess.fogNum, tess.bspSurface );
}
/*
==============
Tess_SurfaceVertsAndTris
Defines ATTR_POSITION, ATTR_TEXCOORD, ATTR_COLOR, ATTR_QTANGENT
==============
*/
static void Tess_SurfaceVertsAndTris( const srfVert_t *verts, const srfTriangle_t *triangles, int numVerts, int numTriangles )
{
int i;
const srfTriangle_t *tri = triangles;
const srfVert_t *vert = verts;
const int numIndexes = numTriangles * 3;
Tess_CheckOverflow( numVerts, numIndexes );
for ( i = 0; i < numIndexes; i+=3, tri++ )
{
tess.indexes[ tess.numIndexes + i + 0 ] = tess.numVertexes + tri->indexes[ 0 ];
tess.indexes[ tess.numIndexes + i + 1 ] = tess.numVertexes + tri->indexes[ 1 ];
tess.indexes[ tess.numIndexes + i + 2 ] = tess.numVertexes + tri->indexes[ 2 ];
}
tess.numIndexes += numIndexes;
for ( i = 0; i < numVerts; i++, vert++ )
{
VectorCopy( vert->xyz, tess.verts[ tess.numVertexes + i ].xyz );
Vector4Copy( vert->qtangent, tess.verts[ tess.numVertexes + i ].qtangents );
tess.verts[ tess.numVertexes + i ].texCoords[ 0 ] = vert->st[ 0 ];
tess.verts[ tess.numVertexes + i ].texCoords[ 1 ] = vert->st[ 1 ];
tess.verts[ tess.numVertexes + i ].texCoords[ 2 ] = vert->lightmap[ 0 ];
tess.verts[ tess.numVertexes + i ].texCoords[ 3 ] = vert->lightmap[ 1 ];
tess.verts[ tess.numVertexes + i ].color = vert->lightColor;
}
tess.numVertexes += numVerts;
}
static bool Tess_SurfaceVBO( VBO_t *vbo, IBO_t *ibo, int numIndexes, int firstIndex )
{
if ( ( !vbo && !tr.skipVBO ) || !ibo )
{
return false;
}
Tess_CheckVBOAndIBO( vbo, ibo );
//lazy merge multidraws together
bool mergeBack = false;
bool mergeFront = false;
glIndex_t *firstIndexOffset = ( glIndex_t* ) BUFFER_OFFSET( firstIndex * sizeof( glIndex_t ) );
if ( tess.multiDrawPrimitives > 0 )
{
int lastPrimitive = tess.multiDrawPrimitives - 1;
glIndex_t *lastIndexOffset = firstIndexOffset + numIndexes;
glIndex_t *prevLastIndexOffset = tess.multiDrawIndexes[ lastPrimitive ] + tess.multiDrawCounts[ lastPrimitive ];
if ( firstIndexOffset == prevLastIndexOffset )
{
mergeFront = true;
}
else if ( lastIndexOffset == tess.multiDrawIndexes[ lastPrimitive ] )
{
mergeBack = true;
}
}
if ( mergeFront )
{
tess.multiDrawCounts[ tess.multiDrawPrimitives - 1 ] += numIndexes;
}
else if ( mergeBack )
{
tess.multiDrawIndexes[ tess.multiDrawPrimitives - 1 ] = firstIndexOffset;
tess.multiDrawOffsets[ tess.multiDrawPrimitives - 1 ] = (GLuint) firstIndex;
tess.multiDrawCounts[ tess.multiDrawPrimitives - 1 ] += numIndexes;
}
else
{
tess.multiDrawIndexes[ tess.multiDrawPrimitives ] = firstIndexOffset;
tess.multiDrawOffsets[ tess.multiDrawPrimitives ] = (GLuint) firstIndex;
tess.multiDrawCounts[ tess.multiDrawPrimitives ] = numIndexes;
tess.multiDrawPrimitives++;
}
return true;
}
/*
==============
Tess_AddQuadStampExt
Defines ATTR_POSITION, ATTR_QTANGENT, ATTR_COLOR, ATTR_TEXCOORD
==============
*/
void Tess_AddQuadStampExt( vec3_t origin, vec3_t left, vec3_t up, const Color::Color& color, float s1, float t1, float s2, float t2 )
{
int i;
vec3_t normal;
int ndx;
GLIMP_LOGCOMMENT( "--- Tess_AddQuadStampExt ---" );
Tess_CheckOverflow( 4, 6 );
ndx = tess.numVertexes;
// triangle indexes for a simple quad
tess.indexes[ tess.numIndexes ] = ndx;
tess.indexes[ tess.numIndexes + 1 ] = ndx + 1;
tess.indexes[ tess.numIndexes + 2 ] = ndx + 3;
tess.indexes[ tess.numIndexes + 3 ] = ndx + 3;
tess.indexes[ tess.numIndexes + 4 ] = ndx + 1;
tess.indexes[ tess.numIndexes + 5 ] = ndx + 2;
tess.verts[ ndx ].xyz[ 0 ] = origin[ 0 ] + left[ 0 ] + up[ 0 ];
tess.verts[ ndx ].xyz[ 1 ] = origin[ 1 ] + left[ 1 ] + up[ 1 ];
tess.verts[ ndx ].xyz[ 2 ] = origin[ 2 ] + left[ 2 ] + up[ 2 ];
tess.verts[ ndx + 1 ].xyz[ 0 ] = origin[ 0 ] - left[ 0 ] + up[ 0 ];
tess.verts[ ndx + 1 ].xyz[ 1 ] = origin[ 1 ] - left[ 1 ] + up[ 1 ];
tess.verts[ ndx + 1 ].xyz[ 2 ] = origin[ 2 ] - left[ 2 ] + up[ 2 ];
tess.verts[ ndx + 2 ].xyz[ 0 ] = origin[ 0 ] - left[ 0 ] - up[ 0 ];
tess.verts[ ndx + 2 ].xyz[ 1 ] = origin[ 1 ] - left[ 1 ] - up[ 1 ];
tess.verts[ ndx + 2 ].xyz[ 2 ] = origin[ 2 ] - left[ 2 ] - up[ 2 ];
tess.verts[ ndx + 3 ].xyz[ 0 ] = origin[ 0 ] + left[ 0 ] - up[ 0 ];
tess.verts[ ndx + 3 ].xyz[ 1 ] = origin[ 1 ] + left[ 1 ] - up[ 1 ];
tess.verts[ ndx + 3 ].xyz[ 2 ] = origin[ 2 ] + left[ 2 ] - up[ 2 ];
// constant normal all the way around
VectorSubtract( vec3_origin, backEnd.viewParms.orientation.axis[ 0 ], normal );
i16vec4_t qtangents;
R_TBNtoQtangents( left, up, normal, qtangents );
Vector4Copy( qtangents, tess.verts[ ndx ].qtangents );
Vector4Copy( qtangents, tess.verts[ ndx + 1 ].qtangents );
Vector4Copy( qtangents, tess.verts[ ndx + 2 ].qtangents );
Vector4Copy( qtangents, tess.verts[ ndx + 3 ].qtangents );
// standard square texture coordinates
tess.verts[ ndx ].texCoords[ 0 ] = s1;
tess.verts[ ndx ].texCoords[ 1 ] = t1;
tess.verts[ ndx + 1 ].texCoords[ 0 ] = s2;
tess.verts[ ndx + 1 ].texCoords[ 1 ] = t1;
tess.verts[ ndx + 2 ].texCoords[ 0 ] = s2;
tess.verts[ ndx + 2 ].texCoords[ 1 ] = t2;
tess.verts[ ndx + 3 ].texCoords[ 0 ] = s1;
tess.verts[ ndx + 3 ].texCoords[ 1 ] = t2;
// constant color all the way around
// should this be identity and let the shader specify from entity?
Color::Color32Bit iColor = color;
for ( i = 0; i < 4; i++ )
{
tess.verts[ ndx + i ].color = iColor;
}
tess.numVertexes += 4;
tess.numIndexes += 6;
}
/*
==============
Tess_AddQuadStamp
==============
*/
void Tess_AddQuadStamp( vec3_t origin, vec3_t left, vec3_t up, const Color::Color& color )
{
Tess_AddQuadStampExt( origin, left, up, color, 0, 0, 1, 1 );
}
/*
==============
Tess_AddQuadStampExt2
Defines ATTR_POSITION, ATTR_COLOR, ATTR_TEXCOORD, ATTR_QTANGENT
==============
*/
void Tess_AddQuadStampExt2( vec4_t quadVerts[ 4 ], const Color::Color& color, float s1, float t1, float s2, float t2 )
{
int i;
vec3_t normal, tangent, binormal;
int ndx;
GLIMP_LOGCOMMENT( "--- Tess_AddQuadStampExt2 ---" );
Tess_CheckOverflow( 4, 6 );
ndx = tess.numVertexes;
// triangle indexes for a simple quad
tess.indexes[ tess.numIndexes ] = ndx;
tess.indexes[ tess.numIndexes + 1 ] = ndx + 1;
tess.indexes[ tess.numIndexes + 2 ] = ndx + 3;
tess.indexes[ tess.numIndexes + 3 ] = ndx + 3;
tess.indexes[ tess.numIndexes + 4 ] = ndx + 1;
tess.indexes[ tess.numIndexes + 5 ] = ndx + 2;
VectorCopy( quadVerts[ 0 ], tess.verts[ ndx + 0 ].xyz );
VectorCopy( quadVerts[ 1 ], tess.verts[ ndx + 1 ].xyz );
VectorCopy( quadVerts[ 2 ], tess.verts[ ndx + 2 ].xyz );
VectorCopy( quadVerts[ 3 ], tess.verts[ ndx + 3 ].xyz );
// constant normal all the way around
vec2_t st[ 3 ] = { { s1, t1 }, { s2, t1 }, { s2, t2 } };
R_CalcFaceNormal( normal, quadVerts[ 0 ], quadVerts[ 1 ], quadVerts[ 2 ] );
R_CalcTangents( tangent, binormal,
quadVerts[ 0 ], quadVerts[ 1 ], quadVerts[ 2 ],
st[ 0 ], st[ 1 ], st[ 2 ] );
//if ( !calcNormals )
//{
// VectorNegate( backEnd.viewParms.orientation.axis[ 0 ], normal );
//}
i16vec4_t qtangents;
R_TBNtoQtangents( tangent, binormal, normal, qtangents );
Vector4Copy( qtangents, tess.verts[ ndx ].qtangents );
Vector4Copy( qtangents, tess.verts[ ndx + 1 ].qtangents );
Vector4Copy( qtangents, tess.verts[ ndx + 2 ].qtangents );
Vector4Copy( qtangents, tess.verts[ ndx + 3 ].qtangents );
// standard square texture coordinates
tess.verts[ ndx ].texCoords[ 0 ] = s1;
tess.verts[ ndx ].texCoords[ 1 ] = t1;
tess.verts[ ndx + 1 ].texCoords[ 0 ] = s2;
tess.verts[ ndx + 1 ].texCoords[ 1 ] = t1;
tess.verts[ ndx + 2 ].texCoords[ 0 ] = s2;
tess.verts[ ndx + 2 ].texCoords[ 1 ] = t2;
tess.verts[ ndx + 3 ].texCoords[ 0 ] = s1;
tess.verts[ ndx + 3 ].texCoords[ 1 ] = t2;
// constant color all the way around
// should this be identity and let the shader specify from entity?
Color::Color32Bit iColor = color;
for ( i = 0; i < 4; i++ )
{
tess.verts[ ndx + i ].color = iColor;
}
tess.numVertexes += 4;
tess.numIndexes += 6;
}
/*
==============
Tess_AddQuadStamp2
==============
*/
void Tess_AddQuadStamp2( vec4_t quadVerts[ 4 ], const Color::Color& color )
{
Tess_AddQuadStampExt2( quadVerts, color, 0, 0, 1, 1 );
}
void Tess_AddQuadStamp2WithNormals( vec4_t quadVerts[ 4 ], const Color::Color& color )
{
Tess_AddQuadStampExt2( quadVerts, color, 0, 0, 1, 1 );
}
// Defines ATTR_POSITION, ATTR_COLOR
void Tess_AddTetrahedron( vec4_t tetraVerts[ 4 ], const Color::Color& colorf )
{
int k;
Tess_CheckOverflow( 12, 12 );
Color::Color32Bit color = colorf;
// ground triangle
for ( k = 0; k < 3; k++ )
{
VectorCopy( tetraVerts[ k ], tess.verts[ tess.numVertexes ].xyz );
tess.verts[ tess.numVertexes ].color = color;
tess.indexes[ tess.numIndexes++ ] = tess.numVertexes;
tess.numVertexes++;
}
// side triangles
for ( k = 0; k < 3; k++ )
{
VectorCopy( tetraVerts[ 3 ], tess.verts[ tess.numVertexes ].xyz ); // offset
tess.verts[ tess.numVertexes ].color = color;
tess.indexes[ tess.numIndexes++ ] = tess.numVertexes;
tess.numVertexes++;
VectorCopy( tetraVerts[ k ], tess.verts[ tess.numVertexes ].xyz );
tess.verts[ tess.numVertexes ].color = color;
tess.indexes[ tess.numIndexes++ ] = tess.numVertexes;
tess.numVertexes++;
VectorCopy( tetraVerts[( k + 1 ) % 3 ], tess.verts[ tess.numVertexes ].xyz );
tess.verts[ tess.numVertexes ].color = color;
tess.indexes[ tess.numIndexes++ ] = tess.numVertexes;
tess.numVertexes++;
}
}
void Tess_AddCube( const vec3_t position, const vec3_t minSize, const vec3_t maxSize, const Color::Color& color )
{
vec4_t quadVerts[ 4 ];
vec3_t mins;
vec3_t maxs;
VectorAdd( position, minSize, mins );
VectorAdd( position, maxSize, maxs );
Vector4Set( quadVerts[ 0 ], mins[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], mins[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], mins[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], mins[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Tess_AddQuadStamp2( quadVerts, color );
Vector4Set( quadVerts[ 0 ], maxs[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], maxs[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], maxs[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2( quadVerts, color );
Vector4Set( quadVerts[ 0 ], mins[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], mins[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], maxs[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Tess_AddQuadStamp2( quadVerts, color );
Vector4Set( quadVerts[ 0 ], maxs[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], maxs[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], mins[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], mins[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2( quadVerts, color );
Vector4Set( quadVerts[ 0 ], mins[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], mins[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], maxs[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], maxs[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2( quadVerts, color );
Vector4Set( quadVerts[ 0 ], maxs[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], mins[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], mins[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2( quadVerts, color );
}
void Tess_AddCubeWithNormals( const vec3_t position, const vec3_t minSize, const vec3_t maxSize, const Color::Color& color )
{
vec4_t quadVerts[ 4 ];
vec3_t mins;
vec3_t maxs;
VectorAdd( position, minSize, mins );
VectorAdd( position, maxSize, maxs );
Vector4Set( quadVerts[ 0 ], mins[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], mins[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], mins[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], mins[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Tess_AddQuadStamp2WithNormals( quadVerts, color );
Vector4Set( quadVerts[ 0 ], maxs[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], maxs[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], maxs[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2WithNormals( quadVerts, color );
Vector4Set( quadVerts[ 0 ], mins[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], mins[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], maxs[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Tess_AddQuadStamp2WithNormals( quadVerts, color );
Vector4Set( quadVerts[ 0 ], maxs[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], maxs[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], mins[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], mins[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2WithNormals( quadVerts, color );
Vector4Set( quadVerts[ 0 ], mins[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], mins[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], maxs[ 0 ], mins[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], maxs[ 0 ], mins[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2WithNormals( quadVerts, color );
Vector4Set( quadVerts[ 0 ], maxs[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Vector4Set( quadVerts[ 1 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 2 ], mins[ 0 ], maxs[ 1 ], maxs[ 2 ], 1 );
Vector4Set( quadVerts[ 3 ], mins[ 0 ], maxs[ 1 ], mins[ 2 ], 1 );
Tess_AddQuadStamp2WithNormals( quadVerts, color );
}
void Tess_InstantScreenSpaceQuad() {
GLIMP_LOGCOMMENT( "--- Tess_InstantScreenSpaceQuad ---" );
if ( glConfig2.gpuShader4Available )
{
tr.skipVBO = true;
Tess_Begin( Tess_StageIteratorDummy, nullptr, true, -1, 0 );
rb_surfaceTable[Util::ordinal( *( tr.genericTriangle->surface ) )]( tr.genericTriangle->surface );
Tess_DrawElements();
tr.skipVBO = false;
}
else
{
Tess_Begin( Tess_StageIteratorDummy, nullptr, true, -1, 0 );
rb_surfaceTable[Util::ordinal( *( tr.genericQuad->surface ) )]( tr.genericQuad->surface );
GL_VertexAttribsState( ATTR_POSITION );
Tess_DrawElements();
}
GL_CheckErrors();
Tess_Clear();
}
void Tess_InstantQuad( u_ModelViewProjectionMatrix &shader, const float x, const float y, const float width, const float height )
{
GLIMP_LOGCOMMENT( "--- Tess_InstantQuad ---" );
Tess_Begin( Tess_StageIteratorDummy, nullptr, true, -1, 0 );
/* We don't use x, y, width, height directly to make it compatible
with R_InitGenericVBOs() in tr_vbo.cpp.
See: https://github.com/DaemonEngine/Daemon/pull/1739 */
matrix_t modelViewMatrix;
MatrixCopy( matrixIdentity, modelViewMatrix );
modelViewMatrix[12] = 0.5f * width + x;
modelViewMatrix[13] = 0.5f * height + y;
modelViewMatrix[0] = 0.5f * width;
modelViewMatrix[5] = 0.5f * height;
GL_LoadModelViewMatrix( modelViewMatrix );
shader.SetUniform_ModelViewProjectionMatrix(
glState.modelViewProjectionMatrix[ glState.stackIndex ] );
rb_surfaceTable[Util::ordinal( *( tr.genericQuad->surface ) )]( tr.genericQuad->surface );
GL_VertexAttribsState( ATTR_POSITION | ATTR_TEXCOORD | ATTR_COLOR );
Tess_DrawElements();
GL_CheckErrors();
Tess_Clear();
}
/*
==============
Tess_SurfaceSprite
==============
*/
static const float NORMAL_EPSILON = 0.0001;
static void Tess_SurfaceSprite()
{
vec3_t delta, left, up;
float radius;
GLIMP_LOGCOMMENT( "--- Tess_SurfaceSprite ---" );
radius = backEnd.currentEntity->e.radius;
if ( tess.surfaceShader->autoSpriteMode != 0 && !tess.surfaceShader->autoSpriteWarned )
{
// This function does similarly to autosprite mode 1. Autospriting it again would be a
// waste and would probably lose the rotation angle
Log::Warn( "RT_SPRITE entity should NOT configure its shader (%s) as autosprite",
tess.surfaceShader->name );
tess.surfaceShader->autoSpriteWarned = true;
}
VectorSubtract( backEnd.currentEntity->e.origin, backEnd.viewParms.orientation.origin, delta );
if( VectorNormalize( delta ) < NORMAL_EPSILON )
return;
vec3_t forward;
if ( tess.surfaceShader->entitySpriteFaceViewDirection )
{
// Face opposite to view direction, triggered by RSF_SPRITE.
// Good for particles that may appear very close to the viewer and thus have extreme
// difference between the view direction and the direction to the viewer, so
// as to avoid cases where they appear obviously planar
VectorCopy( backEnd.viewParms.orientation.axis[ 0 ], forward );
}
else
{
// Face toward viewer. Used by light flares
VectorCopy( delta, forward );
}
CrossProduct( backEnd.viewParms.orientation.axis[ 2 ], forward, left );
if( VectorNormalize( left ) < NORMAL_EPSILON )
VectorSet( left, 1, 0, 0 );
if( backEnd.currentEntity->e.rotation != 0 )
RotatePointAroundVector( left, forward, left, backEnd.currentEntity->e.rotation );
CrossProduct( forward, left, up );
VectorScale( left, radius, left );
VectorScale( up, radius, up );
if ( backEnd.viewParms.isMirror )
VectorSubtract( vec3_origin, left, left );
Color::Color32Bit color = backEnd.currentEntity->e.shaderRGBA;
color = tr.convertColorFromSRGB( color );
Tess_AddQuadStamp( backEnd.currentEntity->e.origin, left, up, color );
}
/*
=============
Tess_SurfacePolychain
Defines ATTR_POSITION, ATTR_TEXCOORD, ATTR_COLOR, and maybe ATTR_QTANGENT
=============
*/
static void Tess_SurfacePolychain( srfPoly_t *p )
{
int i;
GLIMP_LOGCOMMENT( "--- Tess_SurfacePolychain ---" );
int numVertexes = p->numVerts;
int numIndexes = 3 * (p->numVerts - 2);
Tess_CheckOverflow( numVertexes, numIndexes );
if ( tess.skipTangents )
{
// fan triangles into the tess array
for (i = 0; i < p->numVerts; i++)
{
VectorCopy(p->verts[i].xyz, tess.verts[tess.numVertexes + i].xyz);
Color::Color32Bit color = Color::Adapt( p->verts[ i ].modulate );
color = tr.convertColorFromSRGB( color );
tess.verts[tess.numVertexes + i].color = color;
tess.verts[tess.numVertexes + i].texCoords[0] = p->verts[i].st[0];
tess.verts[tess.numVertexes + i].texCoords[1] = p->verts[i].st[1];
}
// generate fan indexes into the tess array
for (i = 0; i < p->numVerts - 2; i++)
{
tess.indexes[tess.numIndexes + i * 3 + 0] = tess.numVertexes;
tess.indexes[tess.numIndexes + i * 3 + 1] = tess.numVertexes + i + 1;
tess.indexes[tess.numIndexes + i * 3 + 2] = tess.numVertexes + i + 2;
}
}
else
{
vec3_t tangent, *tangents;
vec3_t binormal, *binormals;
vec3_t normal, *normals;
tangents = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
binormals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
normals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
for ( i = 0; i < numVertexes; i++ )
{
VectorClear( tangents[ i ] );
VectorClear( binormals[ i ] );
VectorClear( normals[ i ] );
}
// generate fan indexes into the tess array and generate tangent vectors for the triangles
for (i = 0; i < p->numVerts - 2; i++ )
{
polyVert_t* v0 = &p->verts[0];
polyVert_t* v1 = &p->verts[i + 1];
polyVert_t* v2 = &p->verts[i + 2];
R_CalcFaceNormal( normal, v0->xyz, v1->xyz, v2->xyz );
R_CalcTangents( tangent, binormal, v0->xyz, v1->xyz, v2->xyz, v0->st, v1->st, v2->st );
VectorAdd(tangents[0], tangent, tangents[0]);
VectorAdd(normals[0], normal, normals[0]);
VectorAdd(binormals[0], binormal, binormals[0]);
VectorAdd(tangents[i + 1], tangent, tangents[i + 1]);
VectorAdd(binormals[i + 1], binormal, binormals[i + 1]);
VectorAdd(normals[i + 1], normal, normals[i + 1]);
VectorAdd(tangents[i + 2], tangent, tangents[i + 2]);
VectorAdd(binormals[i + 2], binormal, binormals[i + 2]);
VectorAdd(normals[i + 2], normal, normals[i + 2]);
tess.indexes[tess.numIndexes + i * 3 + 0] = tess.numVertexes;
tess.indexes[tess.numIndexes + i * 3 + 1] = tess.numVertexes + i + 1;
tess.indexes[tess.numIndexes + i * 3 + 2] = tess.numVertexes + i + 2;
}
// generate qtangents
for ( i = 0; i < numVertexes; i++ )
{
i16vec4_t qtangents;
VectorNormalizeFast(normals[i]);
R_TBNtoQtangents(tangents[i], binormals[i],
normals[i], qtangents);
VectorCopy(p->verts[i].xyz, tess.verts[tess.numVertexes + i].xyz);
Color::Color32Bit color = Color::Adapt( p->verts[ i ].modulate );
color = tr.convertColorFromSRGB( color );
tess.verts[tess.numVertexes + i].color = color;
Vector4Copy(qtangents, tess.verts[tess.numVertexes + i].qtangents);
tess.verts[tess.numVertexes + i].texCoords[0] = p->verts[i].st[0];
tess.verts[tess.numVertexes + i].texCoords[1] = p->verts[i].st[1];
}
ri.Hunk_FreeTempMemory( normals );
ri.Hunk_FreeTempMemory( binormals );
ri.Hunk_FreeTempMemory( tangents );
}
tess.numIndexes += numIndexes;
tess.numVertexes += numVertexes;
}
/*
==============
Tess_SurfaceFace
==============
*/
static void Tess_SurfaceFace( srfGeneric_t* srf )
{
GLIMP_LOGCOMMENT( "--- Tess_SurfaceFace ---" );
if ( !r_vboFaces->integer || !Tess_SurfaceVBO( srf->vbo, srf->ibo, srf->numTriangles * 3, srf->firstIndex ) )
{
Tess_SurfaceVertsAndTris( srf->verts, srf->triangles, srf->numVerts, srf->numTriangles );
}
}
/*
=============
Tess_SurfaceGrid
=============
*/
static void Tess_SurfaceGrid( srfGridMesh_t *srf )
{
GLIMP_LOGCOMMENT( "--- Tess_SurfaceGrid ---" );
if ( !r_vboCurves->integer || !Tess_SurfaceVBO( srf->vbo, srf->ibo, srf->numTriangles * 3, srf->firstIndex ) )
{
Tess_SurfaceVertsAndTris( srf->verts, srf->triangles, srf->numVerts, srf->numTriangles );
}
}
/*
=============
Tess_SurfaceTriangles
=============
*/
static void Tess_SurfaceTriangles( srfGeneric_t* srf )
{
GLIMP_LOGCOMMENT( "--- Tess_SurfaceTriangles ---" );
if ( !r_vboTriangles->integer || !Tess_SurfaceVBO( srf->vbo, srf->ibo, srf->numTriangles * 3, srf->firstIndex ) )
{
Tess_SurfaceVertsAndTris( srf->verts, srf->triangles, srf->numVerts, srf->numTriangles );
}
}
//================================================================================
/*
=============
Tess_SurfaceMDV
Defines ATTR_POSITION, ATTR_TEXCOORD, and maybe ATTR_QTANGENT
=============
*/
static void Tess_SurfaceMDV( mdvSurface_t *srf )
{
int i, j;
int numIndexes = 0;
int numVertexes;
mdvXyz_t *oldVert, *newVert;
mdvNormal_t *oldNormal, *newNormal;
mdvSt_t *st;
srfTriangle_t *tri;
float backlerp;
float oldXyzScale, newXyzScale;
GLIMP_LOGCOMMENT( "--- Tess_SurfaceMDV ---" );
if ( backEnd.currentEntity->e.oldframe == backEnd.currentEntity->e.frame )
{
backlerp = 0;
}
else
{
backlerp = backEnd.currentEntity->e.backlerp;
}
newXyzScale = ( 1.0f - backlerp );
oldXyzScale = backlerp;
Tess_CheckOverflow( srf->numVerts, srf->numTriangles * 3 );
numIndexes = srf->numTriangles * 3;
for ( i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++ )
{
tess.indexes[ tess.numIndexes + i * 3 + 0 ] = tess.numVertexes + tri->indexes[ 0 ];
tess.indexes[ tess.numIndexes + i * 3 + 1 ] = tess.numVertexes + tri->indexes[ 1 ];
tess.indexes[ tess.numIndexes + i * 3 + 2 ] = tess.numVertexes + tri->indexes[ 2 ];
}
newVert = srf->verts + ( backEnd.currentEntity->e.frame * srf->numVerts );
oldVert = srf->verts + ( backEnd.currentEntity->e.oldframe * srf->numVerts );
newNormal = srf->normals + ( backEnd.currentEntity->e.frame * srf->numVerts );
oldNormal = srf->normals + ( backEnd.currentEntity->e.oldframe * srf->numVerts );
st = srf->st;
numVertexes = srf->numVerts;
if (tess.skipTangents)
{
for (j = 0; j < numVertexes; j++, newVert++, oldVert++, st++)
{
vec3_t tmpVert;
if (backlerp == 0)
{
// just copy
VectorCopy(newVert->xyz, tmpVert);
}
else
{
// interpolate the xyz
VectorScale(oldVert->xyz, oldXyzScale, tmpVert);
VectorMA(tmpVert, newXyzScale, newVert->xyz, tmpVert);
}
tess.verts[tess.numVertexes + j].xyz[0] = tmpVert[0];
tess.verts[tess.numVertexes + j].xyz[1] = tmpVert[1];
tess.verts[tess.numVertexes + j].xyz[2] = tmpVert[2];
tess.verts[tess.numVertexes + j].texCoords[0] = st->st[0];
tess.verts[tess.numVertexes + j].texCoords[1] = st->st[1];
}
}
else
{
// calc tangent spaces
float *v;
const float *v0, *v1, *v2;
const float *t0, *t1, *t2;
vec3_t* xyz;
vec3_t tangent, *tangents;
vec3_t binormal, *binormals;
vec3_t *normals;
xyz = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof(vec3_t) );
tangents = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
binormals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
normals = (vec3_t *)ri.Hunk_AllocateTempMemory( numVertexes * sizeof( vec3_t ) );
for ( i = 0; i < numVertexes; i++, newVert++, oldVert++, oldNormal++, newNormal++ )
{
VectorClear( tangents[ i ] );
VectorClear( binormals[ i ] );
if ( backlerp == 0 )
{
// just copy
VectorCopy( newNormal->normal, normals[ i ] );
}
else
{
// interpolate the xyz
VectorScale( oldNormal->normal, oldXyzScale, normals[ i ] );
VectorMA( normals[ i ], newXyzScale, newNormal->normal, normals[ i ] );
VectorNormalizeFast( normals[ i ] );
}
if ( backlerp == 0 )
{
// just copy
VectorCopy(newVert->xyz, xyz[i]);
}
else
{
// interpolate the xyz
VectorScale(oldVert->xyz, oldXyzScale, xyz[i]);
VectorMA(xyz[i], newXyzScale, newVert->xyz, xyz[i]);
}
}
for (i = 0, tri = srf->triangles; i < srf->numTriangles; i++, tri++)
{
int* indices = tri->indexes;
v0 = xyz[ indices[0] ];
v1 = xyz[ indices[1] ];
v2 = xyz[ indices[2] ];
t0 = st[ indices[ 0 ] ].st;
t1 = st[ indices[ 1 ] ].st;
t2 = st[ indices[ 2 ] ].st;
R_CalcTangents( tangent, binormal, v0, v1, v2, t0, t1, t2 );
for ( j = 0; j < 3; j++ )
{
v = tangents[ indices[ j ] ];
VectorAdd( v, tangent, v );
v = binormals[ indices[ j ] ];
VectorAdd( v, binormal, v );
}
}
for ( i = 0; i < numVertexes; i++ )
{
i16vec4_t qtangents;
R_TBNtoQtangents( tangents[ i ], binormals[ i ],
normals[ i ], qtangents );
VectorCopy(xyz[i], tess.verts[tess.numVertexes + i].xyz);
Vector4Copy(qtangents, tess.verts[tess.numVertexes + i].qtangents);
tess.verts[tess.numVertexes + i].texCoords[0] = st[i].st[0];
tess.verts[tess.numVertexes + i].texCoords[1] = st[i].st[1];
}
ri.Hunk_FreeTempMemory( normals );
ri.Hunk_FreeTempMemory( binormals );
ri.Hunk_FreeTempMemory( tangents );
ri.Hunk_FreeTempMemory( xyz );
}
tess.numIndexes += numIndexes;
tess.numVertexes += numVertexes;
}
/*
==============
Tess_SurfaceMD5
Defines ATTR_POSITION, ATTR_TEXCOORD, and maybe ATTR_QTANGENT
==============
*/
static void Tess_SurfaceMD5( md5Surface_t *srf )
{
md5Model_t *model = srf->model;
GLIMP_LOGCOMMENT( "--- Tess_SurfaceMD5 ---" );
int numIndexes = srf->numTriangles * 3;
Tess_CheckOverflow( srf->numVerts, numIndexes );