- e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
- VectorCopy(origin, e->origin);
- VectorCopy(angles, e->angles);
- VectorCopy(color, e->color);
- e->radius = radius;
- e->style = style;
- if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
- {
- Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
- e->style = 0;
- }
- e->drawshadows = shadowenable;
- e->corona = corona;
-
- Matrix4x4_CreateFromQuakeEntity(&e->matrix_lighttoworld, e->origin[0], e->origin[1], e->origin[2], e->angles[0], e->angles[1], e->angles[2], e->radius);
- Matrix4x4_Invert_Simple(&e->matrix_worldtolight, &e->matrix_lighttoworld);
- Matrix4x4_Concat(&e->matrix_worldtoattenuationxyz, &matrix_attenuationxyz, &e->matrix_worldtolight);
- Matrix4x4_Concat(&e->matrix_worldtoattenuationz, &matrix_attenuationz, &e->matrix_worldtolight);
-
- e->cullradius = e->radius;
- for (k = 0;k < 3;k++)
- {
- mins[k] = e->origin[k] - e->radius;
- maxs[k] = e->origin[k] + e->radius;
- }
-
- e->next = r_shadow_worldlightchain;
- r_shadow_worldlightchain = e;
- if (cubemapname && cubemapname[0])
- {
- e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
- strcpy(e->cubemapname, cubemapname);
- e->cubemap = R_Shadow_Cubemap(e->cubemapname);
- }
- // FIXME: rewrite this to store ALL geometry into a cache in the light
- if (e->drawshadows)
- castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
- e->meshchain_light = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, true, false, true);
- if (cl.worldmodel)
- {
- lightpvsbytes = cl.worldmodel->brush.FatPVS(cl.worldmodel, origin, 0, lightfullpvs, sizeof(lightfullpvs));
- memset(lightpvs, 0, lightpvsbytes);
- if (cl.worldmodel->brushq3.num_leafs)
- {
- q3mleaf_t *leaf;
- q3mface_t *face;
-
- // make a pvs that only includes things within the box
- for (i = 0, leaf = cl.worldmodel->brushq3.data_leafs;i < cl.worldmodel->brushq3.num_leafs;i++, leaf++)
- if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
- SETPVSBIT(lightpvs, leaf->clusterindex);
-
- // make a cluster list for fast visibility checking during rendering
- for (i = 0, e->numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
- if (CHECKPVSBIT(lightpvs, i))
- e->numclusters++;
- e->clusterindices = Mem_Alloc(r_shadow_mempool, e->numclusters * sizeof(int));
- for (i = 0, e->numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
- if (CHECKPVSBIT(lightpvs, i))
- e->clusterindices[e->numclusters++] = i;
-
- VectorCopy(e->origin, e->mins);
- VectorCopy(e->origin, e->maxs);
- for (i = 0, face = cl.worldmodel->brushq3.data_thismodel->firstface;i < cl.worldmodel->brushq3.data_thismodel->numfaces;i++, face++)
- face->lighttemp_castshadow = false;
- for (i = 0, leaf = cl.worldmodel->brushq3.data_leafs;i < cl.worldmodel->brushq3.num_leafs;i++, leaf++)
- {
- if (CHECKPVSBIT(lightpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
- {
- for (k = 0;k < 3;k++)
- {
- if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
- if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
- }
- for (j = 0;j < leaf->numleaffaces;j++)
- {
- face = leaf->firstleafface[j];
- if (BoxesOverlap(face->mins, face->maxs, mins, maxs))
- face->lighttemp_castshadow = true;
- }
- }
- }
-
- // add surfaces to shadow casting mesh and light mesh
- for (i = 0, face = cl.worldmodel->brushq3.data_thismodel->firstface;i < cl.worldmodel->brushq3.data_thismodel->numfaces;i++, face++)
- {
- if (face->lighttemp_castshadow)
- {
- face->lighttemp_castshadow = false;
- if (!(face->texture->surfaceflags & (Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY)))
- {
- if (e->drawshadows)
- if (!(face->texture->nativecontents & CONTENTSQ3_TRANSLUCENT))
- Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, face->data_vertex3f, NULL, NULL, NULL, NULL, face->num_triangles, face->data_element3i);
- if (!(face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
- Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->meshchain_light, face->texture->skin.base, face->texture->skin.gloss, face->texture->skin.nmap, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, face->num_triangles, face->data_element3i);
- }
- }
- }
- }
- else if (cl.worldmodel->brushq1.num_leafs)
- {
- mleaf_t *leaf;
- msurface_t *surf;
- VectorCopy(e->origin, e->mins);
- VectorCopy(e->origin, e->maxs);
- i = CL_PointQ1Contents(e->origin);
-
- for (i = 0, surf = cl.worldmodel->brushq1.surfaces + cl.worldmodel->brushq1.firstmodelsurface;i < cl.worldmodel->brushq1.nummodelsurfaces;i++, surf++)
- surf->lighttemp_castshadow = false;
-
- if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
- {
- qbyte *byteleafpvs;
- qbyte *bytesurfacepvs;
-
- byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->brushq1.num_leafs);
- bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->brushq1.numsurfaces);
-
- Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, mins, maxs, e->mins, e->maxs);
-
- // make a pvs that only includes things within the box
- for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
- {
- if (byteleafpvs[i] && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
- {
- SETPVSBIT(lightpvs, leaf->clusterindex);
- for (k = 0;k < 3;k++)
- {
- if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
- if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
- }
- }
- }
-
- for (i = 0, surf = cl.worldmodel->brushq1.surfaces;i < cl.worldmodel->brushq1.numsurfaces;i++, surf++)
- if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, mins, maxs))
- surf->lighttemp_castshadow = true;
-
- Mem_Free(byteleafpvs);
- Mem_Free(bytesurfacepvs);
-
- // make a cluster list for fast visibility checking during rendering
- for (i = 0, e->numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
- if (CHECKPVSBIT(lightpvs, i))
- e->numclusters++;
- e->clusterindices = Mem_Alloc(r_shadow_mempool, e->numclusters * sizeof(int));
- for (i = 0, e->numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
- if (CHECKPVSBIT(lightpvs, i))
- e->clusterindices[e->numclusters++] = i;
- }
- else
- {
- for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
- {
- if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
- {
- // make a pvs that only includes things within the box
- SETPVSBIT(lightpvs, leaf->clusterindex);
- for (k = 0;k < 3;k++)
- {
- if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
- if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
- }
- for (j = 0;j < leaf->nummarksurfaces;j++)
- {
- surf = cl.worldmodel->brushq1.surfaces + leaf->firstmarksurface[j];
- if (!surf->lighttemp_castshadow && BoxesOverlap(surf->poly_mins, surf->poly_maxs, mins, maxs))
- surf->lighttemp_castshadow = true;
- }
- }
- }
-
- // make a pvs that only includes things within the box
- for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
- if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
- SETPVSBIT(lightpvs, leaf->clusterindex);
-
- // make a cluster list for fast visibility checking during rendering
- for (i = 0, e->numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
- if (CHECKPVSBIT(lightpvs, i))
- e->numclusters++;
- e->clusterindices = Mem_Alloc(r_shadow_mempool, e->numclusters * sizeof(int));
- for (i = 0, e->numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
- if (CHECKPVSBIT(lightpvs, i))
- e->clusterindices[e->numclusters++] = i;
- }
-
- // add surfaces to shadow casting mesh and light mesh
- for (i = 0, surf = cl.worldmodel->brushq1.surfaces + cl.worldmodel->brushq1.firstmodelsurface;i < cl.worldmodel->brushq1.nummodelsurfaces;i++, surf++)
- {
- if (surf->lighttemp_castshadow)
- {
- surf->lighttemp_castshadow = false;
- if (e->drawshadows && (surf->flags & SURF_SHADOWCAST))
- Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, surf->mesh.data_vertex3f, NULL, NULL, NULL, NULL, surf->mesh.num_triangles, surf->mesh.data_element3i);
- if (!(surf->flags & SURF_DRAWSKY))
- Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->meshchain_light, surf->texinfo->texture->skin.base, surf->texinfo->texture->skin.gloss, surf->texinfo->texture->skin.nmap, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, surf->mesh.num_triangles, surf->mesh.data_element3i);
- }
- }
- }
- }
-
- // limit box to light bounds (in case it grew larger)
- for (k = 0;k < 3;k++)
- {
- if (e->mins[k] < e->origin[k] - e->radius) e->mins[k] = e->origin[k] - e->radius;
- if (e->maxs[k] > e->origin[k] + e->radius) e->maxs[k] = e->origin[k] + e->radius;
- }
- e->cullradius = RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin);
-
- // cast shadow volume from castmesh
- castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh, false, true);
- if (castmesh)