11#include "voxel3D_vert.h"
12#include "voxel3D_frag.h"
38 sbgl_VoxelConfig vConfig = { .max_slots = 256, .chunk_radius = 3, .enable_telemetry =
true };
42 fprintf(stderr,
"Failed to initialize VoxelSystem\n");
57 fprintf(stderr,
"Failed to load graphics shaders\n");
72 fprintf(stderr,
"Failed to create graphics pipeline\n");
83 for (uint32_t i = 0; i < 36; i++)
94 float pitch = -0.4f, yaw =
SBGL_PI / 2.0f;
95 bool mouseLocked =
false;
96 float moveSpeed = 400.0f, sensitivity = 0.005f;
99 printf(
"--- Voxel Controls ---\n");
100 printf(
"W/A/S/D: Move\n");
101 printf(
"Q/E: Vertical Move\n");
102 printf(
"TAB: Lock/Unlock Mouse\n");
103 printf(
"ESC: Exit\n");
104 printf(
"----------------------\n");
108 float dt = (float)(currentTime - lastTime);
109 lastTime = currentTime;
120 mouseLocked = !mouseLocked;
135 sbgl_Vec3Set(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch))
138 float velocity = moveSpeed * dt;
159 camera.
aspect = (float)width / (
float)height;
API for the SiputBiru Graphics Library (SBgl).
sbgl_InitResult sbgl_Init(int w, int h, const char *title)
Initializes the engine and opens a window.
void sbgl_GetWindowSize(sbgl_Context *ctx, int *w, int *h)
Retrieves the current window dimensions.
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.
void sbgl_DestroyShader(sbgl_Context *ctx, sbgl_Shader shader)
Destroys a shader module.
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_PushConstants(sbgl_Context *ctx, size_t size, const void *data)
Updates push constants for the currently bound pipeline.
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.
void sbgl_BeginCompute(sbgl_Context *ctx)
Prepares the engine for compute operations before the main drawing pass.
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.
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.
void sbgl_DestroyPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Destroys a graphics 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
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.
#define SBGL_INVALID_HANDLE
Public API for the SBgl voxel rendering system.
uint64_t sbgl_Voxel_GetInstanceAddress(sbgl_VoxelSystem *sys)
Returns the device address of the instance buffer.
void sbgl_Voxel_Cull(sbgl_VoxelSystem *sys, sbgl_Mat4 view_proj)
Performs GPU-driven frustum culling on voxel chunks. MUST be called BEFORE sbgl_BeginDrawing.
uint64_t sbgl_Voxel_GetPaletteAddress(sbgl_VoxelSystem *sys)
Returns the device address of the material palette buffer.
uint64_t sbgl_Voxel_GetAABBAddress(sbgl_VoxelSystem *sys)
Returns the device address of the AABB buffer.
void sbgl_Voxel_Update(sbgl_VoxelSystem *sys, sbgl_Vec3 camera_pos)
Updates the voxel system state based on the current camera position. This function handles chunk gene...
void sbgl_Voxel_Render(sbgl_VoxelSystem *sys)
Issues the indirect draw calls for visible voxel chunks. MUST be called BETWEEN sbgl_BeginDrawing and...
void sbgl_Voxel_Destroy(sbgl_VoxelSystem *sys)
Destroys the voxel system and releases all associated resources.
sbgl_VoxelSystem * sbgl_Voxel_Create(sbgl_Context *ctx, const sbgl_VoxelConfig *config)
Creates and initializes a new voxel system. This operation allocates GPU resources and internal track...
Push constants for the graphics pipeline.
uint64_t voxelDataAddress
Stateful camera representation.
Result structure for initialization.
4x4 Matrix, 16-byte aligned, column-major.
Configuration for creating a graphics pipeline.
Configuration parameters for initializing the voxel system.
Internal state for the voxel system. This structure adheres to Data-Oriented Design principles by mai...
3D Vector, 16-byte aligned and padded for SIMD safety.