-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexture.cpp
58 lines (54 loc) · 1.26 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
#include "texture.hpp"
#include "bitmap_image.hpp"
void
Texture::load(const char * filename)
{
bimg=new bitmap_image(filename);
height = bimg->height();
width = bimg->width();
}
void
Texture::operator()(int x, int y, unsigned char * color)
{
x = clamp(x,0,width-1);
y = clamp(y,0,height-1);
bimg->get_pixel(x,y,color[0],color[1],color[2]);
}
bool Texture::valid()
{
return bimg!=0;
}
///@param x assumed to be between 0 and 1
Vector3f
Texture::operator()(float x, float y)
{
Vector3f color;
int ix,iy;
x=x*width;
y=(1-y)*height;
ix = (int)x;
iy = (int)y;
unsigned char pixels[4][3];
float alpha = x-ix;
float beta = y-iy;
operator()(ix,iy,pixels[0]);
operator()(ix+1,iy,pixels[1]);
operator()(ix,iy+1,pixels[2]);
operator()(ix+1,iy+1,pixels[3]);
for(int ii=0; ii<3; ii++) {
color[ii] = (1-alpha)*(1-beta)*pixels[0][ii]
+ alpha *(1-beta)*pixels[1][ii]
+ (1-alpha)* beta *pixels[2][ii]
+ alpha * beta *pixels[3][ii];
}
return color/255;
}
Texture::~Texture()
{
if(bimg!=0) {
delete bimg;
}
}
Texture::Texture() : bimg(0),width(0),height(0)
{
}