summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaditya Dhruv <[email protected]>2026-01-31 17:17:25 -0600
committerAaditya Dhruv <[email protected]>2026-01-31 17:17:25 -0600
commitb20a6103cb202c1721aa2d300b1e7bdafef3366d (patch)
treebdf75ec1a9ac33a23f38b8bddf997f83873b7f53
parent42401e4e1c34f9ddc7246550227281ec6aeeceac (diff)
Fix OpenGL vs local axis issue
- World coordinates is positive x to the left, positive y inwards, positive z upwards All math before sending data to OpenGL happens in these world coordinates. For example, curr_chunk is in world coordinates. chunk coordinates are also in world system. Only when we get/set data to OpenGL functions do we perform the 2 step translation - world y is negated and - then flipped with world z so { x, y, z } --> { x, z, -y } basically. These translations are done for example when in the chunk->model translation, converting camera coordinates (that are in opengl coords) to world coords for curr_chunk data, and setting vertex positions in chunk_load. These have been marked with "//OpenGL FLIP" so it's easy to find
-rw-r--r--src/chunk.c25
-rw-r--r--src/engine.c20
2 files changed, 23 insertions, 22 deletions
diff --git a/src/chunk.c b/src/chunk.c
index 302c080..67631e9 100644
--- a/src/chunk.c
+++ b/src/chunk.c
@@ -364,21 +364,21 @@ void chunk_load(struct world* world, struct chunk *chunk, int coord[2]) {
int v_count[6] = { 0 };
int blk_c = 0;
for (int x = 0; x < CHUNK_WIDTH; x++) {
- for (int y = 0; y < CHUNK_HEIGHT; y++) {
- for (int z = 0; z < CHUNK_LENGTH; z++) {
- struct block* blk = chunk->blocks[x][z][y];
+ for (int y = 0; y < CHUNK_LENGTH; y++) {
+ for (int z = 0; z < CHUNK_HEIGHT; z++) {
+ struct block* blk = chunk->blocks[x][y][z];
// If not air block
if (blk != NULL) {
blk_c += 1;
- vec3 front = { x, z - 1, y };
- vec3 back = { x, z + 1, y };
- vec3 pos = { x, y, -z };
- vec3 right = { x + 1, z, y };
- vec3 left = { x - 1, z, y };
- vec3 top = { x, z, y + 1 };
- vec3 bottom = { x, z, y - 1 };
- // Position of block in world coords
- // glm_vec3_add(pos, translation, pos);
+ // Position of block in OpenGL coords
+ // NOTE: OpenGL FLIP
+ vec3 pos = { x, z, -y };
+ vec3 front = { x, y - 1, z };
+ vec3 back = { x, y + 1, z };
+ vec3 right = { x + 1, y, z };
+ vec3 left = { x - 1, y, z };
+ vec3 top = { x, y, z + 1 };
+ vec3 bottom = { x, y, z - 1 };
if (_chunk_check_neighbor_block(world, chunk, front) == 0) {
VECTOR_INSERT(vertices, _chunk_face_add(front_face,
@@ -491,6 +491,7 @@ void chunk_load(struct world* world, struct chunk *chunk, int coord[2]) {
//VAO!! Always clear before use.
glBindVertexArray(0);
// Translation to WORLD units
+ // NOTE: OpenGL FLIP
vec3 translation = {CHUNK_WIDTH * coord[0], 0, - (CHUNK_LENGTH * coord[1])};
// Set the matrix for world coordinate translation
glm_mat4_identity(chunk->model);
diff --git a/src/engine.c b/src/engine.c
index 314c4ed..551ab65 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -10,6 +10,7 @@
#include <SDL2/SDL_render.h>
#include <bits/types/timer_t.h>
#include <junk/vector.h>
+#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
@@ -56,7 +57,7 @@ int engine_init(struct engine *engine) {
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk;
- int chunk_coord[2] = { engine->curr_chunk[0] + i, -engine->curr_chunk[1] + j };
+ int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
// Load chunk
chunk_load(world, chunk, chunk_coord);
@@ -68,11 +69,11 @@ int engine_init(struct engine *engine) {
}
void engine_update(struct engine* engine) {
- int curr_chunk[2] = { (engine->camera->position[0] / CHUNK_WIDTH), (engine->camera->position[2]) / CHUNK_LENGTH };
+ //NOTE: OpenGL FLIP
+ int curr_chunk[2] = { floorf(engine->camera->position[0] / (float)CHUNK_WIDTH), floorf(-engine->camera->position[2] / (float)CHUNK_LENGTH) };
// Chunk update
struct chunk* c = {0};
- int coord[2] = { curr_chunk[0], curr_chunk[1] };
- world_get_chunk(engine->world, coord, &c);
+ world_get_chunk(engine->world, curr_chunk, &c);
// We moved a chunk - load new chunks with chunk_load
if (engine->curr_chunk[0] != curr_chunk[0] || engine->curr_chunk[1] != curr_chunk[1]) {
@@ -85,7 +86,7 @@ void engine_update(struct engine* engine) {
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk;
- int chunk_coord[2] = { engine->curr_chunk[0] + i, -engine->curr_chunk[1] + j };
+ int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
// unload chunk
// TODO: Fix some VAO/VBO bug when negative y
@@ -98,7 +99,7 @@ void engine_update(struct engine* engine) {
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk;
- int chunk_coord[2] = { engine->curr_chunk[0] + i, -engine->curr_chunk[1] + j };
+ int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
// Load chunk
chunk_load(engine->world, chunk, chunk_coord);
@@ -131,6 +132,8 @@ void engine_start(struct engine* engine) {
frames = 0;
frame_last_time = now;
fprintf(stderr, "FPS: %.2f\n", fps);
+ glm_vec3_print(engine->camera->position, stderr);
+ fprintf(stderr, "x: %d, y: %d\n", engine->curr_chunk[0], engine->curr_chunk[1]);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -148,10 +151,7 @@ void engine_start(struct engine* engine) {
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk = {0};
- // // Load chunk
- // Ensure the y coordinate is negative, because in OpenGL +z-axis (y in chunk system) is towards
- // user, so we want inwards to be positive, so flip sign
- int chunk_coord[2] = { engine->curr_chunk[0] + i, -engine->curr_chunk[1] + j };
+ int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
chunk_draw(chunk, engine->shader, engine->texture);
}