summaryrefslogblamecommitdiff
path: root/hw6/src/texture_base.cpp
blob: 2a90146a83af625b7813eddbf1f4ff5e71171eb0 (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 "texture_base.h"


//////////////////
// Constructors //
//////////////////
texture_base::texture_base(bool repeat)
  : image()
{
  _repeat = repeat;
}

texture_base::texture_base(const texture_base& src)
  : image(src)
{
  _repeat = src._repeat;
}


///////////////
// Operators //
///////////////
texture_base& texture_base::operator=(const texture_base& src)
{
  _assign(src);
  return *this;
}


/////////////////////
// Private Methods //
/////////////////////
void texture_base::_assign(const texture_base& src)
{
  image::_assign(src);
  _repeat = src._repeat;
}


static void repeatCoordinate(signed int& p, signed int maxCoord)
{
  if(p < 0) p = 0;
  else if(p >= maxCoord) p = maxCoord-1;
}

static void wrapCoordinate(signed int& p, signed int maxCoord)
{
  if(p < 0) p = (maxCoord  + (p % maxCoord)) % maxCoord;
  else if(p >= maxCoord) p %= maxCoord;
}

color& texture_base::_at(signed int x, signed int y)
{
  // repeat
  if(_repeat) 
  {
    repeatCoordinate(x, width());
    repeatCoordinate(y, height());
  }
  // wrap
  else {
    wrapCoordinate(x, width());
    wrapCoordinate(y, height());
  }

  // get value
  return image::operator()(x,y);
}


const color& texture_base::_at(signed int x, signed int y) const
{
  // repeat
  if(_repeat) 
  {
    repeatCoordinate(x, width());
    repeatCoordinate(y, height());
  }
  // wrap
  else {
    wrapCoordinate(x, width());
    wrapCoordinate(y, height());
  }

  // get value
  return image::operator()(x,y);
}