cleanup
This commit is contained in:
parent
c4181e5c42
commit
678db8574f
6 changed files with 125 additions and 116 deletions
41
const.h
41
const.h
|
@ -1,41 +0,0 @@
|
|||
#ifndef V_CONST_H
|
||||
#define V_CONST_H
|
||||
#include "linmath.h"
|
||||
|
||||
typedef struct Vertex {
|
||||
vec3 pos;
|
||||
} Vertex;
|
||||
|
||||
typedef struct VertexI {
|
||||
int pos[3];
|
||||
} VertexI;
|
||||
|
||||
typedef struct Vertex2 {
|
||||
vec2 pos;
|
||||
} Vertex2;
|
||||
|
||||
#define CHUNK_LENGTH 16
|
||||
#define CHUNK_RADIUS_H 4 // 8
|
||||
#define CHUNK_RADIUS_V 4 // 4
|
||||
|
||||
#define TEXT_GAP_H 1 / 3.0
|
||||
#define TEXT_GAP_V 1 / 6.0
|
||||
|
||||
#define CHUNK_DIAMETER_H (CHUNK_RADIUS_H * 2 + 1)
|
||||
#define CHUNK_DIAMETER_V (CHUNK_RADIUS_V * 2 + 1)
|
||||
#define CHUNK_ALL CHUNK_DIAMETER_H *CHUNK_DIAMETER_H *CHUNK_DIAMETER_V
|
||||
#define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH
|
||||
#define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL
|
||||
#define CBUF_ALL 2
|
||||
|
||||
#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
|
61
cubes.c
61
cubes.c
|
@ -1,16 +1,15 @@
|
|||
// the messiest part of this whole codebase
|
||||
|
||||
#include "const.h"
|
||||
#include "cubes.h"
|
||||
#include "gen.h"
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct args {
|
||||
Vertex *cube;
|
||||
Vertex *text;
|
||||
VertexI pos;
|
||||
// int is_text;
|
||||
struct v3f *cube;
|
||||
struct v3f *text;
|
||||
struct v3i pos;
|
||||
int a;
|
||||
int i;
|
||||
int a3;
|
||||
|
@ -21,12 +20,12 @@ int ox = -1024;
|
|||
int oy = -1024;
|
||||
int oz = -1024;
|
||||
|
||||
Chunk *chunk_old = NULL;
|
||||
struct chunk *chunk_old = NULL;
|
||||
|
||||
void gen_face(Vertex *cube, int i, int x, int y, int z, int x2, int y2, int z2,
|
||||
int *j, int is_text, Chunk dat, int ci, int a3) {
|
||||
VertexI a = {{x, y, z}};
|
||||
Vertex a2 = {{x, y, z}};
|
||||
void gen_face(struct v3f *cube, int i, int x, int y, int z, int x2, int y2,
|
||||
int z2, int *j, int is_text, struct chunk dat, int ci, int a3) {
|
||||
struct v3i a = {{x, y, z}};
|
||||
struct v3f a2 = {{x, y, z}};
|
||||
|
||||
if (is_text) {
|
||||
a.pos[i]--;
|
||||
|
@ -44,9 +43,9 @@ void gen_face(Vertex *cube, int i, int x, int y, int z, int x2, int y2, int z2,
|
|||
}
|
||||
}
|
||||
|
||||
Vertex b = a2;
|
||||
Vertex c = a2;
|
||||
Vertex d = a2;
|
||||
struct v3f b = a2;
|
||||
struct v3f c = a2;
|
||||
struct v3f d = a2;
|
||||
|
||||
if (is_text) {
|
||||
b.pos[0] += TEXT_GAP_H;
|
||||
|
@ -82,10 +81,10 @@ void *gen_chunk(void *args) {
|
|||
int y2 = args2.pos.pos[1];
|
||||
int z2 = args2.pos.pos[2];
|
||||
|
||||
Vertex *cube = args2.cube;
|
||||
Vertex *text = args2.text;
|
||||
struct v3f *cube = args2.cube;
|
||||
struct v3f *text = args2.text;
|
||||
|
||||
Vertex vNul = {{-10.0, -10.0, -10.0}};
|
||||
struct v3f vNul = {{-10.0, -10.0, -10.0}};
|
||||
if (x2 < 0 || y2 < 0 || z2 < 0) {
|
||||
for (int a = i; a < args2.i + (BLOCK_ALL * 18) * a3; a += 6 * a3) {
|
||||
cube[a] = vNul;
|
||||
|
@ -102,8 +101,8 @@ void *gen_chunk(void *args) {
|
|||
CHUNK_DIAMETER_H) %
|
||||
(CHUNK_ALL);*/
|
||||
|
||||
Chunk chunk = fetch_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH,
|
||||
z2 / CHUNK_LENGTH, a, 0);
|
||||
struct chunk chunk = fetch_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH,
|
||||
z2 / CHUNK_LENGTH, a, 0);
|
||||
|
||||
if (chunk.exists != 0 && the_j > -1)
|
||||
chunk_old[the_j] = chunk;
|
||||
|
@ -139,9 +138,11 @@ void *gen_chunk(void *args) {
|
|||
return (void *)i;
|
||||
}
|
||||
|
||||
int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
||||
int y, int z, int is_purge, int *i2) {
|
||||
Vertex noth = {{-1, -1, -1}};
|
||||
void free_chunks_old() { free(chunk_old); }
|
||||
|
||||
int gen_cubes(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
|
||||
struct v3f *textO, int x, int y, int z, int is_purge, int *i2) {
|
||||
struct v3f noth = {{-1, -1, -1}};
|
||||
for (int i = 0; i < CBLOCK_ALL * 18; i++) {
|
||||
cube[i] = noth;
|
||||
text[i] = noth;
|
||||
|
@ -167,7 +168,7 @@ int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
|||
}
|
||||
|
||||
if (!chunk_old) {
|
||||
chunk_old = calloc((CHUNK_ALL), sizeof(struct Chunk));
|
||||
chunk_old = calloc((CHUNK_ALL), sizeof(struct chunk));
|
||||
}
|
||||
pthread_t thread_id[a3];
|
||||
int j = 0;
|
||||
|
@ -180,7 +181,7 @@ int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
|||
for (int z2 = z - (CHUNK_RADIUS_H)*CHUNK_LENGTH;
|
||||
z2 <= z + (CHUNK_RADIUS_H)*CHUNK_LENGTH; z2 += CHUNK_LENGTH) {
|
||||
|
||||
VertexI pos = {
|
||||
struct v3i pos = {
|
||||
{x + (((x2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) *
|
||||
CHUNK_LENGTH,
|
||||
y + (((y2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_V)) - CHUNK_RADIUS_V) *
|
||||
|
@ -200,7 +201,7 @@ int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
|||
|
||||
/*gen_chunk((void *)&args); */
|
||||
|
||||
Chunk old_chunk = chunk_old[a];
|
||||
struct chunk old_chunk = chunk_old[a];
|
||||
|
||||
int cached =
|
||||
(old_chunk.x == pos.pos[0] / CHUNK_LENGTH &&
|
||||
|
@ -238,8 +239,10 @@ int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
|||
max += 6;
|
||||
i2[(a / a3) - 1] = max;
|
||||
if (max > i) {
|
||||
memcpy(&(cubeO[j]), &(cube[i - 6 * a3]), (max - i) * sizeof(Vertex));
|
||||
memcpy(&(textO[j]), &(text[i - 6 * a3]), (max - i) * sizeof(Vertex));
|
||||
memcpy(&(cubeO[j]), &(cube[i - 6 * a3]),
|
||||
(max - i) * sizeof(struct v3f));
|
||||
memcpy(&(textO[j]), &(text[i - 6 * a3]),
|
||||
(max - i) * sizeof(struct v3f));
|
||||
j += max - i;
|
||||
}
|
||||
i = (BLOCK_ALL * 18) * a; // i = max + 6;
|
||||
|
@ -249,12 +252,6 @@ int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x,
|
|||
ox = x;
|
||||
oy = y;
|
||||
oz = z;
|
||||
/* if (i < CBLOCK_ALL * 9 && r < MAX_R) {
|
||||
printf("Expanding radius...\n");
|
||||
return gen_cubes(cube, text, x / CHUNK_LENGTH, y / CHUNK_LENGTH,
|
||||
z / CHUNK_LENGTH, r + 1);
|
||||
}
|
||||
*/
|
||||
|
||||
return j; // i;
|
||||
}
|
46
cubes.h
46
cubes.h
|
@ -1,3 +1,45 @@
|
|||
#include "const.h"
|
||||
#ifndef V_CUBES_H
|
||||
#define V_CUBES_H
|
||||
#include "linmath.h"
|
||||
|
||||
int gen_cubes(Vertex *cube, Vertex *text, Vertex *cubeO, Vertex *textO, int x, int y, int z, int is_purge, int *i2);
|
||||
#define CHUNK_LENGTH 16
|
||||
#define CHUNK_RADIUS_H 4 // 8
|
||||
#define CHUNK_RADIUS_V 4 // 4
|
||||
|
||||
#define TEXT_GAP_H 1 / 3.0
|
||||
#define TEXT_GAP_V 1 / 6.0
|
||||
|
||||
#define CHUNK_DIAMETER_H (CHUNK_RADIUS_H * 2 + 1)
|
||||
#define CHUNK_DIAMETER_V (CHUNK_RADIUS_V * 2 + 1)
|
||||
#define CHUNK_ALL CHUNK_DIAMETER_H *CHUNK_DIAMETER_H *CHUNK_DIAMETER_V
|
||||
#define BLOCK_ALL CHUNK_LENGTH *CHUNK_LENGTH *CHUNK_LENGTH
|
||||
#define CBLOCK_ALL BLOCK_ALL *CHUNK_ALL
|
||||
#define CBUF_ALL 2
|
||||
|
||||
#define CTRI_ALL CBLOCK_ALL * 18 * sizeof(struct v3f)
|
||||
|
||||
struct chunk {
|
||||
unsigned char exists;
|
||||
unsigned int x;
|
||||
unsigned int y;
|
||||
unsigned int z;
|
||||
unsigned int blocks[BLOCK_ALL];
|
||||
};
|
||||
|
||||
struct v3f {
|
||||
vec3 pos;
|
||||
};
|
||||
|
||||
struct v3i {
|
||||
int pos[3];
|
||||
};
|
||||
|
||||
struct v2f {
|
||||
vec2 pos;
|
||||
};
|
||||
|
||||
int gen_cubes(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
|
||||
struct v3f *textO, int x, int y, int z, int is_purge, int *i2);
|
||||
|
||||
void free_chunks_old();
|
||||
#endif
|
30
gen.c
30
gen.c
|
@ -1,10 +1,10 @@
|
|||
#include "const.h"
|
||||
#include "cubes.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
Chunk *chunks = NULL;
|
||||
struct chunk *chunks = NULL;
|
||||
int lastI = 0;
|
||||
int seed = 69420;
|
||||
int prevExists = 0;
|
||||
|
@ -14,17 +14,17 @@ double interpolate(double a0, double a1, double w) {
|
|||
|
||||
int hash_combine(int a, int b) { return b + 0x9e3779b9 + (a << 6) + (a >> 2); }
|
||||
|
||||
Vertex2 perlin_grad(int x, int y) {
|
||||
struct v2f perlin_grad(int x, int y) {
|
||||
int x_hash = hash_combine(x, seed);
|
||||
int y_hash = hash_combine(seed, y);
|
||||
|
||||
double random = hash_combine(x_hash, y_hash) * (M_PI / ~(~0u >> 1));
|
||||
Vertex2 out = {cos(random), sin(random)};
|
||||
struct v2f out = {cos(random), sin(random)};
|
||||
return out;
|
||||
}
|
||||
|
||||
double perlin_dot(double ix, double iy, double x, double y) {
|
||||
Vertex2 grad = perlin_grad(ix, iy);
|
||||
struct v2f grad = perlin_grad(ix, iy);
|
||||
|
||||
double dx = x - (double)ix;
|
||||
double dy = y - (double)iy;
|
||||
|
@ -58,11 +58,11 @@ double perlin_calc(double x, double y, double s) {
|
|||
return value;
|
||||
}
|
||||
|
||||
Chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
||||
struct chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
||||
|
||||
Chunk *chunks2 = chunks; // &chunks[CBUF_ALL * ci];
|
||||
struct chunk *chunks2 = chunks; // &chunks[CBUF_ALL * ci];
|
||||
|
||||
Chunk chunk;
|
||||
struct chunk chunk;
|
||||
|
||||
if (x < 0 || y < 0 || z < 0) {
|
||||
chunk.exists = 0;
|
||||
|
@ -102,7 +102,7 @@ Chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
|||
|
||||
FILE *fp = fopen(fname, "rb+");
|
||||
|
||||
int code = (fp) ? fread(&chunk, sizeof(Chunk), 1, fp) : 0;
|
||||
int code = (fp) ? fread(&chunk, sizeof(struct chunk), 1, fp) : 0;
|
||||
if (code != 0) {
|
||||
fclose(fp);
|
||||
fp = 0;
|
||||
|
@ -151,7 +151,7 @@ Chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
|||
return chunk;
|
||||
}
|
||||
|
||||
fwrite(&chunk, sizeof(Chunk), 1, fp);
|
||||
fwrite(&chunk, sizeof(struct chunk), 1, fp);
|
||||
fclose(fp);
|
||||
|
||||
return chunk;
|
||||
|
@ -160,15 +160,17 @@ Chunk fetch_chunk(int x, int y, int z, int ci, int h) {
|
|||
void purge_chunks(int ci) {
|
||||
/*
|
||||
free(chunks);
|
||||
chunks = calloc(CBUF_ALL, sizeof(Chunk));
|
||||
chunks = calloc(CBUF_ALL, sizeof(struct chunk));
|
||||
*/
|
||||
if (chunks == NULL) {
|
||||
chunks = calloc(CHUNK_ALL, sizeof(Chunk));
|
||||
chunks = calloc(CHUNK_ALL, sizeof(struct chunk));
|
||||
}
|
||||
// memset(chunks, 0, sizeof(Chunk) *CHUNK_ALL);
|
||||
// memset(chunks, 0, sizeof(struct chunk) *CHUNK_ALL);
|
||||
}
|
||||
|
||||
int cube_exists(int x, int y, int z, Chunk dat, int ci) {
|
||||
void free_chunks() { free(chunks); }
|
||||
|
||||
int cube_exists(int x, int y, int z, struct chunk dat, int ci) {
|
||||
if (x < 0 || y < 0 || z < 0)
|
||||
return 0;
|
||||
|
||||
|
|
10
gen.h
10
gen.h
|
@ -1,5 +1,9 @@
|
|||
#include "const.h"
|
||||
#ifndef V_GEN_H
|
||||
#define V_GEN_H
|
||||
#include "cubes.h"
|
||||
|
||||
int cube_exists(int x, int y, int z, Chunk dat, int ci);
|
||||
Chunk fetch_chunk(int x, int y, int z, int ci, int h);
|
||||
int cube_exists(int x, int y, int z, struct chunk dat, int ci);
|
||||
struct chunk fetch_chunk(int x, int y, int z, int ci, int h);
|
||||
void purge_chunks(int ci);
|
||||
void free_chunks();
|
||||
#endif
|
51
main.c
51
main.c
|
@ -1,21 +1,19 @@
|
|||
#include <math.h>
|
||||
#define GLAD_GL_IMPLEMENTATION
|
||||
#include "gl.h"
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include <GLFW/glfw3.h>
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#define GLAD_GL_IMPLEMENTATION
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "const.h"
|
||||
#include "cubes.h"
|
||||
#include "linmath.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "cubes.h"
|
||||
#include "gen.h"
|
||||
#include "gl.h"
|
||||
#include "linmath.h"
|
||||
#include "stb_image.h"
|
||||
|
||||
static const char *vertex_shader_text =
|
||||
"#version 330\n"
|
||||
|
@ -78,10 +76,10 @@ static void cursor_position_callback(GLFWwindow *window, double x, double y) {
|
|||
|
||||
struct args {
|
||||
int *cube_count;
|
||||
Vertex **cube;
|
||||
Vertex **text;
|
||||
Vertex **cubeO;
|
||||
Vertex **textO;
|
||||
struct v3f **cube;
|
||||
struct v3f **text;
|
||||
struct v3f **cubeO;
|
||||
struct v3f **textO;
|
||||
vec3 pos;
|
||||
GLuint *vertex_buffer;
|
||||
int *is_render;
|
||||
|
@ -128,10 +126,10 @@ int main(void) {
|
|||
gladLoadGL(glfwGetProcAddress);
|
||||
glfwSwapInterval(1);
|
||||
|
||||
Vertex *cube = malloc(CTRI_ALL);
|
||||
Vertex *text = malloc(CTRI_ALL);
|
||||
Vertex *cubeO = malloc(CTRI_ALL);
|
||||
Vertex *textO = malloc(CTRI_ALL);
|
||||
struct v3f *cube = malloc(CTRI_ALL);
|
||||
struct v3f *text = malloc(CTRI_ALL);
|
||||
struct v3f *cubeO = malloc(CTRI_ALL);
|
||||
struct v3f *textO = malloc(CTRI_ALL);
|
||||
int bleh = (CHUNK_DIAMETER_H) * (CHUNK_DIAMETER_V);
|
||||
int *i2 = calloc(bleh, sizeof(int));
|
||||
|
||||
|
@ -148,12 +146,14 @@ int main(void) {
|
|||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
|
||||
glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, text, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct v3f),
|
||||
(void *)0);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, cube, GL_STATIC_DRAW);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(struct v3f),
|
||||
(void *)0);
|
||||
|
||||
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
||||
|
@ -186,7 +186,7 @@ int main(void) {
|
|||
|
||||
vec3 pos = {10240, 48, 10240};
|
||||
vec3 oldPos = {0, 0, 0};
|
||||
Chunk ch7;
|
||||
struct chunk ch7;
|
||||
ch7.exists = 0;
|
||||
int state = cube_exists(pos[0], pos[1], pos[2], ch7, 0);
|
||||
|
||||
|
@ -288,11 +288,11 @@ int main(void) {
|
|||
|
||||
if (is_render == 2) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(struct v3f),
|
||||
textO); // text);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(struct v3f),
|
||||
cubeO); // cube);
|
||||
if (thread_id != 0)
|
||||
pthread_join(thread_id, NULL);
|
||||
|
@ -326,6 +326,11 @@ int main(void) {
|
|||
|
||||
free(cube);
|
||||
free(text);
|
||||
free(cubeO);
|
||||
free(textO);
|
||||
free(i2);
|
||||
free_chunks();
|
||||
free_chunks_old();
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue