~ubuntu-branches/ubuntu/quantal/aqsis/quantal

« back to all changes in this revision

Viewing changes to libs/tex/filtering/texturecache.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-08-06 04:53:26 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806045326-z6xeaaao62idxcc6
Tags: 1.6.0-0ubuntu1
* New upstream release
* debian/control:
  - changed name of lib package to libaqsis1 instead of aqsis-libsc2a
  - changed name of dev package to libaqsis-dev instead of aqsis-libs-dev
  - Added aqsis-data package
  - Revised summary text according to that specified by the RISpec (Pixar)
* Moved examples installation from aqsis.install to aqsis-data.install
* debian/rules: 
  - added content to binary-indep target
* debian/rules: added explicit name of mime file to force dh_installmime
  to generate postinst and prerm scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright (C) 1997 - 2007, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.org
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
/** \file
 
21
 *
 
22
 * \brief Texture cache implementation.
 
23
 *
 
24
 * \author Chris Foster [ chris42f (at) gmail (dot) com ]
 
25
 */
 
26
 
 
27
#include "texturecache.h"
 
28
 
 
29
#include <aqsis/util/exception.h>
 
30
#include <aqsis/util/file.h>
 
31
#include <aqsis/tex/filtering/ienvironmentsampler.h>
 
32
#include <aqsis/tex/filtering/iocclusionsampler.h>
 
33
#include <aqsis/tex/filtering/ishadowsampler.h>
 
34
#include <aqsis/tex/io/itiledtexinputfile.h>
 
35
#include <aqsis/tex/filtering/itexturesampler.h>
 
36
#include <aqsis/util/logging.h>
 
37
#include <aqsis/util/sstring.h>
 
38
#include <aqsis/tex/texexception.h>
 
