source: trunk/src/engine/material_manager.cc @ 549

Last change on this file since 549 was 539, checked in by epyon, 8 years ago
  • temporary_proxy implemented
  • table_guard now uses temporary_proxy as main read functionality
File size: 2.8 KB
Line 
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
12using namespace nv;
13
14
15nv::gpu_material_manager::gpu_material_manager( context* context, material_manager* matmgr, image_manager* imgmgr )
16        : dependant_resource_manager( matmgr ), m_context( context )
17        , m_image_manager( imgmgr )
18{
19        uint8 data[2 * 2 * 4];
20        nv::raw_fill_n( data, 2 * 2 * 4, 0 );
21        m_default = m_context->create_texture( ivec2(2,2), nv::RGBA8, nv::sampler(), data );
22}
23
24nv::resource< nv::gpu_material > nv::gpu_material_manager::create_resource( resource< material > m )
25{
26        resource_id id = m.id();
27        if ( auto mat = m.lock() )
28        {
29                gpu_material* result = new gpu_material;
30                sampler smp( sampler::LINEAR_MIPMAP_LINEAR, sampler::REPEAT );
31                for ( uint32 i = 0; i < size( mat->paths ); ++i )
32                        if ( !mat->paths[i].empty() )
33                        {
34                                if ( auto data = m_image_manager->get( mat->paths[i] ).lock() )
35                                {
36                                        result->textures[i] = m_context->create_texture( &*data, smp );
37                                }
38                        }
39
40                // HACK
41                for ( uint32 i = 0; i < 5; ++i )
42                        if ( result->textures[i].is_nil() )
43                                result->textures[i] = m_default;
44
45                return add( id, result );
46        }
47        return resource< nv::gpu_material >();
48}
49
50void gpu_material_manager::release( gpu_material* m )
51{
52        for ( const texture& t : m->textures )
53        {
54                m_context->release( t );
55        }
56}
57
58bool material_manager::load_resource( nv::lua::table_guard& table, nv::shash64 id )
59{
60        c_file_system fs;
61        material* m = new material;
62
63        if ( table["path"].is_string() )
64        {
65                m->id = table["id"].get_string128();
66                string128 path = table["path"].get_string128();
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] );
80                                if ( m_clear_paths )
81                                        m->paths[i].clear();
82                        }
83                }
84        }
85        else
86        {
87                m->paths[ TEX_DIFFUSE  ] = table[ "diffuse"   ].get_string128();
88                m->paths[ TEX_NORMAL   ] = table[ "normal"    ].get_string128();
89                m->paths[ TEX_METALLIC ] = table[ "metallic"  ].get_string128();
90                m->paths[ TEX_ROUGHNESS] = table[ "roughness" ].get_string128();
91                m->paths[ TEX_EMISSIVE ] = table[ "emissive"  ].get_string128();
92        }
93        add( id, m );
94        return true;
95}
96
Note: See TracBrowser for help on using the repository browser.