Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
shaders.c

A small example of using bj_bitmap_apply_shader to generate animated images.

A small example of using bj_bitmap_apply_shader to generate animated images.

// This shader comes from https://www.shadertoy.com/view/mtyGWy
// Designed by kishimisu at https://www.youtube.com/watch?v=f4s1h2YETNY
#define BJ_AUTOMAIN_CALLBACKS
#include <banjo/bitmap.h>
#include <banjo/log.h>
#include <banjo/main.h>
#include <banjo/shader.h>
#include <banjo/system.h>
#include <banjo/time.h>
#include <banjo/window.h>
#define CANVAS_W 512
#define CANVAS_H 512
bj_window* window = 0;
bj_bitmap* framebuffer = 0;
void palette(bj_vec3 res, float t) {
const float f = 6.28318f;
const bj_vec3 a = {.5f, .5f, .5f};
const bj_vec3 b = {.5f, .5f, .5f};
const bj_vec3 c = {1.f, 1.f, 1.f};
const bj_vec3 d = {.263f, .416f, .557f};
res[0] = a[0] + b[0] * bj_cosf(f * (c[0] * t + d[0]));
res[1] = a[1] + b[1] * bj_cosf(f * (c[1] * t + d[1]));
res[2] = a[2] + b[2] * bj_cosf(f * (c[2] * t + d[2]));
}
int shader_code(bj_vec3 frag_color, const bj_vec2 frag_coords, void* data) {
float time = *(float*)data;
bj_vec2 uv;
bj_vec3 final_color = { 0 };
bj_vec2_copy(uv, frag_coords);
const float uv0_len = bj_vec2_len(uv);
for(float i = 0.f ; i < 4.f ; ++i) {
bj_vec3 col;
palette(col, uv0_len + i * .4f + time * .4f);
bj_vec2_scale(uv, uv, 1.5f);
bj_vec2_sub(uv, uv, (bj_vec2) { .5f, .5f });
const float d = bj_powf(0.01f / (
bj_fabsf(
bj_sinf(
bj_vec2_len(uv) * bj_expf(-uv0_len) * 8.f + time
) / 8.f
)
), 1.2f);
bj_vec3_scale(col, col, d);
bj_vec3_add(final_color, final_color, col);
}
bj_vec3_copy(frag_color, final_color);
return 1;
}
int bj_app_begin(void** user_data, int argc, char* argv[]) {
(void)user_data; (void)argc; (void)argv;
bj_error* p_error = 0;
if(!bj_begin(&p_error)) {
bj_err("Error 0x%08X: %s", p_error->code, p_error->message);
return bj_callback_exit_error;
}
window = bj_window_new("Shader Art Coding Introduction", 1000, 500, CANVAS_W, CANVAS_H, 0);
framebuffer = bj_window_get_framebuffer(window, 0);
return bj_callback_continue;
}
int bj_app_iterate(void* user_data) {
(void)user_data;
float time = (float)bj_get_time();
bj_bitmap_apply_shader(framebuffer, shader_code, &time, BJ_SHADER_STANDARD_FLAGS);
bj_sleep(15);
return bj_window_should_close(window)
? bj_callback_exit_success
: bj_callback_continue;
}
int bj_app_end(void* user_data, int status) {
(void)user_data;
bj_window_del(window);
bj_end(0);
return status;
}
Header file for Bitmap type.
struct bj_bitmap_t bj_bitmap
Typedef for the bj_bitmap struct.
Definition bitmap.h:22
uint32_t code
Error code.
Definition error.h:132
char message[BJ_ERROR_MESSAGE_MAX_LEN+1]
Optional error description.
Definition error.h:133
Error structure.
Definition error.h:131
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:131
BJ_INLINE bj_real_t bj_vec2_len(const bj_vec2 v)
Computes the length of the vec2.
Definition linmath.h:179
BJ_INLINE void bj_vec2_copy(bj_vec2 res, const bj_vec2 src)
Copies the contents of one 2D vector to another.
Definition linmath.h:225
BJ_INLINE void bj_vec3_add(bj_vec3 res, const bj_vec3 lhs, const bj_vec3 rhs)
Set res to the result of lhs+rhs.
Definition linmath.h:265
BJ_INLINE void bj_vec2_sub(bj_vec2 res, const bj_vec2 lhs, const bj_vec2 rhs)
Set res to the result of lhs-rhs.
Definition linmath.h:133
BJ_INLINE void bj_vec2_scale(bj_vec2 res, const bj_vec2 v, bj_real_t s)
Set res to the result of multiplying v by s.
Definition linmath.h:145
BJ_INLINE void bj_vec2_apply(bj_vec2 res, const bj_vec2 a, bj_real_t(*f)(bj_real_t))
Invoke the given function to each scalar of the bj_vec2.
Definition linmath.h:109
BJ_INLINE void bj_vec3_scale(bj_vec3 res, const bj_vec3 v, bj_real_t s)
Set res to the result of multiplying v by s.
Definition linmath.h:291
BJ_INLINE void bj_vec3_copy(bj_vec3 res, const bj_vec3 src)
Copies the contents of one 3D vector to another.
Definition linmath.h:363
bj_real_t bj_vec3[3]
Defines a 3D vector type.
Definition linmath.h:54
bj_real_t bj_vec2[2]
Defines a 2D vector type.
Definition linmath.h:43
void bj_bitmap_apply_shader(bj_bitmap *p_bitmap, bj_bitmap_shading_fn_t p_shader, void *p_data, uint8_t flags)
Applies a shader function to every pixel in a bitmap.
#define BJ_SHADER_STANDARD_FLAGS
Flagset alias for bj_bitmap_apply_shader.
Definition shader.h:209
float bj_fract(float x)
Returns the fractional part of a float.
void bj_end(bj_error **p_error)
De-initializes the system.
bj_bool bj_begin(bj_error **p_error)
Initializes the system.
void bj_sleep(int milliseconds)
Suspends the current thread for a specified duration.
double bj_get_time(void)
Gets the current time in seconds.
bj_window * bj_window_new(const char *p_title, uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t flags)
Create a new bj_window with the specified attributes.
bj_bool bj_window_should_close(bj_window *p_window)
Get the close flag state of a window.
struct bj_window_t bj_window
Opaque typedef for the window type.
Definition window.h:18
bj_bitmap * bj_window_get_framebuffer(bj_window *p_window, bj_error **p_error)
Return the framebuffer attached to the window.
void bj_close_on_escape(bj_window *, bj_event_action, bj_key, int)
An event call back for closing the window when escape key is pressed.
void bj_poll_events(void)
Polls all pending events and dispatch them to callbacks.
void bj_window_del(bj_window *p_window)
Deletes a bj_window object and releases associated memory.
void bj_window_update_framebuffer(bj_window *p_window)
Copy window's framebuffer onto screen.
bj_window_key_event_t bj_window_set_key_event(bj_window *p_window, bj_window_key_event_t p_callback)
Set the callback for key events.
Logging utility functions.
Basic shader-like bitmap manipulation.
Header file for system interactions.
Header file for time manipulation.
Header file for bj_window type.