aorecomp

Anarchy Online classic in the browser

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


commit 3bb42f76fe9b8d2d3add76aea5eecd5ff5e03c93
parent d6d0fbb0f70e55a92b8ac05d959c5eded784ac77
Author: anon <anon@example.com>
Date:   Sun, 25 Oct 2026 11:36:18 +0000

Render projective world and view transforms

Diffstat:
Mclient/runtime/linux_graphics.c | 63++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mclient/tests/devices.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 117 insertions(+), 11 deletions(-)

diff --git a/client/runtime/linux_graphics.c b/client/runtime/linux_graphics.c @@ -1933,6 +1933,39 @@ static void transform_vector(const float input[4], const float matrix[16], float input[2] * matrix[8 + j] + input[3] * matrix[12 + j]; } +static int projective_normal_matrix(const float matrix[16], float normal[9]) +{ + /* Invert the complete world/view matrix before taking its normal portion. */ + float rows[4][8] = {0}; + for (unsigned i = 0; i < 4; ++i) { + memcpy(rows[i], matrix + i * 4, 16); + rows[i][i + 4] = 1; + } + for (unsigned column = 0; column < 4; ++column) { + unsigned pivot = column; + for (unsigned row = column + 1; row < 4; ++row) + if (fabsf(rows[row][column]) > fabsf(rows[pivot][column])) pivot = row; + if (!rows[pivot][column]) return 0; + if (pivot != column) for (unsigned i = 0; i < 8; ++i) { + float value = rows[column][i]; + rows[column][i] = rows[pivot][i]; + rows[pivot][i] = value; + } + + /* Eliminate this column from the remaining rows. */ + float divisor = rows[column][column]; + for (unsigned i = 0; i < 8; ++i) rows[column][i] /= divisor; + for (unsigned row = 0; row < 4; ++row) if (row != column) { + float factor = rows[row][column]; + for (unsigned i = 0; i < 8; ++i) rows[row][i] -= factor * rows[column][i]; + } + } + for (unsigned row = 0; row < 3; ++row) + for (unsigned column = 0; column < 3; ++column) + normal[row * 3 + column] = rows[column][row + 4]; + return 1; +} + static float normalize_vector(float vector[3]) { float length = sqrtf(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]); @@ -1999,21 +2032,29 @@ static void draw_primitive(struct device_object *device, uint32_t primitive, uin uint32_t index_count = primitive == 2 || primitive == 4 ? element_count : (element_count - 2) * 3; uint16_t *indices = malloc(index_count * sizeof(*indices)); if (!indices) abort(); - /* Affine world/view transforms also determine the inverse-transpose normal matrix. */ - float world_view[16], normal_matrix[9]; + /* World/view transforms also determine the inverse-transpose normal matrix. */ + float world_view[16], normal_matrix[9] = {0}; if (untransformed) { const float *world = (const float *)device->transforms[0], *view = (const float *)device->transforms[1]; for (unsigned i = 0; i < 4; ++i) transform_vector(world + i * 4, view, world_view + i * 4); - if (world_view[3] || world_view[7] || world_view[11] || world_view[15] != 1) { - fprintf(stderr, "Unsupported non-affine world/view transform\n"); exit(3); + int transform_normals = device->lighting; + for (unsigned stage = 0; stage < 2 && device->stages[stage].color_op != 1; ++stage) + transform_normals |= device->stages[stage].texture_coordinate == 0x10000; + if ((fvf & 0x10) && transform_normals) { + if (world_view[3] || world_view[7] || world_view[11] || world_view[15] != 1) { + if (!projective_normal_matrix(world_view, normal_matrix)) { + fprintf(stderr, "Unsupported singular world/view transform\n"); exit(3); + } + } else { + const float *m = world_view; + float cofactors[] = {m[5]*m[10]-m[6]*m[9], m[6]*m[8]-m[4]*m[10], m[4]*m[9]-m[5]*m[8], + m[2]*m[9]-m[1]*m[10], m[0]*m[10]-m[2]*m[8], m[1]*m[8]-m[0]*m[9], + m[1]*m[6]-m[2]*m[5], m[2]*m[4]-m[0]*m[6], m[0]*m[5]-m[1]*m[4]}; + float determinant = m[0]*cofactors[0] + m[1]*cofactors[1] + m[2]*cofactors[2]; + 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; + } } - const float *m = world_view; - float cofactors[] = {m[5]*m[10]-m[6]*m[9], m[6]*m[8]-m[4]*m[10], m[4]*m[9]-m[5]*m[8], - m[2]*m[9]-m[1]*m[10], m[0]*m[10]-m[2]*m[8], m[1]*m[8]-m[0]*m[9], - m[1]*m[6]-m[2]*m[5], m[2]*m[4]-m[0]*m[6], m[0]*m[5]-m[1]*m[4]}; - float determinant = m[0]*cofactors[0] + m[1]*cofactors[1] + m[2]*cofactors[2]; - 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; } /* Prepare enabled lights and their transforms in the original accumulation order. */ device->enabled_lights = NULL; diff --git a/client/tests/devices.c b/client/tests/devices.c @@ -695,6 +695,70 @@ static void line_lists_draw_independent_segments(uint32_t draw, uint32_t device, invoke(d3d, 2, 1); } +static void projective_world_and_view_matrices_render(uint32_t device, uint32_t target) +{ + uint32_t data = workspace() + 256, input = workspace() + 512, desc = workspace() + 128; + const float identity[] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1}; + const float projective[] = {1,0,0,.5f, 0,1,0,0, 0,0,1,0, .5f,0,0,1}; + const float positions[][3] = {{-.75f,.75f,.5f}, {.75f,.75f,.5f}, {.75f,-.75f,.5f}, {-.75f,-.75f,.5f}}; + for (unsigned i = 0; i < 4; ++i) { + const float normal[] = {1,0,0}; + memcpy(memory(input + i * 36), positions[i], 12); + memcpy(memory(input + i * 36 + 12), normal, 12); + *memory(input + i * 36 + 24) = 0xff5a96d2; + memset(memory(input + i * 36 + 28), 0, 8); + } + const float material[] = {.5f,0,0,1, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0}; + memcpy(memory(data), material, sizeof(material)); + assert(!invoke(device, 16, 2, data)); + float light[26] = {0}; + light[1] = 1; light[16] = -1; + memcpy(memory(data), light, sizeof(light)); + *memory(data) = 3; + assert(!invoke(device, 18, 3, 0u, data)); + for (unsigned i = 0; i < 9; ++i) assert(!invoke(device, 44, 3, i, i == 0)); + assert(!invoke(device, 20, 3, 139u, 0u)); + assert(!invoke(device, 20, 3, 143u, 0u)); + assert(!invoke(device, 20, 3, 145u, 0u)); + assert(!invoke(device, 20, 3, 148u, 0u)); + assert(!invoke(device, 20, 3, 28u, 0u)); + assert(!invoke(device, 20, 3, 27u, 0u)); + assert(!invoke(device, 20, 3, 15u, 0u)); + assert(!invoke(device, 20, 3, 22u, 1u)); + assert(!invoke(device, 35, 3, 0u, 0u)); + assert(!invoke(device, 37, 4, 0u, 1u, 2u)); + assert(!invoke(device, 37, 4, 0u, 2u, 0u)); + assert(!invoke(device, 37, 4, 0u, 11u, 0u)); + assert(!invoke(device, 37, 4, 0u, 24u, 0u)); + assert(!invoke(device, 37, 4, 1u, 1u, 1u)); + /* Disabled stages can retain normal-coordinate generation without using it. */ + assert(!invoke(device, 37, 4, 1u, 11u, 0x10000u)); + + for (unsigned transform = 1; transform <= 2; ++transform) + for (unsigned cpu = 0; cpu <= 1; ++cpu) + for (unsigned lighting = 0; lighting <= 1; ++lighting) + for (unsigned singular = 0; singular <= !lighting; ++singular) { + for (unsigned state = 1; state <= 3; ++state) { + memcpy(memory(data), state == transform ? projective : identity, 64); + if (state == transform && singular) *memory(data + 40) = 0; + assert(!invoke(device, 11, 3, state, data)); + } + assert(!invoke(device, 20, 3, 29u, cpu)); + assert(!invoke(device, 20, 3, 137u, lighting)); + assert(!invoke(device, 10, 7, 0u, 0u, 3u, 0xff102030u, 0x3f800000u, 0u)); + assert(!invoke(device, 5, 1)); + assert(!invoke(device, 25, 6, 6u, 0x152u, input, 4u, 0u)); + assert(!invoke(device, 6, 1)); + + *memory(desc) = 124; + assert(!invoke(target, 25, 5, 0u, desc, 0x11u, 0u)); + uint32_t pixels = *memory(desc + 36), pitch = *memory(desc + 16); + assert((*memory(pixels + 16 * pitch + 16 * 4) & 0xffffff) == (lighting ? 0xaa0000u : 0x5a96d2u)); + assert((*memory(pixels + 4 * pitch + 4 * 4) & 0xffffff) == 0x102030); + assert(!invoke(target, 32, 2, 0u)); + } +} + static void untransformed_vertices_apply_camera_and_lighting(uint32_t device, uint32_t target, FILE *file) { for (unsigned emissive = 0; emissive < 3; ++emissive) @@ -2051,6 +2115,7 @@ static void device_lifetime_and_rendering(const char *pixels_path) fourfold_modulation_clamps_before_the_next_stage(device, color); two_texture_stages_combine_independent_samples(draw, device, color, file); line_lists_draw_independent_segments(draw, device, color); + projective_world_and_view_matrices_render(device, color); assert(!fclose(file)); assert(!invoke(device, 2, 1)); assert(!invoke(draw, 2, 1));