00001 #include "viewport.h"
00002
00019 #include <glt/texture.h>
00020 #include <glt/error.h>
00021
00022 #include <math/bbox.h>
00023
00024 #include <cstring>
00025 using namespace std;
00026
00027 GltViewport::GltViewport(bool getIt)
00028 {
00029 memset(_viewport,0,sizeof(_viewport));
00030
00031 if (getIt)
00032 get();
00033 }
00034
00035 GltViewport::GltViewport(const GltViewport &viewport)
00036 {
00037 memcpy(_viewport,viewport._viewport,sizeof(_viewport));
00038 }
00039
00040 GltViewport::GltViewport(const GLint x,const GLint y,const GLint width,const GLint height)
00041 {
00042 set(x,y,width,height);
00043 }
00044
00045 GltViewport::~GltViewport()
00046 {
00047 }
00048
00049 GltViewport &
00050 GltViewport::operator=(const GltViewport &viewport)
00051 {
00052 memcpy(_viewport,viewport._viewport,sizeof(_viewport));
00053 return *this;
00054 }
00055
00056 bool
00057 GltViewport::operator==(const GltViewport &viewport) const
00058 {
00059 return memcmp(_viewport,viewport._viewport,sizeof(_viewport))==0;
00060 }
00061
00062 bool
00063 GltViewport::operator!=(const GltViewport &viewport) const
00064 {
00065 return memcmp(_viewport,viewport._viewport,sizeof(_viewport))!=0;
00066 }
00067
00068 bool
00069 GltViewport::valid() const
00070 {
00071 return x()>=0 && y()>=0 && width()>0 && height()>0;
00072 }
00073
00074 void
00075 GltViewport::get()
00076 {
00077 GLERROR
00078 glGetIntegerv(GL_VIEWPORT,(GLint *) _viewport);
00079 GLERROR
00080 }
00081
00082 void
00083 GltViewport::set() const
00084 {
00085 GLERROR
00086 glViewport(x(),y(),width(),height());
00087 GLERROR
00088 }
00089
00090 void
00091 GltViewport::set(const GLint width,const GLint height)
00092 {
00093 GltViewport::width() = width;
00094 GltViewport::height() = height;
00095 set();
00096 }
00097
00098 void
00099 GltViewport::set(const GLint x,const GLint y,const GLint width,const GLint height)
00100 {
00101 GltViewport::x() = x;
00102 GltViewport::y() = y;
00103 GltViewport::width() = width;
00104 GltViewport::height() = height;
00105 set();
00106 }
00107
00108
00109
00110 GLint &GltViewport::x() { return _viewport[0]; }
00111 GLint &GltViewport::y() { return _viewport[1]; }
00112 GLint &GltViewport::width() { return _viewport[2]; }
00113 GLint &GltViewport::height() { return _viewport[3]; }
00114
00115 const GLint &GltViewport::x() const { return _viewport[0]; }
00116 const GLint &GltViewport::y() const { return _viewport[1]; }
00117 const GLint &GltViewport::width() const { return _viewport[2]; }
00118 const GLint &GltViewport::height() const { return _viewport[3]; }
00119
00120 const GLint GltViewport::pixels() const { return _viewport[2]*_viewport[3]; }
00121
00122 GltViewport::operator const GLint *() const { return _viewport; }
00123 GltViewport::operator GLint *() { return _viewport; }
00124
00125
00126
00127 bool GltViewport::resize(const GLint width,const GLint height)
00128 {
00129 GltViewport::width() = width;
00130 GltViewport::height() = height;
00131
00132 return valid();
00133 }
00134
00135 bool GltViewport::resize(const GLint x,const GLint y,const GLint width,const GLint height)
00136 {
00137 GltViewport::x() = x;
00138 GltViewport::y() = y;
00139 GltViewport::width() = width;
00140 GltViewport::height() = height;
00141
00142 return valid();
00143 }
00144
00145 bool GltViewport::resize(const GltTexture &texture)
00146 {
00147 GltViewport::width() = texture.width();
00148 GltViewport::height() = texture.height();
00149
00150 return valid();
00151 }
00152
00153 bool GltViewport::resize(const BoundingBox &box)
00154 {
00155 GltViewport::x() = (GLint) floor(box.min().x());
00156 GltViewport::y() = (GLint) floor(box.min().y());
00157 GltViewport::width() = (GLint) ceil(box.width());
00158 GltViewport::height() = (GLint) ceil(box.height());
00159
00160 return valid();
00161 }
00162
00163 bool GltViewport::align(const GltHorizontalAlignment &align,const GltViewport &window)
00164 {
00165 switch (align)
00166 {
00167 case GLT_ALIGN_LEFT: x() = window.x(); break;
00168 case GLT_ALIGN_HCENTER: x() = window.x() + ((window.width()-width())>>1); break;
00169 case GLT_ALIGN_RIGHT: x() = window.x() + window.width()-width(); break;
00170 }
00171
00172 return valid();
00173 }
00174
00175 bool GltViewport::align(const GltVerticalAlignment &align,const GltViewport &window)
00176 {
00177 switch (align)
00178 {
00179 case GLT_ALIGN_TOP: y() = window.y() + window.height()-height(); break;
00180 case GLT_ALIGN_VCENTER: y() = window.y() + ((window.height()-height())>>1); break;
00181 case GLT_ALIGN_BOTTOM: y() = window.y(); break;
00182 }
00183
00184 return valid();
00185 }
00186
00187 bool GltViewport::shrink(const GLint w)
00188 {
00189 x() += w;
00190 y() += w;
00191
00192 width() -= w<<1;
00193 height() -= w<<1;
00194
00195 return true;
00196 }
00197
00198
00199
00200 void GltViewport::drawQuad() const
00201 {
00202 const GLint x = width();
00203 const GLint y = height();
00204
00205 glBegin(GL_QUADS);
00206 glVertex2i(0,0);
00207 glVertex2i(x,0);
00208 glVertex2i(x,y);
00209 glVertex2i(0,y);
00210 glEnd();
00211 }
00212
00213 void GltViewport::drawLines() const
00214 {
00215 const GLint x = width()-1;
00216 const GLint y = height()-1;
00217
00218 glBegin(GL_LINE_LOOP);
00219 glVertex2i(0,0);
00220 glVertex2i(x,0);
00221 glVertex2i(x,y);
00222 glVertex2i(0,y);
00223 glEnd();
00224 }
00225
00226
00227