From 59652c75b43d0437217c0000b03428545905801e Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 20 Mar 2017 20:34:44 +0100 Subject: Review some comments --- src/gestures.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/gestures.h') diff --git a/src/gestures.h b/src/gestures.h index 99f49d2a..42ced889 100644 --- a/src/gestures.h +++ b/src/gestures.h @@ -1,6 +1,6 @@ /********************************************************************************************** * -* raylib Gestures System - Gestures Processing based on input gesture events (touch/mouse) +* raylib.gestures - Gestures system, gestures processing based on input events (touch/mouse) * * NOTE: Memory footprint of this library is aproximately 128 bytes (global variables) * @@ -24,7 +24,7 @@ * * LICENSE: zlib/libpng * -* Copyright (c) 2014-2016 Ramon Santamaria (@raysan5) +* Copyright (c) 2014-2017 Ramon Santamaria (@raysan5) * * This software is provided "as-is", without any express or implied warranty. In no event * will the authors be held liable for any damages arising from the use of this software. -- cgit v1.2.3 From 9875198a56263b5e282c016c67221ddfcfb51d31 Mon Sep 17 00:00:00 2001 From: RDR8 Date: Fri, 24 Mar 2017 01:20:24 -0500 Subject: c99 fix, some linux housekeeping --- examples/Makefile | 31 +++++++++++++++---------------- src/Makefile | 16 ++++++++++------ src/core.c | 10 +++++----- src/gestures.h | 4 ++-- src/physac.h | 6 +++--- 5 files changed, 35 insertions(+), 32 deletions(-) (limited to 'src/gestures.h') diff --git a/examples/Makefile b/examples/Makefile index 98129990..e271355d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -40,7 +40,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP) else UNAMEOS:=$(shell uname) ifeq ($(UNAMEOS),Linux) - PLATFORM_OS=LINUX + PLATFORM_OS=linux LIBPATH=linux else ifeq ($(UNAMEOS),Darwin) @@ -73,7 +73,9 @@ endif ifeq ($(PLATFORM),PLATFORM_RPI) CFLAGS = -O2 -s -Wall -std=gnu99 -fgnu89-inline else - CFLAGS = -O2 -s -Wall -std=c99 + ifeq ($(PLATFORM_OS),LINUX) + CFLAGS = -O2 -s -Wall -std=c99 --D_DEFAULT_SOURCE + endif endif ifeq ($(PLATFORM),PLATFORM_WEB) CFLAGS = -O1 -Wall -std=c99 -s USE_GLFW=3 -s ASSERTIONS=1 --preload-file resources @@ -88,7 +90,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS) RAYLIB_PATH = ../release/win32/mingw32 endif - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) RAYLIB_PATH = ../release/linux endif ifeq ($(PLATFORM_OS),OSX) @@ -110,7 +112,7 @@ ifeq ($(PLATFORM),PLATFORM_RPI) endif ifeq ($(PLATFORM),PLATFORM_DESKTOP) # add standard directories for GNU/Linux - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) INCLUDES += -I/usr/local/include/raylib/ else ifeq ($(PLATFORM_OS),WINDOWS) # external libraries headers @@ -141,7 +143,7 @@ endif # define any libraries to link into executable # if you want to link libraries (libname.so or libname.a), use the -lname ifeq ($(PLATFORM),PLATFORM_DESKTOP) - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) # libraries for Debian GNU/Linux desktop compiling # requires the following packages: # libglfw3-dev libopenal-dev libegl1-mesa-dev @@ -185,6 +187,11 @@ ifeq ($(PLATFORM_OS),WINDOWS) WINFLAGS = ../src/resources -Wl,--subsystem,windows endif +# Linux Fix to timespect from +ifeq ($(PLATFORM_OS),linux) + CFLAGS += -D_DEFAULT_SOURCE + endif + ifeq ($(PLATFORM),PLATFORM_WEB) EXT = .html endif @@ -207,7 +214,6 @@ EXAMPLES = \ core_3d_camera_first_person \ core_2d_camera \ core_world_screen \ - core_oculus_rift \ shapes_logo_raylib \ shapes_basic_shapes \ shapes_colors_palette \ @@ -338,8 +344,8 @@ core_world_screen: core_world_screen.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) # compile [core] example - oculus rift -core_oculus_rift: core_oculus_rift.c - $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) +#core_oculus_rift: core_oculus_rift.c +# $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) # compile [shapes] example - raylib logo (with basic shapes) shapes_logo_raylib: shapes_logo_raylib.c @@ -497,13 +503,6 @@ audio_module_playing: audio_module_playing.c audio_raw_stream: audio_raw_stream.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -D$(PLATFORM) $(WINFLAGS) -# Linux Fix to timespect from -ifeq ($(PLATFORM),PLATFORM_DESKTOP) - ifeq ($(PLATFORM_OS),LINUX) - CFLAGS += -D_POSIX_C_SOURCE=199309L - endif -endif - # compile [physac] example - physics demo physics_demo: physics_demo.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDES) $(LFLAGS) $(LIBS) -lpthread -D$(PLATFORM) $(WINFLAGS) @@ -537,7 +536,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP) find . -type f -perm +ugo+x -delete rm -f *.o else - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) find -type f -executable | xargs file -i | grep -E 'x-object|x-archive|x-sharedlib|x-executable' | rev | cut -d ':' -f 2- | rev | xargs rm -f else del *.o *.exe diff --git a/src/Makefile b/src/Makefile index 4c2278f5..eeb0ce35 100644 --- a/src/Makefile +++ b/src/Makefile @@ -60,7 +60,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP) else UNAMEOS:=$(shell uname) ifeq ($(UNAMEOS),Linux) - PLATFORM_OS=LINUX + PLATFORM_OS=linux else ifeq ($(UNAMEOS),Darwin) PLATFORM_OS=OSX @@ -153,12 +153,16 @@ endif # define compiler flags: # -O1 defines optimization level +# -Og enable debugging # -Wall turns on most, but not all, compiler warnings # -std=c99 defines C language mode (standard C from 1999 revision) # -std=gnu99 defines C language mode (GNU C from 1999 revision) # -fgnu89-inline declaring inline functions support (GCC optimized) # -Wno-missing-braces ignore invalid warning (GCC bug 53119) -CFLAGS = -O1 -Wall -std=gnu99 -fgnu89-inline -Wno-missing-braces +# -D_DEFAULT_SOURCE use with -std=c99 on Linux to enable timespec and audio +#CFLAGS = -O1 -Wall -std=gnu99 -fgnu89-inline -Wno-missing-braces +CFLAGS = -O1 -Wall -std=c99 -D_DEFAULT_SOURCE + # if shared library required, make sure code is compiled as position independent ifeq ($(SHARED),YES) @@ -213,7 +217,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP) ifeq ($(PLATFORM_OS),WINDOWS) OUTPUT_PATH = ../release/win32/mingw32 endif - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) OUTPUT_PATH = ../release/linux endif ifeq ($(PLATFORM_OS),OSX) @@ -264,7 +268,7 @@ ifeq ($(PLATFORM),PLATFORM_WEB) @echo "libraylib.bc generated (web version)!" else ifeq ($(SHARED),YES) - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) # compile raylib to shared library version for GNU/Linux. # WARNING: you should type "make clean" before doing this target $(CC) -shared -o $(OUTPUT_PATH)/libraylib.so $(OBJS) @@ -333,7 +337,7 @@ utils.o : utils.c utils.h # TODO: add other platforms. install : ifeq ($(ROOT),root) - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) # On GNU/Linux there are some standard directories that contain # libraries and header files. These directory (/usr/local/lib and # /usr/local/include/) are for libraries that are installed @@ -356,7 +360,7 @@ endif # TODO: see 'install' target. unistall : ifeq ($(ROOT),root) - ifeq ($(PLATFORM_OS),LINUX) + ifeq ($(PLATFORM_OS),linux) rm --force /usr/local/include/raylib.h ifeq ($(SHARED),YES) rm --force /usr/local/lib/libraylib.so diff --git a/src/core.c b/src/core.c index 1423cf7c..1a0e5a66 100644 --- a/src/core.c +++ b/src/core.c @@ -105,7 +105,7 @@ #include // Required for: strcmp() //#include // Macros for reporting and retrieving error conditions through error codes -#if defined __linux || defined(PLATFORM_WEB) +#if defined __linux__ || defined(PLATFORM_WEB) #include // Required for: timespec, nanosleep(), select() - POSIX #elif defined __APPLE__ #include // Required for: usleep() @@ -115,7 +115,7 @@ //#define GLFW_INCLUDE_NONE // Disable the standard OpenGL header inclusion on GLFW3 #include // GLFW3 library: Windows, OpenGL context and Input management - #ifdef __linux + #ifdef __linux__ #define GLFW_EXPOSE_NATIVE_X11 // Linux specific definitions for getting #define GLFW_EXPOSE_NATIVE_GLX // native functions like glfwGetX11Window #include // which are required for hiding mouse @@ -641,7 +641,7 @@ int GetScreenHeight(void) void ShowCursor() { #if defined(PLATFORM_DESKTOP) - #ifdef __linux + #ifdef __linux__ XUndefineCursor(glfwGetX11Display(), glfwGetX11Window(window)); #else glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); @@ -654,7 +654,7 @@ void ShowCursor() void HideCursor() { #if defined(PLATFORM_DESKTOP) - #ifdef __linux + #ifdef __linux__ XColor col; const char nil[] = {0}; @@ -2036,7 +2036,7 @@ static void Wait(float ms) #else #if defined _WIN32 Sleep(ms); - #elif defined __linux || defined(PLATFORM_WEB) + #elif defined __linux__ || defined(PLATFORM_WEB) struct timespec req = { 0 }; time_t sec = (int)(ms/1000.0f); ms -= (sec*1000); diff --git a/src/gestures.h b/src/gestures.h index 42ced889..c97871e5 100644 --- a/src/gestures.h +++ b/src/gestures.h @@ -147,7 +147,7 @@ float GetGesturePinchAngle(void); // Get gesture pinch ang // Functions required to query time on Windows int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount); int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency); -#elif defined(__linux) +#elif defined(__linux__) #include // Required for: timespec #include // Required for: clock_gettime() #endif @@ -517,7 +517,7 @@ static double GetCurrentTime(void) time = (double)currentTime/clockFrequency*1000.0f; // Time in miliseconds #endif -#if defined(__linux) +#if defined(__linux__) // NOTE: Only for Linux-based systems struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); diff --git a/src/physac.h b/src/physac.h index ff56615d..1aa0adee 100644 --- a/src/physac.h +++ b/src/physac.h @@ -249,7 +249,7 @@ PHYSACDEF void ClosePhysics(void); // Functions required to query time on Windows int __stdcall QueryPerformanceCounter(unsigned long long int *lpPerformanceCount); int __stdcall QueryPerformanceFrequency(unsigned long long int *lpFrequency); -#elif defined(__linux) || defined(PLATFORM_WEB) +#elif defined(__linux__) || defined(PLATFORM_WEB) #include // Required for: timespec #include // Required for: clock_gettime() #include @@ -277,7 +277,7 @@ PHYSACDEF void ClosePhysics(void); static unsigned int usedMemory = 0; // Total allocated dynamic memory static bool physicsThreadEnabled = false; // Physics thread enabled state static double currentTime = 0; // Current time in milliseconds -#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(__linux) || defined(PLATFORM_WEB) +#if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(__linux__) || defined(PLATFORM_WEB) static double baseTime = 0; // Android and RPI platforms base time #endif static double startTime = 0; // Start time in milliseconds @@ -1906,7 +1906,7 @@ static double GetCurrentTime(void) time = (double)((double)currentTime/clockFrequency)*1000; #endif - #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(__linux) || defined(PLATFORM_WEB) + #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) || defined(__linux__) || defined(PLATFORM_WEB) struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); uint64_t temp = (uint64_t)ts.tv_sec*1000000000LLU + (uint64_t)ts.tv_nsec; -- cgit v1.2.3 From f5894278b74df34d7850438c36f9d7202ea08091 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 16 Apr 2017 13:48:46 +0200 Subject: Added Vector2 math functions Reviewed some Vector3 functions Added auxiliary Clamp() function --- src/gestures.h | 12 ++- src/raymath.h | 312 ++++++++++++++++++++++++++++++++------------------------- 2 files changed, 183 insertions(+), 141 deletions(-) (limited to 'src/gestures.h') diff --git a/src/gestures.h b/src/gestures.h index c97871e5..f04bf091 100644 --- a/src/gestures.h +++ b/src/gestures.h @@ -213,8 +213,11 @@ static unsigned int enabledGestures = 0b0000001111111111; //---------------------------------------------------------------------------------- // Module specific Functions Declaration //---------------------------------------------------------------------------------- +#if defined(GESTURES_STANDALONE) +// Some required math functions provided by raymath.h static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition); static float Vector2Distance(Vector2 v1, Vector2 v2); +#endif static double GetCurrentTime(void); //---------------------------------------------------------------------------------- @@ -477,13 +480,11 @@ float GetGesturePinchAngle(void) //---------------------------------------------------------------------------------- // Module specific Functions Definition //---------------------------------------------------------------------------------- - +#if defined(GESTURES_STANDALONE) // Returns angle from two-points vector with X-axis -static float Vector2Angle(Vector2 initialPosition, Vector2 finalPosition) +static float Vector2Angle(Vector2 v1, Vector2 v2) { - float angle; - - angle = atan2f(finalPosition.y - initialPosition.y, finalPosition.x - initialPosition.x)*(180.0f/PI); + float angle = angle = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI); if (angle < 0) angle += 360.0f; @@ -502,6 +503,7 @@ static float Vector2Distance(Vector2 v1, Vector2 v2) return result; } +#endif // Time measure returned are milliseconds static double GetCurrentTime(void) diff --git a/src/raymath.h b/src/raymath.h index 7e760957..3bde10fc 100644 --- a/src/raymath.h +++ b/src/raymath.h @@ -112,45 +112,67 @@ typedef struct Quaternion { #ifndef RAYMATH_EXTERN_INLINE +//------------------------------------------------------------------------------------ +// Functions Declaration - math utils +//------------------------------------------------------------------------------------ +RMDEF float Clamp(float value, float min, float max); // Clamp float value + +//------------------------------------------------------------------------------------ +// Functions Declaration to work with Vector2 +//------------------------------------------------------------------------------------ +RMDEF Vector2 Vector2Zero(void); // Vector with components value 0.0f +RMDEF Vector2 Vector2One(void); // Vector with components value 1.0f +RMDEF Vector2 Vector2Add(Vector2 v1, Vector2 v2); // Add two vectors (v1 + v2) +RMDEF Vector2 Vector2Subtract(Vector2 v1, Vector2 v2); // Subtract two vectors (v1 - v2) +RMDEF float Vector2Lenght(Vector2 v); // Calculate vector lenght +RMDEF float Vector2DotProduct(Vector2 v1, Vector2 v2); // Calculate two vectors dot product +RMDEF float Vector2Distance(Vector2 v1, Vector2 v2); // Calculate distance between two vectors +RMDEF float Vector2Angle(Vector2 v1, Vector2 v2); // Calculate angle between two vectors in X-axis +RMDEF void Vector2Scale(Vector2 *v, float scale); // Scale vector (multiply by value) +RMDEF void Vector2Negate(Vector2 *v); // Negate vector +RMDEF void Vector2Divide(Vector2 *v, float div); // Divide vector by a float value +RMDEF void Vector2Normalize(Vector2 *v); // Normalize provided vector + //------------------------------------------------------------------------------------ // Functions Declaration to work with Vector3 //------------------------------------------------------------------------------------ -RMDEF Vector3 VectorAdd(Vector3 v1, Vector3 v2); // Add two vectors -RMDEF Vector3 VectorSubtract(Vector3 v1, Vector3 v2); // Substract two vectors -RMDEF Vector3 VectorCrossProduct(Vector3 v1, Vector3 v2); // Calculate two vectors cross product -RMDEF Vector3 VectorPerpendicular(Vector3 v); // Calculate one vector perpendicular vector -RMDEF float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product -RMDEF float VectorLength(const Vector3 v); // Calculate vector lenght -RMDEF void VectorScale(Vector3 *v, float scale); // Scale provided vector -RMDEF void VectorNegate(Vector3 *v); // Negate provided vector (invert direction) -RMDEF void VectorNormalize(Vector3 *v); // Normalize provided vector -RMDEF float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points +RMDEF Vector3 VectorZero(void); // Vector with components value 0.0f +RMDEF Vector3 VectorOne(void); // Vector with components value 1.0f +RMDEF Vector3 VectorAdd(Vector3 v1, Vector3 v2); // Add two vectors +RMDEF Vector3 VectorSubtract(Vector3 v1, Vector3 v2); // Substract two vectors +RMDEF Vector3 VectorCrossProduct(Vector3 v1, Vector3 v2); // Calculate two vectors cross product +RMDEF Vector3 VectorPerpendicular(Vector3 v); // Calculate one vector perpendicular vector +RMDEF float VectorLength(const Vector3 v); // Calculate vector lenght +RMDEF float VectorDotProduct(Vector3 v1, Vector3 v2); // Calculate two vectors dot product +RMDEF float VectorDistance(Vector3 v1, Vector3 v2); // Calculate distance between two points +RMDEF void VectorScale(Vector3 *v, float scale); // Scale provided vector +RMDEF void VectorNegate(Vector3 *v); // Negate provided vector (invert direction) +RMDEF void VectorNormalize(Vector3 *v); // Normalize provided vector +RMDEF void VectorTransform(Vector3 *v, Matrix mat); // Transforms a Vector3 by a given Matrix RMDEF Vector3 VectorLerp(Vector3 v1, Vector3 v2, float amount); // Calculate linear interpolation between two vectors -RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal); // Calculate reflected vector to normal -RMDEF void VectorTransform(Vector3 *v, Matrix mat); // Transforms a Vector3 by a given Matrix -RMDEF Vector3 VectorZero(void); // Return a Vector3 init to zero -RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2); // Return min value for each pair of components -RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2); // Return max value for each pair of components -RMDEF Vector3 Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c); // Barycenter coords for p in triangle abc +RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal); // Calculate reflected vector to normal +RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2); // Return min value for each pair of components +RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2); // Return max value for each pair of components +RMDEF Vector3 VectorBarycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c); // Barycenter coords for p in triangle abc //------------------------------------------------------------------------------------ // Functions Declaration to work with Matrix //------------------------------------------------------------------------------------ -RMDEF float MatrixDeterminant(Matrix mat); // Compute matrix determinant -RMDEF float MatrixTrace(Matrix mat); // Returns the trace of the matrix (sum of the values along the diagonal) -RMDEF void MatrixTranspose(Matrix *mat); // Transposes provided matrix -RMDEF void MatrixInvert(Matrix *mat); // Invert provided matrix -RMDEF void MatrixNormalize(Matrix *mat); // Normalize provided matrix -RMDEF Matrix MatrixIdentity(void); // Returns identity matrix -RMDEF Matrix MatrixAdd(Matrix left, Matrix right); // Add two matrices -RMDEF Matrix MatrixSubstract(Matrix left, Matrix right); // Substract two matrices (left - right) -RMDEF Matrix MatrixTranslate(float x, float y, float z); // Returns translation matrix -RMDEF Matrix MatrixRotate(Vector3 axis, float angle); // Returns rotation matrix for an angle around an specified axis (angle in radians) -RMDEF Matrix MatrixRotateX(float angle); // Returns x-rotation matrix (angle in radians) -RMDEF Matrix MatrixRotateY(float angle); // Returns y-rotation matrix (angle in radians) -RMDEF Matrix MatrixRotateZ(float angle); // Returns z-rotation matrix (angle in radians) -RMDEF Matrix MatrixScale(float x, float y, float z); // Returns scaling matrix -RMDEF Matrix MatrixMultiply(Matrix left, Matrix right); // Returns two matrix multiplication +RMDEF float MatrixDeterminant(Matrix mat); // Compute matrix determinant +RMDEF float MatrixTrace(Matrix mat); // Returns the trace of the matrix (sum of the values along the diagonal) +RMDEF void MatrixTranspose(Matrix *mat); // Transposes provided matrix +RMDEF void MatrixInvert(Matrix *mat); // Invert provided matrix +RMDEF void MatrixNormalize(Matrix *mat); // Normalize provided matrix +RMDEF Matrix MatrixIdentity(void); // Returns identity matrix +RMDEF Matrix MatrixAdd(Matrix left, Matrix right); // Add two matrices +RMDEF Matrix MatrixSubstract(Matrix left, Matrix right); // Substract two matrices (left - right) +RMDEF Matrix MatrixTranslate(float x, float y, float z); // Returns translation matrix +RMDEF Matrix MatrixRotate(Vector3 axis, float angle); // Returns rotation matrix for an angle around an specified axis (angle in radians) +RMDEF Matrix MatrixRotateX(float angle); // Returns x-rotation matrix (angle in radians) +RMDEF Matrix MatrixRotateY(float angle); // Returns y-rotation matrix (angle in radians) +RMDEF Matrix MatrixRotateZ(float angle); // Returns z-rotation matrix (angle in radians) +RMDEF Matrix MatrixScale(float x, float y, float z); // Returns scaling matrix +RMDEF Matrix MatrixMultiply(Matrix left, Matrix right); // Returns two matrix multiplication RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far); // Returns perspective projection matrix RMDEF Matrix MatrixPerspective(double fovy, double aspect, double near, double far); // Returns perspective projection matrix RMDEF Matrix MatrixOrtho(double left, double right, double bottom, double top, double near, double far); // Returns orthographic projection matrix @@ -159,9 +181,9 @@ RMDEF Matrix MatrixLookAt(Vector3 position, Vector3 target, Vector3 up); // Ret //------------------------------------------------------------------------------------ // Functions Declaration to work with Quaternions //------------------------------------------------------------------------------------ -RMDEF float QuaternionLength(Quaternion quat); // Compute the length of a quaternion -RMDEF void QuaternionNormalize(Quaternion *q); // Normalize provided quaternion -RMDEF void QuaternionInvert(Quaternion *quat); // Invert provided quaternion +RMDEF float QuaternionLength(Quaternion quat); // Compute the length of a quaternion +RMDEF void QuaternionNormalize(Quaternion *q); // Normalize provided quaternion +RMDEF void QuaternionInvert(Quaternion *quat); // Invert provided quaternion RMDEF Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2); // Calculate two quaternion multiplication RMDEF Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float slerp); // Calculates spherical linear interpolation between two quaternions RMDEF Quaternion QuaternionFromMatrix(Matrix matrix); // Returns a quaternion for a given rotation matrix @@ -179,32 +201,113 @@ RMDEF void QuaternionTransform(Quaternion *q, Matrix mat); // Transfo #include // Required for: sinf(), cosf(), tan(), fabs() +//---------------------------------------------------------------------------------- +// Module Functions Definition - Utils math +//---------------------------------------------------------------------------------- + +// Clamp float value +RMDEF float Clamp(float value, float min, float max) +{ + const float res = value < min ? min : value; + return res > max ? max : res; +} + +//---------------------------------------------------------------------------------- +// Module Functions Definition - Vector2 math +//---------------------------------------------------------------------------------- + +// Vector with components value 0.0f +RMDEF Vector2 Vector2Zero(void) { return (Vector2){ 0.0f, 0.0f }; } + +// Vector with components value 1.0f +RMDEF Vector2 Vector2One(void) { return (Vector2){ 1.0f, 1.0f }; } + +// Add two vectors (v1 + v2) +RMDEF Vector2 Vector2Add(Vector2 v1, Vector2 v2) +{ + return (Vector2){ v1.x + v2.x, v1.y + v2.y }; +} + +// Subtract two vectors (v1 - v2) +RMDEF Vector2 Vector2Subtract(Vector2 v1, Vector2 v2) +{ + return (Vector2){ v1.x - v2.x, v1.y - v2.y }; +} + +// Calculate vector lenght +RMDEF float Vector2Lenght(Vector2 v) +{ + return sqrtf((v.x*v.x) + (v.y*v.y)); +} + +// Calculate two vectors dot product +RMDEF float Vector2DotProduct(Vector2 v1, Vector2 v2) +{ + return (v1.x*v2.x + v1.y*v2.y); +} + +// Calculate distance between two vectors +RMDEF float Vector2Distance(Vector2 v1, Vector2 v2) +{ + return sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)); +} + +// Calculate angle from two vectors in X-axis +RMDEF float Vector2Angle(Vector2 v1, Vector2 v2) +{ + float angle = atan2f(v2.y - v1.y, v2.x - v1.x)*(180.0f/PI); + + if (angle < 0) angle += 360.0f; + + return angle; +} + +// Scale vector (multiply by value) +RMDEF void Vector2Scale(Vector2 *v, float scale) +{ + v->x *= scale; + v->y *= scale; +} + +// Negate vector +RMDEF void Vector2Negate(Vector2 *v) +{ + v->x = -v->x; + v->y = -v->y; +} + +// Divide vector by a float value +RMDEF void Vector2Divide(Vector2 *v, float div) +{ + *v = (Vector2){v->x/div, v->y/div}; +} + +// Normalize provided vector +RMDEF void Vector2Normalize(Vector2 *v) +{ + Vector2Divide(v, Vector2Lenght(*v)); +} + //---------------------------------------------------------------------------------- // Module Functions Definition - Vector3 math //---------------------------------------------------------------------------------- +// Vector with components value 0.0f +RMDEF Vector3 VectorZero(void) { return (Vector3){ 0.0f, 0.0f, 0.0f }; } + +// Vector with components value 1.0f +RMDEF Vector3 VectorOne(void) { return (Vector3){ 1.0f, 1.0f, 1.0f }; } + // Add two vectors RMDEF Vector3 VectorAdd(Vector3 v1, Vector3 v2) { - Vector3 result; - - result.x = v1.x + v2.x; - result.y = v1.y + v2.y; - result.z = v1.z + v2.z; - - return result; + return (Vector3){ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z }; } // Substract two vectors RMDEF Vector3 VectorSubtract(Vector3 v1, Vector3 v2) { - Vector3 result; - - result.x = v1.x - v2.x; - result.y = v1.y - v2.y; - result.z = v1.z - v2.z; - - return result; + return (Vector3){ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z }; } // Calculate two vectors cross product @@ -233,7 +336,7 @@ RMDEF Vector3 VectorPerpendicular(Vector3 v) cardinalAxis = (Vector3){0.0f, 1.0f, 0.0f}; } - if(fabsf(v.z) < min) + if (fabsf(v.z) < min) { cardinalAxis = (Vector3){0.0f, 0.0f, 1.0f}; } @@ -243,24 +346,26 @@ RMDEF Vector3 VectorPerpendicular(Vector3 v) return result; } +// Calculate vector lenght +RMDEF float VectorLength(const Vector3 v) +{ + return sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); +} + // Calculate two vectors dot product RMDEF float VectorDotProduct(Vector3 v1, Vector3 v2) { - float result; - - result = v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; - - return result; + return (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); } -// Calculate vector lenght -RMDEF float VectorLength(const Vector3 v) +// Calculate distance between two vectors +RMDEF float VectorDistance(Vector3 v1, Vector3 v2) { - float length; - - length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); + float dx = v2.x - v1.x; + float dy = v2.y - v1.y; + float dz = v2.z - v1.z; - return length; + return sqrtf(dx*dx + dy*dy + dz*dz); } // Scale provided vector @@ -295,19 +400,18 @@ RMDEF void VectorNormalize(Vector3 *v) v->z *= ilength; } -// Calculate distance between two points -RMDEF float VectorDistance(Vector3 v1, Vector3 v2) +// Transforms a Vector3 by a given Matrix +// TODO: Review math (matrix transpose required?) +RMDEF void VectorTransform(Vector3 *v, Matrix mat) { - float result; - - float dx = v2.x - v1.x; - float dy = v2.y - v1.y; - float dz = v2.z - v1.z; - - result = sqrtf(dx*dx + dy*dy + dz*dz); + float x = v->x; + float y = v->y; + float z = v->z; - return result; -} + v->x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12; + v->y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13; + v->z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14; +}; // Calculate linear interpolation between two vectors RMDEF Vector3 VectorLerp(Vector3 v1, Vector3 v2, float amount) @@ -339,27 +443,6 @@ RMDEF Vector3 VectorReflect(Vector3 vector, Vector3 normal) return result; } -// Transforms a Vector3 by a given Matrix -// TODO: Review math (matrix transpose required?) -RMDEF void VectorTransform(Vector3 *v, Matrix mat) -{ - float x = v->x; - float y = v->y; - float z = v->z; - - v->x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12; - v->y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13; - v->z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14; -}; - -// Return a Vector3 init to zero -RMDEF Vector3 VectorZero(void) -{ - Vector3 zero = { 0.0f, 0.0f, 0.0f }; - - return zero; -} - // Return min value for each pair of components RMDEF Vector3 VectorMin(Vector3 vec1, Vector3 vec2) { @@ -386,7 +469,7 @@ RMDEF Vector3 VectorMax(Vector3 vec1, Vector3 vec2) // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c) // NOTE: Assumes P is on the plane of the triangle -RMDEF Vector3 Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c) +RMDEF Vector3 VectorBarycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c) { //Vector v0 = b - a, v1 = c - a, v2 = p - a; @@ -663,49 +746,6 @@ RMDEF Matrix MatrixRotate(Vector3 axis, float angle) return result; } -/* -// Another implementation for MatrixRotate... -RMDEF Matrix MatrixRotate(float angle, float x, float y, float z) -{ - Matrix result = MatrixIdentity(); - - float c = cosf(angle); // cosine - float s = sinf(angle); // sine - float c1 = 1.0f - c; // 1 - c - - float m0 = result.m0, m4 = result.m4, m8 = result.m8, m12 = result.m12, - m1 = result.m1, m5 = result.m5, m9 = result.m9, m13 = result.m13, - m2 = result.m2, m6 = result.m6, m10 = result.m10, m14 = result.m14; - - // build rotation matrix - float r0 = x*x*c1 + c; - float r1 = x*y*c1 + z*s; - float r2 = x*z*c1 - y*s; - float r4 = x*y*c1 - z*s; - float r5 = y*y*c1 + c; - float r6 = y*z*c1 + x*s; - float r8 = x*z*c1 + y*s; - float r9 = y*z*c1 - x*s; - float r10= z*z*c1 + c; - - // multiply rotation matrix - result.m0 = r0*m0 + r4*m1 + r8*m2; - result.m1 = r1*m0 + r5*m1 + r9*m2; - result.m2 = r2*m0 + r6*m1 + r10*m2; - result.m4 = r0*m4 + r4*m5 + r8*m6; - result.m5 = r1*m4 + r5*m5 + r9*m6; - result.m6 = r2*m4 + r6*m5 + r10*m6; - result.m8 = r0*m8 + r4*m9 + r8*m10; - result.m9 = r1*m8 + r5*m9 + r9*m10; - result.m10 = r2*m8 + r6*m9 + r10*m10; - result.m12 = r0*m12+ r4*m13 + r8*m14; - result.m13 = r1*m12+ r5*m13 + r9*m14; - result.m14 = r2*m12+ r6*m13 + r10*m14; - - return result; -} -*/ - // Returns x-rotation matrix (angle in radians) RMDEF Matrix MatrixRotateX(float angle) { -- cgit v1.2.3