worldsim/src/graphicalwobject.cpp

00001 /********************************************************************************
00002  *  FARSA Experiments Library                                                   *
00003  *  Copyright (C) 2007-2012                                                     *
00004  *  Stefano Nolfi <stefano.nolfi@istc.cnr.it>                                   *
00005  *  Onofrio Gigliotta <onofrio.gigliotta@istc.cnr.it>                           *
00006  *  Gianluca Massera <emmegian@yahoo.it>                                        *
00007  *  Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it>                         *
00008  *                                                                              *
00009  *  This program is free software; you can redistribute it and/or modify        *
00010  *  it under the terms of the GNU General Public License as published by        *
00011  *  the Free Software Foundation; either version 2 of the License, or           *
00012  *  (at your option) any later version.                                         *
00013  *                                                                              *
00014  *  This program is distributed in the hope that it will be useful,             *
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00017  *  GNU General Public License for more details.                                *
00018  *                                                                              *
00019  *  You should have received a copy of the GNU General Public License           *
00020  *  along with this program; if not, write to the Free Software                 *
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00022  ********************************************************************************/
00023 
00024 #include "graphicalwobject.h"
00025 
00026 namespace farsa {
00027 
00028 GraphicalWObject::GraphicalWObject(World* world, QString name, const wMatrix& tm) :
00029     WObject(world, name, tm, false),
00030     m_attachedObject(NULL)
00031 {
00032     // Adding here to the world, even if not at the end of the hierarchy (all classes inheriting
00033     // from this have the same renderer anyway)
00034     world->pushObject(this);
00035 }
00036 
00037 GraphicalWObject::~GraphicalWObject()
00038 {
00039     // Nothing do to here
00040 }
00041 
00042 void GraphicalWObject::attachToObject(WObject* object, bool makeOwner)
00043 {
00044     m_attachedObject = object;
00045 
00046     if (makeOwner) {
00047         setOwner(m_attachedObject, true);
00048     } else {
00049         setOwner(NULL);
00050     }
00051 }
00052 
00053 void GraphicalWObject::updateAndRender(RenderWObject* renderer, QGLContext* gw)
00054 {
00055     updateMatrixFromAttachedObject();
00056 
00057     render(renderer, gw);
00058 }
00059 
00060 void GraphicalWObject::updateAndRenderAABB(RenderWObject* renderer, RenderWorld* gw)
00061 {
00062     updateMatrixFromAttachedObject();
00063 
00064     renderAABB(renderer, gw);
00065 }
00066 
00067 void GraphicalWObject::updateAndCalculateAABB(wVector& minPoint, wVector& maxPoint, const wMatrix tm)
00068 {
00069     updateMatrixFromAttachedObject();
00070 
00071     calculateAABB(minPoint, maxPoint, tm);
00072 }
00073 
00074 void GraphicalWObject::updateAndCalculateOBB(wVector& dimension, wVector& minPoint, wVector& maxPoint)
00075 {
00076     updateMatrixFromAttachedObject();
00077 
00078     calculateOBB(dimension, minPoint, maxPoint);
00079 }
00080 
00081 void GraphicalWObject::renderAABB(RenderWObject*, RenderWorld*)
00082 {
00083     // The default implementation does nothing
00084 }
00085 
00086 void GraphicalWObject::calculateAABB(wVector& minPoint, wVector& maxPoint, const wMatrix)
00087 {
00088     // The default implementation does nothing
00089     minPoint = wVector(0, 0, 0);
00090     maxPoint = wVector(0, 0, 0);
00091 }
00092 
00093 void GraphicalWObject::calculateOBB(wVector& dimension, wVector& minPoint, wVector& maxPoint)
00094 {
00095     // The default implementation does nothing
00096     dimension = wVector(0, 0, 0);
00097     minPoint = wVector(0, 0, 0);
00098     maxPoint = wVector(0, 0, 0);
00099 }
00100 
00101 void GraphicalWObject::updateMatrixFromAttachedObject()
00102 {
00103     if (m_attachedObject == NULL) {
00104         return;
00105     }
00106 
00107     setMatrix(m_attachedObject->matrix());
00108 }
00109 
00110 }