Return to 2D.

This commit is contained in:
2025-12-06 00:56:30 -05:00
parent d671b124b0
commit d412349d9b
13 changed files with 186 additions and 432 deletions

View File

@@ -1,51 +1,59 @@
#include "include/render.h"
#include "include/vol.h"
#include "include/gen.h"
#include <raylib.h>
#include <stdio.h>
void render(void) {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Terrin");
Vector2 PointToVector2(Pt p) {
Vector2 v;
v.x = (float)p.x * CELL_SZ;
v.y = (float)p.y * CELL_SZ;
return v;
}
Camera3D cam = {0};
cam.position =
(Vector3){VOL_WIDTH / 2.f, VOL_HEIGHT * .75f, VOL_DEPTH / 2.f};
cam.target = (Vector3){0, 0, 0};
cam.up = (Vector3){0.0f, 1.0f, 0.0f};
cam.fovy = 70.f;
cam.projection = CAMERA_PERSPECTIVE;
void render() {
for (int i = 0; i < 2; i++) {
Plate plate = lith[i];
SetTargetFPS(110);
// Skip plates with less than 3 vertices (can't form a polygon).
if (plate.nverts < 3) continue;
while (!WindowShouldClose()) {
UpdateCamera(&cam, CAMERA_FREE);
BeginDrawing();
ClearBackground((Color){0, 0, 0, 0});
BeginMode3D(cam);
for (int x = 0; x < VOL_WIDTH; x++) {
for (int y = 0; y < VOL_HEIGHT; y++) {
for (int z = 0; z < VOL_DEPTH; z++) {
Pt v = volume[x][y][z];
DrawSphere(
(Vector3){(float)x, (float)y, (float)z}, 0.2f,
(Color){v.col.r, v.col.g, v.col.b, 255}
);
}
}
// Convert the vertices from world coordinates (Pt) to screen
// coordinates (Vector2).
Vector2 screen_verts[4];
for (int j = 0; j < plate.nverts; j++) {
screen_verts[j] = PointToVector2(plate.verts[j]);
}
EndMode3D();
if (plate.nverts == 3) {
DrawTriangle(
screen_verts[0], screen_verts[1], screen_verts[2],
(Color){plate.col.r, plate.col.g, plate.col.b, plate.col.a}
);
DrawFPS(10, 10);
} else if (plate.nverts == 4) {
// Draw a quadrilateral (split into two triangles for filling)
// Triangle 1: V0, V1, V2
DrawTriangle(
screen_verts[0], screen_verts[1], screen_verts[2],
(Color){plate.col.r, plate.col.g, plate.col.b, plate.col.a}
EndDrawing();
);
// Triangle 2: V0, V2, V3 (Uses V0 and V2 as the base diagonal)
DrawTriangle(
screen_verts[0], screen_verts[2], screen_verts[3],
(Color){plate.col.r, plate.col.g, plate.col.b, plate.col.a}
);
}
// OPTIONAL: Draw the plate outline to make it clearer
/*
for (int j = 0; j < plate.nverts; j++) {
Vector2 p1 = screen_verts[j];
Vector2 p2 =
screen_verts[(j + 1) % plate.nverts]; // Loop back to the first
// point
DrawLineV(p1, p2, BLACK);
}*/
}
CloseWindow();
}