X-Git-Url: https://eleni.mutantstargoat.com/git/?a=blobdiff_plain;f=src%2Fimage.cc;h=818b686744a658d17cf6da35d7c63ee2b3c3a999;hb=1e8963fc3f8191e328bbecd04cfbcba31d7d0bdf;hp=a99f75ade69ad7fc1c18e15e5560bffe74f36cd7;hpb=fd601d4218b63fdf92c5e4dfa32eac8adbda82fa;p=demo diff --git a/src/image.cc b/src/image.cc index a99f75a..818b686 100644 --- a/src/image.cc +++ b/src/image.cc @@ -121,21 +121,54 @@ bool Image::load(const char *fname) return true; } -Vec4 Image::lookup_nearest(float u, float v) const +Vec4 Image::get_pixel(int x, int y) const { - int x = (int)(u * w) % w; - int y = (int)(v * h) % h; - if(is_float) { - return ((Vec4*)pixels)[y * w + x]; + return ((Vec4 *)pixels)[y * w + x]; } Vec4 color; - unsigned char *pptr = ((unsigned char*)pixels) + (y * w + x) * 4; + unsigned char *pptr = ((unsigned char *)pixels) + (y * w + x) * 4; color.x = pptr[0] / 255.0; color.y = pptr[1] / 255.0; color.z = pptr[2] / 255.0; color.w = pptr[3] / 255.0; return color; +} + +Vec4 Image::lookup_nearest(float u, float v) const +{ + int x = (int)(u * w) % w; + int y = (int)(v * h) % h; + + return get_pixel(x, y); +} + +Vec4 Image::lookup_linear(float u, float v, float du, float dv) const +{ + /* bottom left */ + int x0 = (int)(u * w) % w; + int y0 = (int)(v * h) % h; + int x1 = (x0 + 1) % w; + int y1 = (y0 + 1) % h; + + /* uv coordinates at the img corners */ + float u0 = (float)x0 / (float)w; + float v0 = (float)y0 / (float)h; + float u1 = (float)(x0 + 1) / (float)w; + float v1 = (float)(y0 + 1) / (float)h; + + float tu = (u - u0) / (u1 - u0); + float tv = (v - v0) / (v1 - v0); + + Vec4 p00 = get_pixel(x0, y0); + Vec4 p01 = get_pixel(x0, y1); + Vec4 p11 = get_pixel(x1, y1); + Vec4 p10 = get_pixel(x1, y0); + + Vec4 ptop = lerp(p01, p11, tu); + Vec4 pbot = lerp(p00, p10, tu); + + return lerp(pbot, ptop, tv); } \ No newline at end of file