93 lines
1.9 KiB
C++
93 lines
1.9 KiB
C++
|
|
#include <cstdlib>
|
|
#include <Spectre/Math/Transform.h>
|
|
#include <Spectre/Math/Math.h>
|
|
|
|
namespace sp { namespace math
|
|
{
|
|
float rand(float min, float max) {
|
|
|
|
float r = ((float) ::rand()) / RAND_MAX;
|
|
return ((max - min) * r) + min;
|
|
}
|
|
|
|
float deg2rad(float deg) {
|
|
|
|
return deg * 3.141592654f / 180.0f;
|
|
}
|
|
|
|
unsigned int nextPowerOfTwo(unsigned int value) {
|
|
|
|
if (value == 0) {
|
|
return 1;
|
|
}
|
|
|
|
unsigned int exp = static_cast<unsigned int>(::ceil(log2(value)));
|
|
return ::pow(2.0f, exp);
|
|
}
|
|
|
|
Matrix4f orthoProjection(float left, float right,
|
|
float bottom, float top,
|
|
float zNear, float zFar) {
|
|
|
|
float rl = (right - left),
|
|
tb = (top - bottom),
|
|
fn = (zFar - zNear),
|
|
rl2 = 2.0f / rl,
|
|
tb2 = 2.0f / tb,
|
|
fn2 = 2.0f / fn,
|
|
x = (right + left) / rl,
|
|
y = (top + bottom) / tb,
|
|
z = (zFar + zNear) / fn;
|
|
|
|
return Matrix4f(
|
|
rl2 , 0.0f, 0.0f, -x,
|
|
0.0f, tb2 , 0.0f, -y,
|
|
0.0f, 0.0f, -fn2, -z,
|
|
0.0f, 0.0f, 0.0f, 1.0f
|
|
);
|
|
}
|
|
|
|
Vector2f Vector2UnProject(Vector2f point, Transform InverseMVP, Vector4u screen) {
|
|
|
|
// Convert to NDC from pixel cordinates first using screen size
|
|
point.x = -1.f + 2.f * (point.x - screen.x) / screen.z;
|
|
point.y = 1.f - 2.f * (point.y - screen.y) / screen.w;
|
|
|
|
// Then transform the point using the inverse MVP matrix.
|
|
return InverseMVP.transformPoint(point);
|
|
}
|
|
|
|
Matrix4f rotation(float theta) {
|
|
|
|
float r = deg2rad(theta);
|
|
float s = ::std::sin(r);
|
|
float c = ::std::cos(r);
|
|
|
|
return Matrix4f(
|
|
c, -s, 0.0f, 0.0f,
|
|
s, c, 0.0f, 0.0f,
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
|
|
Matrix4f translate(float x, float y) {
|
|
|
|
return Matrix4f(
|
|
1.0f, 0.0f, 0.0f, x,
|
|
0.0f, 1.0f, 0.0f, y,
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
Matrix4f scale(float x, float y) {
|
|
|
|
return Matrix4f(
|
|
x , 0.0f, 0.0f, 0.0f,
|
|
0.0f, y , 0.0f, 0.0f,
|
|
0.0f, 0.0f, 1.0f, 0.0f,
|
|
0.0f, 0.0f, 0.0f, 1.0f);
|
|
}
|
|
|
|
} } // namespace sp::math
|