39
 
 
40
namespace Aqsis {
 
41
 
 
42
//------------------------------------------------------------------------------
 
43
// IqTextureCache creation function.
 
44
 
 
45
boost::shared_ptr<IqTextureCache> IqTextureCache::create(
 
46
                TqSearchPathCallback searchPathCallback)
 
47
{
 
48
        return boost::shared_ptr<IqTextureCache>(
 
49
                        new CqTextureCache(searchPathCallback));
 
50
}
 
51
 
 
52
//------------------------------------------------------------------------------
 
53
// CqTextureCache
 
54
 
 
55
CqTextureCache::CqTextureCache(TqSearchPathCallback searchPathCallback)
 
56
        : m_textureCache(),
 
57
        m_environmentCache(),
 
58
        m_shadowCache(),
 
59
        m_occlusionCache(),
 
60
        m_texFileCache(),
 
61
        m_currToWorld(),
 
62
        m_searchPathCallback(searchPathCallback)
 
63
{ }
 
64
 
 
65
IqTextureSampler& CqTextureCache::findTextureSampler(const char* name)
 
66
{
 
67
        return findSampler(m_textureCache, name);
 
68
}
 
69
 
 
70
IqEnvironmentSampler& CqTextureCache::findEnvironmentSampler(const char* name)
 
71
{
 
72
        return findSampler(m_environmentCache, name);
 
73
}
 
74
 
 
75
IqShadowSampler& CqTextureCache::findShadowSampler(const char* name)
 
76
{
 
77
        return findSampler(m_shadowCache, name);
 
78
}
 
79
 
 
80
IqOcclusionSampler& CqTextureCache::findOcclusionSampler(const char* name)
 
81
{
 
82
        return findSampler(m_occlusionCache, name);
 
83
}
 
84
 
 
85
void CqTextureCache::flush()
 
86
{
 
87
        m_textureCache.clear();
 
88
        m_environmentCache.clear();
 
89
        m_shadowCache.clear();
 
90
        m_occlusionCache.clear();
 
91
        m_texFileCache.clear();
 
92
}
 
93
 
 
94
const CqTexFileHeader* CqTextureCache::textureInfo(const char* name)
 
95
{
 
96
        boost::shared_ptr<IqTiledTexInputFile> file;
 
97
        try
 
98
        {
 
99
                file = getTextureFile(name);
 
100
                return &(file->header());
 
101
        }
 
102
        catch(XqInvalidFile& /*e*/)
 
103
        {
 
104
                return 0;
 
105
        }
 
106
}
 
107
 
 
108
void CqTextureCache::setCurrToWorldMatrix(const CqMatrix& currToWorld)
 
109
{
 
110
        m_currToWorld = currToWorld;
 
111
}
 
112
 
 
113
//--------------------------------------------------
 
114
// Private methods
 
115
template<typename SamplerT>
 
116
SamplerT& CqTextureCache::findSampler(
 
117
                std::map<TqUlong, boost::shared_ptr<SamplerT> >& samplerMap,
 
118
                const char* name)
 
119
{
 
120
        TqUlong hash = CqString::hash(name);
 
121
        typename std::map<TqUlong, boost::shared_ptr<SamplerT> >::const_iterator
 
122
                texIter = samplerMap.find(hash);
 
123
        if(texIter != samplerMap.end())
 
124
        {
 
125
                // The desired texture sampler is already created - return it.
 
126
                return *(texIter->second);
 
127
        }
 
128
        else
 
129
        {
 
130
                // Couldn't find in the currently open texture samplers - create a new
 
131
                // instance.
 
132
                boost::shared_ptr<SamplerT> newTex;
 
133
                try
 
134
                {
 
135
                        // Find the file in the current file cache.
 
136
                        newTex = newSamplerFromFile<SamplerT>(getTextureFile(name));
 
137
                }
 
138
                catch(XqInvalidFile& e)
 
139
                {
 
140
                        Aqsis::log() << error
 
141
                                << "Invalid texture file - " << e.what() << "\n";
 
142
                        newTex = SamplerT::createDummy();
 
143
                }
 
144
                catch(XqBadTexture& e)
 
145
                {
 
146
                        Aqsis::log() << error
 
147
                                << "Bad texture file - " << e.what() << "\n";
 
148
                        newTex = SamplerT::createDummy();
 
149
                }
 
150
                samplerMap[CqString::hash(name)] = newTex;
 
151
                return *newTex;
 
152
        }
 
153
}
 
154
 
 
155
boost::shared_ptr<IqTiledTexInputFile> CqTextureCache::getTextureFile(
 
156
                const char* name)
 
157
{
 
158
        TqUlong hash = CqString::hash(name);
 
159
        std::map<TqUlong, boost::shared_ptr<IqTiledTexInputFile> >::const_iterator
 
160
                fileIter = m_texFileCache.find(hash);
 
161
        if(fileIter != m_texFileCache.end())
 
162
                // File exists in the cache; return it.
 
163
                return fileIter->second;
 
164
        // Else try to open the file and store it in the cache before returning it.
 
165
        boostfs::path fullName = findFile(name, m_searchPathCallback());
 
166
        boost::shared_ptr<IqTiledTexInputFile> file;
 
167
        try
 
168
        {
 
169
                file = IqTiledTexInputFile::open(fullName);
 
170
        }
 
171
        catch(XqBadTexture& e)
 
172
        {
 
173
                file = IqTiledTexInputFile::openAny(fullName);
 
174
                /// \todo Make sure this warning doesn't apply to files used only for
 
175
                /// the textureInfo() function...
 
176
                Aqsis::log() << warning << "Could not open file as a tiled texture: "
 
177
                        << e.what() << ".  Rendering will continue, but may be slower.\n";
 
178
        }
 
179
        m_texFileCache[hash] = file;
 
180
        return file;
 
181
}
 
182
 
 
183
template<typename SamplerT>
 
184
boost::shared_ptr<SamplerT> CqTextureCache::newSamplerFromFile(
 
185
                const boost::shared_ptr<IqTiledTexInputFile>& file)
 
186
{
 
187
        return SamplerT::create(file);
 
188
}
 
189
 
 
190
// Special case of newSamplerFromFile() for shadow and occlusion maps - they
 
191
// need access to the camera->world transformation matrix.
 
192
template<>
 
193
boost::shared_ptr<IqShadowSampler>
 
194
CqTextureCache::newSamplerFromFile(const boost::shared_ptr<IqTiledTexInputFile>& file)
 
195
{
 
196
        return IqShadowSampler::create(file, m_currToWorld);
 
197
}
 
198
template<>
 
199
boost::shared_ptr<IqOcclusionSampler>
 
200
CqTextureCache::newSamplerFromFile(const boost::shared_ptr<IqTiledTexInputFile>& file)
 
201
{
 
202
        return IqOcclusionSampler::create(file, m_currToWorld);
 
203
}
 
204
 
 
205
} // namespace Aqsis