prosperon/source/engine/texture.c

544 lines
13 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "texture.h"
#include "log.h"
2023-05-12 13:22:05 -05:00
#include "render.h"
2023-05-04 17:07:00 -05:00
#include "sokol/sokol_gfx.h"
2023-05-12 13:22:05 -05:00
#include <math.h>
#include <stb_image.h>
2024-06-18 16:14:23 -05:00
#include <stb_image_write.h>
2023-05-12 22:21:11 -05:00
#include "resources.h"
2024-03-03 16:58:59 -06:00
#include "stb_image_resize2.h"
2023-05-12 13:22:05 -05:00
#include <stdio.h>
2021-11-30 21:29:18 -06:00
#include "qoi.h"
2024-10-05 08:43:51 -05:00
#define CUTE_ASEPRITE_IMPLEMENTATION
#include "cute_aseprite.h"
#ifndef NSVG
#include "nanosvgrast.h"
#endif
struct rect ST_UNIT = {0.f, 0.f, 1.f, 1.f};
2023-01-18 14:43:07 -06:00
static inline void write_pixel(unsigned char *data, int idx, rgba color)
{
2024-10-02 09:55:32 -05:00
memcpy(data+idx, &color, sizeof(color));
}
static inline rgba get_pixel(unsigned char *data, int idx)
{
rgba color;
2024-10-02 09:55:32 -05:00
memcpy(&color, data+idx, sizeof(color));
return color;
}
static inline unsigned char c_clamp(float value) { return (unsigned char) fmaxf(0.0f, fminf(255.0f, roundf(value))); }
static inline rgba blend_colors(rgba a, rgba b)
{
float a_a = a.a / 255.0f;
float b_a = b.a / 255.0f;
float out_a = a_a + b_a * (1.0f - a_a);
rgba result;
if (out_a == 0.0f) {
result.r = result.g = result.b = result.a = 0;
return result;
}
// Use the c_clamp function to safely clamp the values within the range [0, 255]
result.r = c_clamp(((a.r * a_a) + (b.r * b_a * (1.0f - a_a))) / out_a);
result.g = c_clamp(((a.g * a_a) + (b.g * b_a * (1.0f - a_a))) / out_a);
result.b = c_clamp(((a.b * a_a) + (b.b * b_a * (1.0f - a_a))) / out_a);
result.a = c_clamp(out_a * 255.0f);
return result;
}
static inline rgba additive_blend(rgba a, rgba b) {
rgba result;
result.r = c_clamp(a.r + b.r);
result.g = c_clamp(a.g + b.g);
result.b = c_clamp(a.b + b.b);
result.a = c_clamp((a.a + b.a) * 0.5f); // Blend alpha channels evenly
return result;
}
static inline rgba subtractive_blend(rgba a, rgba b) {
rgba result;
result.r = c_clamp(a.r - b.r);
result.g = c_clamp(a.g - b.g);
result.b = c_clamp(a.b - b.b);
result.a = c_clamp((a.a + b.a) * 0.5f); // Blend alpha channels evenly
return result;
}
static inline rgba multiplicative_blend(rgba a, rgba b) {
rgba result;
result.r = c_clamp((a.r * b.r) / 255.0f);
result.g = c_clamp((a.g * b.g) / 255.0f);
result.b = c_clamp((a.b * b.b) / 255.0f);
result.a = c_clamp((a.a + b.a) * 0.5f); // Blend alpha channels evenly
return result;
}
static inline rgba dodge_blend(rgba a, rgba b) {
rgba result;
result.r = c_clamp(a.r == 255 ? 255 : (b.r * 255) / (255 - a.r));
result.g = c_clamp(a.g == 255 ? 255 : (b.g * 255) / (255 - a.g));
result.b = c_clamp(a.b == 255 ? 255 : (b.b * 255) / (255 - a.b));
result.a = c_clamp((a.a + b.a) * 0.5f); // Blend alpha channels evenly
return result;
}
static inline rgba burn_blend(rgba a, rgba b) {
rgba result;
result.r = c_clamp(a.r == 0 ? 0 : 255 - ((255 - b.r) * 255) / a.r);
result.g = c_clamp(a.g == 0 ? 0 : 255 - ((255 - b.g) * 255) / a.g);
result.b = c_clamp(a.b == 0 ? 0 : 255 - ((255 - b.b) * 255) / a.b);
result.a = c_clamp((a.a + b.a) * 0.5f); // Blend alpha channels evenly
return result;
}
2023-05-24 20:45:50 -05:00
unsigned int next_pow2(unsigned int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
2023-05-12 22:21:11 -05:00
int mip_levels(int width, int height)
{
2023-05-24 20:45:50 -05:00
int levels = 0;
while (width > 1 || height > 1)
{
width >>= 1;
height >>= 1;
levels++;
}
return levels;
}
int mip_wh(int w, int h, int *mw, int *mh, int lvl)
{
w >>= lvl;
h >>= lvl;
if (w == 0 && h == 0)
return 1;
*mw = w ? w : 1;
*mh = h ? h : 1;
return 0;
2023-05-12 22:21:11 -05:00
}
void texture_offload(texture *tex)
{
if (tex->data) {
free(tex->data);
tex->data = NULL;
}
}
2023-02-08 15:30:12 -06:00
/* If an empty string or null is put for path, loads default texture */
struct texture *texture_from_file(const char *path) {
if (!path) return NULL;
2023-05-12 13:22:05 -05:00
size_t rawlen;
unsigned char *raw = slurp_file(path, &rawlen);
if (!raw) return NULL;
unsigned char *data;
struct texture *tex = calloc(1, sizeof(*tex));
2024-10-03 17:36:29 -05:00
2024-10-17 17:23:33 -05:00
stbi_set_flip_vertically_on_load(1);
2023-05-12 13:22:05 -05:00
int n;
char *ext = strrchr(path, '.');
2024-10-05 08:43:51 -05:00
if (!strcmp(ext, ".ase")) {
ase_t *ase = cute_aseprite_load_from_memory(raw, rawlen, NULL);
// frame is ase->w, ase->h
/* sprite anim is
anim = {
frames: [
rect: { <---- gathered after rect packing
x:
y:
w:
h:
},
time: ase->duration_milliseconds / 1000 (so it's in seconds)
],
path: path,
};
*/
for (int i = 0; i < ase->frame_count; i++) {
ase_frame_t *frame = ase->frames+i;
// add to thing with frame->pixels
}
cute_aseprite_free(ase);
} else if (!strcmp(ext, ".qoi")) {
qoi_desc qoi;
data = qoi_decode(raw, rawlen, &qoi, 4);
tex->width = qoi.width;
tex->height = qoi.height;
n = qoi.channels;
} else if (!strcmp(ext, ".gif")) {
2023-12-29 19:08:53 -06:00
data = stbi_load_gif_from_memory(raw, rawlen, &tex->delays, &tex->width, &tex->height, &tex->frames, &n, 4);
int *dd = tex->delays;
tex->delays = NULL;
arrsetlen(tex->delays, tex->frames);
for (int i = 0; i < tex->frames; i++) tex->delays[i] = dd[i];
2023-12-29 19:08:53 -06:00
free(dd);
2023-09-25 08:21:02 -05:00
tex->height *= tex->frames;
} else if (!strcmp(ext, ".svg")) {
#ifndef NSVG
NSVGimage *svg = nsvgParse(raw, "px", 96);
struct NSVGrasterizer *rast = nsvgCreateRasterizer();
n=4;
tex->width=100;
tex->height=100;
float scale = tex->width/svg->width;
data = malloc(tex->width*tex->height*n);
nsvgRasterize(rast, svg, 0, 0, scale, data, tex->width, tex->height, tex->width*n);
free(svg);
free(rast);
#else
2024-02-19 20:31:26 -06:00
YughWarn("Prosperon was built without SVG capabilities.");
return;
#endif
} else {
data = stbi_load_from_memory(raw, rawlen, &tex->width, &tex->height, &n, 4);
}
free(raw);
2023-05-12 13:22:05 -05:00
if (data == NULL) {
free(tex);
return NULL;
2023-05-12 22:21:11 -05:00
}
2024-04-28 13:33:37 -05:00
tex->data = data;
2023-05-12 13:22:05 -05:00
return tex;
2021-11-30 21:29:18 -06:00
}
void texture_free(texture *tex)
{
if (!tex) return;
2024-05-12 18:36:14 -05:00
if (tex->data)
2024-08-12 15:19:34 -05:00
free(tex->data);
if (tex->delays) arrfree(tex->delays);
2024-09-15 12:22:45 -05:00
if (tex->simgui.id)
2024-10-05 08:43:51 -05:00
simgui_destroy_image(tex->simgui);
free(tex);
2022-08-24 12:24:21 -05:00
}
struct texture *texture_empty(int w, int h)
2024-06-18 16:14:23 -05:00
{
int n = 4;
2024-06-18 16:14:23 -05:00
texture *tex = calloc(1,sizeof(*tex));
tex->data = calloc(w*h*n, sizeof(unsigned char));
tex->width = w;
tex->height = h;
return tex;
}
struct texture *texture_fromdata(void *raw, long size)
2023-11-03 22:01:30 -05:00
{
struct texture *tex = calloc(1, sizeof(*tex));
2023-11-03 22:01:30 -05:00
int n;
void *data = stbi_load_from_memory(raw, size, &tex->width, &tex->height, &n, 4);
if (data == NULL) {
free(tex);
return NULL;
2023-11-03 22:01:30 -05:00
}
tex->data = data;
2023-11-03 22:01:30 -05:00
texture_load_gpu(tex);
2023-11-03 22:01:30 -05:00
return tex;
}
2024-01-02 07:55:22 -06:00
static double fade (double t) { return t*t*t*(t*(t*6-15)+10); }
double grad (int hash, double x, double y, double z)
{
int h = hash&15;
double u = h<8 ? x : y;
double v = h<4 ? y : h==12||h==14 ? x : z;
return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
/* alt */
/* switch(hash & 0xF)
{
case 0x0: return x + y;
case 0x1: return -x + y;
case 0x2: return x - y;
case 0x3: return -x - y;
case 0x4: return x + z;
case 0x5: return -x + z;
case 0x6: return x - z;
case 0x7: return -x - z;
case 0x8: return y + z;
case 0x9: return -y + z;
case 0xA: return y - z;
case 0xB: return -y - z;
case 0xC: return y + x;
case 0xD: return -y + z;
case 0xE: return y - x;
case 0xF: return -y - z;
default: return 0; // never happens
}*/
}
2024-06-18 16:14:23 -05:00
void texture_save(texture *tex, const char *file)
{
2024-09-29 06:10:42 -05:00
if (!tex->data) return;
2024-06-18 16:14:23 -05:00
char *ext = strrchr(file, '.');
if (!strcmp(ext, ".png"))
stbi_write_png(file, tex->width, tex->height, 4, tex->data, 4*tex->width);
else if (!strcmp(ext, ".bmp"))
stbi_write_bmp(file, tex->width, tex->height, 4, tex->data);
else if (!strcmp(ext, ".tga"))
stbi_write_tga(file, tex->width, tex->height, 4, tex->data);
else if (!strcmp(ext, ".jpg") || !strcmp(ext, ".jpeg"))
stbi_write_jpg(file, tex->width, tex->height, 4, tex->data, 5);
2024-10-01 18:10:52 -05:00
else if (!strcmp(ext, ".qoi"))
qoi_write(file, tex->data, &(qoi_desc) {
.width = tex->width,
.height = tex->height,
.channels = 4,
.colorspace = QOI_SRGB
});
2024-06-18 16:14:23 -05:00
}
// copy texture src to dest
// sx and sy are the destination coordinates to copy to
2024-08-30 14:17:37 -05:00
// sw the width of the destination to take in pixels
// sh the height of the destination to take in pixels
2024-10-01 17:57:05 -05:00
int texture_blit(texture *dst, texture *src, rect dstrect, rect srcrect, int tile) {
2024-10-02 09:55:32 -05:00
if (!src || !dst || !src->data || !dst->data) return 0;
float scaleX = srcrect.w / dstrect.w;
float scaleY = srcrect.h / dstrect.h;
2024-10-02 09:55:32 -05:00
if (srcrect.x < 0 || srcrect.y < 0 || srcrect.x + srcrect.w > src->width ||
dstrect.x < 0 || dstrect.y < 0 || dstrect.x + dstrect.w > dst->width ||
srcrect.y + srcrect.h > src->height || dstrect.y + dstrect.h > dst->height) {
return false; // Rectangles exceed texture bounds
2024-10-02 09:55:32 -05:00
}
for (int dstY = 0; dstY < dstrect.h; ++dstY) {
for (int dstX = 0; dstX < dstrect.w; ++dstX) {
int srcX;
int srcY;
if (tile) {
srcX = srcrect.x + (dstX % (int)srcrect.w);
srcY = srcrect.y + (dstY % (int)srcrect.h);
} else {
srcX = srcrect.x + (int)(dstX * scaleX);
srcY = srcrect.y + (int)(dstY * scaleY);
}
int srcIndex = (srcY * src->width + srcX) * 4;
int dstIndex = ((dstrect.y + dstY) * dst->width + (dstrect.x + dstX)) * 4;
rgba srccolor = get_pixel(src->data, srcIndex);
rgba dstcolor = get_pixel(dst->data, dstIndex);
2024-10-01 17:57:05 -05:00
rgba color = blend_colors(srccolor, dstcolor);
write_pixel(dst->data, dstIndex, color);
}
}
return 1;
}
2024-10-01 17:57:05 -05:00
int texture_fill_rect(texture *tex, struct rect rect, struct rgba color)
{
if (!tex || !tex->data) return 0;
2024-10-01 17:57:05 -05:00
int x_end = rect.x+rect.w;
int y_end = rect.y+rect.h;
2024-10-01 17:57:05 -05:00
if (rect.x < 0 || rect.y < 0 || x_end > tex->width || y_end > tex->height) return 0;
2024-10-01 17:57:05 -05:00
for (int j = rect.y; j < y_end; ++j)
for (int i = rect.x; i < x_end; ++i) {
int index = (j*tex->width+i)*4;
write_pixel(tex->data, index, color);
2024-10-01 17:57:05 -05:00
}
return 1;
}
void swap_pixels(unsigned char *p1, unsigned char *p2) {
for (int i = 0; i < 4; ++i) {
unsigned char tmp = p1[i];
p1[i] = p2[i];
p2[i] = tmp;
}
}
texture *texture_scale(texture *tex, int width, int height)
{
texture *new = calloc(1, sizeof(*new));
new->width = width;
new->height = height;
new->data = malloc(4*width*height);
stbir_resize_uint8_linear(tex->data, tex->width, tex->height, 0, new->data, width, height, 0, 4);
return new;
}
int texture_flip(texture *tex, int y)
{
if (!tex || !tex->data) return -1;
int width = tex->width;
int height = tex->height;
if (y) {
for (int row = 0; row < height / 2; ++row) {
for (int col = 0; col < width; ++col) {
2024-10-01 17:57:05 -05:00
unsigned char *top = tex->data+((row*width+col)*4);
unsigned char *bottom = tex->data+(((height-row-1)*width+col)*4);
swap_pixels(top,bottom);
}
}
} else {
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width / 2; ++col) {
2024-10-01 17:57:05 -05:00
unsigned char *left = tex->data+((row*width+col)*4);
unsigned char *right = tex->data+((row*width+(width-col-1))*4);
swap_pixels(left,right);
}
2024-06-18 16:14:23 -05:00
}
}
return 0;
2024-06-18 16:14:23 -05:00
}
int texture_write_pixel(texture *tex, int x, int y, rgba color)
{
if (x < 0 || x >= tex->width || y < 0 || y >= tex->height) return 0;
int i = (y * tex->width + x) * 4;
write_pixel(tex->data, i, color);
return 1;
2024-06-18 16:14:23 -05:00
}
texture *texture_dup(texture *tex)
2024-06-18 16:14:23 -05:00
{
texture *new = calloc(1, sizeof(*new));
*new = *tex;
new->data = malloc(new->width*new->height*4);
memcpy(new->data, tex->data, new->width*new->height*4*sizeof(new->data));
2024-06-18 16:14:23 -05:00
return new;
2024-06-18 16:14:23 -05:00
}
sg_image_data tex_img_data(texture *tex, int mipmaps)
2024-01-02 07:55:22 -06:00
{
if (!mipmaps) {
sg_image_data sg_img_data = {0};
sg_img_data.subimage[0][0] = (sg_range) {.ptr = tex->data, .size = tex->width*tex->height*4};
return sg_img_data;
}
sg_image_data sg_img_data = {0};
int mips = mip_levels(tex->width, tex->height)+1;
int mipw, miph;
mipw = tex->width;
miph = tex->height;
sg_img_data.subimage[0][0] = (sg_range){ .ptr = tex->data, .size = mipw*miph*4 };
unsigned char *mipdata[mips];
mipdata[0] = tex->data;
for (int i = 1; i < mips; i++) {
int w, h, mipw, miph;
mip_wh(tex->width, tex->height, &mipw, &miph, i-1); // mipw miph are previous iteration
mip_wh(tex->width, tex->height, &w, &h, i);
mipdata[i] = malloc(w * h * 4);
stbir_resize_uint8_linear(mipdata[i-1], mipw, miph, 0, mipdata[i], w, h, 0, 4);
sg_img_data.subimage[0][i] = (sg_range){ .ptr = mipdata[i], .size = w*h*4 };
tex->vram += w*h*4;
mipw = w;
miph = h;
}
}
2024-10-01 17:57:05 -05:00
int texture_fill(texture *tex, struct rgba color)
{
if (!tex || !tex->data) return 0; // Ensure valid texture and pixel data
// Loop through every pixel in the texture
for (int y = 0; y < tex->height; ++y) {
for (int x = 0; x < tex->width; ++x) {
int index = (y * tex->width + x) * 4;
write_pixel(tex->data, index, color);
}
}
return 1;
}
void texture_load_gpu(texture *tex)
{
if (!tex->data) return;
if (tex->id.id == 0) {
// Doesn't exist, so make a new one
sg_image_data img_data = tex_img_data(tex, 0);
tex->id = sg_make_image(&(sg_image_desc){
.type = SG_IMAGETYPE_2D,
.width = tex->width,
.height = tex->height,
.usage = SG_USAGE_IMMUTABLE,
.num_mipmaps = 1,
.data = img_data
});
2024-10-05 08:43:51 -05:00
} //else {
// Simple update
2024-10-05 08:43:51 -05:00
// sg_image_data img_data = tex_img_data(tex,0);
// sg_update_image(tex->id, &img_data);
// }
2024-01-02 07:55:22 -06:00
}