+ if (gl_mesh_sorttransbymesh.integer)
+ {
+ int i, j, k;
+ float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
+ buf_vertex_t *vert1, *vert2, *vert3;
+ buf_transtri_t *tri;
+ buf_mesh_t *mesh, *transmesh;
+
+ // process and add transparent mesh triangles
+ if (!currenttranstriangle)
+ return;
+
+ // map farclip to 0-4095 list range
+ centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
+ viewdistcompare = viewdist + 4.0f;
+
+ memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
+
+ k = 0;
+ for (j = 0;j < currenttranstriangle;j++)
+ {
+ tri = &buf_transtri[j];
+
+ vert1 = &buf_transvertex[tri->index[0]];
+ vert2 = &buf_transvertex[tri->index[1]];
+ vert3 = &buf_transvertex[tri->index[2]];
+
+ dist1 = DotProduct(vert1->v, vpn);
+ dist2 = DotProduct(vert2->v, vpn);
+ dist3 = DotProduct(vert3->v, vpn);
+
+ maxdist = max(dist1, max(dist2, dist3));
+ if (maxdist < viewdistcompare)
+ continue;
+
+ center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
+ #if SLOWMATH
+ i = (int) center;
+ i = bound(0, i, (TRANSDEPTHRES - 1));
+ #else
+ if (center < 0.0f)
+ center = 0.0f;
+ center += 8388608.0f;
+ i = *((long *)¢er) & 0x7FFFFF;
+ i = min(i, (TRANSDEPTHRES - 1));
+ #endif
+ tri->next = buf_transtri_list[i];
+ buf_transtri_list[i] = tri;
+ k++;
+ }
+
+ #ifndef TRANSBATCH
+ if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
+ R_Mesh_Render();
+
+ // note: can't batch these because they can be rendered in any order
+ // there can never be more transparent triangles than fit in main buffers
+ memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
+#if FLOATCOLORS
+ if (floatcolors)
+ memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
+ else
+#endif
+ memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[0], currenttransvertex * sizeof(buf_bcolor_t));
+ for (i = 0;i < backendunits;i++)
+ memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
+ #endif
+
+ for (i = 0;i < currenttransmesh;i++)
+ {
+ buf_transmesh[i].transchain = NULL;
+ //buf_transmesh[i].transchainpointer = &buf_transmesh[i].transchain;
+ buf_transmesh[i].triangles = 0;
+ }
+ transmesh = NULL;
+ for (j = 0;j < TRANSDEPTHRES;j++)
+ {
+ if ((tri = buf_transtri_list[j]))
+ {
+ for (;tri;tri = tri->next)
+ {
+ if (!tri->mesh->transchain)
+ {
+ tri->mesh->chain = transmesh;
+ transmesh = tri->mesh;
+ }
+ tri->meshsortchain = tri->mesh->transchain;
+ tri->mesh->transchain = tri;
+ /*
+ *tri->mesh->transchainpointer = tri;
+ tri->meshsortchain = NULL;
+ tri->mesh->transchainpointer = &tri->meshsortchain;
+ */
+ tri->mesh->triangles++;
+ }
+ }
+ }
+
+ #if TRANSBATCH
+ for (;transmesh;transmesh = transmesh->chain)
+ {
+ int meshvertexadjust;
+ int numverts = transmesh->lastvert - transmesh->firstvert + 1;
+ if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + numverts > max_verts)
+ R_Mesh_Render();
+
+ memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], numverts * sizeof(buf_vertex_t));
+#if FLOATCOLORS
+ if (floatcolors)
+ memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], numverts * sizeof(buf_fcolor_t));
+ else
+#endif
+ memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[transmesh->firstvert], numverts * sizeof(buf_bcolor_t));
+ for (i = 0;i < backendunits && transmesh->textures[i];i++)
+ memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], numverts * sizeof(buf_texcoord_t));
+
+ mesh = &buf_mesh[currentmesh++];
+ *mesh = *transmesh; // copy mesh properties
+ mesh->firstvert = currentvertex;
+ mesh->lastvert = currentvertex + numverts - 1;
+ currentvertex += numverts;
+ meshvertexadjust = mesh->firstvert - transmesh->firstvert;
+ mesh->firsttriangle = currenttriangle;
+ for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
+ {
+ buf_tri[currenttriangle].index[0] = tri->index[0] + meshvertexadjust;
+ buf_tri[currenttriangle].index[1] = tri->index[1] + meshvertexadjust;
+ buf_tri[currenttriangle].index[2] = tri->index[2] + meshvertexadjust;
+ /*
+ if (tri->mesh != transmesh)
+ Sys_Error("!?!");
+ if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
+ || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
+ || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
+ || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
+ || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
+ || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
+ Sys_Error("!?");
+ */
+ currenttriangle++;
+ }
+ /*
+ if (mesh->triangles != currenttriangle - mesh->firsttriangle)
+ Sys_Error("!");
+ */
+ }
+ #else
+ for (;transmesh;transmesh = transmesh->chain)
+ {
+ mesh = &buf_mesh[currentmesh++];
+ *mesh = *transmesh; // copy mesh properties
+ mesh->firstvert += currentvertex;
+ mesh->lastvert += currentvertex;
+ mesh->firsttriangle = currenttriangle;
+ for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
+ {
+ buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
+ buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
+ buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
+ /*
+ if (tri->mesh != transmesh)
+ Sys_Error("!?!");
+ if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
+ || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
+ || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
+ || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
+ || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
+ || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
+ Sys_Error("!?");
+ */
+ currenttriangle++;
+ }
+ /*
+ if (mesh->triangles != currenttriangle - mesh->firsttriangle)
+ Sys_Error("!");
+ */
+ }
+ currentvertex += currenttransvertex;
+ #endif
+
+ currenttransmesh = 0;
+ currenttranstriangle = 0;
+ currenttransvertex = 0;
+ }
+ else if (gl_mesh_dupetransverts.integer)