62 {
63
66 return 1;
68
69
71
74 float noise = 0;
75 float amp = 1.0f;
76 for (int oct = 0; oct < 6; oct++) {
77
78
79
80
82 float oct_freq = base_freq * (float)(1 << oct);
83
84 int wrap = 16 << oct;
85 if (wrap > 256) {
86 wrap = 256;
87 }
88
89
90
91
92
93
95 (float)x * oct_freq,
96 (float)z * oct_freq,
97 0,
98 wrap,
99 wrap,
100 wrap
101 ) *
102 amp;
103 amp *= 0.5f;
104 }
105 height_data[z *
VOXEL_WORLD_SIZE + x] = floorf((noise + 1.0f) * 0.5f * 64.0f);
106 }
107 }
108
110 ctx,
113 height_data
114 );
115 free(height_data);
116
117
120
121
126
127
131
132
134 uint32_t* pro_indices = malloc(sizeof(uint32_t) * PRO_INDEX_COUNT);
135 for (uint32_t i = 0; i < PRO_INDEX_COUNT; i++)
136 pro_indices[i] = i;
138 ctx,
140 sizeof(uint32_t) * PRO_INDEX_COUNT,
141 pro_indices
142 );
143 free(pro_indices);
144
145
147 .fragmentShader = frag,
148 .vertexLayout = {
sizeof(
sbgl_Vertex), 0, NULL } };
151
152
153 int radius = 5;
156
160
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;
166 int frame_count = 0;
167
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");
175
179
182 break;
183
185 float dt = (float)(current_time - last_time);
186 last_time = current_time;
187
188 frame_count++;
189 fps_timer += dt;
190 if (fps_timer >= 1.0f) {
192 printf(
193 "FPS: %d | CPU: %.2fms (Sort: %.2fms) | GPU: %.2fms\n",
194 frame_count,
198 );
199 fps_timer = 0.0f;
200 frame_count = 0;
201 }
202
204 mouse_locked = !mouse_locked;
206 ctx,
208 );
209 }
210
211
214 }
217 }
218
219 if (mouse_locked) {
220 yaw += (float)input->
mouseDeltaX * mouse_sensitivity;
221 pitch -= (float)input->
mouseDeltaY * mouse_sensitivity;
222 if (pitch > 1.5f)
223 pitch = 1.5f;
224 if (pitch < -1.5f)
225 pitch = -1.5f;
226 }
227
229 sbgl_Vec3Set(cosf(yaw) * cosf(pitch), sinf(pitch), sinf(yaw) * cosf(pitch))
230 );
232 float velocity = move_speed * dt;
233
248
251
252
253
256
257 for (int i = 0; i < instance_count; i++) {
259
260
264
266 }
267
269 .viewProj =
272 };
273
278
280 }
281
285 return 0;
286}
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.
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.
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.
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.
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 void apply_radius_change(sbgl_Camera *camera, int *radius, int *instance_count, int delta)