00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "cameracontrollercomponent.h"
00021
00022 #include <core/debughelper.h>
00023 #include <graphics/camera.h>
00024 #include <graphics/engine.h>
00025 #include <graphics/frustrum.h>
00026 #include <graphics/viewport.h>
00027 #include <engine/gameobject.h>
00028
00029 #include <QtCore/QSizeF>
00030 #include <QtGui/QMatrix4x4>
00031
00032 REGISTER_OBJECTTYPE( GluonEngine, CameraControllerComponent )
00033
00034 using namespace GluonEngine;
00035
00036 class CameraControllerComponent::CameraControllerComponentPrivate
00037 {
00038 public:
00039 CameraControllerComponentPrivate()
00040 {
00041 camera = 0;
00042 active = true;
00043 visibleArea = QSizeF( 100.0f, 100.0f );
00044 nearPlane = 1.0f;
00045 farPlane = 100.0f;
00046 }
00047
00048 GluonGraphics::Camera* camera;
00049 bool active;
00050
00051 QSizeF visibleArea;
00052 float nearPlane;
00053 float farPlane;
00054
00055 static GluonGraphics::Camera* activeCamera;
00056 };
00057
00058 GluonGraphics::Camera* CameraControllerComponent::CameraControllerComponentPrivate::activeCamera = 0;
00059
00060 CameraControllerComponent::CameraControllerComponent( QObject* parent )
00061 : Component( parent )
00062 , d( new CameraControllerComponentPrivate )
00063 {
00064
00065 }
00066
00067 CameraControllerComponent::CameraControllerComponent( const CameraControllerComponent& other )
00068 : Component( other )
00069 , d( other.d )
00070 {
00071 }
00072
00073 CameraControllerComponent::~CameraControllerComponent()
00074 {
00075 delete d;
00076 }
00077
00078 QString CameraControllerComponent::category() const
00079 {
00080 return QString( "Graphics" );
00081 }
00082
00083 void CameraControllerComponent::initialize()
00084 {
00085 if( !d->camera )
00086 d->camera = new GluonGraphics::Camera();
00087
00088 if( d->active )
00089 {
00090 GluonGraphics::Engine::instance()->setActiveCamera( d->camera );
00091 }
00092
00093 d->camera->frustrum()->setOrthoAdjusted( d->visibleArea, GluonGraphics::Engine::instance()->currentViewport()->aspectRatio(), d->nearPlane, d->farPlane );
00094 }
00095
00096 void CameraControllerComponent::start()
00097 {
00098 }
00099
00100 void CameraControllerComponent::draw( int timeLapse )
00101 {
00102 Q_UNUSED( timeLapse )
00103
00104 if( d->camera )
00105 d->camera->setViewMatrix( gameObject()->transform().inverted() );
00106 }
00107
00108 void CameraControllerComponent::cleanup()
00109 {
00110 CameraControllerComponentPrivate::activeCamera = 0;
00111 GluonGraphics::Engine::instance()->setActiveCamera( 0 );
00112
00113 delete d->camera;
00114 d->camera = 0;
00115 }
00116
00117 bool CameraControllerComponent::isActive()
00118 {
00119 return d->active;
00120 }
00121
00122 void CameraControllerComponent::setActive( bool active )
00123 {
00124 d->active = active;
00125 if( active && d->camera )
00126 {
00127 CameraControllerComponentPrivate::activeCamera = d->camera;
00128 GluonGraphics::Engine::instance()->setActiveCamera( d->camera );
00129 }
00130 }
00131
00132 void CameraControllerComponent::setVisibleArea( const QSizeF& area )
00133 {
00134 d->visibleArea = area;
00135
00136 if( d->camera )
00137 d->camera->frustrum()->setOrthoAdjusted( d->visibleArea, GluonGraphics::Engine::instance()->currentViewport()->aspectRatio(), d->nearPlane, d->farPlane );
00138 }
00139
00140 QSizeF CameraControllerComponent::visibleArea()
00141 {
00142 return d->visibleArea;
00143 }
00144
00145 float CameraControllerComponent::nearPlane()
00146 {
00147 return d->nearPlane;
00148 }
00149
00150 float CameraControllerComponent::farPlane()
00151 {
00152 return d->farPlane;
00153 }
00154
00155 void CameraControllerComponent::setNearPlane( float near )
00156 {
00157 d->nearPlane = near;
00158
00159 if( d->camera )
00160 d->camera->frustrum()->setOrthoAdjusted( d->visibleArea, GluonGraphics::Engine::instance()->currentViewport()->aspectRatio(), d->nearPlane, d->farPlane );
00161 }
00162
00163 void CameraControllerComponent::setFarPlane( float far )
00164 {
00165 d->farPlane = far;
00166
00167 if( d->camera )
00168 d->camera->frustrum()->setOrthoAdjusted( d->visibleArea, GluonGraphics::Engine::instance()->currentViewport()->aspectRatio(), d->nearPlane, d->farPlane );
00169 }
00170
00171 Q_EXPORT_PLUGIN2( gluon_component_cameracontroller, GluonEngine::CameraControllerComponent );
00172
00173 #include "cameracontrollercomponent.moc"