summaryrefslogtreecommitdiff
path: root/hw5/src/texture_base.cpp
blob: 39a2a63a4896d9abf0bc4cdd771ee9601d78445a (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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/******************************************************************/
/* 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;
}