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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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);
}
|