aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 72ef60a81632627afa253c82520dfecc1578a913
parent 7bc50381cc778015b911665a5be605a0aa082a44
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 05:47:37 +0000

Prepare light transforms once per draw

Diffstat:
Msrc/runtime/linux_graphics.c | 52+++++++++++++++++++++++++++++++++++-----------------
1 file changed, 35 insertions(+), 17 deletions(-)

diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -60,6 +60,7 @@ static struct surface_object { static struct surface_object *surface_buckets[2][4096]; struct device_light { uint32_t index, enabled, data[26]; + float camera_vector[4], spot_direction[4], spot_inner, spot_outer; struct device_light *next; }; struct texture_stage { @@ -1839,28 +1840,20 @@ static void light_vertex(struct device_object *device, const float position[4], for (struct device_light *light = device->lights; light; light = light->next) { if (!light->enabled) continue; const float *data = (const float *)light->data; - float direction[4], source[4], attenuation = 1; - if (light->data[0] == 3) { - for (unsigned j = 0; j < 3; ++j) source[j] = -data[16 + j]; - source[3] = 0; - transform_vector(source, (const float *)device->transforms[1], direction); - normalize_vector(direction); - } else { - memcpy(source, data + 13, 12); source[3] = 1; - transform_vector(source, (const float *)device->transforms[1], direction); + float direction[4], attenuation = 1; + memcpy(direction, light->camera_vector, sizeof(direction)); + if (light->data[0] != 3) { for (unsigned j = 0; j < 3; ++j) direction[j] -= position[j]; float distance = normalize_vector(direction); if (distance > data[19]) continue; attenuation = 1 / (data[21] + distance * data[22] + distance * distance * data[23]); if (light->data[0] == 2) { - float spot[4]; - memcpy(source, data + 16, 12); source[3] = 0; - transform_vector(source, (const float *)device->transforms[1], spot); - normalize_vector(spot); - float cosine = -(spot[0] * direction[0] + spot[1] * direction[1] + spot[2] * direction[2]); - float inner = cosf(data[24] * .5f), outer = cosf(data[25] * .5f); - if (cosine <= outer) continue; - if (cosine < inner) attenuation *= powf((cosine - outer) / (inner - outer), data[20]); + float cosine = -(light->spot_direction[0] * direction[0] + + light->spot_direction[1] * direction[1] + light->spot_direction[2] * direction[2]); + if (cosine <= light->spot_outer) continue; + if (cosine < light->spot_inner) + attenuation *= powf((cosine - light->spot_outer) / + (light->spot_inner - light->spot_outer), data[20]); } } float diffuse = fmaxf(0, normal[0] * direction[0] + normal[1] * direction[1] + normal[2] * direction[2]); @@ -1915,6 +1908,31 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin if (!determinant) { fprintf(stderr, "Unsupported singular world/view transform\n"); exit(3); } for (unsigned i = 0; i < 9; ++i) normal_matrix[i] = cofactors[i] / determinant; } + /* Light transforms and spot cone angles are constant throughout a draw. */ + if (untransformed && device->lighting) { + for (struct device_light *light = device->lights; light; light = light->next) { + if (!light->enabled) continue; + const float *data = (const float *)light->data; + float source[4]; + if (light->data[0] == 3) { + for (unsigned j = 0; j < 3; ++j) source[j] = -data[16 + j]; + source[3] = 0; + } else { + memcpy(source, data + 13, 12); + source[3] = 1; + } + transform_vector(source, (const float *)device->transforms[1], light->camera_vector); + if (light->data[0] == 3) normalize_vector(light->camera_vector); + if (light->data[0] == 2) { + memcpy(source, data + 16, 12); + source[3] = 0; + transform_vector(source, (const float *)device->transforms[1], light->spot_direction); + normalize_vector(light->spot_direction); + light->spot_inner = cosf(data[24] * .5f); + light->spot_outer = cosf(data[25] * .5f); + } + } + } /* Pretransformed vertices carry diffuse and optional specular colors. */ uint32_t uv_offset = 20 + (fvf & 0x80 ? 4 : 0), stride = uv_offset + texture_count * 8; if (untransformed) stride = 12 + (fvf & 0x10 ? 12 : 0) + (fvf & 0x40 ? 4 : 0) + texture_count * 8;