aorecomp

Anarchy Online classic in the browser

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


commit ecefa6ebb75fe9d65a8c5a2ca886cad94b0be804
parent f8d4a6da34d720792ba8701491cabb65bb4151b0
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 23:50:11 +0000

Honor material lighting sources used by indoor scenes

Diffstat:
Msrc/runtime/linux_graphics.c | 15+++++++++++----
Msrc/tests/devices.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -78,7 +78,7 @@ static struct device_object { uint32_t specular; uint32_t alpha_test, alpha_reference, alpha_function; float specular_power; - uint32_t emissive_source; + uint32_t diffuse_source, specular_source, emissive_source; uint32_t ambient, normalize_normals, material[17], viewport[6], transforms[5][16]; int scene; struct draw_object *owner; @@ -1464,6 +1464,8 @@ static void create_device(void) device->depth_function = 4; device->clipping = 1; device->lighting = 1; + device->diffuse_source = 1; + device->specular_source = 2; device->texture_factor = UINT32_MAX; device->fog_end = device->fog_density = 0x3f800000; for (unsigned matrix = 0; matrix < 5; ++matrix) @@ -1676,7 +1678,8 @@ static uint32_t device_state(struct device_object *device, uint32_t state) case 60: return device->texture_factor; case 128: case 129: case 130: case 131: case 132: case 133: case 134: case 135: return 0; /* Coordinate wrapping. */ - case 141: case 142: case 145: return 1; + case 141: case 142: return 1; + case 145: return device->diffuse_source; case 136: return device->clipping; case 139: return device->ambient; case 140: return device->fog_vertex_mode; @@ -1684,7 +1687,7 @@ static uint32_t device_state(struct device_object *device, uint32_t state) case 138: case 144: case 147: case 151: case 152: return 0; case 148: return device->emissive_source; - case 146: return 2; /* Specular material uses vertex color 2. */ + case 146: return device->specular_source; case 7: return 1; /* Z enable. */ case 137: return device->lighting; case 14: return device->depth_write; @@ -1783,6 +1786,9 @@ static void device_set_state(void) else if (state == 139) device->ambient = value; else if (state == 140 && (value == 0 || value == 3)) device->fog_vertex_mode = value; else if (state == 143 && value <= 1) device->normalize_normals = value; + else if (state == 145 && value <= 2) device->diffuse_source = value; + /* Supported untransformed formats omit color 2, which falls back to the material. */ + else if (state == 146 && (value == 0 || value == 2)) device->specular_source = value; else if (state == 148 && value <= 2) device->emissive_source = value; else if (state == 19 && (value == 1 || value == 2 || value == 3 || value == 5 || value == 6)) device->source_blend = value; else if (state == 20 && (value == 1 || value == 2 || value == 3 || value == 5 || value == 6)) device->destination_blend = value; @@ -1922,7 +1928,8 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin else for (unsigned j = 0; j < 4; ++j) diffuse[j] = 1; /* Missing vertex color fields fall back to the material's emissive RGB. */ const float *emissive = device->emissive_source == 1 && (fvf & 0x40) ? diffuse : (const float *)device->material + 12; - if (device->lighting) light_vertex(device, camera, normal, diffuse, emissive, vertices[i].color, vertices[i].specular); + const float *lit_diffuse = device->diffuse_source == 1 && (fvf & 0x40) ? diffuse : (const float *)device->material; + if (device->lighting) light_vertex(device, camera, normal, lit_diffuse, emissive, vertices[i].color, vertices[i].specular); else memcpy(vertices[i].color, diffuse, 16); vertices[i].fog = 0; if (device->fog_vertex_mode == 3) { diff --git a/src/tests/devices.c b/src/tests/devices.c @@ -651,6 +651,8 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui material[16] = specular == 2 || specular == 4 ? 0 : specular == 3 ? 2 : 8; light[5] = .5f; light[6] = .6f; light[7] = .7f; assert(!invoke(device, 20, 3, 29u, specular ? 1u : 0u)); + assert(!invoke(device, 20, 3, 146u, specular ? 0u : 2u)); + assert(!invoke(device, 21, 3, 146u, data) && *memory(data) == (specular ? 0u : 2u)); assert(!invoke(device, 20, 3, 148u, emissive)); assert(!invoke(device, 21, 3, 148u, data) && *memory(data) == emissive); uint32_t type = sample >= 9 && sample <= 10 ? 1 : sample >= 11 && sample <= 12 ? 2 : 3; @@ -721,9 +723,58 @@ static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, ui assert(!invoke(device, 20, 3, 140u, 0u)); assert(!invoke(device, 20, 3, 27u, 0u)); assert(!invoke(device, 20, 3, 29u, 0u)); + assert(!invoke(device, 20, 3, 146u, 2u)); assert(!invoke(device, 20, 3, 148u, 0u)); } +static void diffuse_material_source_selects_lighting_color(uint32_t device, uint32_t target) +{ + uint32_t input = workspace() + 512, data = workspace() + 256, desc = workspace() + 128; + struct vertex { float x, y, z, nx, ny, nz; uint32_t color; float u, v; } vertices[] = { + {-.75f,.75f,.5f,0,0,-1,0xffff0000,0,0}, + {.75f,.75f,.5f,0,0,-1,0xffff0000,0,0}, + {-.75f,-.75f,.5f,0,0,-1,0xffff0000,0,0} + }; + const float material[] = {0,1,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0}; + const float light[] = {0, 1,1,1,0, 0,0,0,0, 0,0,0,0, 0,0,0, 0,0,1, 0,0, 0,0,0, 0,0}; + memcpy(memory(data), material, sizeof(material)); + assert(!invoke(device, 16, 2, data)); + memcpy(memory(data), light, sizeof(light)); + *memory(data) = 3; + assert(!invoke(device, 18, 3, 0u, data)); + assert(!invoke(device, 44, 3, 0u, 1u)); + assert(!invoke(device, 20, 3, 139u, 0u)); + for (unsigned lighting = 0; lighting <= 1; ++lighting) + for (unsigned colored = 0; colored <= 1; ++colored) + for (unsigned source = 0; source <= 2; ++source) { + for (unsigned i = 0; i < 3; ++i) { + if (colored) memcpy(memory(input + i * 36), &vertices[i], 36); + else { + memcpy(memory(input + i * 32), &vertices[i], 24); + memcpy(memory(input + i * 32 + 24), &vertices[i].u, 8); + } + } + assert(!invoke(device, 20, 3, 137u, lighting)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + + assert(!invoke(device, 20, 3, 145u, source)); + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 4u, colored ? 0x152u : 0x112u, input, 3u, 0u)); + assert(!invoke(device, 6, 1)); + + assert(!invoke(device, 21, 3, 145u, data) && *memory(data) == source); + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + uint32_t expected = lighting ? (colored && source == 1 ? 0xff0000 : 0x00ff00) : + (colored ? 0xff0000 : 0xffffff); + assert((*memory(pixels + 8 * pitch + 8 * 4) & 0xffffff) == expected); + assert(!invoke(target, 32, 2, 0u)); + } + assert(!invoke(device, 44, 3, 0u, 0u)); + assert(!invoke(device, 20, 3, 145u, 1u)); +} + static void initial_device_states(uint32_t device) { const uint32_t states[][2] = { @@ -1557,6 +1608,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) transformed_render_states_preserve_rasterization(device, color); viewport_limits_clearing_and_drawing(device, color, file); untransformed_vertices_apply_camera_and_lighting(device, color, file); + diffuse_material_source_selects_lighting_color(device, color); depth_bias_offsets_overlapping_geometry(device, color, 16, file); assert(!invoke(color, 8, 3, 0u, depth)); memset(memory(desc), 0, 124);