MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / ApplyVolumeLightToObject

Function ApplyVolumeLightToObject

Descent3/lighting.cpp:933–989  ·  view source on GitHub ↗

Applys dynamic volumetric lighting to an object

Source from the content-addressed store, hash-verified

931
932// Applys dynamic volumetric lighting to an object
933void ApplyVolumeLightToObject(vector *pos, object *obj, float light_dist, float red_scale, float green_scale,
934 float blue_scale, vector *light_direction, float dot_range) {
935 vector subvec = *pos - obj->pos;
936 float mag = vm_GetMagnitudeFast(&subvec);
937
938 float scalar = mag / light_dist;
939 if (scalar > 1)
940 scalar = 1;
941
942 scalar = 1 - scalar;
943
944 if (light_direction) {
945 // Do a directional light
946 vector dir_vec = obj->pos - *pos;
947 float mag = vm_NormalizeVectorFast(&dir_vec);
948 float dp = dir_vec * *light_direction;
949
950 if (mag > 1.0) {
951 if (dp < dot_range)
952 return;
953 else {
954 float add_scale = (dp - dot_range) / (1.0 - dot_range);
955 scalar *= add_scale;
956 }
957 }
958 }
959
960 ASSERT(obj->effect_info != NULL);
961
962 if (!obj->effect_info->dynamic_this_frame) {
963 if (Num_volume_objects < MAX_VOLUME_OBJECTS) {
964 Dynamic_volume_object_list[Num_volume_objects].handle = obj->handle;
965 Dynamic_volume_object_list[Num_volume_objects++].objnum = obj - Objects;
966 obj->effect_info->spec_mag = -100000;
967
968 obj->effect_info->dynamic_this_frame = 1;
969 }
970 }
971
972 // See if this specular light source is greater than our current one
973 if ((light_dist - mag) > obj->effect_info->spec_mag && Detail_settings.Specular_lighting &&
974 !(Object_info[obj->id].lighting_info.flags & OLF_NO_SPECULARITY)) {
975 // mprintf(0,"Setting specular!\n");
976 obj->effect_info->type_flags |= EF_SPECULAR;
977 obj->effect_info->spec_mag = light_dist - mag;
978 obj->effect_info->spec_pos = *pos;
979 obj->effect_info->spec_r = red_scale;
980 obj->effect_info->spec_g = green_scale;
981 obj->effect_info->spec_b = blue_scale;
982 }
983
984 if (obj->effect_info->type_flags & EF_VOLUME_LIT) {
985 obj->effect_info->dynamic_red = std::min<float>(1, obj->effect_info->dynamic_red + (scalar * red_scale));
986 obj->effect_info->dynamic_green = std::min<float>(1, obj->effect_info->dynamic_green + (scalar * green_scale));
987 obj->effect_info->dynamic_blue = std::min<float>(1, obj->effect_info->dynamic_blue + (scalar * blue_scale));
988 }
989}
990

Callers 1

ApplyLightingToObjectsFunction · 0.85

Calls 2

vm_NormalizeVectorFastFunction · 0.85
vm_GetMagnitudeFastFunction · 0.50

Tested by

no test coverage detected