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;
13 meshqueue_sortcategory_t category;
17 int trans_sortarraysize;
18 meshqueue_t **trans_hash = NULL;
19 meshqueue_t ***trans_hashpointer = NULL;
21 float mqt_viewplanedist;
22 float mqt_viewmaxdist;
23 meshqueue_t *mqt_array;
27 void R_MeshQueue_BeginScene(void)
30 mqt_viewplanedist = DotProduct(r_refdef.view.origin, r_refdef.view.forward);
34 void R_MeshQueue_AddTransparent(meshqueue_sortcategory_t category, 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)
37 if (mqt_count >= mqt_total || !mqt_array)
39 int newtotal = max(1024, mqt_total * 2);
40 meshqueue_t *newarray = (meshqueue_t *)Mem_Alloc(cls.permanentmempool, newtotal * sizeof(meshqueue_t));
43 memcpy(newarray, mqt_array, mqt_total * sizeof(meshqueue_t));
49 mq = &mqt_array[mqt_count++];
50 mq->callback = callback;
52 mq->surfacenumber = surfacenumber;
53 mq->rtlight = rtlight;
54 mq->category = category;
55 if (r_transparent_useplanardistance.integer)
56 mq->dist = DotProduct(center, r_refdef.view.forward) - mqt_viewplanedist;
58 mq->dist = VectorDistance(center, r_refdef.view.origin);
60 mqt_viewmaxdist = max(mqt_viewmaxdist, mq->dist);
63 void R_MeshQueue_RenderTransparent(void)
65 int i, hashindex, maxhashindex, batchnumsurfaces;
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];
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_sortmindist.integer < 1 || r_transparent_sortmindist.integer >= r_transparent_sortmaxdist.integer)
80 Cvar_SetValueQuick(&r_transparent_sortmindist, 0);
81 if (r_transparent_sortmaxdist.integer < r_transparent_sortmindist.integer || r_transparent_sortmaxdist.integer > 32768)
82 Cvar_SetValueQuick(&r_transparent_sortmaxdist, bound(r_transparent_sortmindist.integer, r_transparent_sortmaxdist.integer, 32768));
85 if (trans_sortarraysize != r_transparent_sortarraysize.integer)
87 trans_sortarraysize = r_transparent_sortarraysize.integer;
90 trans_hash = (meshqueue_t **)Mem_Alloc(cls.permanentmempool, sizeof(trans_hash) * trans_sortarraysize);
91 if (trans_hashpointer)
92 Mem_Free(trans_hashpointer);
93 trans_hashpointer = (meshqueue_t ***)Mem_Alloc(cls.permanentmempool, sizeof(trans_hashpointer) * trans_sortarraysize);
97 memset(trans_hash, 0, sizeof(trans_hash) * trans_sortarraysize);
98 for (i = 0; i < trans_sortarraysize; i++)
99 trans_hashpointer[i] = &trans_hash[i];
100 distscale = (trans_sortarraysize - 1) / min(mqt_viewmaxdist, r_transparent_sortmaxdist.integer);
101 maxhashindex = trans_sortarraysize - 1;
102 for (i = 0, mqt = mqt_array; i < mqt_count; i++, mqt++)
104 switch(mqt->category)
107 case MESHQUEUE_SORT_HUD:
110 case MESHQUEUE_SORT_DISTANCE:
111 // this could use a reduced range if we need more categories
112 hashindex = bound(0, (int)(bound(0, mqt->dist - r_transparent_sortmindist.integer, r_transparent_sortmaxdist.integer) * distscale), maxhashindex);
114 case MESHQUEUE_SORT_SKY:
115 hashindex = maxhashindex;
118 // link to tail of hash chain (to preserve render order)
120 *trans_hashpointer[hashindex] = mqt;
121 trans_hashpointer[hashindex] = &mqt->next;
126 batchnumsurfaces = 0;
129 for (i = maxhashindex; i >= 0; i--)
133 for (mqt = trans_hash[i]; mqt; mqt = mqt->next)
135 if (ent != mqt->ent || rtlight != mqt->rtlight || callback != mqt->callback || batchnumsurfaces >= MESHQUEUE_TRANSPARENT_BATCHSIZE)
137 if (batchnumsurfaces)
138 callback(ent, rtlight, batchnumsurfaces, batchsurfaceindex);
139 batchnumsurfaces = 0;
141 rtlight = mqt->rtlight;
142 callback = mqt->callback;
144 batchsurfaceindex[batchnumsurfaces++] = mqt->surfacenumber;
148 if (batchnumsurfaces)
149 callback(ent, rtlight, batchnumsurfaces, batchsurfaceindex);