changed: gcc8 base update
[opensg.git] / Source / System / Window / Background / OSGTextureBackground.cpp
blob1a2af8e8973b216745d2f4f426107062a8c5bfee
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
47 #include "OSGViewport.h"
48 #include "OSGTextureObjChunk.h"
50 #include "OSGTextureBackground.h"
52 #include<iostream>
54 OSG_USING_NAMESPACE
56 // Documentation for this class is emited in the
57 // OSGTextureBackgroundBase.cpp file.
58 // To modify it, please change the .fcd file (OSGTextureBackground.fcd) and
59 // regenerate the base file.
61 /***************************************************************************\
62 * Class variables *
63 \***************************************************************************/
65 /***************************************************************************\
66 * Class methods *
67 \***************************************************************************/
69 void TextureBackground::initMethod(InitPhase ePhase)
71 Inherited::initMethod(ePhase);
75 /***************************************************************************\
76 * Instance methods *
77 \***************************************************************************/
79 /*-------------------------------------------------------------------------*\
80 - private -
81 \*-------------------------------------------------------------------------*/
83 /*----------------------- constructors & destructors ----------------------*/
85 TextureBackground::TextureBackground(void) :
86 Inherited ( ),
87 _textureCoordArray ( ),
88 _vertexCoordArray ( ),
89 _indexArray ( ),
90 _hor (0 ),
91 _vert (0 ),
92 _radialDistortion (0.f),
93 _centerOfDistortion( )
97 TextureBackground::TextureBackground(const TextureBackground &source) :
98 Inherited (source ),
99 _textureCoordArray (source._textureCoordArray ),
100 _vertexCoordArray (source._vertexCoordArray ),
101 _indexArray (source._indexArray ),
102 _hor (source._hor ),
103 _vert (source._vert ),
104 _radialDistortion (source._radialDistortion ),
105 _centerOfDistortion(source._centerOfDistortion)
109 TextureBackground::~TextureBackground(void)
113 /*----------------------------- class specific ----------------------------*/
118 void TextureBackground::changed(ConstFieldMaskArg whichField,
119 UInt32 origin,
120 BitVector details)
122 Inherited::changed(whichField, origin, details);
124 // all updates are handled in updateGrid()
128 void TextureBackground::updateGrid(void)
130 bool gridChanged=( (getHor() != _hor) ||
131 (getVert() != _vert) );
133 if(gridChanged)
135 //resize grid
136 UInt32 gridCoords=(getHor()+2)*(getVert()+2);
138 _textureCoordArray.resize(gridCoords);
139 _vertexCoordArray .resize(gridCoords);
141 int indexArraySize=(getHor()+2)*((getVert()+1)*2);
142 _indexArray.resize(indexArraySize);
144 _hor = getHor();
145 _vert = getVert();
148 if(gridChanged || _radialDistortion != getRadialDistortion() ||
149 _centerOfDistortion != getCenterOfDistortion() )
151 _radialDistortion = getRadialDistortion();
152 _centerOfDistortion = getCenterOfDistortion();
154 // calculate grid coordinates and triangle strip indices
155 float xStep=1.0/float(getHor()+1);
156 float yStep=1.0/float(getVert()+1);
157 std::vector<Vec2f>::iterator texCoord=_textureCoordArray.begin();
158 std::vector<Vec2f>::iterator vertexCoord=_vertexCoordArray.begin();
159 std::vector<UInt32>::iterator index=_indexArray.begin();
160 UInt32 coord0(0),coord1(0);
161 GLfloat x,y;
162 Int16 xx,yy;
163 Int16 xxmax=getHor()+2,yymax=getVert()+2;
164 for(yy=0,y=0.0f;yy<yymax;yy++,y+=yStep)
166 if(yy>0)
168 coord1=yy*xxmax;
169 coord0=coord1-xxmax;
170 *index++=coord1++;
171 *index++=coord0++;
173 float dy=y-getCenterOfDistortion().y();
174 float dy2=dy*dy;
175 for(xx=0,x=0.0f;xx<xxmax;xx++,x+=xStep)
177 *texCoord++=Vec2f(x,y);
178 float dx=(x-getCenterOfDistortion().x());
179 float dx2=dx*dx;
180 float dist2=dx2+dy2;
181 float deltaX=dx*getRadialDistortion()*dist2;
182 float deltaY=dy*getRadialDistortion()*dist2;
183 *vertexCoord++=Vec2f(x+deltaX,y+deltaY);
184 if(yy>0&&xx>0)
186 *index++=coord1++;
187 *index++=coord0++;
194 void TextureBackground::clear(DrawEnv *pEnv)
196 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
197 TextureBaseChunk *tex = getTexture();
199 if(tex == NULL)
201 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
202 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
203 return;
206 glPushAttrib(GL_POLYGON_BIT | GL_DEPTH_BUFFER_BIT |
207 GL_LIGHTING_BIT);
209 glDisable(GL_LIGHTING);
211 #if 1
212 // original mode
213 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
214 #else
215 // for testing the grid
216 glColor3f(1.0f, 1.0f, 1.0f);
217 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
218 #endif
219 glClear(GL_DEPTH_BUFFER_BIT);
221 glDisable(GL_DEPTH_TEST);
222 glDepthFunc(GL_ALWAYS);
223 glDepthMask(GL_FALSE);
225 glMatrixMode(GL_MODELVIEW);
226 glPushMatrix();
227 glLoadIdentity();
229 glMatrixMode(GL_PROJECTION);
230 glPushMatrix();
232 glLoadIdentity();
233 glOrtho(0, 1, 0, 1, 0, 1);
235 glColor4fv(getColor().getValuesRGBA());
237 tex->activate(pEnv);
239 if(tex->isTransparent())
241 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
242 glEnable(GL_BLEND);
244 if(osgAbs(getRadialDistortion()) < TypeTraits<Real32>::getDefaultEps())
246 if(getMFTexCoords()->size() < 4)
248 // set some default texture coordinates.
249 glBegin(GL_QUADS);
250 glTexCoord2f(0.0f, 0.0f);
251 glVertex3f(0.0f, 0.0f, 0.0f);
252 glTexCoord2f(1.0f, 0.0f);
253 glVertex3f(1.0f, 0.0f, 0.0f);
254 glTexCoord2f(1.0f, 1.0f);
255 glVertex3f(1.0f, 1.0f, 0.0f);
256 glTexCoord2f(0.0f, 1.0f);
257 glVertex3f(0.0f, 1.0f, 0.0f);
258 glEnd();
260 else
262 glBegin(GL_QUADS);
264 glTexCoord2f(getTexCoords(0).getValues()[0],
265 getTexCoords(0).getValues()[1]);
266 glVertex3f(0.0f, 0.0f, 0.0f);
267 glTexCoord2f(getTexCoords(1).getValues()[0],
268 getTexCoords(1).getValues()[1]);
269 glVertex3f(1.0f, 0.0f, 0.0f);
270 glTexCoord2f(getTexCoords(2).getValues()[0],
271 getTexCoords(2).getValues()[1]);
272 glVertex3f(1.0f, 1.0f, 0.0f);
273 glTexCoord2f(getTexCoords(3).getValues()[0],
274 getTexCoords(3).getValues()[1]);
275 glVertex3f(0.0f, 1.0f, 0.0f);
277 glEnd();
280 else // map texture to distortion grid
282 updateGrid();
283 Int16 xxmax=getHor()+2,yymax=getVert()+2;
285 UInt32 gridCoords=xxmax*yymax;
286 UInt32 indexArraySize=xxmax*((getVert()+1)*2);
288 if(_vertexCoordArray.size()==gridCoords &&
289 _textureCoordArray.size()==gridCoords &&
290 _indexArray.size()==indexArraySize)
292 // clear background, because possibly the distortion grid
293 // could not cover th whole window
294 glClearColor(.5f, 0.5f, 0.5f, 1.0f);
295 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
296 UInt32 yMax=getVert()+1;
297 for(UInt32 y=0;y<yMax;y++)
299 glBegin(GL_TRIANGLE_STRIP);
300 std::vector<UInt32>::iterator begin=_indexArray.begin()+(y*2*xxmax);
301 std::vector<UInt32>::iterator end=begin+2*xxmax;
302 for(std::vector<UInt32>::iterator i=begin;i!=end;i++)
304 glTexCoord2fv(_textureCoordArray[*i].getValues());
305 glVertex2fv(_vertexCoordArray[*i].getValues());
308 glEnd();
312 if(tex->isTransparent())
314 glDisable(GL_BLEND);
317 tex->deactivate(pEnv);
319 Int32 bit = getClearStencilBit();
321 if (bit >= 0)
323 glClearStencil(bit);
324 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
326 else
328 glClear(GL_DEPTH_BUFFER_BIT);
331 glPopMatrix();
332 glMatrixMode(GL_MODELVIEW);
333 glPopMatrix();
335 glPopAttrib();
337 glColor3f(1.0f, 1.0f, 1.0f);
338 #endif
342 void TextureBackground::dump( UInt32 ,
343 const BitVector ) const
345 SLOG << "Dump TextureBackground NI" << std::endl;