Skip to content

Instantly share code, notes, and snippets.

@aolo2
Created May 20, 2019 12:15
Show Gist options
  • Select an option

  • Save aolo2/8035f3a6a316efb51bd29911270b371f to your computer and use it in GitHub Desktop.

Select an option

Save aolo2/8035f3a6a316efb51bd29911270b371f to your computer and use it in GitHub Desktop.
static void
m3_unity(f32 result[static 9])
{
memset(result, 0x00, 9 * sizeof(f32));
result[0] = result[4] = result[8] = 1.0f;
}
static void
m3_mul(f32 result[static 9], f32 A[static 9], f32 B[static 9])
{
result[0] = A[0] * B[0] + A[1] * B[3] + A[2] * B[6];
result[1] = A[0] * B[1] + A[1] * B[4] + A[2] * B[7];
result[2] = A[0] * B[2] + A[1] * B[5] + A[2] * B[8];
result[3] = A[3] * B[0] + A[4] * B[3] + A[5] * B[6];
result[4] = A[3] * B[1] + A[4] * B[4] + A[5] * B[7];
result[5] = A[3] * B[2] + A[4] * B[5] + A[5] * B[8];
result[6] = A[6] * B[0] + A[7] * B[3] + A[8] * B[6];
result[7] = A[6] * B[1] + A[7] * B[4] + A[8] * B[7];
result[8] = A[6] * B[2] + A[7] * B[5] + A[8] * B[8];
}
static v3
m3_apply(f32 A[static 9], v3 vec)
{
v3 result;
result.x = A[0] * vec.x + A[1] * vec.y + A[2] * vec.w;
result.y = A[3] * vec.x + A[4] * vec.y + A[5] * vec.w;
result.w = A[6] * vec.x + A[7] * vec.y + A[8] * vec.w;
return(result);
}
static void
m3_scale(f32 result[static 9], v2 factor)
{
m3_unity(result);
result[0] = factor.x;
result[4] = factor.y;
}
static void
m3_translate(f32 result[static 9], v2 dir)
{
m3_unity(result);
result[2] = dir.x;
result[5] = dir.y;
}
static void
m3_rotate(f32 result[static 9], v2 center, f32 rad)
{
v2 center_back;
center_back.x = -1.0f * center.x;
center_back.y = -1.0f * center.y;
f32 translate_and_rotate[9];
f32 translate[9];
f32 translate_back[9];
f32 rotate[9];
m3_translate(translate, center);
m3_translate(translate_back, center_back);
m3_unity(rotate);
rotate[0] = cosf(rad);
rotate[1] = sinf(rad);
rotate[3] = -1.0f * sinf(rad);
rotate[4] = cosf(rad);
m3_mul(translate_and_rotate, translate, rotate);
m3_mul(result, translate_and_rotate, translate_back);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment