medfall

A super great game engine
Log | Files | Refs

commit e84f4c90b0ee0a50896577521e0d78a42fd8bb05
parent 2fe65bab1e760d7ab40e053789ac0b2b35d4fdba
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri May 19 02:17:40 +0300

Fix normals when loading OBJs. Implementation is not ideal!

Diffstat:
shadow_map.cc | 43+++++++++++++++----------------------------
1 file changed, 15 insertions(+), 28 deletions(-)
diff --git a/shadow_map.cc b/shadow_map.cc @@ -23,6 +23,8 @@ static Mesh tree_mesh; static float bias = 0.00005; +static const u32 SHADOW_SIZE = 1024; + static v3 light_pos( -0, -10, 15 ); static v3 light_dir = v3_forward( -60, 90 ); @@ -62,20 +64,6 @@ static void draw_scene( const RenderState & render_state, bool draw_light = fals glCullFace( GL_FRONT ); } -const u32 SHADOW_SIZE = 1024; - -struct UniformsMVP { - m4 M; - m4 V; - m4 P; -}; - -struct UniformsLightMVP { - m4 M; - m4 light_V; - m4 light_P; -}; - extern "C" GAME_INIT( game_init ) { ub_model = renderer_new_ub(); ub_light_view = renderer_new_ub(); @@ -111,9 +99,11 @@ extern "C" GAME_INIT( game_init ) { } { + MEMARENA_SCOPED_CHECKPOINT( &mem->persistent_arena ); + size_t len; const u8 * tree_data = file_get_contents( "models/trees/PineTree.obj", &len ); - tinyobj_attrib_t attrib; + static tinyobj_attrib_t attrib; tinyobj_shape_t * shapes = NULL; size_t num_shapes; tinyobj_material_t * materials = NULL; @@ -122,26 +112,23 @@ extern "C" GAME_INIT( game_init ) { ggprint( "ok = {} num_shapes = {} num_materials = {}\n", ok, num_shapes, num_materials ); - MEMARENA_SCOPED_CHECKPOINT( &mem->persistent_arena ); - u32 num_verts = attrib.num_face_num_verts * 3; - array< v3 > colours = memarena_push_array( &mem->persistent_arena, v3, attrib.num_vertices ); - array< s32 > indices = memarena_push_array( &mem->persistent_arena, s32, num_verts ); - - for( v3 & c : colours ) { - c = v3( 0.2, 0.6, 0 ); - } + array< v3 > positions = memarena_push_array( &mem->persistent_arena, v3, num_verts ); + array< v3 > normals = memarena_push_array( &mem->persistent_arena, v3, num_verts ); + array< v3 > colours = memarena_push_array( &mem->persistent_arena, v3, num_verts ); for( u32 i = 0; i < num_verts; i++ ) { - indices[ i ] = attrib.faces[ i ].v_idx; + tinyobj_vertex_index_t idx = attrib.faces[ i ]; + positions[ i ] = ( ( v3 * ) attrib.vertices )[ idx.v_idx ]; + normals[ i ] = ( ( v3 * ) attrib.normals )[ idx.vn_idx ]; + colours[ i ] = v3( 0.2, 0.6, 0 ); } MeshConfig mesh_config; - mesh_config.positions = renderer_new_vb( attrib.vertices, attrib.num_vertices * sizeof( attrib.vertices[ 0 ] ) * 3 ); - mesh_config.normals = renderer_new_vb( attrib.normals, attrib.num_vertices * sizeof( attrib.normals[ 0 ] ) * 3 ); + mesh_config.positions = renderer_new_vb( positions.ptr(), positions.num_bytes() ); + mesh_config.normals = renderer_new_vb( normals.ptr(), normals.num_bytes() ); mesh_config.colours = renderer_new_vb( colours.ptr(), colours.num_bytes() ); - mesh_config.indices = renderer_new_ib( indices.ptr(), indices.num_bytes() ); - mesh_config.num_vertices = indices.n; + mesh_config.num_vertices = num_verts; tree_mesh = renderer_new_mesh( mesh_config ); } }