perlin noise

This commit is contained in:
onezplpl 2024-07-13 11:18:12 -04:00
parent 48ecd8d9e8
commit 186ea5f204
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
7 changed files with 279 additions and 61 deletions

31
const.h
View file

@ -1,15 +1,25 @@
#ifndef V_CONST_H #ifndef V_CONST_H
#define V_CONST_H #define V_CONST_H
#include "linmath.h"
typedef struct Vertex { typedef struct Vertex {
float pos[3]; vec3 pos;
} Vertex; } Vertex;
#define CHUNK_LENGTH 64 //32 typedef struct VertexI {
#define CHUNK_RADIUS_H 8 int pos[3];
#define CHUNK_RADIUS_V 4 } VertexI;
typedef struct Vertex2 {
vec2 pos;
} Vertex2;
#define CHUNK_LENGTH 16
#define CHUNK_RADIUS_H 4//8
#define CHUNK_RADIUS_V 4 //4
#define CTRI_ALL BLOCK_ALL * 18 * sizeof(Vertex)
#define TEXT_GAP_H 1 / 3.0 #define TEXT_GAP_H 1 / 3.0
#define TEXT_GAP_V 1 / 1.0 #define TEXT_GAP_V 1 / 1.0
@ -18,5 +28,16 @@ typedef struct Vertex {
#define CHUNK_ALL CHUNK_DIAMETER_H *CHUNK_DIAMETER_H *CHUNK_DIAMETER_V #define CHUNK_ALL CHUNK_DIAMETER_H *CHUNK_DIAMETER_H *CHUNK_DIAMETER_V
#define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH #define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH
#define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL #define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL
#define CBUF_ALL 25
#define CTRI_ALL CBLOCK_ALL * 18 * sizeof(Vertex)
typedef struct Chunk {
unsigned char exists;
unsigned int x;
unsigned int y;
unsigned int z;
unsigned int blocks[BLOCK_ALL];
} Chunk;
#endif #endif

126
cubes.c
View file

@ -1,28 +1,35 @@
#include "const.h" #include "const.h"
//#include <stdio.h> #include "gen.h"
#include <stdlib.h> #include <pthread.h>
#include <stdio.h>
// placeholder function struct args {
int cubeExists(int x, int y, int z) { Vertex *cube;
srand(x ^ y ^ z); VertexI pos;
return ((rand() % 3 == 0) && y == 1); int is_text;
} int a;
int i;
};
void gen_face(Vertex *cube, int i, int x, int y, int z, int j, int is_text) { void gen_face(Vertex *cube, int i, int x, int y, int z, int x2, int y2, int z2,
Vertex a = {{x, y, z}}; int j, int is_text, Chunk dat) {
VertexI a = {{x, y, z}};
Vertex a2 = {{x, y, z}};
if (is_text) { if (is_text) {
a.pos[i]--; a.pos[i]--;
int exists = int exists = 0;
cubeExists(x, y, z) != cubeExists(a.pos[0], a.pos[1], a.pos[2]); exists = (cube_exists(x, y, z, dat) !=
a.pos[0] = exists ? TEXT_GAP_H * i : -1.0; cube_exists(a.pos[0], a.pos[1], a.pos[2], dat));
a.pos[1] = exists ? TEXT_GAP_V * i : -1.0;
a.pos[2] = 0; a2.pos[0] = exists ? TEXT_GAP_H * i : -1.0;
a2.pos[1] = exists ? TEXT_GAP_V * i : -1.0;
a2.pos[2] = 0;
} }
Vertex b = a; Vertex b = a2;
Vertex c = a; Vertex c = a2;
Vertex d = a; Vertex d = a2;
if (is_text) { if (is_text) {
b.pos[0] += TEXT_GAP_H; b.pos[0] += TEXT_GAP_H;
@ -36,7 +43,7 @@ void gen_face(Vertex *cube, int i, int x, int y, int z, int j, int is_text) {
d.pos[(i == 2) ? 1 : 2]++; d.pos[(i == 2) ? 1 : 2]++;
} }
cube[j] = a; cube[j] = a2;
cube[j + 1] = b; cube[j + 1] = b;
cube[j + 2] = c; cube[j + 2] = c;
cube[j + 3] = c; cube[j + 3] = c;
@ -44,24 +51,79 @@ void gen_face(Vertex *cube, int i, int x, int y, int z, int j, int is_text) {
cube[j + 5] = d; cube[j + 5] = d;
} }
void gen_cubes(Vertex *cube, int x, int y, int z, int is_text) { void *gen_chunk(void *args) {
int i = 0; struct args args2 = ((struct args *)args)[0];
for (int y2 = y; y2 < y + CHUNK_LENGTH; y2++) {
int y3 = y2;
for (int x2 = x; x2 < x + CHUNK_LENGTH; x2++) {
int x3 = x2;
for (int z2 = z; z2 < z + CHUNK_LENGTH; z2++) {
int z3 = z2;
if (is_text) { int i = args2.i;
x3 = y3 = z3 = 0; int x2 = args2.pos.pos[0];
} int y2 = args2.pos.pos[1];
int z2 = args2.pos.pos[2];
Vertex *cube = args2.cube;
int is_text = args2.is_text;
Chunk chunk =
fetch_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH, z2 / CHUNK_LENGTH);
gen_face(cube, 0, x2, y2, z2, i, is_text); printf("progress on this thread (%i / %i)\n", args2.a, CHUNK_ALL);
gen_face(cube, 1, x2, y2, z2, i + 6, is_text);
gen_face(cube, 2, x2, y2, z2, i + 12, is_text); purge_chunks();
for (int y4 = 0; y4 < CHUNK_LENGTH; y4++) {
for (int x4 = 0; x4 < CHUNK_LENGTH; x4++) {
for (int z4 = 0; z4 < CHUNK_LENGTH; z4++) {
int y3 = y2 + y4;
int x3 = x2 + x4;
int z3 = z2 + z4;
gen_face(cube, 0, x3, y3, z3, x4, y4, z4, i, is_text, chunk);
gen_face(cube, 1, x3, y3, z3, x4, y4, z4, i + 6, is_text, chunk);
gen_face(cube, 2, x3, y3, z3, x4, y4, z4, i + 12, is_text, chunk);
i += 18; i += 18;
} }
} }
} }
return NULL;
}
void gen_cubes(Vertex *cube, int x, int y, int z, int is_text) {
int i = 0;
int a = 0;
x *= CHUNK_LENGTH;
y *= CHUNK_LENGTH;
z *= CHUNK_LENGTH;
pthread_t thread_id[24];
for (int y2 = y - CHUNK_RADIUS_H * CHUNK_LENGTH;
y2 <= y + CHUNK_RADIUS_H * CHUNK_LENGTH; y2 += CHUNK_LENGTH) {
for (int x2 = x - CHUNK_RADIUS_H * CHUNK_LENGTH;
x2 <= x + CHUNK_RADIUS_H * CHUNK_LENGTH; x2 += CHUNK_LENGTH) {
for (int z2 = z - CHUNK_RADIUS_V * CHUNK_LENGTH;
z2 <= z + CHUNK_RADIUS_V * CHUNK_LENGTH; z2 += CHUNK_LENGTH) {
VertexI pos = {{x2, y2, z2}};
struct args args = {cube, pos, is_text, a, i};
gen_chunk((void *)&args);
i += CHUNK_LENGTH * CHUNK_LENGTH * CHUNK_LENGTH * 18;
/* buggy
pthread_create(&(thread_id[i]), NULL, gen_chunk, (void *)&args);
if (i == 23) {
for (int k = 0; k < 24; k++) {
pthread_join((thread_id[k]), NULL);
}
i = 0;
} else {
i++;
}
*/
a++;
}
}
}
} }

View file

@ -1,3 +1,4 @@
#include "const.h" #include "const.h"
void gen_cubes(Vertex *cube, int x, int y, int z, int is_text); void gen_cubes(Vertex *cube, int x, int y, int z, int is_text);
void gen_face(Vertex *cube, int i, int x, int y, int z, int j, int is_text);

131
gen.c Normal file
View 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;
}

