Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

ModelMatrices.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: ModelMatrices.cpp,v 1.8 2002/02/16 12:41:39 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Models/Model.h"
00026 using namespace Teddy::Maths;  //  Natrix
00027 
00028 
00029 namespace Teddy  {
00030 namespace Models {
00031 
00032 
00039 Matrix Model::getWorldToLocalMatrix() const {
00040     Matrix m = attitude;
00041     float  x = (float) -position.v[0];
00042     float  y = (float) -position.v[1];
00043     float  z = (float) -position.v[2];
00044 
00045     m.translate( x, y, z );
00046     return m;
00047 }
00048 
00049 
00056 Matrix Model::getLocalToWorldMatrix() const {
00057     Matrix r = attitude;
00058     Matrix t;
00059     r.transpose();
00060     t.translateMatrix( position );
00061     return t * r;
00062 }
00063 
00064 
00070 Matrix Model::getViewMatrix() const {
00071     // Don't use the normal view matrix because it causes precision
00072     // problems if the camera is too far away from the origin.
00073     // Instead, pretend the camera is at the origin and offset all
00074     // model matrices by subtracting the camera's position.
00075     Matrix m = attitude;
00076     return m;
00077 }
00078 
00079 
00084 Matrix Model::getModelMatrix( Model *camera ) const {
00085     Matrix m;
00086     m.modelMatrix( attitude, position-camera->position );
00087     return m;
00088 }
00089 
00090 
00097 Matrix Model::getScaledModelMatrix( Model *camera ) const {
00098     // This code scales the planet's size and distance to the camera
00099     // down when it's too far away.
00100     // This solves a problem with many video card drivers where objects
00101     // too far away aren't rendering properly
00102     // It also alleviates the Z-buffer precision problem caused by having
00103     // your near and far clipping planes too far apart.
00104     const double MAX_DISTANCE    =   256.0;             // Distance to desired far clipping plane
00105     const double MAX_DISCERNABLE = (512.0f*512.0f);     // Beyond this distance, everything is rendered at MAX_DISTANCE
00106     const double HALF_MAX        = (MAX_DISTANCE*0.5);  // Everything between HALF_MAX and MAX_DISCERNABLE is scaled exponentially between HALF_MAX and MAX_DISTANCE
00107     Matrix mat      = getModelMatrix( camera );
00108     Vector vec      = position - camera->position;
00109     double distance = vec.magnitude();
00110 
00111     if( distance > HALF_MAX ){
00112         vec           /= distance;
00113         double factor  = MAX_DISTANCE;
00114 
00115         if( distance < MAX_DISCERNABLE ){
00116             factor = (HALF_MAX + HALF_MAX * (1 - exp((HALF_MAX - distance) / MAX_DISCERNABLE)));
00117         }
00118         vec *= factor;
00119         mat.m[3][0]  = (float)vec.v[0];
00120         mat.m[3][1]  = (float)vec.v[1];
00121         mat.m[3][2]  = (float)vec.v[2];
00122         factor /= distance;
00123         mat.scale( (float)factor, (float)factor, (float)factor );
00124     }
00125     return mat;
00126 }
00127 
00128 
00129 };  //  namespace Models
00130 };  //  namespace Teddy
00131