AaronCameron.net
I care not for your petty politics.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 05:44pm
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

matrixUtilities.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #include "geometry/matrixUtilities.h"
00027 #include "geometry/misc.h"
00028 
00029 #include <math.h>
00030 
00031 namespace n2l
00032 {
00033     void n2lBuildRotationMatrix(    cMatrix<tFloat,4,4> & oMatrix,
00034                                     const tFloat & iTheta,
00035                                     const tVector3f & iAxis )
00036     {
00037         // This implementation has be copied (in spirit) from page:
00038         //  http://www.sgi.com/software/opengl/advanced97/programs/billboard.c
00039         tFloat x( iAxis.x() );
00040         tFloat y( iAxis.y() );
00041         tFloat z( iAxis.z() );
00042         
00043         tFloat d = x*x + y*y + z*z;
00044         const tFloat Ct( cos( degToRad(iTheta) ) );
00045         const tFloat St( sin( degToRad(iTheta) ) );
00046 
00047         // Normalize
00048         if (d>0) {
00049             d = 1/d;
00050             x *= d;
00051             y *= d;
00052             z *= d;
00053         }
00054 
00055         oMatrix.identity();
00056 
00057         tFloat *m( oMatrix.data() );
00058         m[0] = x*x + Ct*(1-x*x) + St*0;
00059         m[4] = x*y + Ct*(0-x*y) + St*-z;
00060         m[8] = x*z + Ct*(0-x*z) + St*y;
00061 
00062         m[1] = y*x + Ct*(0-y*x) + St*z;
00063         m[5] = y*y + Ct*(1-y*y) + St*0;
00064         m[9] = y*z + Ct*(0-y*z) + St*-x;
00065 
00066         m[2] = z*x + Ct*(0-z*x) + St*-y;
00067         m[6] = z*y + Ct*(0-z*y) + St*x;
00068         m[10]= z*z + Ct*(1-z*z) + St*0;
00069     }
00070 
00071 } // namespace n2l
©2012 Aaron Cameron