5
gen.h Normal file
View file

@ -0,0 +1,5 @@
#include "const.h"
int cube_exists(int x, int y, int z, Chunk dat);
Chunk fetch_chunk(int x, int y, int z);
void purge_chunks();

View file

@ -1,4 +1,5 @@
// https://github.com/glfw/glfw/blob/master/deps/linmath.h // adapted from
// https://github.com/datenwolf/linmath.h
#ifndef LINMATH_H #ifndef LINMATH_H
#define LINMATH_H #define LINMATH_H

39
main.c
View file

@ -1,6 +1,3 @@
// adapted from
// https://github.com/glfw/glfw/blob/master/examples/triangle-opengl.c
#define GLAD_GL_IMPLEMENTATION #define GLAD_GL_IMPLEMENTATION
#include "gl.h" #include "gl.h"
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
@ -17,26 +14,26 @@
static const char *vertex_shader_text = static const char *vertex_shader_text =
"#version 330\n" "#version 330\n"
"layout (location = 0) in vec3 vPos;" "layout (location = 0) in vec3 v_pos;"
"layout (location = 1) in vec3 vOff;" "layout (location = 1) in vec3 v_off;"
"uniform mat4 MVP;\n" "uniform mat4 mvp;\n"
"out vec2 vTex;\n" "out vec2 v_tex;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" gl_Position = MVP * vec4(vPos, 1.0);\n" " gl_Position = mvp * vec4(v_pos, 1.0);\n"
" vTex=vec2(vOff);\n" " v_tex=vec2(v_off);\n"
"}\n"; "}\n";
static const char *fragment_shader_text = static const char *fragment_shader_text =
"#version 330\n" "#version 330\n"
"out vec4 color;\n" "out vec4 color;\n"
"in vec2 vTex;\n" "in vec2 v_tex;\n"
"uniform sampler2D texSampler;\n" "uniform sampler2D sampler_tex;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
//" color=vec4(vTex.rg,0.5,0.5);\n" //" color=vec4(v_tex.rg,0.5,0.5);\n"
" if (vTex.x < 0.0 || vTex.y < 0.0) { discard; }\n" " if (v_tex.x < 0.0 || v_tex.y < 0.0) { discard; }\n"
" color=vec4(texture(texSampler, vTex).rgb,0.5);\n" " color=vec4(texture(sampler_tex, v_tex).rgb,0.5);\n"
"}\n"; "}\n";
int isCursor = 0; int isCursor = 0;
@ -87,9 +84,8 @@ int main(void) {
Vertex *cube = malloc(CTRI_ALL); Vertex *cube = malloc(CTRI_ALL);
Vertex *text = malloc(CTRI_ALL); Vertex *text = malloc(CTRI_ALL);
gen_cubes(cube, 0, 0, 0, 0); gen_cubes(cube, 1024,0,1024, 0);
gen_cubes(text, 0, 0, 0, 1); gen_cubes(text, 1024,0,1024, 1);
GLuint vertex_buffer[2]; GLuint vertex_buffer[2];
GLuint vertex_array; GLuint vertex_array;
@ -121,8 +117,8 @@ int main(void) {
glAttachShader(program, fragment_shader); glAttachShader(program, fragment_shader);
glLinkProgram(program); glLinkProgram(program);
GLint tex_location = glGetUniformLocation(program, "texSampler"); GLint tex_location = glGetUniformLocation(program, "sampler_tex");
GLint mvp_location = glGetUniformLocation(program, "MVP"); GLint mvp_location = glGetUniformLocation(program, "mvp");
unsigned char *pixels; unsigned char *pixels;
@ -136,7 +132,7 @@ int main(void) {
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
vec3 pos = {0, 0, 0}; vec3 pos = {1024*CHUNK_LENGTH, 0, 1024*CHUNK_LENGTH};
int x = 0, y = 25, z = 0; int x = 0, y = 25, z = 0;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -195,13 +191,14 @@ int main(void) {
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glBindVertexArray(vertex_array); glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, BLOCK_ALL * 18); glDrawArrays(GL_TRIANGLES, 0, CBLOCK_ALL * 18);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
free(cube); free(cube);
free(text);
glfwDestroyWindow(window); glfwDestroyWindow(window);