loading some geometry, no display yet

This commit is contained in:
Anton Ljungdahl 2025-08-04 22:02:13 +02:00
parent 0cc9eb0b38
commit 159de19873
5 changed files with 108 additions and 16 deletions

View File

@ -1,4 +1,4 @@
// raddbg 0.9.17 project file
// raddbg 0.9.21 project file
recent_file: path: "src/render/render_core.c"
recent_file: path: "d:/os/obj/amd64fre/minkernel/crts/ucrt/src/appcrt/heap/mt/objfre/amd64/minkernel/crts/ucrt/src/appcrt/heap/free_base.cpp"
@ -13,8 +13,3 @@ target:
working_directory: build
enabled: 1
}
breakpoint:
{
source_location: "src/render/render_core.c:102:1"
hit_count: 1
}

View File

@ -40,7 +40,7 @@ entry_point()
os_event_init();
r_init();
r_load_obj(obj_filename);
r_load_obj(arena, obj_filename);
//test_arena(arena);
@ -59,8 +59,6 @@ entry_point()
DispatchMessageW(&Message);
}
for(U64 i = 0; i < os_events_received_on_frame(); i += 1)
{
OS_Event *event = &os_events.list[i];
@ -79,10 +77,8 @@ entry_point()
}
}
r_render();
os_event_reset();
//r_log_debug_messages();
if(g_do_exit)
@ -94,5 +90,4 @@ entry_point()
r_cleanup();
arena_release(arena);
}

View File

@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//~
R_GeomList *r_geomlist = 0;
internal void
r_tinyobj_get_file_data(void *ctx, const char *filename, const int is_mtl,
@ -66,8 +66,12 @@ r_tinyobj_get_file_data(void *ctx, const char *filename, const int is_mtl,
internal void
r_load_obj(const char *filename)
r_load_obj(Arena *arena, const char *filename)
{
r_geomlist = (R_GeomList *)push_array(arena, R_GeomList, 1);
R_Geom *geom = &r_geomlist->geom[r_geomlist->free_idx];
r_geomlist->free_idx += 1;
// tinyobj stuff, TODO(anton): Write my own obj loader and remove this.
tinyobj_attrib_t attrib;
tinyobj_shape_t *shapes = 0;
@ -77,7 +81,6 @@ r_load_obj(const char *filename)
// Init tinyobj
//tinyobj_attrib_init(&attrib);
R_TinyOBJ_Helper tinyobj_helper = {0};
U32 ret = tinyobj_parse_obj(&attrib, &shapes, &shape_count, &materials, &material_count,
filename, r_tinyobj_get_file_data, (void *)&tinyobj_helper, TINYOBJ_FLAG_TRIANGULATE);
@ -93,6 +96,85 @@ r_load_obj(const char *filename)
LOG("Material count: %u \n", material_count);
}
// Size of one element of the OBJ
U64 element_byte_count = sizeof(F32) * 3 // pos
+ sizeof(F32) * 3; // normal
U32 stride = element_byte_count / sizeof(F32);
U32 num_triangles = attrib.num_face_num_verts;
U64 buffer_byte_count = element_byte_count * num_triangles * 3;
// Allocate space for the vertex buffer data and store necessary info for geom
geom->data = (F32 *)push_array(arena, U8, buffer_byte_count);
geom->stride = stride;
geom->num_triangles = num_triangles;
geom->element_byte_count = element_byte_count;
geom->byte_count = buffer_byte_count;
U32 face_offset = 0;
for (U32 tri_idx = 0; tri_idx < attrib.num_face_num_verts; tri_idx += 1)
{
U32 face_num_vert = attrib.face_num_verts[tri_idx];
if(face_num_vert % 3 != 0)
{
LOG("Got non triangular face! abort!! \n");
Trap();
}
for (U32 f = 0; f < face_num_vert / 3; f += 1)
{
F32 v[3][3];
F32 n[3][3];
tinyobj_vertex_index_t vi0 = attrib.faces[face_offset + f * 3 + 0];
tinyobj_vertex_index_t vi1 = attrib.faces[face_offset + f * 3 + 1];
tinyobj_vertex_index_t vi2 = attrib.faces[face_offset + f * 3 + 2];
U32 k;
// For each component k of the three vertices for this face:
for(k = 0; k < 3; k += 1)
{
S32 idx0 = vi0.v_idx;
S32 idx1 = vi1.v_idx;
S32 idx2 = vi2.v_idx;
Assert(idx0 >= 0 && idx1 >= 0 && idx2 >= 0);
v[0][k] = attrib.vertices[3 * idx0 + k];
v[1][k] = attrib.vertices[3 * idx1 + k];
v[2][k] = attrib.vertices[3 * idx2 + k];
// Can add BBox calculation here
}
for(k = 0; k < 3; k += 1)
{
S32 idx0 = vi0.vn_idx;
S32 idx1 = vi1.vn_idx;
S32 idx2 = vi2.vn_idx;
Assert(idx0 >= 0 && idx1 >= 0 && idx2 >= 0);
n[0][k] = attrib.normals[3 * idx0 + k];
n[1][k] = attrib.normals[3 * idx1 + k];
n[2][k] = attrib.normals[3 * idx2 + k];
// Can add BBox calculation here
}
for(k = 0; k < 3; k +=1)
{
geom->data[(3 * tri_idx + k) * stride + 0] = v[k][0];
geom->data[(3 * tri_idx + k) * stride + 1] = v[k][1];
geom->data[(3 * tri_idx + k) * stride + 2] = v[k][2];
geom->data[(3 * tri_idx + k) * stride + 3] = n[k][0];
geom->data[(3 * tri_idx + k) * stride + 4] = n[k][1];
geom->data[(3 * tri_idx + k) * stride + 5] = n[k][2];
}
}
face_offset += face_num_vert;
}
tinyobj_attrib_free(&attrib);

View File

@ -37,6 +37,24 @@ struct R_BatchList
U64 byte_per_instance;
};
typedef struct R_Geom R_Geom;
struct R_Geom
{
F32 *data;
U64 stride;
U64 num_triangles;
U64 element_byte_count;
U64 byte_count;
};
typedef struct R_GeomList R_GeomList;
struct R_GeomList
{
Arena *arena;
R_Geom geom[4];
U64 free_idx;
};
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//~
@ -44,7 +62,7 @@ struct R_BatchList
internal void r_init();
internal void r_render();
internal void r_load_obj(const char *filename);
internal void r_load_obj(Arena *arena, const char *filename);
internal void r_log_debug_messages();

View File

@ -0,0 +1,2 @@
// tinyobj_loader_c.c