diff options
Diffstat (limited to 'hw6/src/sceneIO_texture.cpp')
-rw-r--r-- | hw6/src/sceneIO_texture.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/hw6/src/sceneIO_texture.cpp b/hw6/src/sceneIO_texture.cpp new file mode 100644 index 0000000..f537613 --- /dev/null +++ b/hw6/src/sceneIO_texture.cpp @@ -0,0 +1,55 @@ +/******************************************************************/ +/* This file is part of the homework assignments for CSCI-427/527 */ +/* at The College of William & Mary and authored by Pieter Peers. */ +/* No part of this file, whether altered or in original form, can */ +/* be distributed or used outside the context of CSCI-427/527 */ +/* without consent of either the College of William & Mary or */ +/* Pieter Peers. */ +/******************************************************************/ +#include "sceneIO_core.h" +#include "sceneIO_texture.h" + +#include "util.h" +#include "imageIO.h" +#include "nearestTexture.h" +#include "bilinearTexture.h" + +std::shared_ptr<texture_base> importTexture(const XMLNode& node, nodeCache<texture_base>& texture_cache, const std::string& rootDir) +{ + // check if texture + if(!node.isValid() || node.name() != "texture") return std::shared_ptr<texture_base>(nullptr); + + // get attributes + std::string type = getString(node, "type", "nearest"); + std::string filename = getString(node, "filename"); + bool repeatTexture = (getString(node, "repeatTexture", "false") == "false"); + + // sanity check + if(filename == "") + errorMessage("Missing texture 'filename'."); + + // get full filename + std::string path = getDirectory(filename); + if(path == "") filename = rootDir + filename; + + // get key name + std::string key = filename + "#" + type; + if(repeatTexture) key += "Repeat"; + + // check cache + if(std::shared_ptr<texture_base> ctexture = texture_cache.get(key)) + return ctexture; + + // create based on type + std::shared_ptr<texture_base> texture; + if(type == "nearest") texture = std::shared_ptr<texture_base>( new nearestTexture(repeatTexture)); + else if(type == "bilinear") texture = std::shared_ptr<texture_base>( new bilinearTexture(repeatTexture) ); + else errorMessage("Unknown texture type (%s).", type.c_str()); + + // load and add texture to cache + importImage(filename, *texture); + texture_cache.add(key, texture); + + // Done. + return texture; +} |