CrystalSpace

Public API Reference

csgeom/projections.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2008 by Frank Richter
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_CSGEOM_PROJECTIONS_H__
00020 #define __CS_CSGEOM_PROJECTIONS_H__
00021 
00029 #include "csgeom/matrix4.h"
00030 
00031 namespace CS
00032 {
00033   namespace Math
00034   {
00038     struct Projections
00039     {
00046       static Matrix4 Ortho (float left, float right, float bottom, float top,
00047         float nearz, float farz)
00048       {
00049         return Matrix4 (2.0f/(right-left), 0, 0, -(right+left)/(right-left),
00050                         0, 2.0f/(top-bottom), 0, -(top+bottom)/(top-bottom),
00051                         0, 0, -2.0f/(farz-nearz), -(farz+nearz)/(farz-nearz),
00052                         0, 0, 0, 1);
00053       }
00054       
00058       static Matrix4 CSPerspective (float viewWidth, float viewHeight, 
00059                                     float shiftX, float shiftY,
00060                                     float invAspect,
00061                                     float nearClip = 0.1f)
00062       {
00063         const float nearz = -1.0f;
00064         float farz = 1.0f/nearClip;
00065         Matrix4 Mortho (Ortho (0, viewWidth, 0, viewHeight, nearz, farz));
00066 
00067         CS::Math::Matrix4 Mtranslate (
00068           1, 0, 0, shiftX,
00069           0, 1, 0, shiftY,
00070           0, 0, 1,      0,
00071           0, 0, 0,      1);
00072 
00073         CS::Math::Matrix4 Mprojection (
00074           1, 0, 0, 0,
00075           0, 1, 0, 0,
00076           0, 0, 0, -invAspect,
00077           0, 0, invAspect, 0);
00078         
00079         return (Mortho * Mtranslate) * Mprojection;
00080       }
00081 
00085       static Matrix4 Frustum (float left, float right, float bottom, float top,
00086         float nearz, float farz)
00087       {
00088         float two_n = nearz * 2;
00089         return Matrix4 (two_n/(right-left), 0, -(right+left)/(right-left), 0,
00090                         0, two_n/(top-bottom), -(top+bottom)/(top-bottom), 0,
00091                         0, 0, -(farz+nearz)/(farz-nearz), -(2*farz*nearz)/(farz-nearz),
00092                         0, 0, -1, 0);
00093       }
00094     };
00095       
00096   } // namespace Math
00097 } // namespace CS
00098 
00101 #endif // __CS_CSGEOM_PROJECTIONS_H__
00102 

Generated for Crystal Space 2.0 by doxygen 1.7.6.1