+void Mod_SetDrawSkyAndWater(model_t* mod)
+{
+ int j;
+ uint64_t basematerialflags = 0;
+ // by default assume there is no sky or water used in this model
+ mod->DrawSky = NULL;
+ mod->DrawAddWaterPlanes = NULL;
+ // combine all basematerialflags observed in the submodelsurfaces range, then check for special flags
+ for (j = mod->submodelsurfaces_start; j < mod->submodelsurfaces_end; j++)
+ if (mod->data_surfaces[j].texture)
+ basematerialflags |= mod->data_surfaces[j].texture->basematerialflags;
+ if (basematerialflags & MATERIALFLAG_SKY)
+ mod->DrawSky = R_Mod_DrawSky;
+ if (basematerialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA))
+ mod->DrawAddWaterPlanes = R_Mod_DrawAddWaterPlanes;
+}
+
+typedef struct Mod_MakeSortedSurfaces_qsortsurface_s
+{
+ int surfaceindex;
+ q3deffect_t* effect;
+ texture_t* texture;
+ rtexture_t* lightmaptexture;
+}
+Mod_MakeSortedSurfaces_qsortsurface_t;
+
+static int Mod_MakeSortedSurfaces_qsortfunc(const void *a, const void *b)
+{
+ const Mod_MakeSortedSurfaces_qsortsurface_t* l = (Mod_MakeSortedSurfaces_qsortsurface_t*)a;
+ const Mod_MakeSortedSurfaces_qsortsurface_t* r = (Mod_MakeSortedSurfaces_qsortsurface_t*)b;
+ if (l->effect < r->effect)
+ return -1;
+ if (l->effect > r->effect)
+ return 1;
+ if (l->texture < r->texture)
+ return -1;
+ if (l->texture > r->texture)
+ return 1;
+ if (l->lightmaptexture < r->lightmaptexture)
+ return -1;
+ if (l->lightmaptexture > r->lightmaptexture)
+ return 1;
+ if (l->surfaceindex < r->surfaceindex)
+ return -1;
+ if (l->surfaceindex > r->surfaceindex)
+ return 1;
+ return 0;
+}
+
+void Mod_MakeSortedSurfaces(model_t *mod)