1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
47 #include "OSGViewport.h"
48 #include "OSGTextureObjChunk.h"
50 #include "OSGTextureBackground.h"
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 /***************************************************************************\
63 \***************************************************************************/
65 /***************************************************************************\
67 \***************************************************************************/
69 void TextureBackground::initMethod(InitPhase ePhase
)
71 Inherited::initMethod(ePhase
);
75 /***************************************************************************\
77 \***************************************************************************/
79 /*-------------------------------------------------------------------------*\
81 \*-------------------------------------------------------------------------*/
83 /*----------------------- constructors & destructors ----------------------*/
85 TextureBackground::TextureBackground(void) :
87 _textureCoordArray ( ),
88 _vertexCoordArray ( ),
92 _radialDistortion (0.f
),
93 _centerOfDistortion( )
97 TextureBackground::TextureBackground(const TextureBackground
&source
) :
99 _textureCoordArray (source
._textureCoordArray
),
100 _vertexCoordArray (source
._vertexCoordArray
),
101 _indexArray (source
._indexArray
),
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
,
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
) );
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
);
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);
163 Int16 xxmax
=getHor()+2,yymax
=getVert()+2;
164 for(yy
=0,y
=0.0f
;yy
<yymax
;yy
++,y
+=yStep
)
173 float dy
=y
-getCenterOfDistortion().y();
175 for(xx
=0,x
=0.0f
;xx
<xxmax
;xx
++,x
+=xStep
)
177 *texCoord
++=Vec2f(x
,y
);
178 float dx
=(x
-getCenterOfDistortion().x());
181 float deltaX
=dx
*getRadialDistortion()*dist2
;
182 float deltaY
=dy
*getRadialDistortion()*dist2
;
183 *vertexCoord
++=Vec2f(x
+deltaX
,y
+deltaY
);
194 void TextureBackground::clear(DrawEnv
*pEnv
)
196 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
197 TextureBaseChunk
*tex
= getTexture();
201 glClearColor(0.0f
, 0.0f
, 0.0f
, 1.0f
);
202 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
206 glPushAttrib(GL_POLYGON_BIT
| GL_DEPTH_BUFFER_BIT
|
209 glDisable(GL_LIGHTING
);
213 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
215 // for testing the grid
216 glColor3f(1.0f
, 1.0f
, 1.0f
);
217 glPolygonMode(GL_FRONT_AND_BACK
, GL_LINE
);
219 glClear(GL_DEPTH_BUFFER_BIT
);
221 glDisable(GL_DEPTH_TEST
);
222 glDepthFunc(GL_ALWAYS
);
223 glDepthMask(GL_FALSE
);
225 glMatrixMode(GL_MODELVIEW
);
229 glMatrixMode(GL_PROJECTION
);
233 glOrtho(0, 1, 0, 1, 0, 1);
235 glColor4fv(getColor().getValuesRGBA());
239 if(tex
->isTransparent())
241 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
244 if(osgAbs(getRadialDistortion()) < TypeTraits
<Real32
>::getDefaultEps())
246 if(getMFTexCoords()->size() < 4)
248 // set some default texture coordinates.
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
);
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
);
280 else // map texture to distortion grid
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());
312 if(tex
->isTransparent())
317 tex
->deactivate(pEnv
);
319 Int32 bit
= getClearStencilBit();
324 glClear(GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
328 glClear(GL_DEPTH_BUFFER_BIT
);
332 glMatrixMode(GL_MODELVIEW
);
337 glColor3f(1.0f
, 1.0f
, 1.0f
);
342 void TextureBackground::dump( UInt32
,
343 const BitVector
) const
345 SLOG
<< "Dump TextureBackground NI" << std::endl
;