OSGPolygonForeground.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <cstdlib>
00044 #include <cstdio>
00045
00046 #include "OSGConfig.h"
00047
00048 #include "OSGViewport.h"
00049 #include "OSGMaterial.h"
00050
00051 #include "OSGPolygonForeground.h"
00052
00053 #include "OSGCamera.h"
00054 #include "OSGBackground.h"
00055 #include "OSGTileCameraDecorator.h"
00056 #include "OSGDrawEnv.h"
00057 #include "OSGRenderActionBase.h"
00058
00059 OSG_USING_NAMESPACE
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 void PolygonForeground::initMethod(InitPhase ePhase)
00075 {
00076 Inherited::initMethod(ePhase);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 PolygonForeground::PolygonForeground(void) :
00091 Inherited()
00092 {
00093 }
00094
00095 PolygonForeground::PolygonForeground(const PolygonForeground &source) :
00096 Inherited(source)
00097 {
00098 }
00099
00100 PolygonForeground::~PolygonForeground(void)
00101 {
00102 }
00103
00104
00105
00106 void PolygonForeground::changed(ConstFieldMaskArg whichField,
00107 UInt32 origin,
00108 BitVector details)
00109 {
00110 Inherited::changed(whichField, origin, details);
00111 }
00112
00113 void PolygonForeground::dump( UInt32 ,
00114 const BitVector ) const
00115 {
00116 SLOG << "Dump PolygonForeground NI" << std::endl;
00117 }
00118
00119 Real32 PolygonForeground::mapCoordinate(Real32 val, Real32 max, bool norm)
00120 {
00121 if(val >= 0)
00122 {
00123 if(norm)
00124 val *= max;
00125 }
00126 else
00127 {
00128 val += 1;
00129
00130 if(norm)
00131 val *= max;
00132
00133 val = max + val;
00134 }
00135
00136 return val;
00137 }
00138
00139 void PolygonForeground::draw(DrawEnv *pEnv, Viewport *pPort)
00140 {
00141 if(getActive() == false)
00142 return;
00143
00144 if(getMFPositions()->size() == 0)
00145 return;
00146
00147 if(pPort->getPixelWidth() == 0 ||
00148 pPort->getPixelHeight() == 0 )
00149 return;
00150
00151 bool bUseTC = true;
00152
00153 if(getMFPositions()->size() != getMFTexCoords()->size())
00154 {
00155 #if 0
00156 FWARNING(("PolygonForeground::draw: positions and texcoords have "
00157 "different sizes (%d vs. %d)!\n",
00158 getMFPositions()->size(), getMFTexCoords()->size()));
00159 return;
00160 #endif
00161
00162 bUseTC = false;
00163 }
00164
00165 glPushAttrib(GL_ALL_ATTRIB_BITS);
00166
00167 Real32 aspectX = 1.0f, aspectY = 1.0f;
00168
00169 if(getAspectHeight() && getAspectWidth())
00170 {
00171 aspectX =
00172 (Real32(pPort->getPixelHeight()) / getAspectHeight()) /
00173 (Real32(pPort->getPixelWidth()) / getAspectWidth());
00174 }
00175
00176 glMatrixMode(GL_MODELVIEW);
00177 glPushMatrix();
00178 glLoadIdentity();
00179
00180 glMatrixMode(GL_PROJECTION);
00181 glPushMatrix();
00182 glLoadIdentity();
00183
00184 Real32 sFac = getScale() > 0 ? getScale() : 1.0f;
00185
00186 UInt32 width = pPort->getPixelWidth(),
00187 height = pPort->getPixelHeight();
00188
00189 if(pEnv->getTileFullSize()[0] != 0 && getTile() == false)
00190 {
00191 width = pEnv->getTileFullSize()[0];
00192 height = pEnv->getTileFullSize()[1];
00193
00194 Real32 t = 0;
00195 #if 0
00196 Real32 left = pEnv->getTileRegion()[0];
00197 Real32 right = pEnv->getTileRegion()[1];
00198 Real32 top = pEnv->getTileRegion()[3];
00199 Real32 bottom = pEnv->getTileRegion()[2];
00200 #endif
00201
00202 if (getAspectHeight() && getAspectWidth() &&
00203 height != 0 && width != 0)
00204 {
00205 aspectX =
00206 (Real32(height/getAspectHeight())) /
00207 (Real32(width / getAspectWidth()));
00208
00209 t = Real32(width) * (1 - aspectX) * 0.5f;
00210 t *= Real32(pPort->getPixelWidth()) / width;
00211 }
00212
00213 Matrix sm = pEnv->calcTileDecorationMatrix();
00214
00215 glLoadMatrixf(sm.getValues());
00216 glOrtho(0, pPort->getPixelWidth(), 0, pPort->getPixelHeight(), 0, 1);
00217
00218 glTranslatef(t, 0, 0);
00219 glScalef(aspectX, aspectY, 1);
00220
00221 float t1 = (1 - sFac) * 0.5f * Real32(pPort->getPixelWidth());
00222 float t2 = (1 - sFac) * 0.5f * Real32(pPort->getPixelHeight());
00223 glTranslatef(t1, t2, 0);
00224 glScalef(sFac,sFac,1);
00225 }
00226 else
00227 {
00228 glScalef(sFac,sFac,1);
00229
00230 glScalef(aspectX, aspectY, 1);
00231
00232 glOrtho(0, pPort->getPixelWidth(), 0, pPort->getPixelHeight(), 0, 1);
00233 }
00234
00235 getMaterial()->getState()->activate(pEnv);
00236
00237 const Vec3f *tc = NULL;
00238
00239 if(bUseTC == true)
00240 {
00241 tc = &((*getMFTexCoords())[0]);
00242 }
00243
00244 const Pnt2f *pos = &((*getMFPositions())[0]);
00245
00246 glBegin(GL_POLYGON);
00247
00248 for(UInt16 i = 0; i < getMFPositions()->size(); i++)
00249 {
00250 if(bUseTC == true)
00251 {
00252 glTexCoord3fv( tc[i].getValues() );
00253 }
00254
00255 glVertex2f( mapCoordinate(pos[i][0], Real32(pPort->getPixelWidth()),
00256 getNormalizedX()),
00257 mapCoordinate(pos[i][1], Real32(pPort->getPixelHeight()),
00258 getNormalizedY()) );
00259 }
00260
00261 glEnd();
00262
00263 getMaterial()->getState()->deactivate(pEnv);
00264
00265 glScalef(1, 1, 1);
00266
00267 glPopMatrix();
00268
00269 glMatrixMode(GL_MODELVIEW);
00270 glPopMatrix();
00271
00272 glPopAttrib();
00273 }