75 lines
2.4 KiB
C
75 lines
2.4 KiB
C
#pragma once
|
|
|
|
#include "math.h"
|
|
#include "matrices.h"
|
|
|
|
inline static struct mat4x4 translation(float x, float y, float z)
|
|
{
|
|
return mat4x4(1.0f, 0.0f, 0.0f, x,
|
|
0.0f, 1.0f, 0.0f, y,
|
|
0.0f, 0.0f, 1.0f, z,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline static struct mat4x4 scaling(float x, float y, float z)
|
|
{
|
|
return mat4x4(x, 0.0f, 0.0f, 0.0f,
|
|
0.0f, y, 0.0f, 0.0f,
|
|
0.0f, 0.0f, z, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline static struct mat4x4 rotation_x(float r)
|
|
{
|
|
return mat4x4(1.0f, 0.0f, 0.0f, 0.0f,
|
|
0.0f, cosf(r), -sinf(r), 0.0f,
|
|
0.0f, sinf(r), cosf(r), 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline static struct mat4x4 rotation_y(float r)
|
|
{
|
|
return mat4x4( cosf(r), 0.0f, sinf(r), 0.0f,
|
|
0.0f, 1.0f, 0.0f, 0.0f,
|
|
-sinf(r), 0.0f, cosf(r), 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline static struct mat4x4 rotation_z(float r)
|
|
{
|
|
return mat4x4( cosf(r), -sinf(r), 0.0f, 0.0f,
|
|
sinf(r), cosf(r), 0.0f, 0.0f,
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline static struct mat4x4 shearing(float xy,
|
|
float xz,
|
|
float yx,
|
|
float yz,
|
|
float zx,
|
|
float zy)
|
|
{
|
|
return mat4x4(1.0f, xy, xz, 0.0f,
|
|
yx, 1.0f, yz, 0.0f,
|
|
zx, zy, 1.0f, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline static struct mat4x4 view_transform(struct tuple from,
|
|
struct tuple to,
|
|
struct tuple up)
|
|
{
|
|
struct tuple forward = tuple_normalize(tuple_sub(to, from));
|
|
struct tuple left = tuple_cross(forward, tuple_normalize(up));
|
|
struct tuple true_up = tuple_cross(left, forward);
|
|
|
|
struct mat4x4 orientation = mat4x4(left.x, left.y, left.z, 0.0f,
|
|
true_up.x, true_up.y, true_up.z, 0.0f,
|
|
-forward.x, -forward.y, -forward.z, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
|
|
struct mat4x4 translate = translation(-from.x, -from.y, -from.z);
|
|
return mat4x4_mul_m(orientation, translate);
|
|
}
|