Compare commits
10 Commits
b6a25dd41b
...
f3646609c1
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f3646609c1 | ||
![]() |
ba67d0366d | ||
![]() |
d7a13b16d1 | ||
![]() |
e3286f119d | ||
![]() |
b5f0d16412 | ||
![]() |
c426f9ab6d | ||
![]() |
203928aa81 | ||
![]() |
1b0465f6eb | ||
![]() |
a9aa97be63 | ||
![]() |
4b0875b01e |
5
CMakeLists.txt
Normal file
5
CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
|
||||||
|
project(GryphnUtils)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS "utils/*.c")
|
||||||
|
add_library(GryphnUtils STATIC ${SOURCE_FILES})
|
7
utils/gryphn_cpp_function.h
Normal file
7
utils/gryphn_cpp_function.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
#define GN_CPP_FUNCTION extern "C"
|
||||||
|
#else
|
||||||
|
#define GN_CPP_FUNCTION
|
||||||
|
#endif
|
13
utils/lists/gryphn_array_list.c
Normal file
13
utils/lists/gryphn_array_list.c
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "gryphn_array_list.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
GN_ARRAY_LIST_DEFINITION(uint32_t)
|
||||||
|
GN_ARRAY_LIST_DEFINITION(int)
|
||||||
|
|
||||||
|
|
||||||
|
void tester() {
|
||||||
|
uint32_tArrayList list = uint32_tArrayListCreate();
|
||||||
|
uint32_tArrayListResize(list, 1);
|
||||||
|
|
||||||
|
malloc(0);
|
||||||
|
}
|
@@ -1,30 +1,43 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
#include "stdlib.h"
|
|
||||||
|
|
||||||
#define GN_ARRAY_LIST(type)\
|
#define GN_ARRAY_LIST_HEADER(type)\
|
||||||
typedef struct type##ArrayList { \
|
typedef struct type##ArrayList_t* type##ArrayList; \
|
||||||
|
type##ArrayList type##ArrayListCreate(void); \
|
||||||
|
void type##ArrayListReserve(type##ArrayList list, int count); \
|
||||||
|
void type##ArrayListExpand(type##ArrayList list, int count); \
|
||||||
|
void type##ArrayListAdd(type##ArrayList list, type data); \
|
||||||
|
void type##ArrayListResize(type##ArrayList list, int count); \
|
||||||
|
void type##ArrayListRemove(type##ArrayList list); \
|
||||||
|
void type##ArrayListPopHead(type##ArrayList list); \
|
||||||
|
uint32_t type##ArrayListCount(type##ArrayList list); \
|
||||||
|
type type##ArrayListAt(type##ArrayList list, int i); \
|
||||||
|
type* type##ArrayListRefAt(type##ArrayList list, int i); \
|
||||||
|
type* type##ArrayListData(type##ArrayList list)
|
||||||
|
|
||||||
|
#define GN_ARRAY_LIST_DEFINITION(type)\
|
||||||
|
typedef struct type##ArrayList_t { \
|
||||||
uint32_t count; \
|
uint32_t count; \
|
||||||
uint32_t maxSize; \
|
uint32_t maxSize; \
|
||||||
type* data; \
|
type* data; \
|
||||||
} type##ArrayList; \
|
} type##ArrayList_t; \
|
||||||
inline static type##ArrayList type##ArrayListCreate() { \
|
type##ArrayList type##ArrayListCreate(void) { \
|
||||||
type##ArrayList list;\
|
type##ArrayList list = malloc(sizeof(type##ArrayList_t));\
|
||||||
list.maxSize = 1; \
|
list->maxSize = 1; \
|
||||||
list.count = 0;\
|
list->count = 0;\
|
||||||
list.data = malloc(sizeof(type) * list.maxSize); \
|
list->data = malloc(sizeof(type) * list->maxSize); \
|
||||||
return list; \
|
return list; \
|
||||||
}\
|
}\
|
||||||
inline static void type##ArrayListReserve(type##ArrayList* list, int count) { \
|
void type##ArrayListReserve(type##ArrayList list, int count) { \
|
||||||
int newCount = count - (list->maxSize - list->count);\
|
int newCount = count - (list->maxSize - list->count);\
|
||||||
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
|
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
|
||||||
list->maxSize += newCount; \
|
list->maxSize += newCount; \
|
||||||
}\
|
}\
|
||||||
inline static void type##ArrayListExpand(type##ArrayList* list, int count) { \
|
void type##ArrayListExpand(type##ArrayList list, int count) { \
|
||||||
list->data = realloc(list->data, sizeof(type) * (count + list->maxSize)); \
|
list->data = realloc(list->data, sizeof(type) * (count + list->maxSize)); \
|
||||||
list->maxSize += count; \
|
list->maxSize += count; \
|
||||||
}\
|
}\
|
||||||
inline static void type##ArrayListAdd(type##ArrayList* list, type data) { \
|
void type##ArrayListAdd(type##ArrayList list, type data) { \
|
||||||
if (list->count >= list->maxSize) {\
|
if (list->count >= list->maxSize) {\
|
||||||
list->maxSize *= 2; \
|
list->maxSize *= 2; \
|
||||||
list->data = realloc(list->data, sizeof(type) * list->maxSize); \
|
list->data = realloc(list->data, sizeof(type) * list->maxSize); \
|
||||||
@@ -32,27 +45,36 @@ if (list->count >= list->maxSize) {\
|
|||||||
list->data[list->count] = data;\
|
list->data[list->count] = data;\
|
||||||
list->count++;\
|
list->count++;\
|
||||||
}\
|
}\
|
||||||
inline static void type##ArrayListResize(type##ArrayList* list, int count) { \
|
void type##ArrayListResize(type##ArrayList list, int count) { \
|
||||||
int newCount = count - (list->maxSize - list->count);\
|
int newCount = count - (list->maxSize - list->count);\
|
||||||
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
|
list->data = realloc(list->data, sizeof(type) * (newCount + list->maxSize)); \
|
||||||
list->maxSize += newCount; \
|
list->maxSize += newCount; \
|
||||||
list->count += count; \
|
list->count += count; \
|
||||||
} \
|
} \
|
||||||
inline static void type##ArrayListRemove(type##ArrayList* list) { \
|
void type##ArrayListRemove(type##ArrayList list) { \
|
||||||
if (list->count == 0) return; \
|
if (list->count == 0) return; \
|
||||||
list->count--; \
|
list->count--; \
|
||||||
} \
|
} \
|
||||||
inline static void type##ArrayListPopHead(type##ArrayList* list) { \
|
void type##ArrayListPopHead(type##ArrayList list) { \
|
||||||
if (list->count == 0) return; \
|
if (list->count == 0) return; \
|
||||||
list->count--; \
|
list->count--; \
|
||||||
for (int i = 0; i < list->count; i++) { \
|
for (uint32_t i = 0; i < list->count; i++) { \
|
||||||
list->data[i] = list->data[i + 1];\
|
list->data[i] = list->data[i + 1];\
|
||||||
} \
|
} \
|
||||||
|
} \
|
||||||
|
uint32_t type##ArrayListCount(type##ArrayList list) { \
|
||||||
|
return list->count; \
|
||||||
|
} \
|
||||||
|
type type##ArrayListAt(type##ArrayList list, int i) { \
|
||||||
|
return list->data[i]; \
|
||||||
|
} \
|
||||||
|
type* type##ArrayListRefAt(type##ArrayList list, int i) { \
|
||||||
|
return &list->data[i]; \
|
||||||
|
} \
|
||||||
|
type* type##ArrayListData(type##ArrayList list) { \
|
||||||
|
return &list->data[0]; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GN_ARRAY_LIST_HEADER(uint32_t);
|
||||||
//metalAvaliableTextureArrayListPopHead
|
GN_ARRAY_LIST_HEADER(int);
|
||||||
|
|
||||||
GN_ARRAY_LIST(int);
|
|
||||||
GN_ARRAY_LIST(uint32_t);
|
|
||||||
|
122
utils/math/gryphn_mat4.c
Normal file
122
utils/math/gryphn_mat4.c
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
#include "gryphn_mat4.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
gnMat4x4 gnIdentity(void) {
|
||||||
|
return (gnMat4x4){
|
||||||
|
.mat = {
|
||||||
|
{ 1.0f, 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 1.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 1.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
gnMat4x4 gnOrthographic(
|
||||||
|
float left, float right, float top, float bottom, float near, float far
|
||||||
|
) {
|
||||||
|
return (gnMat4x4){
|
||||||
|
.mat = {
|
||||||
|
{ (2)/(right-left), 0.0f, 0.0f, -((right+left)/(right-left)) },
|
||||||
|
{ 0.0f, 2/(top-bottom), 0.0f, -((top+bottom)/(top-bottom)) },
|
||||||
|
{ 0.0f, 0.0f, -2/(far-near), -((far+near)/(far-near)) },
|
||||||
|
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
gnMat4x4 gnProjection(
|
||||||
|
float fov, float aspect, float near, float far
|
||||||
|
) {
|
||||||
|
float f = 1.0f / tan(fov * 0.5f);
|
||||||
|
|
||||||
|
return (gnMat4x4){
|
||||||
|
.mat = {
|
||||||
|
{ f / aspect, 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f , f , 0.0f, 0.0f },
|
||||||
|
{ 0.0f , 0.0f, (far + near) / (near - far), -1.0f },
|
||||||
|
{ 0.0f , 0.0f, (2 * far * near) / (near - far), 0.0f }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
gnMat4x4 gnTranslate(gnVec3 translation) {
|
||||||
|
return (gnMat4x4) {
|
||||||
|
.mat = {
|
||||||
|
{ 1.0f, 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 1.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 1.0f, 0.0f },
|
||||||
|
{ translation.x, translation.y, translation.z, 1.0f }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// this function was heavily adapted from GLMs implementation
|
||||||
|
gnMat4x4 gnRotate(gnVec3 axis, float rotation) {
|
||||||
|
float c = cos(rotation), s = sin(rotation);
|
||||||
|
|
||||||
|
axis = gnVec3Normalize(axis);
|
||||||
|
gnVec3 temp = {
|
||||||
|
.x = ((1.0f - c) * axis.x),
|
||||||
|
.y = ((1.0f - c) * axis.y),
|
||||||
|
.z = ((1.0f - c) * axis.z)
|
||||||
|
};
|
||||||
|
|
||||||
|
gnMat4 rotate = {
|
||||||
|
.mat = {
|
||||||
|
{ c + temp.x * axis.x, temp.x * axis.y + s * axis.z, temp.x * axis.z - s * axis.y, 0.0f },
|
||||||
|
{ temp.y * axis.x - s * axis.z, c + temp.y * axis.y, temp.y * axis.z + s * axis.x, 0.0f },
|
||||||
|
{ temp.z * axis.x + s * axis.y, temp.z * axis.y - s * axis.x, c + temp.z * axis.z, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.0f, 1.0f },
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return rotate;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnMat4x4 gnScale(gnVec3 amount) {
|
||||||
|
gnMat4 rotate = {
|
||||||
|
.mat = {
|
||||||
|
{ amount.x, 0.0f, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, amount.y, 0.0f, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, amount.z, 0.0f },
|
||||||
|
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return rotate;
|
||||||
|
}
|
||||||
|
|
||||||
|
// used GLMs lookAtLH from /glm/ext/matrix_transform.inl as a base
|
||||||
|
gnMat4x4 gnLookAt(gnVec3 eye, gnVec3 center, gnVec3 up) {
|
||||||
|
const gnVec3 f = gnVec3Normalize(gnVec3Subtract(center, eye));
|
||||||
|
const gnVec3 s = gnVec3Normalize(gnVec3Cross(f, up));
|
||||||
|
const gnVec3 u = gnVec3Cross(s, f);
|
||||||
|
|
||||||
|
return (gnMat4x4) {
|
||||||
|
.mat = {
|
||||||
|
{ s.x, u.x, -f.x, 0.0f },
|
||||||
|
{ s.y, u.y, -f.y, 0.0f },
|
||||||
|
{ s.z, u.z, -f.z, 0.0f },
|
||||||
|
{ -gnVec3Dot(s, eye), -gnVec3Dot(u, eye), gnVec3Dot(f, eye), 1.0f }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
gnMat4x4 gnMultiply(gnMat4 a, gnMat4 b) {
|
||||||
|
gnMat4 result;
|
||||||
|
|
||||||
|
for (int row = 0; row < 4; ++row)
|
||||||
|
for (int col = 0; col < 4; ++col)
|
||||||
|
result.mat[row][col] = a.mat[row][0] * b.mat[0][col] +
|
||||||
|
a.mat[row][1] * b.mat[1][col] +
|
||||||
|
a.mat[row][2] * b.mat[2][col] +
|
||||||
|
a.mat[row][3] * b.mat[3][col];
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
gnVec3 gnMat4MultiplyVec3(gnMat4 m, gnVec3 v) {
|
||||||
|
gnVec3 result;
|
||||||
|
result.x = m.mat[0][0] * v.x + m.mat[1][0] * v.y + m.mat[2][0] * v.z + m.mat[3][0];
|
||||||
|
result.y = m.mat[0][1] * v.x + m.mat[1][1] * v.y + m.mat[2][1] * v.z + m.mat[3][1];
|
||||||
|
result.z = m.mat[0][2] * v.x + m.mat[1][2] * v.y + m.mat[2][2] * v.z + m.mat[3][2];
|
||||||
|
return result;
|
||||||
|
}
|
@@ -1,5 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "math.h"
|
|
||||||
#include "gryphn_vec3.h"
|
#include "gryphn_vec3.h"
|
||||||
|
|
||||||
typedef struct gnMat4x4 {
|
typedef struct gnMat4x4 {
|
||||||
@@ -7,123 +6,12 @@ typedef struct gnMat4x4 {
|
|||||||
} gnMat4x4;
|
} gnMat4x4;
|
||||||
typedef gnMat4x4 gnMat4;
|
typedef gnMat4x4 gnMat4;
|
||||||
|
|
||||||
static inline const gnMat4x4 gnIdentity() {
|
gnMat4x4 gnIdentity(void);
|
||||||
return (gnMat4x4){
|
gnMat4x4 gnOrthographic(float left, float right, float top, float bottom, float near, float far);
|
||||||
.mat = {
|
gnMat4x4 gnProjection(float fov, float aspect, float near, float far);
|
||||||
{ 1.0f, 0.0f, 0.0f, 0.0f },
|
gnMat4x4 gnTranslate(gnVec3 translation);
|
||||||
{ 0.0f, 1.0f, 0.0f, 0.0f },
|
gnMat4x4 gnRotate(gnVec3 axis, float rotation);
|
||||||
{ 0.0f, 0.0f, 1.0f, 0.0f },
|
gnMat4x4 gnScale(gnVec3 amount);
|
||||||
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
gnMat4x4 gnLookAt(gnVec3 eye, gnVec3 center, gnVec3 up);
|
||||||
}
|
gnMat4x4 gnMultiply(gnMat4 a, gnMat4 b);
|
||||||
};
|
gnVec3 gnMat4MultiplyVec3(gnMat4 m, gnVec3 v);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static inline const gnMat4x4 gnOrthographic(
|
|
||||||
float left, float right, float top, float bottom, float near, float far
|
|
||||||
) {
|
|
||||||
return (gnMat4x4){
|
|
||||||
.mat = {
|
|
||||||
{ (2)/(right-left), 0.0f, 0.0f, -((right+left)/(right-left)) },
|
|
||||||
{ 0.0f, 2/(top-bottom), 0.0f, -((top+bottom)/(top-bottom)) },
|
|
||||||
{ 0.0f, 0.0f, -2/(far-near), -((far+near)/(far-near)) },
|
|
||||||
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const gnMat4x4 gnProjection(
|
|
||||||
float fov, float aspect, float near, float far
|
|
||||||
) {
|
|
||||||
float f = 1.0f / tan(fov * 0.5f);
|
|
||||||
|
|
||||||
return (gnMat4x4){
|
|
||||||
.mat = {
|
|
||||||
{ f / aspect, 0.0f, 0.0f, 0.0f },
|
|
||||||
{ 0.0f , f , 0.0f, 0.0f },
|
|
||||||
{ 0.0f , 0.0f, (far + near) / (near - far), -1.0f },
|
|
||||||
{ 0.0f , 0.0f, (2 * far * near) / (near - far), 0.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const gnMat4x4 gnTranslate(gnVec3 translation) {
|
|
||||||
return (gnMat4x4) {
|
|
||||||
.mat = {
|
|
||||||
{ 1.0f, 0.0f, 0.0f, 0.0f },
|
|
||||||
{ 0.0f, 1.0f, 0.0f, 0.0f },
|
|
||||||
{ 0.0f, 0.0f, 1.0f, 0.0f },
|
|
||||||
{ translation.x, translation.y, translation.z, 1.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const gnMat4x4 gnRotate(gnVec3 axis, float rotation) {
|
|
||||||
float c = cos(rotation), s = sin(rotation);
|
|
||||||
|
|
||||||
axis = gnVec3Normalize(axis);
|
|
||||||
gnVec3 temp = {
|
|
||||||
((1.0f - c) * axis.x),
|
|
||||||
((1.0f - c) * axis.y),
|
|
||||||
((1.0f - c) * axis.z)
|
|
||||||
};
|
|
||||||
|
|
||||||
gnMat4 rotate = {
|
|
||||||
.mat = {
|
|
||||||
{ c + temp.x * axis.x, temp.x * axis.y + s * axis.z, temp.x * axis.z - s * axis.y, 0.0f },
|
|
||||||
{ temp.y * axis.x - s * axis.z, c + temp.y * axis.y, temp.y * axis.z + s * axis.x, 0.0f },
|
|
||||||
{ temp.z * axis.x + s * axis.y, temp.z * axis.y - s * axis.x, c + temp.z * axis.z, 0.0f },
|
|
||||||
{ 0.0f, 0.0f, 0.0f, 1.0f },
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return rotate;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const gnMat4x4 gnScale(gnVec3 amount) {
|
|
||||||
gnMat4 rotate = {
|
|
||||||
.mat = {
|
|
||||||
{ amount.x, 0.0f, 0.0f, 0.0f },
|
|
||||||
{ 0.0f, amount.y, 0.0f, 0.0f },
|
|
||||||
{ 0.0f, 0.0f, amount.z, 0.0f },
|
|
||||||
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
return rotate;
|
|
||||||
}
|
|
||||||
|
|
||||||
// used GLMs lookAtLH from /glm/ext/matrix_transform.inl as a base
|
|
||||||
static inline const gnMat4x4 gnLookAt(gnVec3 eye, gnVec3 center, gnVec3 up) {
|
|
||||||
const gnVec3 f = gnVec3Normalize(gnVec3Subtract(center, eye));
|
|
||||||
const gnVec3 s = gnVec3Normalize(gnVec3Cross(f, up));
|
|
||||||
const gnVec3 u = gnVec3Cross(s, f);
|
|
||||||
|
|
||||||
return (gnMat4x4) {
|
|
||||||
.mat = {
|
|
||||||
{ s.x, u.x, -f.x, 0.0f },
|
|
||||||
{ s.y, u.y, -f.y, 0.0f },
|
|
||||||
{ s.z, u.z, -f.z, 0.0f },
|
|
||||||
{ -gnVec3Dot(s, eye), -gnVec3Dot(u, eye), gnVec3Dot(f, eye), 1.0f }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const gnMat4x4 gnMultiply(gnMat4 a, gnMat4 b) {
|
|
||||||
gnMat4 result;
|
|
||||||
|
|
||||||
for (int row = 0; row < 4; ++row)
|
|
||||||
for (int col = 0; col < 4; ++col)
|
|
||||||
result.mat[row][col] = a.mat[row][0] * b.mat[0][col] +
|
|
||||||
a.mat[row][1] * b.mat[1][col] +
|
|
||||||
a.mat[row][2] * b.mat[2][col] +
|
|
||||||
a.mat[row][3] * b.mat[3][col];
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline const gnVec3 gnMat4MultiplyVec3(gnMat4 m, gnVec3 v) {
|
|
||||||
gnVec3 result;
|
|
||||||
result.x = m.mat[0][0] * v.x + m.mat[1][0] * v.y + m.mat[2][0] * v.z + m.mat[3][0];
|
|
||||||
result.y = m.mat[0][1] * v.x + m.mat[1][1] * v.y + m.mat[2][1] * v.z + m.mat[3][1];
|
|
||||||
result.z = m.mat[0][2] * v.x + m.mat[1][2] * v.y + m.mat[2][2] * v.z + m.mat[3][2];
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
4
utils/math/gryphn_math.c
Normal file
4
utils/math/gryphn_math.c
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include "gryphn_math.h"
|
||||||
|
|
||||||
|
float gnRadians(const float degrees) { return degrees * (3.14159265358979323846f / 180.0f); }
|
||||||
|
float gnRoot(const float input, const float n) { return pow(input, 1./n); }
|
@@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
static const inline float gnRadians(const float degrees) { return degrees * (3.14159265358979323846f / 180.0f); }
|
float gnRadians(const float degrees);
|
||||||
static const inline float gnRoot(const float input, const float n) { return pow(input, 1./n); }
|
float gnRoot(const float input, const float n);
|
||||||
|
8
utils/math/gryphn_vec2.c
Normal file
8
utils/math/gryphn_vec2.c
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#include "gryphn_vec2.h"
|
||||||
|
|
||||||
|
gnVec2 gnVec2Subtract(gnVec2 a, gnVec2 b) {
|
||||||
|
gnVec2 ret;
|
||||||
|
ret.x = a.x - b.x;
|
||||||
|
ret.y = a.y - b.y;
|
||||||
|
return ret;
|
||||||
|
}
|
@@ -6,52 +6,16 @@ typedef struct gnVec2 {
|
|||||||
struct { float a, b; };
|
struct { float a, b; };
|
||||||
struct { float x, y; };
|
struct { float x, y; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GN_UTILS_CPP
|
|
||||||
gnVec2(float x, float y) { this->x = x; this->y = y; }
|
|
||||||
gnVec2(float s) { this->x = s; this->y = s; }
|
|
||||||
gnVec2() {};
|
|
||||||
|
|
||||||
gnVec2 operator-(const gnVec2& other) {
|
|
||||||
gnVec2 returnGnVec2;
|
|
||||||
returnGnVec2.x = this->x - other.x;
|
|
||||||
returnGnVec2.y = this->y - other.y;
|
|
||||||
return returnGnVec2;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const gnVec2& other) const {
|
|
||||||
return this->a == other.a && this->b == other.b;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} gnVec2;
|
} gnVec2;
|
||||||
|
|
||||||
static inline const gnVec2 gnVec2Subtract(gnVec2 a, gnVec2 b) { return (gnVec2){ a.x - b.x, a.y - b.y }; }
|
|
||||||
|
|
||||||
typedef gnVec2 gnFVec2;
|
typedef gnVec2 gnFVec2;
|
||||||
typedef gnVec2 gnFloat2;
|
typedef gnVec2 gnFloat2;
|
||||||
|
gnVec2 gnVec2Subtract(gnVec2 a, gnVec2 b);
|
||||||
|
|
||||||
typedef struct gnUInt2 {
|
typedef struct gnUInt2 {
|
||||||
union {
|
union {
|
||||||
struct { uint32_t a, b; };
|
struct { uint32_t a, b; };
|
||||||
struct { uint32_t x, y; };
|
struct { uint32_t x, y; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GN_UTILS_CPP
|
|
||||||
gnUInt2(uint32_t x, uint32_t y) { this->x = x; this->y = y; }
|
|
||||||
gnUInt2(uint32_t s) { this->x = s; this->y = s; }
|
|
||||||
gnUInt2() {};
|
|
||||||
|
|
||||||
gnUInt2 operator-(const gnUInt2& other) {
|
|
||||||
gnUInt2 returnGnVec2;
|
|
||||||
returnGnVec2.x = this->x - other.x;
|
|
||||||
returnGnVec2.y = this->y - other.y;
|
|
||||||
return returnGnVec2;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const gnUInt2& other) const {
|
|
||||||
return this->a == other.a && this->b == other.b;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} gnUInt2;
|
} gnUInt2;
|
||||||
|
|
||||||
typedef struct gnInt2 {
|
typedef struct gnInt2 {
|
||||||
@@ -59,21 +23,4 @@ typedef struct gnInt2 {
|
|||||||
struct { int a, b; };
|
struct { int a, b; };
|
||||||
struct { int x, y; };
|
struct { int x, y; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GN_UTILS_CPP
|
|
||||||
gnInt2(int x, int y) { this->x = x; this->y = y; }
|
|
||||||
gnInt2(int s) { this->x = s; this->y = s; }
|
|
||||||
gnInt2() {};
|
|
||||||
|
|
||||||
gnInt2 operator-(const gnInt2& other) {
|
|
||||||
gnInt2 returnGnVec2;
|
|
||||||
returnGnVec2.x = this->x - other.x;
|
|
||||||
returnGnVec2.y = this->y - other.y;
|
|
||||||
return returnGnVec2;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const gnInt2& other) const {
|
|
||||||
return this->a == other.a && this->b == other.b;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} gnInt2;
|
} gnInt2;
|
||||||
|
36
utils/math/gryphn_vec3.c
Normal file
36
utils/math/gryphn_vec3.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include "gryphn_vec3.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b) { return (gnVec3){
|
||||||
|
.x = a.x - b.x,
|
||||||
|
.y = a.y - b.y,
|
||||||
|
.z = a.z - b.z };
|
||||||
|
}
|
||||||
|
gnVec3 gnVec3Add(gnVec3 a, gnVec3 b) { return (gnVec3){
|
||||||
|
.x = a.x + b.x,
|
||||||
|
.y = a.y + b.y,
|
||||||
|
.z = a.z + b.z };
|
||||||
|
}
|
||||||
|
gnVec3 gnVec3Normalize(gnVec3 in) {
|
||||||
|
float lengthOfVector = sqrt((in.x * in.x) + (in.y * in.y) + (in.z * in.z));
|
||||||
|
return (gnVec3){
|
||||||
|
.a = in.x / lengthOfVector,
|
||||||
|
.b = in.y / lengthOfVector,
|
||||||
|
.c = in.z / lengthOfVector};
|
||||||
|
}
|
||||||
|
gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b) { return (gnVec3){
|
||||||
|
.x = a.y * b.z - a.z * b.y,
|
||||||
|
.y = a.z * b.x - a.x * b.z,
|
||||||
|
.z = a.x * b.y - a.y * b.x};
|
||||||
|
}
|
||||||
|
float gnVec3Dot(gnVec3 a, gnVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
|
||||||
|
gnVec3 gnVec3Multiply(gnVec3 a, gnVec3 b) { return (gnVec3){
|
||||||
|
.x = a.x * b.x,
|
||||||
|
.y = a.y * b.y,
|
||||||
|
.z = a.z * b.z };
|
||||||
|
}
|
||||||
|
gnVec3 gnVec3MultiplyBy(gnVec3 a, float b) { return (gnVec3){
|
||||||
|
.x = a.x * b,
|
||||||
|
.y= a.y * b,
|
||||||
|
.z = a.z * b };
|
||||||
|
}
|
@@ -1,42 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "stdint.h"
|
#include "stdint.h"
|
||||||
#include "math.h"
|
|
||||||
|
|
||||||
typedef struct gnVec3 {
|
typedef struct gnVec3 {
|
||||||
union {
|
union {
|
||||||
struct { float a, b, c; };
|
struct { float a, b, c; };
|
||||||
struct { float x, y, z; };
|
struct { float x, y, z; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GN_UTILS_CPP
|
|
||||||
gnVec3(float x, float y, float z) { this->x = x; this->y = y; this->z = z; }
|
|
||||||
gnVec3(float s) { this->x = s; this->y = s; this->z = s; }
|
|
||||||
gnVec3() {};
|
|
||||||
|
|
||||||
gnVec3 operator-(const gnVec3& other) {
|
|
||||||
gnVec3 returnGnVec3;
|
|
||||||
returnGnVec3.x = this->x - other.x;
|
|
||||||
returnGnVec3.y = this->y - other.y;
|
|
||||||
returnGnVec3.z = this->z - other.z;
|
|
||||||
return returnGnVec3;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const gnVec3& other) const {
|
|
||||||
return this->a == other.a && this->b == other.b && this->c == other.c;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} gnVec3;
|
} gnVec3;
|
||||||
|
|
||||||
static const inline gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x - b.x, a.y - b.y, a.z - b.z }; }
|
gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b);
|
||||||
static const inline gnVec3 gnVec3Add(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x + b.x, a.y + b.y, a.z + b.z }; }
|
gnVec3 gnVec3Add(gnVec3 a, gnVec3 b);
|
||||||
static const inline gnVec3 gnVec3Normalize(gnVec3 in) {
|
gnVec3 gnVec3Normalize(gnVec3 in);
|
||||||
float lengthOfVector = sqrt((in.x * in.x) + (in.y * in.y) + (in.z * in.z));
|
gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b);
|
||||||
return (gnVec3){in.x / lengthOfVector, in.y / lengthOfVector, in.z / lengthOfVector};
|
float gnVec3Dot(gnVec3 a, gnVec3 b);
|
||||||
}
|
gnVec3 gnVec3Multiply(gnVec3 a, gnVec3 b);
|
||||||
static const inline gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b) { return (gnVec3){a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
|
gnVec3 gnVec3MultiplyBy(gnVec3 a, float b);
|
||||||
static const inline float gnVec3Dot(gnVec3 a, gnVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
|
|
||||||
static const inline gnVec3 gnVec3Multiply(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x * b.x, a.y * b.y, a.z * b.z }; }
|
|
||||||
static const inline gnVec3 gnVec3MultiplyBy(gnVec3 a, float b) { return (gnVec3){ a.x * b, a.y * b, a.z * b }; }
|
|
||||||
|
|
||||||
typedef gnVec3 gnFVec3;
|
typedef gnVec3 gnFVec3;
|
||||||
typedef gnVec3 gnFloat3;
|
typedef gnVec3 gnFloat3;
|
||||||
@@ -47,24 +25,6 @@ typedef struct gnUInt3 {
|
|||||||
struct { uint32_t x, y, z; };
|
struct { uint32_t x, y, z; };
|
||||||
struct { uint32_t width, height, depth; };
|
struct { uint32_t width, height, depth; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GN_UTILS_CPP
|
|
||||||
gnUInt3(uint32_t x, uint32_t y, uint32_t z) { this->x = x; this->y = y; this->z = z; }
|
|
||||||
gnUInt3(uint32_t s) { this->x = s; this->y = s; this->z = s; }
|
|
||||||
gnUInt3() {};
|
|
||||||
|
|
||||||
gnUInt3 operator-(const gnUInt3& other) {
|
|
||||||
gnUInt3 returnGnVec3;
|
|
||||||
returnGnVec3.x = this->x - other.x;
|
|
||||||
returnGnVec3.y = this->y - other.y;
|
|
||||||
returnGnVec3.z = this->z - other.z;
|
|
||||||
return returnGnVec3;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const gnUInt3& other) const {
|
|
||||||
return this->a == other.a && this->b == other.b && this->c == other.c;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} gnUInt3;
|
} gnUInt3;
|
||||||
|
|
||||||
typedef gnUInt3 gnUVec3;
|
typedef gnUInt3 gnUVec3;
|
||||||
@@ -75,22 +35,4 @@ typedef struct gnInt3 {
|
|||||||
struct { int a, b, c; };
|
struct { int a, b, c; };
|
||||||
struct { int x, y, z; };
|
struct { int x, y, z; };
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef GN_UTILS_CPP
|
|
||||||
gnInt3(int x, int y, int z) { this->x = x; this->y = y; this->z = z; }
|
|
||||||
gnInt3(int s) { this->x = s; this->y = s; this->z = s; }
|
|
||||||
gnInt3() {};
|
|
||||||
|
|
||||||
gnInt3 operator-(const gnInt3& other) {
|
|
||||||
gnInt3 returnGnVec3;
|
|
||||||
returnGnVec3.x = this->x - other.x;
|
|
||||||
returnGnVec3.y = this->y - other.y;
|
|
||||||
returnGnVec3.z = this->z - other.z;
|
|
||||||
return returnGnVec3;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const gnInt3& other) const {
|
|
||||||
return this->a == other.a && this->b == other.b && this->c == other.c;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
} gnInt3;
|
} gnInt3;
|
||||||
|
Reference in New Issue
Block a user