-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.cpp
61 lines (52 loc) · 2.15 KB
/
texture.cpp
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
#include "parse.h"
#include "texture.h"
#include "dump_png.h"
#include <math.h>
Texture::Texture(const Parse* parse,std::istream& in)
{
std::string filename;
in>>name>>filename>>use_bilinear_interpolation;
Read_png(data,width,height,filename.c_str());
}
Texture::~Texture()
{
delete [] data;
}
// uses nearest neighbor interpolation to determine color at texture coordinates
// (uv[0],uv[1]). To match the reference image, details on how this mapping is
// done will matter.
// 1. Assume that width=5 and height=4. Texture coordinates with 0<=u<0.2 and
// 0<=v<0.25 should map to pixel index i=0, j=0. Texture coordinates with
// 0.8<=u<1 and 0.75<=v<1 should map to pixel index i=4, j=3.
// 2. Texture coordinates should "wrap around." Some of the objects contain
// (u,v) coordinates that are less than 0 or greater than 1. u=0.4 and u=1.4
// and u=-0.6 should be considered equivalent. There is a wrap function in
// misc.h that you may use.
// 3. Be very careful about indexing out of bounds. For example, u=0.999999
// should result in i=width-1, not i=width. The latter is out of bounds.
// 4. Be careful with your rounding. For example, u=-0.0001 should map to
// i=width-1 in accordance to the wrapping rule. Remember that casting from
// float to integer rounds towards zero, so that (int)u would produce 0. You
// may find the cmath functions floor, ceil, and rint to be helpful, as they
// provide precise control over rounding.
// 5. Although there is a flag called use_bilinear_interpolation, none of the
// test cases exercise this feature. You do not need to implement it, though
// of course you are welcome to do so if you like. You may assume nearest
// neighbor interpolation.
vec3 Texture::Get_Color(const vec2& uv) const
{
Debug_Scope scope;
double w = uv[0];
double h = uv[1];
if (w<0) w-=(int)w-1;
if (w>=1) w-=(int)w;
if (h<0) h-=(int)h-1;
if (h>=1) h-=(int)h;
w*=width;
h*=height;
int i = floor(w);
int j = floor(h);
vec3 c = From_Pixel(data[j*width+i]);
Pixel_Print("texture (u,v): ", uv, " (i,j): ", i, ", ", j, " color: ", c);
return c;
}