Shows how to get a 1-bit dithering effect in WebGL.
I disclaim copyright on this code, feel free to use it without attribution.
The TypeScript file shows how the texture is generated. The GLSL code shows how it is used in a fragment shader.
cc_library( | |
name = "example", | |
srcs = ["file.c"], | |
) |
We want to calculate x/240
, and to do that, we calculate (x*34953)>>23
. This only works for sufficiently small values of x.
The value of 34953 looks like some kind of magic constant. It is really just ceil((1 << 23)/240)
—the value 1/240
, scaled up by 2^23. As you use larger scaling factors (above 1<<23
), the value becomes more accurate, but you need more bits.
GCC can do this optimization for you under certain circumstances. GCC will convert x * 34953
to the same sequence of shifts and adds, if you are targeting an old processor like a 68000.
/* | |
Copyright 2022 Dietrich Epp | |
This file is released under the terms of the Mozilla Public License, version | |
2.0. | |
Fast conversion from binary to decimal! This was originally written for a GBA | |
project. Note that it's fast if you compile it as ARM code, but if you compile | |
it as Thumb code, it will be significantly slower. The reason is because the | |
division operation can be optimized out in ARM. In ARM, GCC will replace the |
CFLAGS += -O2 -Wall -Wextra -std=c17 -D_DEFAULT_SOURCE | |
scanner: scanner.o | |
$(CC) -o $@ $^ | |
scanner.c: scanner.l | |
flex -o$@ $< | |
clean: | |
rm -f scanner.o scanner.c scanner | |
.PHONY: clean |
%{ | |
#include <stdio.h> | |
#include <stdlib.h> | |
enum { | |
End, | |
Comma, | |
Equals, | |
Semicolon, | |
Number, |
// Location of the application: volume & directory, or 0 if unknown. | |
static short gAppVolRefNum; | |
static long gAppParID; | |
// Get an FSSpec pointing to a file with the given name. | |
static OSErr GetDataFile(FSSpec *spec, const char *filename) { | |
ProcessSerialNumber psn = {0, kCurrentProcess}; | |
ProcessInfoRec info = {0}; | |
FSSpec app_spec; | |
Str255 pname; |
all: inject.dylib main | |
inject.dylib: inject.c | |
cc -dynamiclib $^ -o $@ | |
main: main.c | |
cc $^ -o $@ |
Method 1: rejection sampling: sample uniformly in a cube, then reject points outside the unit sphere.
Method 2: polar sampling: use polar coordinates to directly generate points uniformly in the sphere
There are two implementations here. One in JavaScript, one in C. Which method is faster is different depending on the language!
"""rsrc2macbinary.py - convert resource fork to MacBinary file""" | |
import datetime | |
import os | |
import struct | |
import sys | |
import zlib | |
def die(*msg): | |
print('Error:', *msg, file=sys.stderr) | |
raise SystemExit(1) |