| 1 | /****************************************************************************** |
| 2 | * Arachnoid Graphics Plugin for Mupen64Plus |
| 3 | * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/ |
| 4 | * |
| 5 | * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | *****************************************************************************/ |
| 21 | |
| 22 | #ifndef MATH_LIBRARY_H_ |
| 23 | #define MATH_LIBRARY_H_ |
| 24 | |
| 25 | #include <cmath> //sqrtf |
| 26 | #include "m64p.h" |
| 27 | |
| 28 | // Formula: a.b = a0*b0 + a1*b1 + a2*b2 |
| 29 | #define Vec3Dot(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2]) |
| 30 | |
| 31 | #define Vec3Normalize(v) { \ |
| 32 | float lenght = (v)[0]*(v)[0]+(v)[1]*(v)[1]+(v)[2]*(v)[2]; \ |
| 33 | if ( lenght > 0.00001f ) { \ |
| 34 | lenght = 1.0f / sqrt(lenght); \ |
| 35 | (v)[0] *= lenght; \ |
| 36 | (v)[1] *= lenght; \ |
| 37 | (v)[2] *= lenght; \ |
| 38 | } \ |
| 39 | } |
| 40 | |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | // Transform Vector |
| 44 | //----------------------------------------------------------------------------- |
| 45 | |
| 46 | inline void transformVertex( float* m, float* v, float* out ) |
| 47 | { |
| 48 | float x = v[0]; |
| 49 | float y = v[1]; |
| 50 | float z = v[2]; |
| 51 | out[0] = m[0] * x + m[4] * y + m[8] * z + m[12]; |
| 52 | out[1] = m[1] * x + m[5] * y + m[9] * z + m[13]; |
| 53 | out[2] = m[2] * x + m[6] * y + m[10] * z + m[14]; |
| 54 | out[3] = m[3] * x + m[7] * y + m[11] * z + m[15]; |
| 55 | } |
| 56 | |
| 57 | inline void transformVector( float* m, float* v, float* out ) |
| 58 | { |
| 59 | float x = v[0]; |
| 60 | float y = v[1]; |
| 61 | float z = v[2]; |
| 62 | out[0] = m[0] * x + m[4] * y + m[8] * z; |
| 63 | out[1] = m[1] * x + m[5] * y + m[9] * z; |
| 64 | out[2] = m[2] * x + m[6] * y + m[10] * z; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | //----------------------------------------------------------------------------- |
| 70 | // Random Float |
| 71 | //----------------------------------------------------------------------------- |
| 72 | inline float randomFloat(float min, float max) |
| 73 | { |
| 74 | return (float)( min + double(max-min)*rand()/RAND_MAX ); |
| 75 | } |
| 76 | |
| 77 | inline unsigned int pow2( unsigned int dim ) |
| 78 | { |
| 79 | unsigned int i = 1; |
| 80 | |
| 81 | while (i < dim) i <<= 1; |
| 82 | |
| 83 | return i; |
| 84 | } |
| 85 | |
| 86 | inline unsigned int powof( unsigned int dim ) |
| 87 | { |
| 88 | unsigned int num = 1; |
| 89 | unsigned int i = 0; |
| 90 | |
| 91 | while (num < dim) |
| 92 | { |
| 93 | num <<= 1; |
| 94 | i++; |
| 95 | } |
| 96 | |
| 97 | return i; |
| 98 | } |
| 99 | |
| 100 | #endif |