Files
RedScarfEngine/graphics/src/rse_math.h
T
Piotr Krygier bf31b96a2e Add my own math library
GLM works only for C++ and I plan on moving to back to C. There is
a C port, but it would be good for me to figure out vertex math myself.
Hence, my own library has been created.
2023-08-10 09:43:26 +02:00

120 lines
2.7 KiB
C

/**
* @file rse_math.h
* @author Piotr Krygier (piotr.krygier@)
* @brief RedScarfEngine math operations
* @version 0.1
* @date 2023-08-07
*
* @copyright Copyright (c) 2023
*
*/
#ifndef RSE_MATH_H
#define RSE_MATH_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Simple representation of vector
*
*/
struct vec3_t {
float x;
float y;
float z;
};
/* 4x4 matrix.
* [x_1 | x_2 | x_3 | x_4]
* [y_1 | y_2 | y_3 | y_4]
* [z_1 | x_2 | z_3 | z_4]
* [z_1 | w_2 | w_3 | w_4]
*
* Vulkan and OpenGL expects matrix to be passed by columns, this is why members are created in this order.
* */
struct mat4_t {
/* First column */
float x_1;
float y_1;
float z_1;
float w_1;
/* Second column */
float x_2;
float y_2;
float z_2;
float w_2;
/* Third column */
float x_3;
float y_3;
float z_3;
float w_3;
/* Fourth column */
float x_4;
float y_4;
float z_4;
float w_4;
};
/**
* @brief Calculate radians from provided degree
*
* @param degrees degrees
* @return float radians
*/
float rse_math_deg_to_radians(float degrees);
/**
* @brief Create an uniform matrix, the one with ones on diagonal
*
* @return struct mat4_t Diagonal matrix
*/
struct mat4_t rse_math_uniform_mat4();
/**
* @brief Normalizes provided vector. Changes the values inside the vector to range 0.0...1.0
*
* @param vec Vector to normalize
* @return struct vec3_t Normalized vector
*/
struct vec3_t rse_math_normalize_vec3(const struct vec3_t* vec);
/**
* @brief Creates vector cross product, a vector, using right hand rule. Note, that order of vectors is important
*
* @param vec1 Input vector
* @param vec2 Input vector
* @return struct vec3_t Resulting cross product vector
*/
struct vec3_t rse_math_cross_vec3(const struct vec3_t* vec1, const struct vec3_t* vec2);
/**
* @brief Calculates transformation matrix for viewing object from and eye cooridinates
*
* @param eye Location of the camera or the eye
* @param center Where are we looking at
* @param up Where "up" is located. Usually (0, 1, 0), since Y is up in most cases
* @return struct mat4_t Resulting lootAtMatrix
*/
struct mat4_t rse_math_look_at(const struct vec3_t* eye, const struct vec3_t* center, const struct vec3_t* up);
/**
* @brief Creates perspective matrix, used in projection
*
* @param fovy_rad Field of view, in radians
* @param aspect_ratio Aspect ratio (usally screen width / height)
* @param z_near How near objects we can see
* @param z_far How far objects we can see
* @return struct mat4_t Resulting perspective matrix
*/
struct mat4_t rse_math_perspective(float fovy_rad, float aspect_ratio, float z_near, float z_far);
#ifdef __cplusplus
}
#endif
#endif /* RSE_MATH_H */