perlin noise
This commit is contained in:
parent
48ecd8d9e8
commit
186ea5f204
7 changed files with 279 additions and 61 deletions
131
gen.c
Normal file
131
gen.c
Normal file
|
@ -0,0 +1,131 @@
|
|||
#include "const.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Chunk *chunks = NULL;
|
||||
int lastI = 0;
|
||||
|
||||
double interpolate(double a0, double a1, double w) {
|
||||
return (a1 - a0) * (3.0 - w * 2.0) * w * w + a0;
|
||||
}
|
||||
|
||||
int hash_combine(int a, int b) { return b + 0x9e3779b9 + (a << 6) + (a >> 2); }
|
||||
|
||||
Vertex2 perlin_grad(int x, int y) {
|
||||
srand(x);
|
||||
int x_hash = rand();
|
||||
srand(y);
|
||||
int y_hash = rand();
|
||||
|
||||
double random = hash_combine(x_hash, y_hash) * (M_PI / ~(~0u >> 1));
|
||||
Vertex2 out = {cos(random), sin(random)};
|
||||
return out;
|
||||
}
|
||||
|
||||
double perlin_dot(double ix, double iy, double x, double y) {
|
||||
Vertex2 grad = perlin_grad(ix, iy);
|
||||
|
||||
double dx = x - (double)ix;
|
||||
double dy = y - (double)iy;
|
||||
return (dx * grad.pos[0] + dy * grad.pos[1]);
|
||||
}
|
||||
|
||||
double perlin_calc(double x, double y, double s) {
|
||||
x /= s;
|
||||
y /= s;
|
||||
|
||||
int x0 = (int)floor(x);
|
||||
int x1 = x0 + 1;
|
||||
int y0 = (int)floor(y);
|
||||
int y1 = y0 + 1;
|
||||
|
||||
float sx = x - (float)x0;
|
||||
float sy = y - (float)y0;
|
||||
|
||||
float n0, n1, ix0, ix1, value;
|
||||
|
||||
n0 = perlin_dot(x0, y0, x, y);
|
||||
n1 = perlin_dot(x1, y0, x, y);
|
||||
ix0 = interpolate(n0, n1, sx);
|
||||
|
||||
n0 = perlin_dot(x0, y1, x, y);
|
||||
n1 = perlin_dot(x1, y1, x, y);
|
||||
ix1 = interpolate(n0, n1, sx);
|
||||
|
||||
value = interpolate(ix0, ix1, sy);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
Chunk fetch_chunk(int x, int y, int z) {
|
||||
if (chunks == NULL) {
|
||||
chunks = calloc(CBUF_ALL, sizeof(Chunk));
|
||||
}
|
||||
Chunk chunk;
|
||||
|
||||
int i, j;
|
||||
for (j = 0; j < CBUF_ALL; j++) {
|
||||
i = (j + lastI) % CBUF_ALL;
|
||||
chunk = chunks[i];
|
||||
if (chunk.x == x && chunk.y == y && chunk.z == z && chunk.exists) {
|
||||
// lastI = i;
|
||||
return chunk;
|
||||
}
|
||||
}
|
||||
|
||||
for (j = CBUF_ALL - 1; j >= 0; j--) {
|
||||
i = j;
|
||||
chunk = chunks[i];
|
||||
if (!chunk.exists)
|
||||
break;
|
||||
}
|
||||
|
||||
lastI = i;
|
||||
|
||||
if (i > CBUF_ALL - 1)
|
||||
i = CBUF_ALL - 1;
|
||||
|
||||
chunk.exists = 1;
|
||||
chunk.x = x;
|
||||
chunk.y = y;
|
||||
chunk.z = z;
|
||||
|
||||
int c = 0;
|
||||
|
||||
for (int x2 = 0; x2 < CHUNK_LENGTH; x2++) {
|
||||
int x3 = x2 + x * CHUNK_LENGTH;
|
||||
for (int z2 = 0; z2 < CHUNK_LENGTH; z2++) {
|
||||
int z3 = z2 + z * CHUNK_LENGTH;
|
||||
double hp = (perlin_calc(x3, z3, 256.0) * 128.0);
|
||||
int h = hp + 64;
|
||||
|
||||
for (int y2 = 0; y2 < CHUNK_LENGTH; y2++) {
|
||||
int y3 = y2 + y * CHUNK_LENGTH;
|
||||
chunk.blocks[c] = (y3 < h);
|
||||
c++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
chunks[i] = chunk;
|
||||
|
||||
return chunk;
|
||||
}
|
||||
|
||||
void purge_chunks() {
|
||||
free(chunks);
|
||||
chunks = calloc(CBUF_ALL, sizeof(Chunk));
|
||||
}
|
||||
|
||||
int cube_exists(int x, int y, int z, Chunk dat) {
|
||||
if (x < 0 || y < 0 || z < 0) return 0;
|
||||
|
||||
if (dat.exists == 0 || dat.x != (x / CHUNK_LENGTH) || dat.y != (y / CHUNK_LENGTH) || dat.z != (z / CHUNK_LENGTH) )
|
||||
dat =
|
||||
fetch_chunk((x / CHUNK_LENGTH), (y / CHUNK_LENGTH), (z / CHUNK_LENGTH));
|
||||
int h = dat.blocks[(y % CHUNK_LENGTH) + (z % CHUNK_LENGTH) * CHUNK_LENGTH +
|
||||
(x % CHUNK_LENGTH) * CHUNK_LENGTH * CHUNK_LENGTH];
|
||||
|
||||
return h;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue