//! Common vector algebra functions /*! * Math functions useful for Computer Graphics projects * * Note: Assumes normalized directions pretty much everywhere, for efficiency */ /* * Common vector algebra functions * * A.k.a. Biblioteca con funciones útiles para los proyectos de * Computación Gráfica con SDL (Versión pirata de glutamato.h). * ****************************************************************************** * Note: Assumes normalized directions pretty much everywhere, for efficiency * ****************************************************************************** * * Copyright (C) 2006-2011 Alejandro Valenzuela Roca, * * This file is part of MotorJ, a free framework for videogame creation. * * * * MotorJ is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MotorJ is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with MotorJ. If not, see . */ #ifndef SUPPORT_H #define SUPPORT_H #include #include /* Standard Windows Libraries (Generally must be included before anything else..) */ #ifdef VISUAL_STUDIO #include #include #endif #include #include #include #include #ifdef NINTENDO_WII #include "wii_only/gl2gxtrans.h" #else #ifdef PC_SDL #ifdef VISUAL_STUDIO #include #ifndef snprintf #define snprintf _snprintf #endif #ifndef strncasecmp #define strncasecmp _strnicmp #endif #endif #include #endif // Deactivate some Wii-only functions #define mjWiiGLSetVs(x) #endif #ifdef NINTENDO_DS #include #ifndef GLfloat #define GLfloat float #endif #ifndef GLint #define GLint int #endif #ifndef GLuint #define GLuint uint #endif #endif #include #ifdef _WIN32 const char SEPDIR = '\\'; const char NOSEPDIR = '/'; #else const char SEPDIR = '/'; const char NOSEPDIR = '\\'; #endif const float tol = 0.0001; /* Structs */ #ifdef NINTENDO_WII #else // vector3 gets defined in another header when building for the Nintendo Wii typedef struct{ GLfloat x,y,z; } vector3; #endif //! 2 component-vector typedef struct { GLfloat u; GLfloat v; } vector2; //! 2 component integer-vector typedef struct{ GLint u; GLint v; } vector2i; //! Oriented bounding box structure (For collision detection) typedef struct { vector3 u; vector3 v; vector3 w; GLfloat Hu; GLfloat Hv; GLfloat Hw; vector3 c; } obb; //! Triangle structure (For collision detection) typedef struct { vector3 * v0; vector3 * v1; vector3 * v2; vector3 * n; } triangle; /* Prototypes */ /*! Cheap random integers in range between 0 and range-1 (uses rand(), so it must be seeded using srand() */ unsigned rand_range(unsigned upperlimit); /*! Replace directory separation char (for WIN32 compatibility) Note: path _MUST_ be a variable. */ char * replace_dir_char(const char * path); /* Debugging printf */ void _debug(char * file,int line, const char * format,...); #define debug(format,...)\ _debug(__FILE__,__LINE__,format,##__VA_ARGS__) //! Push current GL matrix stack void push(); //! Pop current GL matrix stack void pop(); /* Sets vectors to the state equal to that implied by glLoadIdentity() except for the position.. */ void initial_angle(vector3 * angles); void initial_dir(vector3 * direction); void initial_vdir(vector3 * vert_dir); void initial_pos(vector3 * position); /* Geometry/Math */ //! Convert degrees to radians (useful for sin/cos from math.h ) GLfloat deg_to_rad( GLfloat degs); //! Convert radians to degrees GLfloat rad_to_deg( GLfloat rads); //! Establish vector components vector3 * v3_est(GLfloat x, GLfloat y, GLfloat z, vector3 * u); //! Set vectors as vectorial constants: 0,0,0 void v3_0(vector3 * u); //! Set vectors as vectorial constants: 1,0,0 void v3_i(vector3 * u); //! Set vectors as vectorial constants: 0,1,0 void v3_j(vector3 * u); //! Set vectors as vectorial constants: 0,0,1 void v3_k(vector3 * u); //! Set vectors as vectorial constants: 1,1,1 void v3_1(vector3 * u); //! Vector copy void vec_copy(vector3 & u, vector3 * r); /* Analytical Geometry functions*/ //! R3 Direction to spherical angles void dir_to_ang(vector3 & dir, vector3 * ang); //! Vectorial addition r += a*u void vec_sum(float alpha, vector3 & u, vector3 * r); //! Simple vectorial addition r += u void vec_sum2(vector3 & u, vector3 * r); //! Dot product GLfloat dot(vector3 & u, vector3 & v); //! Cross product void cross(vector3 & u, vector3 & v, vector3 * r); //! Angle between two vectors GLfloat vec_ang(vector3 & u, vector3 & v); //! Scalar component of one vector against another GLfloat scal_comp(vector3 & u, vector3 & v); //void vec_proj(vector3 u, vector3 v, vector3 * r); //! Vectorial norm (length) of a vector GLfloat vec_norm(vector3 & u); //! Vectorial norm (length) of a vector, squared GLfloat vec_norm2(vector3 & u); //! Scalar-vector multiplication void mul_scal(GLfloat alpha, vector3 * u); //! Vector normalization (set length to 1, but don't change its direction) float normalize( vector3 * u); //! Scalar projection of a vector on another vector GLfloat scal_proj(vector3 & u, vector3 & v); //! Vectorial projection of a vector on another void vec_proj(vector3 & u, vector3 & v, vector3 * r); //! Reflect a vector void vec_refl(vector3 & d, vector3 &n, vector3 * r); //! Calculate a triangle's normal void calc_normal(triangle &tr, vector3 * n); //! Sign of a number, 1, -1 or 0 depending if it is positive, negative or 0 GLfloat sign(GLfloat x); //! Distance between 2 points GLfloat dist_p(vector3 & pos1, vector3 pos2); //! Rotate and position an object using vector direction /*! Stolen from gluLookAt.. Source: http://www.mevis.de/opengl/glMultMatrix.html Directions must be _NORMALIZED_ */ void obj_pos_rot(vector3 & pos, vector3 & dir, vector3 & dir_v); //! Get triangle pseudo-center void tr_pseudocen(triangle &tr, vector3 * center); //! Determine whether vector is null bool isNullVector(vector3& v); #ifdef PC_SDL #ifndef VISUAL_STUDIO //! max function is inline max and min from std::max(). Needed to solve GCC/Visual Studio incompatibility >_<* GLfloat max(GLfloat x, GLfloat y); //! max function is inline max and min from std::max(). Needed to solve GCC/Visual Studio incompatibility >_<* GLfloat min(GLfloat x, GLfloat y); #endif #endif #endif