use tiled atlas texture sampling, increase shader speed using inv sqrt
This commit is contained in:
parent
0c0aa792c1
commit
1f717b7c61
57 changed files with 3681 additions and 2982 deletions
|
|
@ -118,7 +118,7 @@ static screen_rect project_portal_to_screen(const pxl8_bsp_portal* portal, const
|
|||
}
|
||||
|
||||
static void collect_face_to_mesh(const pxl8_bsp* bsp, const pxl8_bsp_render_state* state,
|
||||
u32 face_id, pxl8_mesh* mesh) {
|
||||
u32 face_id, pxl8_mesh* mesh, u8 ambient) {
|
||||
const pxl8_bsp_face* face = &bsp->faces[face_id];
|
||||
if (face->num_edges < 3) return;
|
||||
|
||||
|
|
@ -179,7 +179,11 @@ static void collect_face_to_mesh(const pxl8_bsp* bsp, const pxl8_bsp_render_stat
|
|||
|
||||
u8 light = 255;
|
||||
if (bsp->vertex_lights && vert_idx < bsp->num_vertex_lights) {
|
||||
light = (bsp->vertex_lights[vert_idx] >> 24) & 0xFF;
|
||||
u32 packed = bsp->vertex_lights[vert_idx];
|
||||
u8 direct = (packed >> 24) & 0xFF;
|
||||
u8 ao = (packed >> 16) & 0xFF;
|
||||
f32 combined = (f32)direct + ((f32)ambient / 255.0f) * (f32)ao;
|
||||
light = (u8)(combined > 255.0f ? 255.0f : combined);
|
||||
}
|
||||
|
||||
pxl8_vertex vtx = {
|
||||
|
|
@ -230,7 +234,7 @@ void pxl8_bsp_render_face(pxl8_gfx* gfx, const pxl8_bsp* bsp, u32 face_id, const
|
|||
pxl8_mesh* mesh = pxl8_mesh_create(64, 192);
|
||||
if (!mesh) return;
|
||||
|
||||
collect_face_to_mesh(bsp, NULL, face_id, mesh);
|
||||
collect_face_to_mesh(bsp, NULL, face_id, mesh, pxl8_gfx_get_ambient(gfx));
|
||||
|
||||
if (mesh->index_count > 0) {
|
||||
pxl8_mat4 identity = pxl8_mat4_identity();
|
||||
|
|
@ -242,15 +246,8 @@ void pxl8_bsp_render_face(pxl8_gfx* gfx, const pxl8_bsp* bsp, u32 face_id, const
|
|||
|
||||
void pxl8_bsp_render(pxl8_gfx* gfx, const pxl8_bsp* bsp,
|
||||
pxl8_bsp_render_state* state, pxl8_vec3 camera_pos) {
|
||||
if (!gfx || !bsp || !state || bsp->num_faces == 0) {
|
||||
return;
|
||||
}
|
||||
if (!bsp->cell_portals || bsp->num_cell_portals == 0) {
|
||||
return;
|
||||
}
|
||||
if (!state->materials || state->num_materials == 0) {
|
||||
return;
|
||||
}
|
||||
if (!gfx || !bsp || !state || bsp->num_faces == 0) return;
|
||||
if (!state->materials || state->num_materials == 0) return;
|
||||
|
||||
const pxl8_frustum* frustum = pxl8_3d_get_frustum(gfx);
|
||||
const pxl8_mat4* vp = pxl8_3d_get_view_proj(gfx);
|
||||
|
|
@ -259,6 +256,38 @@ void pxl8_bsp_render(pxl8_gfx* gfx, const pxl8_bsp* bsp,
|
|||
i32 camera_leaf = pxl8_bsp_find_leaf(bsp, camera_pos);
|
||||
if (camera_leaf < 0 || (u32)camera_leaf >= bsp->num_leafs) return;
|
||||
|
||||
if (!bsp->cell_portals || bsp->num_cell_portals == 0) {
|
||||
pxl8_mesh* mesh = pxl8_mesh_create(8192, 16384);
|
||||
if (!mesh) return;
|
||||
|
||||
u32 current_material = 0xFFFFFFFF;
|
||||
|
||||
for (u32 face_id = 0; face_id < bsp->num_faces; face_id++) {
|
||||
if (!face_in_frustum(bsp, face_id, frustum)) continue;
|
||||
|
||||
const pxl8_bsp_face* face = &bsp->faces[face_id];
|
||||
u32 material_id = face->material_id;
|
||||
if (material_id >= state->num_materials) continue;
|
||||
|
||||
if (material_id != current_material && mesh->index_count > 0 && current_material < state->num_materials) {
|
||||
pxl8_mat4 identity = pxl8_mat4_identity();
|
||||
pxl8_3d_draw_mesh(gfx, mesh, &identity, &state->materials[current_material]);
|
||||
pxl8_mesh_clear(mesh);
|
||||
}
|
||||
|
||||
current_material = material_id;
|
||||
collect_face_to_mesh(bsp, state, face_id, mesh, pxl8_gfx_get_ambient(gfx));
|
||||
}
|
||||
|
||||
if (mesh->index_count > 0 && current_material < state->num_materials) {
|
||||
pxl8_mat4 identity = pxl8_mat4_identity();
|
||||
pxl8_3d_draw_mesh(gfx, mesh, &identity, &state->materials[current_material]);
|
||||
}
|
||||
|
||||
pxl8_mesh_destroy(mesh);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!state->render_face_flags && state->num_faces > 0) {
|
||||
state->render_face_flags = pxl8_calloc(state->num_faces, 1);
|
||||
if (!state->render_face_flags) return;
|
||||
|
|
@ -370,7 +399,7 @@ void pxl8_bsp_render(pxl8_gfx* gfx, const pxl8_bsp* bsp,
|
|||
}
|
||||
|
||||
current_material = material_id;
|
||||
collect_face_to_mesh(bsp, state, face_id, mesh);
|
||||
collect_face_to_mesh(bsp, state, face_id, mesh, pxl8_gfx_get_ambient(gfx));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -420,10 +449,3 @@ void pxl8_bsp_set_material(pxl8_bsp_render_state* state, u16 material_id, const
|
|||
state->materials[material_id].u_offset = u_offset;
|
||||
state->materials[material_id].v_offset = v_offset;
|
||||
}
|
||||
|
||||
void pxl8_bsp_set_wireframe(pxl8_bsp_render_state* state, bool wireframe) {
|
||||
if (!state || !state->materials) return;
|
||||
for (u32 i = 0; i < state->num_materials; i++) {
|
||||
state->materials[i].wireframe = wireframe;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue