- add mat3, mat4, sphere.h

This commit is contained in:
acoto87
2019-04-30 22:08:17 -05:00
parent f04078dc33
commit 892a7c7dce
5 changed files with 233 additions and 7 deletions

View File

@@ -15,6 +15,9 @@ extern "C" {
#include "types-struct.h"
#include "structs/vec3.h"
#include "structs/vec4.h"
#include "structs/mat3.h"
#include "structs/mat4.h"
#include "structs/sphere.h"
#ifdef __cplusplus
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
/*
Functions:
CGLM_INLINE vec3s glm_mat3_mulv(mat3 m, vec3s v);
CGLM_INLINE float glm_mat3_rmc(vec3s r, mat3 m, vec3s c);
*/
#ifndef cglm_mat3s_h
#define cglm_mat3s_h
#include "../common.h"
#include "../types-struct.h"
#include "../mat3.h"
#include "vec3.h"
/*!
* @brief multiply mat3 with vec3 (column vector) and store in dest vector
*
* @param[in] m mat3 (left)
* @param[in] v vec3 (right, column vector)
* returns vec3 (result, column vector)
*/
CGLM_INLINE
vec3s
glms_mat3_mulv(mat3 m, vec3s v) {
vec3s r;
glm_mat3_mulv(m, v.raw, r.raw);
return r;
}
/*!
* @brief helper for R (row vector) * M (matrix) * C (column vector)
*
* rmc stands for Row * Matrix * Column
*
* the result is scalar because R * M = Matrix1x3 (row vector),
* then Matrix1x3 * Vec3 (column vector) = Matrix1x1 (Scalar)
*
* @param[in] r row vector or matrix1x3
* @param[in] m matrix3x3
* @param[in] c column vector or matrix3x1
*
* @return scalar value e.g. Matrix1x1
*/
CGLM_INLINE
float
glms_mat3_rmc(vec3s r, mat3 m, vec3s c) {
return glm_mat3_rmc(r.raw, m, c.raw);
}
#endif /* cglm_mat3s_h */

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
/*!
* Most of functions in this header are optimized manually with SIMD
* if available. You dont need to call/incude SIMD headers manually
*/
/*
Functions:
CGLM_INLINE void glm_mat4_mulv(mat4 m, vec4 v, vec4 dest);
CGLM_INLINE void glm_mat4_mulv3(mat4 m, vec3 v, vec3 dest);
CGLM_INLINE float glm_mat4_rmc(vec4 r, mat4 m, vec4 c);
*/
#ifndef cglm_mats_h
#define cglm_mats_h
#include "../common.h"
#include "../types-struct.h"
#include "../mat4.h"
#include "vec4.h"
#include "vec3.h"
/*!
* @brief multiply mat4 with vec4 (column vector) and store in dest vector
*
* @param[in] m mat4 (left)
* @param[in] v vec4 (right, column vector)
* returns vec4 (result, column vector)
*/
CGLM_INLINE
vec4s
glms_mat4_mulv(mat4 m, vec4s v) {
vec4s r;
glm_mat4_mulv(m, v.raw, r.raw);
return r;
}
/*!
* @brief multiply vector with mat4
*
* @param[in] m mat4(affine transform)
* @param[in] v vec3
* @param[in] last 4th item to make it vec4
* returns result vector (vec3)
*/
CGLM_INLINE
vec3s
glms_mat4_mulv3(mat4 m, vec3s v, float last) {
vec3s r;
glm_mat4_mulv3(m, v.raw, last, r.raw);
return r;
}
/*!
* @brief helper for R (row vector) * M (matrix) * C (column vector)
*
* rmc stands for Row * Matrix * Column
*
* the result is scalar because R * M = Matrix1x4 (row vector),
* then Matrix1x4 * Vec4 (column vector) = Matrix1x1 (Scalar)
*
* @param[in] r row vector or matrix1x4
* @param[in] m matrix4x4
* @param[in] c column vector or matrix4x1
*
* @return scalar value e.g. B(s)
*/
CGLM_INLINE
float
glms_mat4_rmc(vec4s r, mat4 m, vec4s c) {
return glm_mat4_rmc(r.raw, m, c.raw);
}
#endif /* cglm_mats_h */

View File

@@ -0,0 +1,93 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#ifndef cglm_spheres_h
#define cglm_spheres_h
#include "../common.h"
#include "../types-struct.h"
#include "../mat4.h"
#include "../sphere.h"
/*
Sphere Representation in cglm: [center.x, center.y, center.z, radii]
You could use this representation or you can convert it to vec4 before call
any function
*/
/*!
* @brief helper for getting sphere radius
*
* @param[in] s sphere
*
* @return returns radii
*/
CGLM_INLINE
float
glms_sphere_radii(vec4s s) {
return glm_sphere_radii(s.raw);
}
/*!
* @brief apply transform to sphere, it is just wrapper for glm_mat4_mulv3
*
* @param[in] s sphere
* @param[in] m transform matrix
* @returns transformed sphere
*/
CGLM_INLINE
vec4s
glms_sphere_transform(vec4s s, mat4 m) {
vec4s r;
glm_sphere_transform(s.raw, m, r.raw);
return r;
}
/*!
* @brief merges two spheres and creates a new one
*
* two sphere must be in same space, for instance if one in world space then
* the other must be in world space too, not in local space.
*
* @param[in] s1 sphere 1
* @param[in] s2 sphere 2
* returns merged/extended sphere
*/
CGLM_INLINE
vec4s
glms_sphere_merge(vec4s s1, vec4s s2) {
vec4s r;
glm_sphere_merge(s1.raw, s2.raw, r.raw);
return r;
}
/*!
* @brief check if two sphere intersects
*
* @param[in] s1 sphere
* @param[in] s2 other sphere
*/
CGLM_INLINE
bool
glms_sphere_sphere(vec4s s1, vec4s s2) {
return glm_sphere_sphere(s1.raw, s2.raw);
}
/*!
* @brief check if sphere intersects with point
*
* @param[in] s sphere
* @param[in] point point
*/
CGLM_INLINE
bool
glms_sphere_point(vec4s s, vec3s point) {
return glm_sphere_point(s.raw, point.raw);
}
#endif /* cglm_spheres_h */

View File

@@ -8,10 +8,6 @@
#ifndef cglm_vec4s_h
#define cglm_vec4s_h
#ifdef __cplusplus
extern "C" {
#endif
#include "../common.h"
#include "../types-struct.h"
#include "../util.h"
@@ -255,7 +251,4 @@ glms_vec4_lerp(vec4s from, vec4s to, float t) {
return r;
}
#ifdef __cplusplus
}
#endif
#endif /* cglm_vec4s_h */