Skip to content

Instantly share code, notes, and snippets.

@XProger
XProger / blend_mmx.asm
Created May 4, 2015 16:31
MMX: fast integer alpha blending
macro CANVAS_SET_ALPHA col { ; prepare mmx registers (alpha) by col (0xAAxxxxxx)
push col
shr col, 24
pxor mm5, mm5
movd mm7, col
pshufw mm7, mm7, 0
neg col
add col, 256
movd mm6, col
pshufw mm6, mm6, 0
@XProger
XProger / genCubeStrip.c
Last active November 27, 2015 09:02
14 vertices cube (TriStrip)
b = 1 << i;
x = (0x287A & b) != 0
y = (0x02AF & b) != 0
z = (0x31E3 & b) != 0
@XProger
XProger / cuteSort.glsl
Created November 27, 2015 08:59
sort 3 floats
vec3 cuteSort(vec3 n) {
float a = min(min(n.x, n.y), n.z);
float b = max(max(n.x, n.y), n.z);
return vec3(a, n.x + n.y + n.z - a - b, b);
}
@XProger
XProger / fast_orho_basis.c
Last active March 13, 2018 04:53
method to get orthonormal basis without normalization by Jeppe Revall Frisvad (jrf@imm.dtu.dk)
void getBasis(const vec3 &n, vec3 &t, vec3 &b) {
if (n.z < -0.9999999f) {
t = vec3( 0.0f, -1.0f, 0.0f);
b = vec3(-1.0f, 0.0f, 0.0f);
return;
}
const float x = 1.0f / (1.0f + n.z);
const float y = -n.x*n.y*x;
t = vec3(1.0f - n.x*n.x*x, y, -n.x);
b = vec3(b, 1.0f - n.y*n.y*x, -n.y);
@XProger
XProger / frustum_sphere.glsl
Created August 23, 2016 09:06
frustum culling for sphere
// Computes signed distance between a point and a plane
// vPlane: Contains plane coefficients (a,b,c,d) where: ax + by + cz = d
// vPoint: Point to be tested against the plane.
float DistanceToPlane( vec4 vPlane, vec3 vPoint )
{
return dot(vec4(vPoint, 1.0), vPlane);
}
// Frustum cullling on a sphere. Returns > 0 if visible, <= 0 otherwise
float CullSphere( vec4 vPlanes[6], vec3 vCenter, float fRadius )
@XProger
XProger / quinqunx_3x3_blur.glsl
Last active February 2, 2017 00:30
Get average color for 3x3 kernel by 4 fetch using bilinear filter
vec4 dx = vec4(0.25, 0.96, -0.25, -0.96) * uTexelSize.xyxy + vTexCoord.xyxy;
vec4 dy = vec4(0.25, 0.96, -0.25, -0.96) * uTexelSize.yxyx + vTexCoord.yxyx;
vec4 average = (texture2D(sTex, dx.zy) + texture2D(sTex, dx.xw) +
texture2D(sTex, dy.yx) + texture2D(sTex, dy.wz)) * 0.25;
@XProger
XProger / aabb_ray.cpp
Last active February 9, 2017 16:25
AABB vs Ray intersection test
bool AABB::intersect(const vec3 &rayPos, const vec3 &rayDir, float &t) const {
float t1 = INF, t0 = -t1;
for (int i = 0; i < 3; i++)
if (rayDir[i] != 0) {
float lo = (min[i] - rayPos[i]) / rayDir[i];
float hi = (max[i] - rayPos[i]) / rayDir[i];
t0 = _max(t0, _min(lo, hi));
t1 = _min(t1, _max(lo, hi));
} else
if (rayPos[i] < min[i] || rayPos[i] > max[i])
@XProger
XProger / quat_quat2.cpp
Created August 22, 2017 20:26
quaternions and dual-quaternions
// quat
const quat quat::IDENTITY(0.0f, 0.0f, 0.0f, 1.0f);
const quat quat::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
quat::quat() {}
quat::quat(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
quat::quat(const vec3 &axis, float angle) {
angle *= 0.5f;
float s = sinf(angle);
@XProger
XProger / circle_vs_segment.cpp
Created September 20, 2017 00:13
circle vs line segment intersection check
bool Circle::intersect(const Segment2D &segment, vec2 &n, float &t) const {
vec2 ap = center - segment.a;
vec2 ab = segment.b - segment.a;
float dab = ab.dot(ab);
if (dab == 0.0f) return false;
t = clamp(ap.dot(ab) / dab, 0.0f, 1.0f);
n = center - (segment.a + ab * t);
t = n.length2();
@XProger
XProger / cloth_gl.cpp
Last active November 6, 2017 02:25
cloth simulation
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <gl/GL.h>
// simulation
#define SIZE_X 129
#define SIZE_Y 129
#define RIGID_FACTOR 0.5f
#define DAMPING_FACTOR 0.998f