]> git.xonotic.org Git - xonotic/darkplaces.git/blob - meshqueue.c
fix two compile errors in C++ mode
[xonotic/darkplaces.git] / meshqueue.c
1
2 #include "quakedef.h"
3 #include "meshqueue.h"
4
5 typedef struct meshqueue_s
6 {
7         struct meshqueue_s *next;
8         void (*callback)(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfaceindices);
9         const entity_render_t *ent;
10         int surfacenumber;
11         const rtlight_t *rtlight;
12         float dist;
13 }
14 meshqueue_t;
15
16 int trans_sortarraysize;
17 meshqueue_t **trans_hash = NULL;
18 meshqueue_t ***trans_hashpointer = NULL;
19 extern cvar_t r_transparent_sortarraysize;
20 extern cvar_t r_transparent_sortmaxdist;
21
22 float mqt_viewplanedist;
23 float mqt_viewmindist;
24 float mqt_viewmaxdist;
25 meshqueue_t *mqt_array;
26 int mqt_count;
27 int mqt_total;
28
29 void R_MeshQueue_BeginScene(void)
30 {
31         mqt_count = 0;
32         mqt_viewplanedist = DotProduct(r_refdef.view.origin, r_refdef.view.forward);
33         mqt_viewmaxdist = 0;
34         mqt_viewmindist = 999999999;
35 }
36
37 void R_MeshQueue_AddTransparent(const vec3_t center, void (*callback)(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist), const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
38 {
39         meshqueue_t *mq;
40         if (mqt_count >= mqt_total || !mqt_array)
41         {
42                 int newtotal = max(1024, mqt_total * 2);
43                 meshqueue_t *newarray = (meshqueue_t *)Mem_Alloc(cls.permanentmempool, newtotal * sizeof(meshqueue_t));
44                 if (mqt_array)
45                 {
46                         memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
47                         Mem_Free(mqt_array);
48                 }
49                 mqt_array = newarray;
50                 mqt_total = newtotal;
51         }
52         mq = &mqt_array[mqt_count++];
53         mq->callback = callback;
54         mq->ent = ent;
55         mq->surfacenumber = surfacenumber;
56         mq->rtlight = rtlight;
57         mq->dist = DotProduct(center, r_refdef.view.forward) - mqt_viewplanedist;
58         mq->next = NULL;
59         mqt_viewmaxdist = max(mqt_viewmaxdist, mq->dist);
60         mqt_viewmindist = min(mqt_viewmindist, mq->dist);
61 }
62
63 void R_MeshQueue_RenderTransparent(void)
64 {
65         int i, hashindex, maxhashindex, batchnumsurfaces;
66         float distscale;
67         const entity_render_t *ent;
68         const rtlight_t *rtlight;
69         void (*callback)(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfaceindices);
70         int batchsurfaceindex[MESHQUEUE_TRANSPARENT_BATCHSIZE];
71         meshqueue_t *mqt;
72
73         if (!mqt_count)
74                 return;
75
76         // check for bad cvars
77         if (r_transparent_sortarraysize.integer < 1 || r_transparent_sortarraysize.integer > 32768)
78                 Cvar_SetValueQuick(&r_transparent_sortarraysize, bound(1, r_transparent_sortarraysize.integer, 32768));
79         if (r_transparent_sortmaxdist.integer < 1 || r_transparent_sortmaxdist.integer > 32768)
80                 Cvar_SetValueQuick(&r_transparent_sortmaxdist, bound(1, r_transparent_sortmaxdist.integer, 32768));
81
82         // update hash array
83         if (trans_sortarraysize != r_transparent_sortarraysize.integer)
84         {
85                 trans_sortarraysize = r_transparent_sortarraysize.integer;
86                 if (trans_hash)
87                         Mem_Free(trans_hash);
88                 trans_hash = (meshqueue_t **)Mem_Alloc(cls.permanentmempool, sizeof(trans_hash) * trans_sortarraysize); 
89                 if (trans_hashpointer)
90                         Mem_Free(trans_hashpointer);
91                 trans_hashpointer = (meshqueue_t ***)Mem_Alloc(cls.permanentmempool, sizeof(trans_hashpointer) * trans_sortarraysize); 
92         }
93
94         // build index
95         memset(trans_hash, 0, sizeof(trans_hash) * trans_sortarraysize);
96         for (i = 0; i < trans_sortarraysize; i++)
97                 trans_hashpointer[i] = &trans_hash[i];
98         distscale = (trans_sortarraysize - 1) / max( min(mqt_viewmaxdist, r_transparent_sortmaxdist.integer) - mqt_viewmindist, 64 );
99         maxhashindex = trans_sortarraysize - 1;
100         for (i = 0, mqt = mqt_array; i < mqt_count; i++, mqt++)
101         {
102                 hashindex = bound(0, (int)(min(mqt->dist - mqt_viewmindist, r_transparent_sortmaxdist.integer) * distscale - 0.1), maxhashindex);
103                 // link to tail of hash chain (to preserve render order)
104                 mqt->next = NULL;
105                 *trans_hashpointer[hashindex] = mqt;
106                 trans_hashpointer[hashindex] = &mqt->next;
107         }
108         callback = NULL;
109         ent = NULL;
110         rtlight = NULL;
111         batchnumsurfaces = 0;
112
113         // draw
114         for (i = maxhashindex; i >= 0; i--)
115         {
116                 if (trans_hash[i])
117                 {
118                         for (mqt = trans_hash[i]; mqt; mqt = mqt->next)
119                         {
120                                 if (ent != mqt->ent || rtlight != mqt->rtlight || callback != mqt->callback || batchnumsurfaces >= MESHQUEUE_TRANSPARENT_BATCHSIZE)
121                                 {
122                                         if (batchnumsurfaces)
123                                                 callback(ent, rtlight, batchnumsurfaces, batchsurfaceindex);
124                                         batchnumsurfaces = 0;
125                                         ent = mqt->ent;
126                                         rtlight = mqt->rtlight;
127                                         callback = mqt->callback;
128                                 }
129                                 batchsurfaceindex[batchnumsurfaces++] = mqt->surfacenumber;
130                         }
131                 }
132         }
133         if (batchnumsurfaces)
134                 callback(ent, rtlight, batchnumsurfaces, batchsurfaceindex);
135         mqt_count = 0;
136 }