/******************************************************************/
/* 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;
}
color& texture_base::_at(signed int x, signed int y)
{
// HW5: Implement this. Retrieve the texel at coordinate
// (x,y) (in texel coordinates, not texture coordinates!)
// taking in account the boundary conditions:
// if _repeat == true, then the texel coordinates outside
// the images are mapped to the nearest edge. E.g., for a
// 10x10 image, a coordinate of (1,1) returns the
// texel at (1,1), at (12,1) -> (9,1), at
// (13,14) -> (9,9), (5,12) -> (5,9), and (-4,1)->(0,1).
// If _repeat is false then the coordinates wrap around:
// (1,1) -> (1,1), (12,1) -> (2,1), and (13,14) -> (3,4).
// Modifies: nothing
// Returns: a reference to the color the image.
signed int xRef, yRef;
if (_repeat) {
if (x < 0) {
xRef = 0;
}
else if (x >= 0 && x < image::width()) {
xRef = x;
}
else { // x >= image::width()
xRef = image::width() - 1;
}
if (y < 0) {
yRef = 0;
}
else if (y >= 0 && y < image::height()) {
yRef = y;
}
else { // y >= image::width()
yRef = image::height() - 1;
}
}
else { // !_repeat
xRef = x % image::width();
yRef = y % image::height();
}
image::reference texel = image::operator()(xRef, yRef);
return texel;
}
const color& texture_base::_at(signed int x, signed int y) const
{
// HW5: Same as above, except that it returns a const color reference.
// Normally just copying the code from above should work.
signed int xRef, yRef;
if (_repeat) {
if (x < 0) {
xRef = 0;
}
else if (x >= 0 && x < image::width()) {
xRef = x;
}
else { // x >= image::width()
xRef = image::width() - 1;
}
if (y < 0) {
yRef = 0;
}
else if (y >= 0 && y < image::height()) {
yRef = y;
}
else { // y >= image::width()
yRef = image::height() - 1;
}
}
else { // !_repeat
xRef = x % image::width();
yRef = y % image::height();
}
image::const_reference texel = image::operator()(xRef, yRef);
return texel;
}