summaryrefslogblamecommitdiff
path: root/hw6/src/sceneIO_texture.cpp
blob: f53761365d86b55b10115fb52ad66e6c8043a32d (plain) (tree)






















































                                                                                                                                    
/******************************************************************/
/* 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;
}