- int smax, tmax, i, j, size, size3, maps, stride, l;
- unsigned int *bl, scale;
- qbyte *lightmap, *out, *stain;
-
- // update cached lighting info
- surface->cached_dlight = 0;
-
- smax = (surface->extents[0]>>4)+1;
- tmax = (surface->extents[1]>>4)+1;
- size = smax*tmax;
- size3 = size*3;
- lightmap = surface->samples;
-
- // set to full bright if no light data
- bl = intblocklights;
- if (!ent->model->brushq1.lightdata)
- {
- for (i = 0;i < size3;i++)
- bl[i] = 255*256;
- }
- else
- {
- // clear to no light
- memset(bl, 0, size*3*sizeof(unsigned int));
-
- // add all the lightmaps
- if (lightmap)
- {
- bl = intblocklights;
- for (maps = 0;maps < MAXLIGHTMAPS && surface->styles[maps] != 255;maps++, lightmap += size3)
- for (scale = d_lightstylevalue[surface->styles[maps]], i = 0;i < size3;i++)
- bl[i] += lightmap[i] * scale;
- }
- }
-
- stain = surface->stainsamples;
- bl = intblocklights;
- out = templight;
- // the >> 16 shift adjusts down 8 bits to account for the stainmap
- // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
- // be doubled during rendering to achieve 2x overbright
- // (0 = 0.0, 128 = 1.0, 256 = 2.0)
- if (ent->model->brushq1.lightmaprgba)
- {
- stride = (surface->lightmaptexturestride - smax) * 4;
- for (i = 0;i < tmax;i++, out += stride)
- {
- for (j = 0;j < smax;j++)
- {
- l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
- l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
- l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
- *out++ = 255;
- }
- }
- }
- else
- {
- stride = (surface->lightmaptexturestride - smax) * 3;
- for (i = 0;i < tmax;i++, out += stride)
- {
- for (j = 0;j < smax;j++)
- {
- l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
- l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
- l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
- }
- }
- }
-
- R_UpdateTexture(surface->lightmaptexture, templight);