/** * @file math.h * @author Piotr Krygier (piotr.krygier@) * @brief RedScarfEngine math operations * @version 0.1 * @date 2023-08-07 * * @copyright Copyright (c) 2023 * */ #ifndef MATH_H #define MATH_H /** * @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 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 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 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 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 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 math_perspective(float fovy_rad, float aspect_ratio, float z_near, float z_far); /** * @brief Rotate matrix around vector for radians degrees * * @param matrix Matrix to rotate * @param radians Degrees * @param rotation_vector Rotation vector * @return struct mat4_t resulting matrix */ struct mat4_t math_rotate(struct mat4_t matrix, float radians, struct vec3_t rotation_vector); #endif /* MATH_H */