From 41a12f066b540e372ef69968e7d91831290cd89b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 5 May 2022 20:36:25 +0900 Subject: [PATCH] [client] Clean up light loading By adding Light_AddLight to scene light management --- include/QF/scene/light.h | 1 + libs/client/cl_light.c | 37 +++++-------------------------------- libs/scene/light.c | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/include/QF/scene/light.h b/include/QF/scene/light.h index 88033c253..542431bf3 100644 --- a/include/QF/scene/light.h +++ b/include/QF/scene/light.h @@ -73,6 +73,7 @@ typedef struct lightingdata_s { lightingdata_t *Light_CreateLightingData (struct scene_s *scene); void Light_DestroyLightingData (lightingdata_t *ldata); void Light_ClearLights (lightingdata_t *ldata); +void Light_AddLight (lightingdata_t *ldata, const light_t *light, int style); void Light_EnableSun (lightingdata_t *ldata); void Light_FindVisibleLights (lightingdata_t *ldata); diff --git a/libs/client/cl_light.c b/libs/client/cl_light.c index e06e921f5..5af325739 100644 --- a/libs/client/cl_light.c +++ b/libs/client/cl_light.c @@ -124,9 +124,7 @@ parse_sun (lightingdata_t *ldata, plitem_t *entity) light.direction = light.position; light.direction[3] = 1; light.attenuation = (vec4f_t) { 0, 0, 1, 0 }; - DARRAY_APPEND (&ldata->lights, light); - DARRAY_APPEND (&ldata->lightstyles, 0); - DARRAY_APPEND (&ldata->lightleafs, -1); + Light_AddLight (ldata, &light, 0); } static vec4f_t @@ -226,25 +224,6 @@ parse_light (light_t *light, int *style, const plitem_t *entity, light->attenuation = attenuation; } -static void -locate_lights (model_t *model, lightingdata_t *ldata) -{ - light_t *lights = ldata->lights.a; - DARRAY_RESIZE (&ldata->lightleafs, ldata->lights.size); - for (size_t i = 0; i < ldata->lights.size; i++) { - if (1 || lights[i].position[3]) { - mleaf_t *leaf = Mod_PointInLeaf (&lights[i].position[0], model); - ldata->lightleafs.a[i] = leaf - model->brush.leafs - 1; - } else { - if (DotProduct (lights[i].direction, lights[i].direction)) { - ldata->lightleafs.a[i] = -1; - } else { - ldata->lightleafs.a[i] = -2; - } - } - } -} - void CL_LoadLights (plitem_t *entities, scene_t *scene) { @@ -285,8 +264,7 @@ CL_LoadLights (plitem_t *entities, scene_t *scene) light.color = (vec4f_t) { 1, 1, 1, atof (str) }; light.attenuation = (vec4f_t) { 0, 0, 1, 0 }; light.direction = (vec4f_t) { 0, 0, 0, 1 }; - DARRAY_APPEND (&ldata->lights, light); - DARRAY_APPEND (&ldata->lightstyles, 0); + Light_AddLight (ldata, &light, 0); } } else if (!strncmp (classname, "light", 5)) { light_t light = {}; @@ -295,12 +273,10 @@ CL_LoadLights (plitem_t *entities, scene_t *scene) parse_light (&light, &style, entity, targets); // some lights have 0 output, so drop them if (light.color[3]) { - DARRAY_APPEND (&ldata->lights, light); - DARRAY_APPEND (&ldata->lightstyles, style); + Light_AddLight (ldata, &light, style); } } } - DARRAY_RESIZE (&ldata->lightvis, ldata->lights.size); // targets does not own the objects, so need to remove them before // freeing targets for (int i = PL_D_NumKeys (targets); i-- > 0; ) { @@ -308,11 +284,8 @@ CL_LoadLights (plitem_t *entities, scene_t *scene) } PL_Free (targets); - if (ldata->lights.size) { - locate_lights (model, ldata); - for (size_t i = 0; i < ldata->lights.size; i++) { - dump_light (&ldata->lights.a[i], ldata->lightleafs.a[i]); - } + for (size_t i = 0; i < ldata->lights.size; i++) { + dump_light (&ldata->lights.a[i], ldata->lightleafs.a[i]); } Sys_MaskPrintf (SYS_lighting, "loaded %zd lights\n", ldata->lights.size); } diff --git a/libs/scene/light.c b/libs/scene/light.c index 7ccc17dda..3b1d1bd3c 100644 --- a/libs/scene/light.c +++ b/libs/scene/light.c @@ -66,6 +66,28 @@ Light_ClearLights (lightingdata_t *ldata) ldata->leaf = 0; } +void +Light_AddLight (lightingdata_t *ldata, const light_t *light, int style) +{ + scene_t *scene = ldata->scene; + model_t *model = scene->worldmodel; + + DARRAY_APPEND (&ldata->lights, *light); + DARRAY_APPEND (&ldata->lightstyles, style); + + int visleaf = -1; // directional light + if (light->position[3]) { + // positional light + mleaf_t *leaf = Mod_PointInLeaf (&light->position[0], model); + visleaf = leaf - model->brush.leafs - 1; + } else if (!DotProduct (light->direction, light->direction)) { + // ambient light + visleaf = -2; + } + DARRAY_APPEND (&ldata->lightleafs, visleaf); + DARRAY_APPEND (&ldata->lightvis, 0); +} + void Light_EnableSun (lightingdata_t *ldata) {