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 |
|
---|
14 | bool gpu_material_manager::load_resource( const string_view& id )
|
---|
15 | {
|
---|
16 | if ( auto mat = m_material_manager->get( id ).lock() )
|
---|
17 | {
|
---|
18 | gpu_material* result = new gpu_material;
|
---|
19 | sampler smp( sampler::LINEAR, sampler::REPEAT );
|
---|
20 | for ( uint32 i = 0; i < size( mat->paths ); ++i )
|
---|
21 | if ( !mat->paths[i].empty() )
|
---|
22 | if ( auto data = m_image_manager->get( mat->paths[i] ).lock() )
|
---|
23 | result->textures[i] = m_context->get_device()->create_texture( &*data, smp );
|
---|
24 | add( id, result );
|
---|
25 | return true;
|
---|
26 | }
|
---|
27 | return false;
|
---|
28 | }
|
---|
29 |
|
---|
30 | void gpu_material_manager::release( gpu_material* m )
|
---|
31 | {
|
---|
32 | for ( const texture& t : m->textures )
|
---|
33 | {
|
---|
34 | m_context->get_device()->release( t );
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | bool material_manager::load_resource( nv::lua::table_guard& table, nv::shash64 id )
|
---|
39 | {
|
---|
40 | material* m = new material;
|
---|
41 | m->paths[ TEX_DIFFUSE ] = table.get_string128( "diffuse" );
|
---|
42 | m->paths[ TEX_SPECULAR ] = table.get_string128( "specular" );
|
---|
43 | m->paths[ TEX_NORMAL ] = table.get_string128( "normal" );
|
---|
44 | m->paths[ TEX_GLOSS ] = table.get_string128( "gloss" );
|
---|
45 | add( id, m );
|
---|
46 | return true;
|
---|
47 | }
|
---|