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