medfall

A super great game engine
Log | Files | Refs

commit 91b858740feaec0ca9ce5f727c5dd1dfed37c966
parent ad65836d6f3cc3226432a1c8ebd40f9bbc6e50c4
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Fri May 19 23:56:23 +0300

Add .obj -> Mesh helper

Diffstat:
make.lua | 2+-
obj.cc | 48++++++++++++++++++++++++++++++++++++++++++++++++
obj.h | 6++++++
shadow_map.cc | 37++-----------------------------------
4 files changed, 57 insertions(+), 36 deletions(-)
diff --git a/make.lua b/make.lua @@ -1,7 +1,7 @@ require( "scripts.gen_makefile" ) local common_objs = { "memory_arena", "log", "ggformat", "strlcpy", "strlcat", "strtonum", "profiler", "stats", "rng/well512" } -local game_objs = { "work_queue", "renderer", "shaders", "gl", "glad", "immediate", "text_renderer", "stb_truetype", common_objs } +local game_objs = { "work_queue", "renderer", "shaders", "gl", "glad", "immediate", "text_renderer", "obj", "stb_truetype", common_objs } local game_libs = { "glfw", "tinyobjloader" } bin( "medfall", { "main", "hm", "heightmap", "terrain_manager", "lz4", "btt", "gpubtt", "skybox", "http", game_objs }, { game_libs } ) diff --git a/obj.cc b/obj.cc @@ -0,0 +1,48 @@ +#include "intrinsics.h" +#include "memory_arena.h" +#include "renderer.h" + +#include "libs/tinyobjloader/tinyobjloader.h" + +Mesh load_obj( const char * path, MemoryArena * arena ) { + MEMARENA_SCOPED_CHECKPOINT( arena ); + + size_t len; + u8 * data = file_get_contents( path, &len ); + + static tinyobj_attrib_t attrib; + tinyobj_shape_t * shapes = NULL; + size_t num_shapes; + tinyobj_material_t * materials = NULL; + size_t num_materials; + int ok = tinyobj_parse_obj( &attrib, &shapes, &num_shapes, &materials, &num_materials, ( const char * ) data, len, TINYOBJ_FLAG_TRIANGULATE ); + ASSERT( ok == 0 ); + + const array< v3 > data_positions = array< v3 >( ( v3 * ) attrib.vertices, attrib.num_vertices ); + const array< v3 > data_normals = array< v3 >( ( v3 * ) attrib.normals, attrib.num_normals ); + const array< v3 > data_uvs = array< v3 >( ( v3 * ) attrib.texcoords, attrib.num_texcoords ); + + u32 num_verts = attrib.num_face_num_verts * 3; + array< v3 > positions = memarena_push_array( arena, v3, num_verts ); + array< v3 > normals = memarena_push_array( arena, v3, num_verts ); + array< v3 > uvs = memarena_push_array( arena, v3, num_verts ); + array< v3 > colours = memarena_push_array( arena, v3, num_verts ); + + for( u32 i = 0; i < num_verts; i++ ) { + tinyobj_vertex_index_t idx = attrib.faces[ i ]; + positions[ i ] = data_positions[ idx.v_idx ]; + normals[ i ] = data_normals[ idx.vn_idx ]; + uvs[ i ] = data_uvs[ idx.vt_idx ]; + colours[ i ] = v3( 0.2, 0.6, 0 ); + } + + free( data ); + + MeshConfig mesh_config; + mesh_config.positions = renderer_new_vb( positions ); + mesh_config.normals = renderer_new_vb( normals ); + mesh_config.tex_coords0 = renderer_new_vb( uvs ); + mesh_config.colours = renderer_new_vb( colours ); + mesh_config.num_vertices = num_verts; + return renderer_new_mesh( mesh_config ); +} diff --git a/obj.h b/obj.h @@ -0,0 +1,6 @@ +#pragma once + +#include "memory_arena.h" +#include "renderer.h" + +Mesh load_obj( const char * path, MemoryArena * arena ); diff --git a/shadow_map.cc b/shadow_map.cc @@ -7,8 +7,7 @@ #include "shaders.h" #include "text_renderer.h" #include "log.h" - -#include "libs/tinyobjloader/tinyobjloader.h" +#include "obj.h" static ImmediateTriangle triangles[ 512000 ]; static ImmediateContext imm; @@ -98,39 +97,7 @@ extern "C" GAME_INIT( game_init ) { square = renderer_new_mesh( config ); } - { - MEMARENA_SCOPED_CHECKPOINT( &mem->persistent_arena ); - - size_t len; - const u8 * tree_data = file_get_contents( "models/trees/PineTree.obj", &len ); - static tinyobj_attrib_t attrib; - tinyobj_shape_t * shapes = NULL; - size_t num_shapes; - tinyobj_material_t * materials = NULL; - size_t num_materials; - int ok = tinyobj_parse_obj( &attrib, &shapes, &num_shapes, &materials, &num_materials, ( const char * ) tree_data, len, TINYOBJ_FLAG_TRIANGULATE ); - - ggprint( "ok = {} num_shapes = {} num_materials = {}\n", ok, num_shapes, num_materials ); - - u32 num_verts = attrib.num_face_num_verts * 3; - 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++ ) { - 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( 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.num_vertices = num_verts; - tree_mesh = renderer_new_mesh( mesh_config ); - } + tree_mesh = load_obj( "models/trees/PineTree.obj", &mem->persistent_arena ); } extern "C" GAME_FRAME( game_frame ) {