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
|
|
|
|
2023-09-18 12:35:40 -05:00
|
|
|
#include "resources.h"
|
|
|
|
|
2024-03-03 16:58:59 -06:00
|
|
|
#include "stb_image_resize2.h"
|
2023-09-12 23:32:14 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <stdio.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-08-31 13:00:33 -05:00
|
|
|
#include "qoi.h"
|
|
|
|
|
2023-11-20 15:57:23 -06:00
|
|
|
#ifndef NSVG
|
|
|
|
#include "nanosvgrast.h"
|
|
|
|
#endif
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
struct rect ST_UNIT = {0.f, 0.f, 1.f, 1.f};
|
2023-01-18 14:43:07 -06:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-08 15:30:12 -06:00
|
|
|
/* If an empty string or null is put for path, loads default texture */
|
2024-03-13 03:51:44 -05:00
|
|
|
struct texture *texture_from_file(const char *path) {
|
2024-03-21 21:07:24 -05:00
|
|
|
if (!path) return NULL;
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-12-21 17:21:01 -06:00
|
|
|
size_t rawlen;
|
2023-10-02 07:58:17 -05:00
|
|
|
unsigned char *raw = slurp_file(path, &rawlen);
|
|
|
|
|
2024-03-21 21:07:24 -05:00
|
|
|
if (!raw) return NULL;
|
2023-10-02 07:58:17 -05:00
|
|
|
|
|
|
|
unsigned char *data;
|
2023-08-31 13:00:33 -05:00
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
struct texture *tex = calloc(1, sizeof(*tex));
|
2024-01-14 10:24:31 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int n;
|
2023-08-31 13:00:33 -05:00
|
|
|
|
2023-09-03 12:50:02 -05:00
|
|
|
char *ext = strrchr(path, '.');
|
2023-09-24 11:26:44 -05:00
|
|
|
|
|
|
|
if (!strcmp(ext, ".qoi")) {
|
2023-08-31 13:00:33 -05:00
|
|
|
qoi_desc qoi;
|
2023-09-18 10:45:51 -05:00
|
|
|
data = qoi_decode(raw, rawlen, &qoi, 4);
|
2023-08-31 13:00:33 -05:00
|
|
|
tex->width = qoi.width;
|
|
|
|
tex->height = qoi.height;
|
|
|
|
n = qoi.channels;
|
2023-09-24 11:26:44 -05:00
|
|
|
} 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);
|
2024-03-21 21:07:24 -05:00
|
|
|
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;
|
2023-11-20 15:57:23 -06:00
|
|
|
} 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.");
|
2023-11-20 15:57:23 -06:00
|
|
|
return;
|
|
|
|
#endif
|
2023-08-31 13:00:33 -05:00
|
|
|
} else {
|
2023-09-18 10:45:51 -05:00
|
|
|
data = stbi_load_from_memory(raw, rawlen, &tex->width, &tex->height, &n, 4);
|
2023-08-31 13:00:33 -05:00
|
|
|
}
|
2023-09-18 10:45:51 -05:00
|
|
|
free(raw);
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2024-03-21 21:07:24 -05:00
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
2023-05-24 20:45:50 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
tex->data = data;
|
2024-03-26 07:53:36 -05:00
|
|
|
|
|
|
|
unsigned int nw = next_pow2(tex->width);
|
|
|
|
unsigned int nh = next_pow2(tex->height);
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-05-12 22:21:11 -05:00
|
|
|
sg_image_data sg_img_data;
|
2024-03-26 07:53:36 -05:00
|
|
|
sg_img_data.subimage[0][0] = (sg_range){.ptr = data, .size=tex->width*tex->height*4};
|
2024-04-28 13:33:37 -05:00
|
|
|
|
2023-05-12 22:21:11 -05:00
|
|
|
int mips = mip_levels(tex->width, tex->height)+1;
|
|
|
|
|
|
|
|
int mipw, miph;
|
|
|
|
mipw = tex->width;
|
|
|
|
miph = tex->height;
|
|
|
|
|
2024-05-12 18:36:14 -05:00
|
|
|
sg_img_data.subimage[0][0] = (sg_range){ .ptr = data, .size = mipw*miph*4 };
|
2023-05-12 22:21:11 -05:00
|
|
|
|
|
|
|
unsigned char *mipdata[mips];
|
|
|
|
mipdata[0] = data;
|
|
|
|
|
|
|
|
for (int i = 1; i < mips; i++) {
|
2023-05-24 20:45:50 -05:00
|
|
|
int w, h, mipw, miph;
|
2024-03-26 07:53:36 -05:00
|
|
|
mip_wh(tex->width, tex->height, &mipw, &miph, i-1); // mipw miph are previous iteration
|
2023-05-24 20:45:50 -05:00
|
|
|
mip_wh(tex->width, tex->height, &w, &h, i);
|
2023-05-12 22:21:11 -05:00
|
|
|
mipdata[i] = malloc(w * h * 4);
|
2024-03-03 16:58:59 -06:00
|
|
|
stbir_resize_uint8_linear(mipdata[i-1], mipw, miph, 0, mipdata[i], w, h, 0, 4);
|
2023-05-12 22:21:11 -05:00
|
|
|
sg_img_data.subimage[0][i] = (sg_range){ .ptr = mipdata[i], .size = w*h*4 };
|
|
|
|
|
|
|
|
mipw = w;
|
|
|
|
miph = h;
|
|
|
|
}
|
2024-04-28 13:33:37 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
tex->id = sg_make_image(&(sg_image_desc){
|
2024-03-21 21:07:24 -05:00
|
|
|
.type = SG_IMAGETYPE_2D,
|
|
|
|
.width = tex->width,
|
|
|
|
.height = tex->height,
|
|
|
|
.usage = SG_USAGE_IMMUTABLE,
|
2024-04-28 13:33:37 -05:00
|
|
|
.num_mipmaps = mips,
|
2024-03-21 21:07:24 -05:00
|
|
|
.data = sg_img_data
|
|
|
|
});
|
2024-04-30 10:32:27 -05:00
|
|
|
|
2024-04-28 13:33:37 -05:00
|
|
|
for (int i = 1; i < mips; i++)
|
|
|
|
free(mipdata[i]);
|
2024-03-13 03:51:44 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return tex;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
void texture_free(texture *tex)
|
|
|
|
{
|
2024-03-21 21:07:24 -05:00
|
|
|
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);
|
2024-03-21 21:07:24 -05:00
|
|
|
if (tex->delays) arrfree(tex->delays);
|
|
|
|
sg_destroy_image(tex->id);
|
|
|
|
free(tex);
|
2022-08-24 12:24:21 -05:00
|
|
|
}
|
|
|
|
|
2024-06-18 16:14:23 -05:00
|
|
|
struct texture *texture_empty(int w, int h, int n)
|
|
|
|
{
|
|
|
|
texture *tex = calloc(1,sizeof(*tex));
|
|
|
|
tex->data = calloc(w*h*n, sizeof(unsigned char));
|
|
|
|
tex->width = w;
|
|
|
|
tex->height = h;
|
|
|
|
sg_image_data sgdata;
|
|
|
|
sgdata.subimage[0][0] = (sg_range){.ptr = tex->data, .size = w*h*4};
|
|
|
|
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 = sgdata,
|
|
|
|
});
|
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
struct texture *texture_fromdata(void *raw, long size)
|
2023-11-03 22:01:30 -05:00
|
|
|
{
|
2024-03-13 03:51:44 -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);
|
|
|
|
|
2024-03-21 21:07:24 -05:00
|
|
|
if (data == NULL)
|
|
|
|
NULL;
|
2023-11-03 22:01:30 -05:00
|
|
|
|
|
|
|
unsigned int nw = next_pow2(tex->width);
|
|
|
|
unsigned int nh = next_pow2(tex->height);
|
|
|
|
|
|
|
|
tex->data = data;
|
|
|
|
|
|
|
|
sg_image_data sg_img_data;
|
|
|
|
|
|
|
|
int mips = mip_levels(tex->width, tex->height)+1;
|
|
|
|
|
|
|
|
YughInfo("Has %d mip levels, from wxh %dx%d, pow2 is %ux%u.", mips, tex->width, tex->height,nw,nh);
|
|
|
|
|
|
|
|
int mipw, miph;
|
|
|
|
mipw = tex->width;
|
|
|
|
miph = tex->height;
|
|
|
|
|
|
|
|
sg_img_data.subimage[0][0] = (sg_range){ .ptr = data, .size = mipw*miph*4 };
|
|
|
|
|
|
|
|
unsigned char *mipdata[mips];
|
|
|
|
mipdata[0] = 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);
|
2024-03-03 16:58:59 -06:00
|
|
|
stbir_resize_uint8_linear(mipdata[i-1], mipw, miph, 0, mipdata[i], w, h, 0, 4);
|
2023-11-03 22:01:30 -05:00
|
|
|
sg_img_data.subimage[0][i] = (sg_range){ .ptr = mipdata[i], .size = w*h*4 };
|
|
|
|
|
|
|
|
mipw = w;
|
|
|
|
miph = h;
|
|
|
|
}
|
|
|
|
|
|
|
|
tex->id = sg_make_image(&(sg_image_desc){
|
|
|
|
.type = SG_IMAGETYPE_2D,
|
|
|
|
.width = tex->width,
|
|
|
|
.height = tex->height,
|
|
|
|
.usage = SG_USAGE_IMMUTABLE,
|
|
|
|
.num_mipmaps = mips,
|
|
|
|
.data = sg_img_data
|
|
|
|
});
|
|
|
|
|
|
|
|
for (int i = 1; i < mips; i++)
|
|
|
|
free(mipdata[i]);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-08-30 14:17:37 -05:00
|
|
|
// all coordinates start at bottom left
|
|
|
|
// src and dest, width, height are pixel buffers and their widths and heights
|
|
|
|
// sx the x coordinate of the destination to copy to
|
|
|
|
// sy the y coordinate of the destination to copy to
|
|
|
|
// sw the width of the destination to take in pixels
|
|
|
|
// sh the height of the destination to take in pixels
|
2024-06-18 16:14:23 -05:00
|
|
|
void blit_image(uint8_t* src, uint8_t* dest, int src_width, int src_height, int dest_width, int dest_height, int sx, int sy, int sw, int sh) {
|
2024-08-31 00:38:26 -05:00
|
|
|
// if (sx + sw > dest_width) return;
|
|
|
|
// if (sy + sh > dest_height) return;
|
|
|
|
|
|
|
|
for (int x = 0; x < sw; x++) {
|
|
|
|
for (int y = 0; y < sh; y++) {
|
|
|
|
int src_index = ((y * src_width) + x ) * 4;
|
|
|
|
int dest_index = ((y + sy) * dest_width) + (x + sx);
|
|
|
|
dest_index *= 4;
|
2024-06-18 16:14:23 -05:00
|
|
|
|
|
|
|
// Calculate the alpha value for the source pixel
|
|
|
|
uint8_t src_alpha = src[src_index + 3];
|
|
|
|
|
|
|
|
// Calculate the alpha value for the destination pixel
|
|
|
|
uint8_t dest_alpha = dest[dest_index + 3];
|
|
|
|
|
|
|
|
// Calculate the resulting alpha value
|
|
|
|
uint8_t result_alpha = src_alpha + (255 - src_alpha) * dest_alpha / 255;
|
|
|
|
|
|
|
|
// Calculate the resulting RGB values
|
|
|
|
uint8_t result_red = (src[src_index + 0] * src_alpha + dest[dest_index + 0] * (255 - src_alpha) * dest_alpha / 255) / result_alpha;
|
|
|
|
uint8_t result_green = (src[src_index + 1] * src_alpha + dest[dest_index + 1] * (255 - src_alpha) * dest_alpha / 255) / result_alpha;
|
|
|
|
uint8_t result_blue = (src[src_index + 2] * src_alpha + dest[dest_index + 2] * (255 - src_alpha) * dest_alpha / 255) / result_alpha;
|
|
|
|
|
|
|
|
// Set the resulting pixel values
|
|
|
|
dest[dest_index + 0] = result_red;
|
|
|
|
dest[dest_index + 1] = result_green;
|
|
|
|
dest[dest_index + 2] = result_blue;
|
|
|
|
dest[dest_index + 3] = result_alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to draw source image pixels on top of a destination image
|
2024-08-31 00:38:26 -05:00
|
|
|
// x,y are the pixel coordinates in the destination image, w,h are the amount of pixels to take from the src image.
|
2024-06-18 16:14:23 -05:00
|
|
|
void texture_blit(texture *dest, texture *src, int x, int y, int w, int h) {
|
2024-08-31 00:38:26 -05:00
|
|
|
blit_image(src->data, dest->data, src->width, src->height, dest->width, dest->height, x, y, w, h);
|
2024-06-18 16:14:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void texture_flip(texture *tex, int y)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-02 07:55:22 -06:00
|
|
|
static int p[512] = {151,160,137,91,90,15,
|
|
|
|
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
|
|
|
|
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
|
|
|
|
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
|
|
|
|
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
|
|
|
|
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
|
|
|
|
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
|
|
|
|
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
|
|
|
|
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
|
|
|
|
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
|
|
|
|
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
|
|
|
|
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
|
|
|
|
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
|
|
|
|
151,160,137,91,90,15,
|
|
|
|
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
|
|
|
|
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
|
|
|
|
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
|
|
|
|
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
|
|
|
|
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
|
|
|
|
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
|
|
|
|
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
|
|
|
|
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
|
|
|
|
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
|
|
|
|
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
|
|
|
|
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
|
|
|
|
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
|
|
|
|
};
|
|
|
|
|
|
|
|
double perlin(double x, double y, double z)
|
|
|
|
{
|
|
|
|
int X = (int)floor(x)&255;
|
|
|
|
int Y = (int)floor(y)&255;
|
|
|
|
int Z = (int)floor(z)&255;
|
|
|
|
x -= floor(x);
|
|
|
|
y -= floor(y);
|
|
|
|
z -= floor(z);
|
|
|
|
double u = fade(x),
|
|
|
|
v = fade(y),
|
|
|
|
w = fade(z);
|
|
|
|
int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z,
|
|
|
|
B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z;
|
|
|
|
|
|
|
|
return lerp(w, lerp(v, lerp(u, grad(p[AA ], x , y , z ),
|
|
|
|
grad(p[BA ], x-1, y , z )),
|
|
|
|
lerp(u, grad(p[AB ], x , y-1, z ),
|
|
|
|
grad(p[BB ], x-1, y-1, z ))),
|
|
|
|
lerp(v, lerp(u, grad(p[AA+1], x , y , z-1 ),
|
|
|
|
grad(p[BA+1], x-1, y , z-1 )),
|
|
|
|
lerp(u, grad(p[AB+1], x , y-1, z-1 ),
|
|
|
|
grad(p[BB+1], x-1, y-1, z-1 ))));
|
|
|
|
}
|