5 typedef struct meshqueue_s
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;
11 const rtlight_t *rtlight;
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;
22 float mqt_viewplanedist;
23 float mqt_viewmaxdist;
24 meshqueue_t *mqt_array;
28 void R_MeshQueue_BeginScene(void)
31 mqt_viewplanedist = DotProduct(r_refdef.view.origin, r_refdef.view.forward);
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)
38 if (mqt_count >= mqt_total || !mqt_array)
40 int newtotal = max(1024, mqt_total * 2);
41 meshqueue_t *newarray = (meshqueue_t *)Mem_Alloc(cls.permanentmempool, newtotal * sizeof(meshqueue_t));
44 memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
50 mq = &mqt_array[mqt_count++];
51 mq->callback = callback;
53 mq->surfacenumber = surfacenumber;
54 mq->rtlight = rtlight;
55 mq->dist = DotProduct(center, r_refdef.view.forward) - mqt_viewplanedist;
57 mqt_viewmaxdist = max(mqt_viewmaxdist, mq->dist);
60 void R_MeshQueue_RenderTransparent(void)
62 int i, hashindex, maxhashindex, batchnumsurfaces;
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];
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));
80 if (trans_sortarraysize != r_transparent_sortarraysize.integer)
82 trans_sortarraysize = r_transparent_sortarraysize.integer;
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);
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++)
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)
102 *trans_hashpointer[hashindex] = mqt;
103 trans_hashpointer[hashindex] = &mqt->next;
108 batchnumsurfaces = 0;
111 for (i = maxhashindex; i >= 0; i--)
115 for (mqt = trans_hash[i]; mqt; mqt = mqt->next)
117 if (ent != mqt->ent || rtlight != mqt->rtlight || callback != mqt->callback || batchnumsurfaces >= MESHQUEUE_TRANSPARENT_BATCHSIZE)
119 if (batchnumsurfaces)
120 callback(ent, rtlight, batchnumsurfaces, batchsurfaceindex);
121 batchnumsurfaces = 0;
123 rtlight = mqt->rtlight;
124 callback = mqt->callback;
126 batchsurfaceindex[batchnumsurfaces++] = mqt->surfacenumber;
130 if (batchnumsurfaces)
131 callback(ent, rtlight, batchnumsurfaces, batchsurfaceindex);