[484] | 1 | // Copyright (C) 2015-2015 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
| 6 |
|
---|
| 7 | #include "nv/engine/material_manager.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/io/c_file_system.hh"
|
---|
| 10 | #include "nv/image/png_loader.hh"
|
---|
| 11 |
|
---|
| 12 | using namespace nv;
|
---|
| 13 |
|
---|
[486] | 14 |
|
---|
| 15 | nv::gpu_material_manager::gpu_material_manager( context* context, material_manager* matmgr, image_manager* imgmgr )
|
---|
[505] | 16 | : dependant_resource_manager( matmgr ), m_context( context )
|
---|
[486] | 17 | , m_image_manager( imgmgr )
|
---|
| 18 | {
|
---|
| 19 | uint8 data[2 * 2 * 3];
|
---|
| 20 | nv::raw_fill_n( data, 2 * 2 * 3, 0 );
|
---|
[501] | 21 | m_default = m_context->create_texture( ivec2(2,2), nv::image_format( nv::RGB ), nv::sampler(), data );
|
---|
[486] | 22 | }
|
---|
| 23 |
|
---|
[505] | 24 | nv::resource< nv::gpu_material > nv::gpu_material_manager::create_resource( resource< material > m )
|
---|
[484] | 25 | {
|
---|
[505] | 26 | resource_id id = m.id();
|
---|
| 27 | if ( auto mat = m.lock() )
|
---|
[484] | 28 | {
|
---|
| 29 | gpu_material* result = new gpu_material;
|
---|
[498] | 30 | sampler smp( sampler::LINEAR_MIPMAP_LINEAR, sampler::REPEAT );
|
---|
[484] | 31 | for ( uint32 i = 0; i < size( mat->paths ); ++i )
|
---|
| 32 | if ( !mat->paths[i].empty() )
|
---|
[486] | 33 | {
|
---|
[484] | 34 | if ( auto data = m_image_manager->get( mat->paths[i] ).lock() )
|
---|
[485] | 35 | {
|
---|
[501] | 36 | result->textures[i] = m_context->create_texture( &*data, smp );
|
---|
[485] | 37 | }
|
---|
[486] | 38 | }
|
---|
| 39 |
|
---|
| 40 | // HACK
|
---|
[508] | 41 | for ( uint32 i = 0; i < 5; ++i )
|
---|
| 42 | if ( result->textures[i].is_nil() )
|
---|
| 43 | result->textures[i] = m_default;
|
---|
[505] | 44 |
|
---|
| 45 | return add( id, result );
|
---|
[484] | 46 | }
|
---|
[505] | 47 | return resource< nv::gpu_material >();
|
---|
[484] | 48 | }
|
---|
| 49 |
|
---|
| 50 | void gpu_material_manager::release( gpu_material* m )
|
---|
| 51 | {
|
---|
| 52 | for ( const texture& t : m->textures )
|
---|
| 53 | {
|
---|
[501] | 54 | m_context->release( t );
|
---|
[484] | 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | bool material_manager::load_resource( nv::lua::table_guard& table, nv::shash64 id )
|
---|
| 59 | {
|
---|
[491] | 60 | c_file_system fs;
|
---|
[484] | 61 | material* m = new material;
|
---|
[491] | 62 |
|
---|
| 63 | if ( table.is_string( "path" ) )
|
---|
| 64 | {
|
---|
[509] | 65 | m->id = table.get_string128( "id" );
|
---|
[491] | 66 | string128 path = table.get_string128( "path" );
|
---|
| 67 | for ( uint32 i = 0; i < 5; ++i )
|
---|
| 68 | m->paths[i] = path;
|
---|
| 69 | m->paths[TEX_DIFFUSE].append( "_diffuse.png" );
|
---|
| 70 | m->paths[TEX_NORMAL].append( "_normal.png" );
|
---|
| 71 | m->paths[TEX_METALLIC].append( "_metallic.png" );
|
---|
| 72 | m->paths[TEX_ROUGHNESS].append( "_roughness.png" );
|
---|
| 73 | m->paths[TEX_EMISSIVE].append( "_emissive.png" );
|
---|
| 74 | for ( uint32 i = 0; i < 5; ++i )
|
---|
| 75 | {
|
---|
| 76 | if ( !fs.exists( m->paths[i] ) )
|
---|
| 77 | {
|
---|
| 78 | if ( i != TEX_EMISSIVE )
|
---|
| 79 | NV_LOG_ERROR( "Texture file not found! : ", m->paths[i] );
|
---|
[509] | 80 | if ( m_clear_paths )
|
---|
| 81 | m->paths[i].clear();
|
---|
[491] | 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
| 87 | m->paths[ TEX_DIFFUSE ] = table.get_string128( "diffuse" );
|
---|
| 88 | m->paths[ TEX_NORMAL ] = table.get_string128( "normal" );
|
---|
| 89 | m->paths[ TEX_METALLIC] = table.get_string128( "metallic" );
|
---|
| 90 | m->paths[ TEX_ROUGHNESS] = table.get_string128( "roughness" );
|
---|
| 91 | m->paths[ TEX_EMISSIVE ] = table.get_string128( "emissive" );
|
---|
| 92 | }
|
---|
[484] | 93 | add( id, m );
|
---|
| 94 | return true;
|
---|
| 95 | }
|
---|
[491] | 96 |
|
---|