+ numtextures = model->numtextures;
+ textures = model->textures;
+ numsurfaces = model->nummodelsurfaces;
+ surfaces = model->surfaces + model->firstmodelsurface;
+ surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
+
+ for (i = 0;i < numtextures;i++)
+ textures[i].surfacechain = NULL;
+
+ for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
+ {
+ if (surfacevisframes[i] == r_framecount)
+ {
+#if !WORLDNODECULLBACKFACES
+ // mark any backface surfaces as not visible
+ if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
+ {
+ if (!(surf->flags & SURF_PLANEBACK))
+ surfacevisframes[i] = -1;
+ }
+ else
+ {
+ if ((surf->flags & SURF_PLANEBACK))
+ surfacevisframes[i] = -1;
+ }
+ if (surfacevisframes[i] == r_framecount)
+#endif
+ {
+ c_faces++;
+ t = surf->texinfo->texture;
+ if (t->animated)
+ {
+ framecount = t->anim_total[alttextures];
+ if (framecount >= 2)
+ t = t->anim_frames[alttextures][texframe % framecount];
+ else
+ t = t->anim_frames[alttextures][0];
+ }
+ surf->currenttexture = t;
+ surf->texturechain = t->surfacechain;
+ t->surfacechain = surf;
+ if (!r_vertexsurfaces.integer && surf->lightmaptexture != NULL)
+ {
+ if (surf->cached_dlight
+ || surf->cached_ambient != r_ambient.value
+ || surf->cached_lightscalebit != lightscalebit)
+ R_BuildLightMap(ent, surf, false); // base lighting changed
+ else if (r_dynamic.integer)
+ {
+ if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
+ || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
+ || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
+ || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
+ R_BuildLightMap(ent, surf, false); // base lighting changed
+ else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
+ R_BuildLightMap(ent, surf, true); // only dlights
+ }
+ }
+ }
+ }
+ }