mirror of
https://github.com/guezoloic/t3dsr.git
synced 2026-01-25 09:34:24 +00:00
fix(branch): rework branch
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#include "math/vector3.h"
|
||||
|
||||
int main() {
|
||||
return 0;
|
||||
}
|
||||
@@ -1,17 +1,20 @@
|
||||
#include "math/matrix3.h"
|
||||
#include "matrix3.h"
|
||||
#include <string.h>
|
||||
|
||||
Mat3 mat3(float arr[9]) {
|
||||
Mat3 mat3(float arr[9])
|
||||
{
|
||||
Mat3 mat;
|
||||
memcpy(mat.m, arr, 9*sizeof(float));
|
||||
return mat;
|
||||
}
|
||||
|
||||
Mat3 mat3Zro() {
|
||||
Mat3 mat3_zro()
|
||||
{
|
||||
return (Mat3){0};
|
||||
}
|
||||
|
||||
Mat3 mat3Ity() {
|
||||
Mat3 mat3_ity()
|
||||
{
|
||||
return (Mat3) {{
|
||||
1, 0, 0,
|
||||
0, 1, 0,
|
||||
@@ -19,37 +22,41 @@ Mat3 mat3Ity() {
|
||||
}};
|
||||
}
|
||||
|
||||
Mat3 mat3Add(Mat3 m1, Mat3 m2) {
|
||||
Mat3 mat3_add(const Mat3* m1, const Mat3* m2)
|
||||
{
|
||||
Mat3 mat;
|
||||
|
||||
for(int i = 0; i<9; i++) {
|
||||
mat.m[i] = m1.m[i] + m2.m[i];
|
||||
mat.m[i] = m1->m[i] + m2->m[i];
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
Mat3 mat3Sub(Mat3 m1, Mat3 m2) {
|
||||
Mat3 mat3_sub(const Mat3* m1, const Mat3* m2)
|
||||
{
|
||||
Mat3 mat;
|
||||
|
||||
for(int i = 0; i<9; i++) {
|
||||
mat.m[i] = m1.m[i] - m2.m[i];
|
||||
mat.m[i] = m1->m[i] - m2->m[i];
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
Mat3 mat3Scl(Mat3 m, float scalar) {
|
||||
Mat3 mat3_scl(const Mat3* m, float scalar)
|
||||
{
|
||||
Mat3 mat;
|
||||
|
||||
for(int i = 0; i<9; i++) {
|
||||
mat.m[i] = m.m[i] * scalar;
|
||||
mat.m[i] = m->m[i] * scalar;
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
Mat3 mat3Mul(Mat3 m1, Mat3 m2) {
|
||||
Mat3 mat3_mul(const Mat3* m1, const Mat3* m2)
|
||||
{
|
||||
Mat3 mat;
|
||||
|
||||
for(int i = 0; i<3; i++) {
|
||||
@@ -58,7 +65,7 @@ Mat3 mat3Mul(Mat3 m1, Mat3 m2) {
|
||||
float sum = 0;
|
||||
|
||||
for (int k = 0; k < 3; k++) {
|
||||
sum += m1.m[i3 + k] * m2.m[k*3 + j];
|
||||
sum += m1->m[i3 + k] * m2->m[k*3 + j];
|
||||
}
|
||||
|
||||
mat.m[i3 + j] = sum;
|
||||
@@ -68,43 +75,45 @@ Mat3 mat3Mul(Mat3 m1, Mat3 m2) {
|
||||
return mat;
|
||||
}
|
||||
|
||||
Mat3 mat3Tpo(Mat3 m) {
|
||||
Mat3 mat3_tpo(const Mat3* m)
|
||||
{
|
||||
Mat3 mat;
|
||||
|
||||
for(int i = 0; i<3; i++) {
|
||||
int i3 = i * 3;
|
||||
|
||||
for (int j = 0; j < 3; j++) {
|
||||
mat.m[i3 + j] = m.m[j*3 + i];
|
||||
mat.m[i3 + j] = m->m[j*3 + i];
|
||||
}
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
float mat3Det(Mat3 m) {
|
||||
return m.m[0] * (m.m[4] * m.m[8] - m.m[5] * m.m[7]) -
|
||||
m.m[1] * (m.m[3] * m.m[8] - m.m[5] * m.m[6]) +
|
||||
m.m[2] * (m.m[3] * m.m[7] - m.m[4] * m.m[6]);
|
||||
float mat3_det(const Mat3* m)
|
||||
{
|
||||
return m->m[0] * (m->m[4] * m->m[8] - m->m[5] * m->m[7]) -
|
||||
m->m[1] * (m->m[3] * m->m[8] - m->m[5] * m->m[6]) +
|
||||
m->m[2] * (m->m[3] * m->m[7] - m->m[4] * m->m[6]);
|
||||
}
|
||||
|
||||
Mat3 mat3Inv(Mat3 m) {
|
||||
Mat3 inv;
|
||||
float det = mat3Det(m);
|
||||
if (det == 0) return mat3Ity();
|
||||
// Mat3 Mat3Inv(Mat3 m) {
|
||||
// Mat3 inv;
|
||||
// float det = Mat3Det(m);
|
||||
// if (det == 0) return Mat3Ity();
|
||||
|
||||
float invDet = 1.0f / det;
|
||||
// float invDet = 1.0f / det;
|
||||
|
||||
// ???
|
||||
inv.m[0] = (m.m[4] * m.m[8] - m.m[5] * m.m[7]) * invDet;
|
||||
inv.m[1] = -(m.m[1] * m.m[8] - m.m[2] * m.m[7]) * invDet;
|
||||
inv.m[2] = (m.m[1] * m.m[5] - m.m[2] * m.m[4]) * invDet;
|
||||
inv.m[3] = -(m.m[3] * m.m[8] - m.m[5] * m.m[6]) * invDet;
|
||||
inv.m[4] = (m.m[0] * m.m[8] - m.m[2] * m.m[6]) * invDet;
|
||||
inv.m[5] = -(m.m[0] * m.m[5] - m.m[2] * m.m[3]) * invDet;
|
||||
inv.m[6] = (m.m[3] * m.m[7] - m.m[4] * m.m[6]) * invDet;
|
||||
inv.m[7] = -(m.m[0] * m.m[7] - m.m[1] * m.m[6]) * invDet;
|
||||
inv.m[8] = (m.m[0] * m.m[4] - m.m[1] * m.m[3]) * invDet;
|
||||
// // ???
|
||||
// inv.m[0] = (m->m[4] * m->m[8] - m->m[5] * m->m[7]) * invDet;
|
||||
// inv.m[1] = -(m->m[1] * m->m[8] - m->m[2] * m->m[7]) * invDet;
|
||||
// inv.m[2] = (m->m[1] * m->m[5] - m->m[2] * m->m[4]) * invDet;
|
||||
// inv.m[3] = -(m->m[3] * m->m[8] - m->m[5] * m->m[6]) * invDet;
|
||||
// inv.m[4] = (m->m[0] * m->m[8] - m->m[2] * m->m[6]) * invDet;
|
||||
// inv.m[5] = -(m->m[0] * m->m[5] - m->m[2] * m->m[3]) * invDet;
|
||||
// inv.m[6] = (m->m[3] * m->m[7] - m->m[4] * m->m[6]) * invDet;
|
||||
// inv.m[7] = -(m->m[0] * m->m[7] - m->m[1] * m->m[6]) * invDet;
|
||||
// inv.m[8] = (m->m[0] * m->m[4] - m->m[1] * m->m[3]) * invDet;
|
||||
|
||||
return inv;
|
||||
}
|
||||
// return inv;
|
||||
// }
|
||||
22
src/math/matrix3.h
Normal file
22
src/math/matrix3.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef MATRIX3_H
|
||||
#define MATRIX3_H
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float m[9];
|
||||
} Mat3;
|
||||
|
||||
Mat3 mat3(float arr[9]);
|
||||
Mat3 mat3_zro();
|
||||
Mat3 mat3_ity();
|
||||
|
||||
Mat3 mat3_add(const Mat3* m1, const Mat3* m2);
|
||||
Mat3 mat3_sub(const Mat3* m1, const Mat3* m2);
|
||||
Mat3 mat3_scl(const Mat3* m, float scalar);
|
||||
Mat3 mat3_mul(const Mat3* m1, const Mat3* m2);
|
||||
|
||||
Mat3 mat3_tpo(const Mat3* m);
|
||||
float mat3_det(const Mat3* m);
|
||||
Mat3 mat3_inv(const Mat3* m);
|
||||
|
||||
#endif // MATRIX3_H
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "math/vector.h"
|
||||
|
||||
Vec3 Vec4ToVec3(Vec4 v)
|
||||
{
|
||||
return vec3(v.x, v.y, v.z);
|
||||
}
|
||||
|
||||
Vec4 Vec3ToVec4(Vec3 v)
|
||||
{
|
||||
return vec4(v.x, v.y, v.z, 0);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "math/vector3.h"
|
||||
#include "vector3.h"
|
||||
|
||||
Vec3 vec3(float x, float y, float z)
|
||||
{
|
||||
|
||||
130
src/math/vector3.h
Normal file
130
src/math/vector3.h
Normal file
@@ -0,0 +1,130 @@
|
||||
#ifndef VEC3_H
|
||||
#define VEC3_H
|
||||
|
||||
typedef struct {
|
||||
float x, y, z;
|
||||
} Vec3;
|
||||
|
||||
/**
|
||||
* @brief Creates a new 3D vector.
|
||||
* @param x X-axis coordinate.
|
||||
* @param y Y-axis coordinate.
|
||||
* @param z Z-axis coordinate.
|
||||
* @return A 3D vector with the specified coordinates.
|
||||
*/
|
||||
Vec3 vec3(float x, float y, float z);
|
||||
|
||||
/**
|
||||
* @brief Adds two 3D vectors and returns a new 3D vector.
|
||||
* @param v1 First vector.
|
||||
* @param v2 Second vector.
|
||||
* @return A 3D vector representing the sum of v1 and v2.
|
||||
*/
|
||||
Vec3 vec3Add(Vec3 v1, Vec3 v2);
|
||||
|
||||
/**
|
||||
* @brief Subtracts two 3D vectors and returns a new 3D vector.
|
||||
* @param v1 First vector.
|
||||
* @param v2 Second vector.
|
||||
* @return A 3D vector representing the result of v1 minus v2.
|
||||
*/
|
||||
Vec3 vec3Sub(Vec3 v1, Vec3 v2);
|
||||
|
||||
/**
|
||||
* @brief Scales a 3D vector by a constant scalar and returns a new 3D vector.
|
||||
* @param v 3D vector.
|
||||
* @param scalar Scalar value.
|
||||
* @return A 3D vector representing the multiplication of v by the scalar.
|
||||
*/
|
||||
Vec3 vec3Scale(Vec3 v, float scalar);
|
||||
|
||||
/**
|
||||
* @brief Computes the dot product of two 3D vectors.
|
||||
* @param a First vector.
|
||||
* @param b Second vector.
|
||||
* @return A scalar value representing the dot product of a and b.
|
||||
* - scalar > 0: Both vectors have the same orientation (the angle between them is acute).
|
||||
* - scalar = 0: Vectors are orthogonal (the angle between them is 90 degrees).
|
||||
* - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse).
|
||||
*/
|
||||
float vec3Dot(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the length (magnitude) of a 3D vector.
|
||||
* @param v 3D vector.
|
||||
* @return A scalar value representing the length (magnitude) of the vector v.
|
||||
*/
|
||||
float vec3Len(Vec3 v);
|
||||
|
||||
/**
|
||||
* @brief Normalizes a 3D vector (scales it to unit length).
|
||||
* @param v 3D vector.
|
||||
* @return A 3D vector representing the normalized version of v.
|
||||
* Returns a zero vector (0, 0, 0) if the input vector is a zero vector.
|
||||
*/
|
||||
Vec3 vec3Norm(Vec3 v);
|
||||
|
||||
/**
|
||||
* @brief Performs linear interpolation between two 3D vectors.
|
||||
* @param a Start vector.
|
||||
* @param b End vector.
|
||||
* @param t Interpolation factor (0.0 to 1.0).
|
||||
* - t = 0 returns the vector a.
|
||||
* - t = 1 returns the vector b.
|
||||
* - t between 0 and 1 returns a point between a and b.
|
||||
* @return A 3D vector representing the interpolated result between a and b.
|
||||
*/
|
||||
Vec3 vec3Lerp(Vec3 a, Vec3 b, float t);
|
||||
|
||||
/**
|
||||
* @brief Computes the cross product of two 3D vectors.
|
||||
* The cross product produces a new vector that is orthogonal (perpendicular) to both input vectors.
|
||||
* The direction of the resulting vector follows the right-hand rule.
|
||||
* @param a First vector.
|
||||
* @param b Second vector.
|
||||
* @return A 3D vector representing the cross product of vectors a and b.
|
||||
*/
|
||||
Vec3 vec3Cross(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the angle between two 3D vectors.
|
||||
* @param a First vector.
|
||||
* @param b Second vector.
|
||||
* @return The angle between vectors a and b in radians.
|
||||
*/
|
||||
float vec3Angle(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the projection of vector a onto vector b.
|
||||
* @param a The vector to be projected.
|
||||
* @param b The vector onto which a is projected.
|
||||
* @return A 3D vector representing the projection of a onto b.
|
||||
*/
|
||||
Vec3 vec3Proj(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the reflection of a vector v against a normal.
|
||||
* @param v The incident vector.
|
||||
* @param normal The normal vector of the surface.
|
||||
* @return A 3D vector representing the reflection of v across normal.
|
||||
*/
|
||||
Vec3 vec3Refl(Vec3 v, Vec3 normal);
|
||||
|
||||
/**
|
||||
* @brief Computes the Euclidean distance between two 3D vectors.
|
||||
* @param a The first vector.
|
||||
* @param b The second vector.
|
||||
* @return The scalar distance between a and b.
|
||||
*/
|
||||
float vec3Dist(Vec3 a, Vec3 b);
|
||||
|
||||
/**
|
||||
* @brief Rotates a 3D vector around a given axis by a specified angle.
|
||||
* @param v The vector to rotate.
|
||||
* @param axis The rotation axis (must be normalized).
|
||||
* @param angle Rotation angle in radians.
|
||||
* @return A 3D vector representing the rotated vector.
|
||||
*/
|
||||
Vec3 vec3Rotate(Vec3 v, Vec3 axis, float angle);
|
||||
|
||||
#endif // VEC3_H
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "math/vector4.h"
|
||||
#include "vector4.h"
|
||||
|
||||
Vec4 vec4(float x, float y, float z, float w)
|
||||
{
|
||||
|
||||
112
src/math/vector4.h
Normal file
112
src/math/vector4.h
Normal file
@@ -0,0 +1,112 @@
|
||||
#ifndef VECTOR4_H
|
||||
#define VECTOR4_H
|
||||
|
||||
typedef struct {
|
||||
float x, y, z, w;
|
||||
} Vec4;
|
||||
|
||||
/**
|
||||
* @brief Creates a new 4D vector.
|
||||
* @param x X-axis coordinate.
|
||||
* @param y Y-axis coordinate.
|
||||
* @param z Z-axis coordinate.
|
||||
* @param w W-axis coordinate.
|
||||
* @return A 4D vector with the specified coordinates.
|
||||
*/
|
||||
Vec4 vec4(float x, float y, float z, float w);
|
||||
|
||||
/**
|
||||
* @brief Adds two 4D vectors and returns a new 4D vector.
|
||||
* @param v1 First vector.
|
||||
* @param v2 Second vector.
|
||||
* @return A 4D vector representing the sum of v1 and v2.
|
||||
*/
|
||||
Vec4 vec4Add(Vec4 v1, Vec4 v2);
|
||||
|
||||
/**
|
||||
* @brief Subtracts two 4D vectors and returns a new 4D vector.
|
||||
* @param v1 First vector.
|
||||
* @param v2 Second vector.
|
||||
* @return A 4D vector representing the result of v1 minus v2.
|
||||
*/
|
||||
Vec4 vec4Sub(Vec4 v1, Vec4 v2);
|
||||
|
||||
/**
|
||||
* @brief Scales a 4D vector by a constant scalar and returns a new 4D vector.
|
||||
* @param v 4D vector.
|
||||
* @param scalar Scalar value.
|
||||
* @return A 4D vector representing the multiplication of v by the scalar.
|
||||
*/
|
||||
Vec4 vec4Scale(Vec4 v, float scalar);
|
||||
|
||||
/**
|
||||
* @brief Computes the dot product of two 4D vectors.
|
||||
* @param a First vector.
|
||||
* @param b Second vector.
|
||||
* @return A scalar value representing the dot product of a and b.
|
||||
* - scalar > 0: Both vectors have the same orientation (the angle between them is acute).
|
||||
* - scalar = 0: Vectors are orthogonal (the angle between them is 90 degrees).
|
||||
* - scalar < 0: Vectors have opposite orientations (the angle between them is obtuse).
|
||||
*/
|
||||
float vec4Dot(Vec4 a, Vec4 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the length (magnitude) of a 4D vector.
|
||||
* @param v 4D vector.
|
||||
* @return A scalar value representing the length (magnitude) of the vector v.
|
||||
*/
|
||||
float vec4Len(Vec4 v);
|
||||
|
||||
/**
|
||||
* @brief Normalizes a 4D vector (scales it to unit length).
|
||||
* @param v 4D vector.
|
||||
* @return A 4D vector representing the normalized version of v.
|
||||
* Returns a zero vector (0, 0, 0) if the input vector is a zero vector.
|
||||
*/
|
||||
Vec4 vec4Norm(Vec4 v);
|
||||
|
||||
/**
|
||||
* @brief Performs linear interpolation between two 4D vectors.
|
||||
* @param a Start vector.
|
||||
* @param b End vector.
|
||||
* @param t Interpolation factor (0.0 to 1.0).
|
||||
* - t = 0 returns the vector a.
|
||||
* - t = 1 returns the vector b.
|
||||
* - t between 0 and 1 returns a point between a and b.
|
||||
* @return A 4D vector representing the interpolated result between a and b.
|
||||
*/
|
||||
Vec4 vec4Lerp(Vec4 a, Vec4 b, float t);
|
||||
|
||||
/**
|
||||
* @brief Computes the angle between two 4D vectors.
|
||||
* @param a First vector.
|
||||
* @param b Second vector.
|
||||
* @return The angle between vectors a and b in radians.
|
||||
*/
|
||||
float vec4Angle(Vec4 a, Vec4 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the projection of vector a onto vector b.
|
||||
* @param a The vector to be projected.
|
||||
* @param b The vector onto which a is projected.
|
||||
* @return A 4D vector representing the projection of a onto b.
|
||||
*/
|
||||
Vec4 vec4Proj(Vec4 a, Vec4 b);
|
||||
|
||||
/**
|
||||
* @brief Computes the reflection of a vector v against a normal.
|
||||
* @param v The incident vector.
|
||||
* @param normal The normal vector of the surface.
|
||||
* @return A 4D vector representing the reflection of v across normal.
|
||||
*/
|
||||
Vec4 vec4Refl(Vec4 v, Vec4 normal);
|
||||
|
||||
/**
|
||||
* @brief Computes the Euclidean distance between two 4D vectors.
|
||||
* @param a The first vector.
|
||||
* @param b The second vector.
|
||||
* @return The scalar distance between a and b.
|
||||
*/
|
||||
float vec4Dist(Vec4 a, Vec4 b);
|
||||
|
||||
#endif // VECTOR4_H
|
||||
Reference in New Issue
Block a user