many updates and improvements to chunk loading

This commit is contained in:
onezplpl 2024-07-13 21:18:55 -04:00
parent 7b9de49502
commit e21945fe23
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
6 changed files with 194 additions and 68 deletions

100
main.c
View file

@ -1,3 +1,4 @@
#include <math.h>
#define GLAD_GL_IMPLEMENTATION
#include "gl.h"
#define GLFW_INCLUDE_NONE
@ -5,6 +6,8 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <pthread.h>
#include "const.h"
#include "cubes.h"
#include "linmath.h"
@ -63,6 +66,33 @@ static void cursor_position_callback(GLFWwindow *window, double x, double y) {
return;
}
struct args {
int *cube_count;
Vertex **cube;
Vertex **text;
vec3 pos;
GLuint *vertex_buffer;
int *is_render;
};
void *render_chunks(void *args) {
struct args args2 = ((struct args *)args)[0];
int *cube_count = args2.cube_count;
// printf("%i\n", *cube_count);
GLuint *vertex_buffer = args2.vertex_buffer;
*args2.is_render = 0;
*cube_count =
gen_cubes(*args2.cube, *args2.text, args2.pos[0] / CHUNK_LENGTH,
args2.pos[1] / CHUNK_LENGTH, args2.pos[2] / CHUNK_LENGTH, 0, 0);
*args2.is_render = 2;
return NULL;
}
int main(void) {
if (!glfwInit())
exit(EXIT_FAILURE);
@ -84,8 +114,9 @@ int main(void) {
Vertex *cube = malloc(CTRI_ALL);
Vertex *text = malloc(CTRI_ALL);
gen_cubes(cube, 1024,0,1024, 0);
gen_cubes(text, 1024,0,1024, 1);
int cube_count = gen_cubes(cube, text, 0 / CHUNK_LENGTH, 0 / CHUNK_LENGTH,
0 / CHUNK_LENGTH, 0,1);
GLuint vertex_buffer[2];
GLuint vertex_array;
@ -132,29 +163,25 @@ int main(void) {
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
vec3 pos = {1024*CHUNK_LENGTH, 0, 1024*CHUNK_LENGTH};
vec3 pos = {1024, 0, 1024};
int x = 0, y = 25, z = 0;
// int x = 0, y = 25, z = 0;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double frames = 0.0;
pthread_t thread_id;
int is_render = 1;
glfwSetTime(7.5);
while (!glfwWindowShouldClose(window)) {
glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tx, ty, 0, GL_RGB, GL_UNSIGNED_BYTE,
pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
mat4x4 m, v, p, mvp;
mat4x4_identity(m);
mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000);
@ -189,12 +216,55 @@ int main(void) {
mat4x4_mul(mvp, mvp, m);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, CBLOCK_ALL * 18);
if (frames >= 8) {
struct args args;
args.cube_count = &cube_count;
args.cube = &cube;
args.text = &text;
args.pos[0] = pos[0];
args.pos[1] = pos[1];
args.pos[2] = pos[2];
args.vertex_buffer = vertex_buffer;
args.is_render = &is_render;
pthread_create(&(thread_id), NULL, render_chunks, (void *)&(args));
frames = 0;
glfwSetTime(0);
}
frames = glfwGetTime();
printf("%f\n", frames);
glfwSwapBuffers(window);
glfwPollEvents();
if (is_render == 2) {
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count*sizeof(Vertex), text);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count*sizeof(Vertex), cube);
is_render = 1;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tx, ty, 0, GL_RGB, GL_UNSIGNED_BYTE,
pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
int val = CTRI_ALL;
glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, cube_count);
}
free(cube);