Skip to content

Instantly share code, notes, and snippets.

View slembcke's full-sized avatar
👣
Gruntled

Scott Lembcke slembcke

👣
Gruntled
View GitHub Profile
@slembcke
slembcke / cull.c
Created August 1, 2023 16:26
Cull a bounding box (-1, 1) using the MVP transform.
// Check if a tile is visible onscreen using it's MVP transform.
static inline bool MapTileFrustumCull(const mat4 mvp){
// Clip space center and extents.
vec4 c = {mvp.m[12], mvp.m[13], mvp.m[14], mvp.m[15]};
vfloat ex = vabs(mvp.m[ 0]) + vabs(mvp.m[ 4]) + vabs(mvp.m[ 8]);
vfloat ey = vabs(mvp.m[ 1]) + vabs(mvp.m[ 5]) + vabs(mvp.m[ 9]);
vfloat ez = vabs(mvp.m[ 2]) + vabs(mvp.m[ 6]) + vabs(mvp.m[10]);
// Check the bounds against the clip space viewport using a conservative w-value.
vfloat w = vmax(0, c.w + vabs(mvp.m[ 3]) + vabs(mvp.m[ 7]) + vabs(mvp.m[11]));
@slembcke
slembcke / inverted_sdl.c
Last active December 12, 2022 20:42
SDL-like API on top of an inversion of control API.
#include <stdbool.h>
#include <math.h>
#include <stdio.h>
#include <GL/gl.h>
// A vaguely SDL-like API
void iSDL_init(void);
bool iSDL_should_exit;
void iSDL_swap_buffers(void);
@slembcke
slembcke / frame-timing.c
Last active June 18, 2023 19:05
Filter frame timing to try and remove scheduler jitter.
double delta_time_filtered(double dt_nanos){
// Warm start the filter by assuming 60 Hz.
static double x[] = {1.6e7, 1.6e7, 1.6e7}, y[] = {1.6e7, 1.6e7, 1.6e7};
// IIR filter coefficients. 2rd order lowpass butterworth at 1/128 the sample rate.
static const double b[] = {6.321391700454014e-5, 0.00012642783400908025, 6.321391700454014e-5};
static const double a[] = {1.0, -1.9681971279272976, 0.9684499835953156};
// Apply IIR filter coefficients.
double value = b[0]*dt_nanos;
for(uint i = 2; i > 0; i--){
@slembcke
slembcke / zoom.c
Created April 28, 2022 08:58
Fractalis zooming
static void do_zoom(AppState* app, tina* coro, int button, double direction){
if(glfwGetMouseButton(app->window, button) != GLFW_PRESS) return;
Camera* cam = &app->camera;
double speed = 0;
double t0 = glfwGetTime();
while(glfwGetMouseButton(app->window, button) == GLFW_PRESS){
double t1 = glfwGetTime();
double dt = t1 - t0;
t0 = t1;
@slembcke
slembcke / drift_physics.c
Created April 27, 2022 05:39
Project Drift physics
#include <string.h>
#include "tracy/TracyC.h"
#include "drift_game.h"
typedef struct PhysicsContext PhysicsContext;
typedef struct {
uint idx0, idx1;
// Appologies for the messy code.
// It was run through SPIRV-Cross
// I cleaned it up and renamed the variables by hand.
#version 330
layout(std140) uniform Locals
{
mat2x4 LightMatrix;
float LightRadius;
@slembcke
slembcke / CullOBB.c
Last active December 9, 2020 14:43
/*
* Used for culling tiles in a map renderer I wrote.
* All tiles used geometry in the [-1, 1] range, and their mvp matrix would get precalculated.
* So it was simply a matter of seeing if their projected OBB overlapped with the screen's clip space.
* Returns true if the cube is visible. (false positives for near misses due to conservative w-value)
*/
static inline bool MapTileFrustumCull(const mat4 mvp){
// Clip space center of the cube is the last column of the matrix.
vec4 c = {mvp.m[12], mvp.m[13], mvp.m[14], mvp.m[15]};
// Clip space extents are the component wise absolute values of the first three columns.
@slembcke
slembcke / FourierLights.hlsl
Created January 21, 2020 22:54
Fourier lighting code
struct FragOutput {
half4 a0 : SV_TARGET0;
half4 a1 : SV_TARGET1;
half4 b1 : SV_TARGET2;
half4 a2 : SV_TARGET3;
half4 b2 : SV_TARGET4;
};
// Double angle formula derived from the trig identity.
float2 DoubleAngle(float2 n){
@slembcke
slembcke / mulx.c
Last active October 30, 2019 04:28
Signed 16.16 fixed point multiply.
static inline fixed mulx_fast(fixed x, fixed y){return (fixed){(x.asint >> 8) * (y.asint >> 8)};}
static inline fixed mulx(fixed x, fixed y){
bool neg = (x.asint ^ y.asint) < 0;
union {
u32 asint;
struct {u16 hi, lo;};
} _x, _y;
if(x.asint >= 0) _x.asint = x.asint; else _x.asint = -x.asint;
00000204 <start>:
204: 1039 00a1 0001 moveb a10001 <VDP_REG_DMA_SRC2+0xa06901>,%d0
20a: 0200 000f andib #15,%d0
20e: 6700 000c beqw 21c <start+0x18>
212: 23fc 04fd 1e0d movel #83697165,a14000 <VDP_REG_DMA_SRC2+0xa0a900>
218: 00a1 4000
21c: 4a79 00c0 0004 tstw c00004 <VDP_CTRL>
222: 41f9 00c0 0004 lea c00004 <VDP_CTRL>,%a0
228: 30bc 8004 movew #-32764,%a0@
22c: 30bc 8144 movew #-32444,%a0@