11#include "voxel_vert.h"
12#include "voxel_frag.h"
14#define STB_PERLIN_IMPLEMENTATION
15#pragma GCC diagnostic push
16#pragma GCC diagnostic ignored "-Wmissing-prototypes"
18#pragma GCC diagnostic pop
33 int width = (radius * 2 + 1);
38 int next = current + delta;
55 "Render Radius: %d (%d chunks), Far Plane: %.1f\n",
76 for (
int oct = 0; oct < 6; oct++) {
82 float oct_freq = base_freq * (float)(1 << oct);
105 height_data[z *
VOXEL_WORLD_SIZE + x] = floorf((noise + 1.0f) * 0.5f * 64.0f);
134 uint32_t* pro_indices = malloc(
sizeof(uint32_t) * PRO_INDEX_COUNT);
135 for (uint32_t i = 0; i < PRO_INDEX_COUNT; i++)
140 sizeof(uint32_t) * PRO_INDEX_COUNT,
147 .fragmentShader = frag,
148 .vertexLayout = {
sizeof(
sbgl_Vertex), 0, NULL } };
161 float pitch = -0.4f, yaw = -
SBGL_PI / 2.0f;
162 bool mouse_locked =
false;
163 float move_speed = 100.0f, mouse_sensitivity = 0.005f;
164 double start_time =
sbgl_GetTime(ctx), last_time = start_time;
165 float fps_timer = 0.0f;
168 printf(
"--- Voxel Controls ---\n");
169 printf(
"W/A/S/D: Move\n");
170 printf(
"Q/E: Vertical Move\n");
171 printf(
"TAB: Lock/Unlock Mouse\n");
172 printf(
"+/-: Change Render Distance\n");
173 printf(
"ESC: Exit\n");
174 printf(
"----------------------\n");
185 float dt = (float)(current_time - last_time);
186 last_time = current_time;
190 if (fps_timer >= 1.0f) {
193 "FPS: %d | CPU: %.2fms (Sort: %.2fms) | GPU: %.2fms\n",
204 mouse_locked = !mouse_locked;
220 yaw += (float)input->
mouseDeltaX * mouse_sensitivity;
221 pitch -= (float)input->
mouseDeltaY * mouse_sensitivity;
229 sbgl_Vec3Set(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch))
232 float velocity = move_speed * dt;
257 for (
int i = 0; i < instance_count; i++) {
API for the SiputBiru Graphics Library (SBgl).
void sbgl_RenderQueuesEx(sbgl_Context *ctx, sbgl_RenderQueue **queues, uint32_t queueCount, const sbgl_Mat4 *viewProj, uint64_t userAddress)
Extended version of sbgl_RenderQueues with user metadata.
sbgl_InitResult sbgl_Init(int w, int h, const char *title)
Initializes the engine and opens a window.
void sbgl_SubmitDraw(sbgl_RenderQueue *queue, uint32_t mesh, uint32_t material, uint32_t blendMode, uint32_t sidedness, uint32_t tags, sbgl_SortKey key, const sbgl_InstanceData *data)
Appends a draw command to the render queue.
sbgl_Telemetry sbgl_GetTelemetry(sbgl_Context *ctx)
Retrieves the performance telemetry data for the previous frame.
bool sbgl_WindowShouldClose(sbgl_Context *ctx)
Checks if the user or OS has requested to close the window.
const sbgl_InputState * sbgl_GetInputState(sbgl_Context *ctx)
Retrieves the input state for the current frame.
sbgl_Pipeline sbgl_CreatePipeline(sbgl_Context *ctx, const sbgl_PipelineConfig *config)
Creates a graphics pipeline.
sbgl_Shader sbgl_LoadShader(sbgl_Context *ctx, sbgl_ShaderStage stage, const uint32_t *bytecode, size_t size)
Loads a shader from SPIR-V bytecode.
void sbgl_SetMouseMode(sbgl_Context *ctx, sbgl_MouseMode mode)
Sets the cursor behavior and visibility.
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
void sbgl_BindPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Binds a graphics pipeline for subsequent draw calls.
#define sbgl_Clear
Backward compatibility alias for sbgl_SetClearColor.
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
sbgl_RenderQueue * sbgl_CreateRenderQueue(sbgl_Context *ctx, struct SblArena *arena)
Creates a thread-local render queue for collecting draw commands.
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
double sbgl_GetTime(sbgl_Context *ctx)
Retrieves the current monotonic system time in seconds.
uint64_t sbgl_GetBufferDeviceAddress(sbgl_Context *ctx, sbgl_Buffer buffer)
Retrieves the 64-bit GPU virtual address for a buffer.
void sbgl_Shutdown(sbgl_Context *ctx)
Gracefully shuts down the engine and releases all resources.
void sbgl_BeginDrawing(sbgl_Context *ctx)
Prepares the engine for a new frame of drawing.
void sbgl_BindBuffer(sbgl_Context *ctx, sbgl_Buffer buffer, sbgl_BufferUsage usage)
Binds a buffer to the pipeline.
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
Camera system and batch collision math for SBgl.
static sbgl_Camera sbgl_CameraPerspective(float fov_y_rad, float aspect, float near_p, float far_p)
Initializes a camera with perspective projection.
static sbgl_Mat4 sbgl_CameraGetProjection(const sbgl_Camera *cam)
Computes the projection matrix for the given camera.
static sbgl_Mat4 sbgl_CameraGetView(const sbgl_Camera *cam)
Computes the view matrix for the given camera.
Single-header math library for SBgl.
static sbgl_Vec3 sbgl_Vec3Set(float x, float y, float z)
Creates a Vec3, correctly padded.
static sbgl_Vec3 sbgl_Vec3Add(sbgl_Vec3 a, sbgl_Vec3 b)
Adds two Vec3 vectors.
static sbgl_Vec3 sbgl_Vec3Mul(sbgl_Vec3 a, float s)
Multiplies a Vec3 by a scalar.
static sbgl_Mat4 sbgl_Mat4Mul(sbgl_Mat4 a, sbgl_Mat4 b)
Multiplies two matrices.
static sbgl_Vec3 sbgl_Vec3Cross(sbgl_Vec3 a, sbgl_Vec3 b)
Computes the cross product of two Vec3 vectors.
static sbgl_Vec3 sbgl_Vec3Normalize(sbgl_Vec3 v)
Normalizes a Vec3.
static sbgl_Vec3 sbgl_Vec3Sub(sbgl_Vec3 a, sbgl_Vec3 b)
Subtracts b from a.
@ SBGL_BUFFER_USAGE_INDEX
@ SBGL_BUFFER_USAGE_STORAGE
@ SBGL_BUFFER_USAGE_VERTEX
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
uint32_t sbgl_Shader
Handle for a shader module.
@ SBGL_SHADER_STAGE_FRAGMENT
@ SBGL_SHADER_STAGE_VERTEX
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
Arena allocator implementation.
SBL_ARENA_DEF bool sbl_arena_init(SblArena *arena, uint64_t initial_size)
float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
Stateful camera representation.
Result structure for initialization.
Per-instance data for automated batching.
4x4 Matrix, 16-byte aligned, column-major.
Configuration for creating a graphics pipeline.
Internal storage for draw packets awaiting submission.
Performance telemetry data for a single frame.
Standard vertex structure for basic geometry rendering. Optimized for cache density (16 bytes).
3D Vector, 16-byte aligned and padded for SIMD safety.
static const int VOXEL_WORLD_SIZE
static const int VOXEL_CHUNK_DIM
static int update_radius(int current, int delta)
static float calculate_far_plane(int radius)
static void apply_radius_change(sbgl_Camera *camera, int *radius, int *instance_count, int delta)
static const float VOXEL_FAR_PLANE_MARGIN
static int calculate_instance_count(int radius)
static const float VOXEL_CHUNK_SIZE