00001 /****************************************************************************** 00002 * This file is part of the Gluon Development Platform 00003 * Copyright (C) 2008 Sacha Schutz <istdasklar@free.fr> 00004 * Copyright (C) 2008 Olivier Gueudelot <gueudelotolive@gmail.com> 00005 * Copyright (C) 2008 Charles Huet <packadal@gmail.com> 00006 * Copyright (c) 2010 Arjen Hiemstra <ahiemstra@heimr.nl> 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "item.h" 00024 00025 #include <QtGui/QMatrix4x4> 00026 00027 #include <core/debughelper.h> 00028 00029 #include "mesh.h" 00030 #include "camera.h" 00031 #include "frustrum.h" 00032 #include "engine.h" 00033 #include "math.h" 00034 #include "materialinstance.h" 00035 #include "material.h" 00036 00037 using namespace GluonGraphics; 00038 00039 class Item::ItemPrivate 00040 { 00041 public: 00042 ItemPrivate() 00043 { 00044 materialInstance = 0; 00045 } 00046 Mesh* mesh; 00047 QMatrix4x4 transform; 00048 MaterialInstance* materialInstance; 00049 }; 00050 00051 Item::Item( QObject* parent ) 00052 : QObject( parent ), 00053 d( new ItemPrivate ) 00054 { 00055 d->materialInstance = Engine::instance()->material( "default" )->instance( "default" ); 00056 } 00057 00058 Item::~Item() 00059 { 00060 delete d; 00061 } 00062 00063 Mesh* 00064 Item::mesh() 00065 { 00066 return d->mesh; 00067 } 00068 00069 QMatrix4x4 00070 Item::transform() 00071 { 00072 return d->transform; 00073 } 00074 00075 MaterialInstance* 00076 Item::materialInstance() 00077 { 00078 return d->materialInstance; 00079 } 00080 00081 void 00082 Item::render() 00083 { 00084 Camera* activeCam = Engine::instance()->activeCamera(); 00085 if( !activeCam ) 00086 return; 00087 00088 #ifdef __GNUC__ 00089 #warning ToDo: Implement view frustum culling. After all, that is what that damn class is for... ;) 00090 #endif 00091 00092 QMatrix4x4 modelViewProj = Math::calculateModelViewProj( d->transform, activeCam->viewMatrix(), activeCam->frustrum()->projectionMatrix() ); 00093 00094 d->materialInstance->bind(); 00095 d->materialInstance->setModelViewProjectionMatrix( modelViewProj ); 00096 d->mesh->render( d->materialInstance ); 00097 d->materialInstance->release(); 00098 } 00099 00100 void 00101 Item::setTransform( const QMatrix4x4 transform ) 00102 { 00103 d->transform = transform; 00104 } 00105 00106 void 00107 Item::setMesh( Mesh* mesh ) 00108 { 00109 d->mesh = mesh; 00110 } 00111 00112 void 00113 Item::setMaterialInstance( MaterialInstance* instance ) 00114 { 00115 d->materialInstance = instance; 00116 } 00117 00118 00119 #include "item.moc"