Files
Visualizer/math3d.cpp

97 lines
1.8 KiB
C++

#include <math.h>
struct Vec3 {
float x, y, z;
};
Vec3 create_Vec3 (float x, float y, float z) {
Vec3 result = {x, y, z};
return result;
}
Vec3 Vec3_add (const Vec3& a, const Vec3& b) {
Vec3 result = {a.x + b.x, a.y + b.y, a.z + b.z};
return result;
}
Vec3 Vec3_subtract (const Vec3& a, const Vec3& b) {
Vec3 result = {a.x - b.x, a.y - b.y, a.z - b.z};
return result;
}
Vec3 Vec3_scalar (const Vec3& a, const float scalar) {
Vec3 result = {a.x*scalar, a.y*scalar, a.z*scalar};
return result;
}
float Vec3_dot (const Vec3& a, const Vec3& b) {
float result = a.x*b.x + a.y*b.y + a.z*b.z;
return result;
}
Vec3 Vec3_cross (const Vec3& a, const Vec3& b) {
Vec3 result = {a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x};
return result;
}
float Vec3_length (const Vec3& a) {
float result = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
return result;
}
Vec3 Vec3_normalize (const Vec3& a) {
Vec3 result;
float len = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
if (len > 0.0f) {
return result = {a.x/len, a.y/len, a.z/len};
} else {
return a;
}
}
struct Mat4{
float m4[16];
};
Mat4 Mat4_createMatrix (float InitialValue) {
Mat4 result;
for (int i = 0; i < 16; i++) {
result.m4[i] = {InitialValue};
}
return result;
}
Mat4 Mat4_createZeroes () {
Mat4 result = {0};
return result;
}
Mat4 Mat4_createIdentity () {
Mat4 result = {0};
result.m4[0] = 1;
result.m4[5] = 1;
result.m4[10] = 1;
result.m4[15] = 1;
return result;
}
Mat4 Mat4_createTranslation (float x, float y, float z) {
Mat4 result = {0};
result.m4[0] = 1;
result.m4[5] = 1;
result.m4[10] = 1;
result.m4[15] = 1;
result.m4[12] = x;
result.m4[13] = y;
result.m4[14] = z;
return result;
}
//translate
//rotate
//scale
//perspective