00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "viewport.h"
00021
00022 #include "engine.h"
00023 #include "camera.h"
00024 #include "frustrum.h"
00025
00026 #include "glheaders.h"
00027
00028 #include <QRect>
00029
00030 using namespace GluonGraphics;
00031
00032 class Viewport::ViewportPrivate
00033 {
00034 public:
00035 float left;
00036 float bottom;
00037 float width;
00038 float height;
00039 float aspectRatio;
00040 };
00041
00042 Viewport::Viewport( QObject* parent )
00043 : QObject( parent ),
00044 d( new ViewportPrivate )
00045 {
00046 }
00047
00048
00049 Viewport::~Viewport()
00050 {
00051 delete d;
00052 }
00053
00054 float
00055 Viewport::left()
00056 {
00057 return d->left;
00058 }
00059
00060 float
00061 Viewport::bottom()
00062 {
00063 return d->bottom;
00064 }
00065
00066 float
00067 Viewport::width()
00068 {
00069 return d->width;
00070 }
00071
00072 float
00073 Viewport::height()
00074 {
00075 return d->height;
00076 }
00077
00078 float
00079 Viewport::aspectRatio()
00080 {
00081 return d->aspectRatio;
00082 }
00083
00084 void
00085 Viewport::setLeft( float left )
00086 {
00087 setSize( left, d->width, d->bottom, d->height );
00088 }
00089
00090 void
00091 Viewport::setBottom( float bottom )
00092 {
00093 setSize( d->left, d->width, bottom, d->height );
00094 }
00095
00096 void
00097 Viewport::setWidth( float width )
00098 {
00099 setSize( d->left, width, d->bottom, d->height );
00100 }
00101
00102 void
00103 Viewport::setHeight( float height )
00104 {
00105 setSize( d->left, d->width, d->bottom, height );
00106 }
00107
00108 void
00109 Viewport::setSize( float left, float width, float bottom, float height )
00110 {
00111 d->left = left;
00112 d->bottom = bottom;
00113 d->width = width;
00114 d->height = height;
00115
00116 d->aspectRatio = width / height;
00117
00118 update();
00119 }
00120
00121 void
00122 Viewport::update()
00123 {
00124 glViewport( d->left, d->bottom, d->width, d->height );
00125
00126 if( Engine::instance()->activeCamera() )
00127 {
00128 Frustrum* frustrum = Engine::instance()->activeCamera()->frustrum();
00129
00130 frustrum->updateFrustrum( d->aspectRatio );
00131
00132 float visibleWidth = d->width;
00133 float actualWidth = 0;
00134 float visibleHeight = d->height;
00135 float actualHeight = 0;
00136 float widthDiff = 0;
00137 float heightDiff = 0;
00138 float aspect = frustrum->viewPlane().width() / frustrum->viewPlane().height();
00139
00140 if( d->aspectRatio > 1 )
00141 {
00142 actualHeight = visibleHeight;
00143 actualWidth = visibleHeight * aspect;
00144 widthDiff = visibleWidth - actualWidth;
00145 }
00146 else
00147 {
00148 actualWidth = visibleWidth;
00149 actualHeight = visibleWidth * ( 1 / aspect );
00150 heightDiff = visibleHeight - actualHeight;
00151 }
00152
00153
00154 }
00155 else
00156 {
00157
00158 }
00159 }
00160
00161 #include "viewport.moc"