SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
camera_main.c
Go to the documentation of this file.
1#include <sbgl.h>
2#include <sbgl_camera.h>
3#include <sbgl_math.h>
4#include <stdio.h>
5#include <time.h>
6#include "pyramid_vert.h"
7#include "pyramid_frag.h"
8
9typedef struct {
11} PushData;
12
13#define PACK_POS(x, y, z) { (int16_t)((x) * 32767.0f), (int16_t)((y) * 32767.0f), (int16_t)((z) * 32767.0f), 0 }
14#define PACK_COL(r, g, b, a) (uint32_t)(((uint32_t)((r)*255.0f) << 0) | ((uint32_t)((g)*255.0f) << 8) | ((uint32_t)((b)*255.0f) << 16) | ((uint32_t)((a)*255.0f) << 24))
15
16int main(void) {
17 sbgl_InitResult res = sbgl_Init(800, 600, "SBgl Unified Camera Example");
18 if (res.error != SBGL_SUCCESS) return 1;
19 sbgl_Context* ctx = res.ctx;
20
21 sbgl_Shader vert =
22 sbgl_LoadShader(ctx, SBGL_SHADER_STAGE_VERTEX, (const uint32_t*)pyramid_vert_spv, pyramid_vert_spv_len);
23 sbgl_Shader frag =
24 sbgl_LoadShader(ctx, SBGL_SHADER_STAGE_FRAGMENT, (const uint32_t*)pyramid_frag_spv, pyramid_frag_spv_len);
25
26 sbgl_Vertex vertices[] = {
27 { PACK_POS(0.0f, -0.5f, 0.0f), PACK_COL(1.0f, 0.0f, 0.0f, 1.0f), 0 },
28 { PACK_POS(0.5f, 0.5f, -0.5f), PACK_COL(1.0f, 0.0f, 0.0f, 1.0f), 0 },
29 { PACK_POS(-0.5f, 0.5f, -0.5f), PACK_COL(1.0f, 0.0f, 0.0f, 1.0f), 0 },
30 { PACK_POS(0.0f, -0.5f, 0.0f), PACK_COL(0.0f, 1.0f, 0.0f, 1.0f), 0 },
31 { PACK_POS(0.5f, 0.5f, 0.5f), PACK_COL(0.0f, 1.0f, 0.0f, 1.0f), 0 },
32 { PACK_POS(0.5f, 0.5f, -0.5f), PACK_COL(0.0f, 1.0f, 0.0f, 1.0f), 0 },
33 { PACK_POS(0.0f, -0.5f, 0.0f), PACK_COL(0.0f, 0.0f, 1.0f, 1.0f), 0 },
34 { PACK_POS(-0.5f, 0.5f, 0.5f), PACK_COL(0.0f, 0.0f, 1.0f, 1.0f), 0 },
35 { PACK_POS(0.5f, 0.5f, 0.5f), PACK_COL(0.0f, 0.0f, 1.0f, 1.0f), 0 },
36 { PACK_POS(0.0f, -0.5f, 0.0f), PACK_COL(1.0f, 1.0f, 0.0f, 1.0f), 0 },
37 { PACK_POS(-0.5f, 0.5f, -0.5f), PACK_COL(1.0f, 1.0f, 0.0f, 1.0f), 0 },
38 { PACK_POS(-0.5f, 0.5f, 0.5f), PACK_COL(1.0f, 1.0f, 0.0f, 1.0f), 0 },
39 { PACK_POS(-0.5f, 0.5f, -0.5f), PACK_COL(1.0f, 0.0f, 1.0f, 1.0f), 0 },
40 { PACK_POS(0.5f, 0.5f, -0.5f), PACK_COL(1.0f, 0.0f, 1.0f, 1.0f), 0 },
41 { PACK_POS(0.5f, 0.5f, 0.5f), PACK_COL(1.0f, 0.0f, 1.0f, 1.0f), 0 },
42 { PACK_POS(-0.5f, 0.5f, -0.5f), PACK_COL(0.0f, 1.0f, 1.0f, 1.0f), 0 },
43 { PACK_POS(0.5f, 0.5f, 0.5f), PACK_COL(0.0f, 1.0f, 1.0f, 1.0f), 0 },
44 { PACK_POS(-0.5f, 0.5f, 0.5f), PACK_COL(0.0f, 1.0f, 1.0f, 1.0f), 0 },
45 };
46
47 sbgl_Buffer vbo =
48 sbgl_CreateBuffer(ctx, SBGL_BUFFER_USAGE_VERTEX, sizeof(vertices), vertices);
49
50 sbgl_VertexAttribute attributes[] = {
51 { 0, offsetof(sbgl_Vertex, position), SBGL_FORMAT_R16G16B16A16_SNORM },
52 { 1, offsetof(sbgl_Vertex, color), SBGL_FORMAT_R8G8B8A8_UNORM }
53 };
54
55 sbgl_PipelineConfig config = { .vertexShader = vert,
56 .fragmentShader = frag,
57 .vertexLayout = { sizeof(sbgl_Vertex), 2, attributes } };
58
59 sbgl_Pipeline pipeline = sbgl_CreatePipeline(ctx, &config);
60
61 int width, height;
62 sbgl_GetWindowSize(ctx, &width, &height);
63 sbgl_Camera camera = sbgl_CameraPerspective(SBGL_PI / 4.0f, (float)width / (float)height, 0.1f, 100.0f);
64 camera.position = sbgl_Vec3Set(0.0f, -2.0f, -3.0f);
65 camera.target = sbgl_Vec3Set(0.0f, 0.0f, 0.0f);
66 camera.up = sbgl_Vec3Set(0.0f, -1.0f, 0.0f);
67
68 double start_time = sbgl_GetTime(ctx);
69
70 printf("--- Camera Controls ---\n");
71 printf("ESC: Exit\n");
72 printf("-----------------------\n");
73
74 // Define a batch of AABBs for collision testing
75 sbgl_AABB boxes[] = {
76 { sbgl_Vec3Set(-1.0f, -1.0f, -1.0f), sbgl_Vec3Set(1.0f, 1.0f, 1.0f) },
77 { sbgl_Vec3Set(2.0f, -0.5f, -0.5f), sbgl_Vec3Set(3.0f, 0.5f, 0.5f) },
78 { sbgl_Vec3Set(-3.0f, -0.5f, -0.5f), sbgl_Vec3Set(-2.0f, 0.5f, 0.5f) }
79 };
80
81 sbgl_HitResult results[3];
82
83 while (!sbgl_WindowShouldClose(ctx)) {
84 if (sbgl_GetInputState(ctx)->keysDown[SBGL_KEY_ESCAPE])
85 break;
86
87 sbgl_Clear(ctx, 0.0f, 0.0f, 0.0f, 0.0f);
89
90 double time = sbgl_GetTime(ctx) - start_time;
91 sbgl_Mat4 model = sbgl_Mat4Rotate((float)time * 10.5f, sbgl_Vec3Set(0.0f, 1.0f, 0.0f));
92
93 // Perform ray-casting test from the camera position forward
94 sbgl_Ray ray;
95 ray.origin = camera.position;
97 sbgl_RayAABBIntersectBatch(ray, boxes, results, 3);
98
99 for (int i = 0; i < 3; i++) {
100 if (results[i].hit) {
101 // Simple logging of hits (in a real app, this could trigger visual feedback)
102 // printf("Hit Box %d at distance %.2f\n", i, results[i].distance);
103 }
104 }
105
106 PushData push = { .viewProj = sbgl_Mat4Mul(
108 sbgl_Mat4Mul(sbgl_CameraGetView(&camera), model)
109 ) };
110
111 sbgl_BindPipeline(ctx, pipeline);
112 sbgl_PushConstants(ctx, sizeof(push), &push);
114 sbgl_Draw(ctx, 18, 0, 1);
115
116 sbgl_EndDrawing(ctx);
117 }
118
120
121 sbgl_DestroyPipeline(ctx, pipeline);
122 sbgl_DestroyShader(ctx, vert);
123 sbgl_DestroyShader(ctx, frag);
124 sbgl_DestroyBuffer(ctx, vbo);
125 sbgl_Shutdown(ctx);
126
127 return 0;
128}
int main(void)
Definition camera_main.c:16
#define PACK_POS(x, y, z)
Definition camera_main.c:13
#define PACK_COL(r, g, b, a)
Definition camera_main.c:14
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.
Definition sbgl_core.c:193
void sbgl_GetWindowSize(sbgl_Context *ctx, int *w, int *h)
Retrieves the current window dimensions.
Definition sbgl_core.c:252
bool sbgl_WindowShouldClose(sbgl_Context *ctx)
Checks if the user or OS has requested to close the window.
Definition sbgl_core.c:238
const sbgl_InputState * sbgl_GetInputState(sbgl_Context *ctx)
Retrieves the input state for the current frame.
Definition sbgl_core.c:413
sbgl_Pipeline sbgl_CreatePipeline(sbgl_Context *ctx, const sbgl_PipelineConfig *config)
Creates a graphics pipeline.
Definition sbgl_core.c:587
void sbgl_DestroyShader(sbgl_Context *ctx, sbgl_Shader shader)
Destroys a shader module.
Definition sbgl_core.c:565
sbgl_Shader sbgl_LoadShader(sbgl_Context *ctx, sbgl_ShaderStage stage, const uint32_t *bytecode, size_t size)
Loads a shader from SPIR-V bytecode.
Definition sbgl_core.c:523
void sbgl_PushConstants(sbgl_Context *ctx, size_t size, const void *data)
Updates push constants for the currently bound pipeline.
Definition sbgl_core.c:733
void sbgl_EndDrawing(sbgl_Context *ctx)
Finalizes the current frame and presents it to the screen.
Definition sbgl_core.c:315
#define SBGL_KEY_ESCAPE
Definition sbgl.h:84
void sbgl_BindPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Binds a graphics pipeline for subsequent draw calls.
Definition sbgl_core.c:673
#define sbgl_Clear
Backward compatibility alias for sbgl_SetClearColor.
Definition sbgl.h:229
void sbgl_DestroyBuffer(sbgl_Context *ctx, sbgl_Buffer buffer)
Destroys a GPU buffer.
Definition sbgl_core.c:457
void sbgl_DeviceWaitIdle(sbgl_Context *ctx)
Synchronizes the CPU with the GPU, waiting for all commands to complete.
Definition sbgl_core.c:391
double sbgl_GetTime(sbgl_Context *ctx)
Retrieves the current monotonic system time in seconds.
Definition sbgl_core.c:245
void sbgl_Draw(sbgl_Context *ctx, uint32_t vertexCount, uint32_t firstVertex, uint32_t instanceCount)
Submits a non-indexed draw command.
Definition sbgl_core.c:698
void sbgl_Shutdown(sbgl_Context *ctx)
Gracefully shuts down the engine and releases all resources.
Definition sbgl_core.c:204
void sbgl_BeginDrawing(sbgl_Context *ctx)
Prepares the engine for a new frame of drawing.
Definition sbgl_core.c:262
void sbgl_BindBuffer(sbgl_Context *ctx, sbgl_Buffer buffer, sbgl_BufferUsage usage)
Binds a buffer to the pipeline.
Definition sbgl_core.c:681
void sbgl_DestroyPipeline(sbgl_Context *ctx, sbgl_Pipeline pipeline)
Destroys a graphics pipeline.
Definition sbgl_core.c:597
sbgl_Buffer sbgl_CreateBuffer(sbgl_Context *ctx, sbgl_BufferUsage usage, size_t size, const void *data)
Creates a GPU buffer.
Definition sbgl_core.c:445
Camera system and batch collision math for SBgl.
static void sbgl_RayAABBIntersectBatch(sbgl_Ray ray, const sbgl_AABB *boxes, sbgl_HitResult *results, uint32_t count)
Performs a batch ray-AABB intersection test.
static sbgl_Camera sbgl_CameraPerspective(float fov_y_rad, float aspect, float near_p, float far_p)
Initializes a camera with perspective projection.
Definition sbgl_camera.h:86
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.
Definition sbgl_math.h:93
#define SBGL_PI
Definition sbgl_math.h:22
static sbgl_Mat4 sbgl_Mat4Rotate(float angle_rad, sbgl_Vec3 axis)
Creates a rotation matrix from axis and angle.
Definition sbgl_math.h:290
static sbgl_Mat4 sbgl_Mat4Mul(sbgl_Mat4 a, sbgl_Mat4 b)
Multiplies two matrices.
Definition sbgl_math.h:240
static sbgl_Vec3 sbgl_Vec3Normalize(sbgl_Vec3 v)
Normalizes a Vec3.
Definition sbgl_math.h:162
static sbgl_Vec3 sbgl_Vec3Sub(sbgl_Vec3 a, sbgl_Vec3 b)
Subtracts b from a.
Definition sbgl_math.h:137
@ SBGL_BUFFER_USAGE_VERTEX
Definition sbgl_types.h:123
@ SBGL_SUCCESS
Definition sbgl_types.h:215
@ SBGL_FORMAT_R8G8B8A8_UNORM
Definition sbgl_types.h:160
@ SBGL_FORMAT_R16G16B16A16_SNORM
Definition sbgl_types.h:159
uint32_t sbgl_Buffer
Handle for a GPU-side buffer.
Definition sbgl_types.h:37
uint32_t sbgl_Shader
Handle for a shader module.
Definition sbgl_types.h:42
@ SBGL_SHADER_STAGE_FRAGMENT
Definition sbgl_types.h:135
@ SBGL_SHADER_STAGE_VERTEX
Definition sbgl_types.h:134
uint32_t sbgl_Pipeline
Handle for a graphics pipeline.
Definition sbgl_types.h:47
sbgl_Mat4 viewProj
Definition camera_main.c:10
Axis-Aligned Bounding Box (AABB).
Definition sbgl_camera.h:56
Stateful camera representation.
Definition sbgl_camera.h:27
sbgl_Vec3 target
Definition sbgl_camera.h:31
sbgl_Vec3 up
Definition sbgl_camera.h:32
sbgl_Vec3 position
Definition sbgl_camera.h:30
Engine context.
Definition sbgl_types.h:268
Intersection result for batch testing.
Definition sbgl_camera.h:72
Result structure for initialization.
Definition sbgl_types.h:339
sbgl_Context * ctx
Definition sbgl_types.h:340
sbgl_Result error
Definition sbgl_types.h:341
4x4 Matrix, 16-byte aligned, column-major.
Definition sbgl_math.h:83
Configuration for creating a graphics pipeline.
Definition sbgl_types.h:204
Mathematical ray.
Definition sbgl_camera.h:48
sbgl_Vec3 direction
Definition sbgl_camera.h:50
sbgl_Vec3 origin
Definition sbgl_camera.h:49
Vertex attribute definition.
Definition sbgl_types.h:166
Standard vertex structure for basic geometry rendering. Optimized for cache density (16 bytes).
Definition sbgl_types.h:28