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
|
#include "texture.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../extern/stb_image.h"
#include <stdio.h>
static void texture_abgr2argb(Texture* tex) {
ssr_assert(tex);
int width = tex->width, height = tex->height;
Color* pixels = tex->pixels;
for (int i = 0; i < width * height; ++i) {
uint r = (pixels[i] & 0xff) << 16;
uint g = ((pixels[i] >> 8) & 0xff) << 8;
uint b = ((pixels[i] >> 16) & 0xff);
uint a = ((pixels[i] >> 24) & 0xff) << 24;
pixels[i] = r | g | b | a;
}
}
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");
ssr_assert(file);
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = ssrM_newvector(char, size);
ssr_assert(buffer);
int l = fread(buffer, 1, size, file);
if ( l!= size) {
free(buffer);
fclose(file);
return NULL;
}
fclose(file);
int width, height;
Color* pixels = (Color*)stbi_load_from_memory((unsigned char*)buffer, size, &width, &height, NULL, STBI_rgb_alpha);
ssrM_free(buffer);
if (!pixels) return NULL;
Texture* texture = ssrM_new(Texture);
texture->width = width; texture->height = height;
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) {
if (wrap_mode == WRAPMODE_CLAMP) { /*clamp*/
x = clamp(x, 0, tex->width - 1);
y = clamp(y, 0, tex->height - 1);
} else { /*repeat*/
x = x % tex->width;
y = y % tex->height;
}
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);
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) {
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) {
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) {
}
*/
}
|