diff options
Diffstat (limited to 'src/core/texture.c')
-rw-r--r-- | src/core/texture.c | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/src/core/texture.c b/src/core/texture.c index 5b23220..c7dc884 100644 --- a/src/core/texture.c +++ b/src/core/texture.c @@ -16,6 +16,15 @@ static void texture_abgr2argb(Texture* tex) { } } +static void texture_abgr2c32(Color* abgr, Color32* color, int count) { + for (int i = 0; i < count; ++i) { + color[i].r = (abgr[i] & 0xff) / 255.f; + color[i].g = ((abgr[i] >> 8) & 0xff) / 255.f; + color[i].b = ((abgr[i] >> 16) & 0xff) / 255.f; + color[i].a = ((abgr[i] >> 24) & 0xff) / 255.f; + } +} + Texture* texture_loadfromfile(const char* path) { ssr_assert(path); FILE* file = fopen(path, "rb"); @@ -38,13 +47,13 @@ Texture* texture_loadfromfile(const char* path) { if (!pixels) return NULL; Texture* texture = ssrM_new(Texture); texture->width = width; texture->height = height; - texture->pixels = pixels; - texture_abgr2argb(texture); + texture->pixels = ssrM_newvector(Color32, width*height); + texture_abgr2c32(pixels, texture->pixels, width*height); + ssrM_free(pixels); return texture; } static Color32 sampling(Texture* tex, WrapMode wrap_mode, int x, int y) { - Color32 color; if (wrap_mode == WRAPMODE_CLAMP) { /*clamp*/ x = clamp(x, 0, tex->width - 1); y = clamp(y, 0, tex->height - 1); @@ -52,30 +61,36 @@ static Color32 sampling(Texture* tex, WrapMode wrap_mode, int x, int y) { x = x % tex->width; y = y % tex->height; } - color_tocolor32(tex->pixels[x + y * tex->width], &color); - return color; + return tex->pixels[x + y * tex->width]; } Color32 texture_sampling(Texture* tex, FilterMode filter_mode, WrapMode wrap_mode, float x01, float y01) { ssr_assert(tex); - int x = x01 * tex->width, y = (1 - y01) * tex->height; /*map to texture coordinate*/ + float x = x01 * tex->width, y = (1 - y01) * tex->height; /*map to texture coordinate*/ + int x_min = floor(x), y_min = floor(y); + int x_max = ceil(x), y_max = ceil(y); if (filter_mode == FILTERMODE_POINT) { - return sampling(tex, wrap_mode, x, y); + int px = (x_max - x > x - x_min) ? x_min : x_max; + int py = (y_max - y > y - y_min) ? y_min : y_max; + + return sampling(tex, wrap_mode, px, py); } else if (filter_mode == FILTERMODE_BILINEAR) { - int x_min = floor(x), y_min = floor(y); - int x_max = ceil(x), y_max = ceil(y); static Color32 tl, tr, bl, br, t, b, out; + tl = sampling(tex, wrap_mode, x_min, y_min); tr = sampling(tex, wrap_mode, x_max, y_min); bl = sampling(tex, wrap_mode, x_min, y_max); br = sampling(tex, wrap_mode, x_max, y_max); + vec4_lerp(&tl, &tr, x - x_min, &t); vec4_lerp(&bl, &br, x - x_min, &b); vec4_lerp(&t, &b, y - y_min, &out); + return out; } + /* else if (filter_mode == FILTERMODE_TRILINEAR) { - /*TODO*/ } + */ } |