diff options
author | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
---|---|---|
committer | 53hornet <53hornet@gmail.com> | 2019-02-02 23:33:15 -0500 |
commit | db072ad4dc181eca5a1458656b130beb43f475bf (patch) | |
tree | a3c03c7f5497cb70503e2486662fa85cfb53415a /hw6/src/texture_base.cpp | |
download | csci427-db072ad4dc181eca5a1458656b130beb43f475bf.tar.xz csci427-db072ad4dc181eca5a1458656b130beb43f475bf.zip |
Diffstat (limited to 'hw6/src/texture_base.cpp')
-rw-r--r-- | hw6/src/texture_base.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/hw6/src/texture_base.cpp b/hw6/src/texture_base.cpp new file mode 100644 index 0000000..2a90146 --- /dev/null +++ b/hw6/src/texture_base.cpp @@ -0,0 +1,95 @@ +/******************************************************************/ +/* 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); +} |