summaryrefslogtreecommitdiff
path: root/hw6/src/sceneIO_texture.cpp
blob: f53761365d86b55b10115fb52ad66e6c8043a32d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
}