medfall

A super great game engine
Log | Files | Refs

obj.cc (2771B)


      1 #include "intrinsics.h"
      2 #include "memory_arena.h"
      3 #include "renderer.h"
      4 
      5 #include "libs/stb/stb_image.h"
      6 
      7 #include "libs/tinyobjloader/tinyobjloader.h"
      8 
      9 Mesh load_obj( const char * path, MemoryArena * arena ) {
     10 	MEMARENA_SCOPED_CHECKPOINT( arena );
     11 
     12 	size_t len;
     13 	u8 * data = file_get_contents( path, &len );
     14 
     15 	static tinyobj_attrib_t attrib;
     16 	tinyobj_shape_t * shapes = NULL;
     17 	size_t num_shapes;
     18 	tinyobj_material_t * materials = NULL;
     19 	size_t num_materials;
     20 	int ok = tinyobj_parse_obj( &attrib, &shapes, &num_shapes, &materials, &num_materials, ( const char * ) data, len, TINYOBJ_FLAG_TRIANGULATE );
     21 	ASSERT( ok == 0 );
     22 
     23 	const array< v3 > data_positions = array< v3 >( ( v3 * ) attrib.vertices, attrib.num_vertices );
     24 	const array< v3 > data_normals = array< v3 >( ( v3 * ) attrib.normals, attrib.num_normals );
     25 	const array< v3 > data_uvs = array< v3 >( ( v3 * ) attrib.texcoords, attrib.num_texcoords );
     26 
     27 	u32 num_verts = attrib.num_face_num_verts * 3;
     28 	array< v3 > positions = memarena_push_array( arena, v3, num_verts );
     29 	array< v3 > normals = memarena_push_array( arena, v3, num_verts );
     30 	array< v3 > uvs = memarena_push_array( arena, v3, num_verts );
     31 	array< v3 > colours = memarena_push_array( arena, v3, num_verts );
     32 
     33 	for( u32 i = 0; i < num_verts; i++ ) {
     34 		tinyobj_vertex_index_t idx = attrib.faces[ i ];
     35 		positions[ i ] = data_positions[ idx.v_idx ];
     36 		normals[ i ] = data_normals[ idx.vn_idx ];
     37 		uvs[ i ] = data_uvs[ idx.vt_idx ];
     38 		colours[ i ] = v3( 0.2f, 0.6f, 0.0f );
     39 	}
     40 
     41 	free( data );
     42 
     43 	MeshConfig mesh_config;
     44 	mesh_config.positions = renderer_new_vb( positions );
     45 	mesh_config.normals = renderer_new_vb( normals );
     46 	mesh_config.tex_coords0 = renderer_new_vb( uvs );
     47 	mesh_config.colours = renderer_new_vb( colours );
     48 	mesh_config.num_vertices = num_verts;
     49 	return renderer_new_mesh( mesh_config );
     50 }
     51 
     52 Texture load_png( const char * path, TextureFormat format, int channels ) {
     53 	int w, h;
     54 	u8 * img = stbi_load( path, &w, &h, NULL, channels );
     55 	ASSERT( img != NULL );
     56 
     57 	TextureConfig texture_config;
     58 	texture_config.width = checked_cast< u32 >( w );
     59 	texture_config.height = checked_cast< u32 >( h );
     60 	texture_config.data = img;
     61 	texture_config.format = format;
     62 	Texture texture = renderer_new_texture( texture_config );
     63 
     64 	stbi_image_free( img );
     65 
     66 	return texture;
     67 }
     68 
     69 Texture load_png_memory( const u8 * data, size_t len, TextureFormat format, int channels ) {
     70 	int w, h;
     71 	u8 * img = stbi_load_from_memory( data, checked_cast< int >( len ), &w, &h, NULL, channels );
     72 	ASSERT( img != NULL );
     73 
     74 	TextureConfig texture_config;
     75 	texture_config.width = w;
     76 	texture_config.height = h;
     77 	texture_config.data = img;
     78 	texture_config.format = format;
     79 	Texture texture = renderer_new_texture( texture_config );
     80 
     81 	stbi_image_free( img );
     82 
     83 	return texture;
     84 }