changed: gcc8 base update
[opensg.git] / Source / System / Window / Background / OSGSkyBackground.cpp
blob2d547defa971fdeef3008fc50e5f2abece3012a0
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"
48 #include "OSGCamera.h"
49 #include "OSGViewport.h"
50 #include "OSGTextureObjChunk.h"
51 #include "OSGImage.h"
52 #include "OSGDrawEnv.h"
53 #include "OSGNode.h"
55 #include "OSGSkyBackground.h"
57 #include "OSGGL.h"
59 OSG_USING_NAMESPACE
61 // Documentation for this class is emited in the
62 // OSGSkyBackgroundBase.cpp file.
63 // To modify it, please change the .fcd file (OSGSkyBackground.fcd) and
64 // regenerate the base file.
66 const Vec3f SkyBackground::_defaultTexCoords[7][4] =
68 // 2D default TCs
69 { Vec3f( 0, 0, 0), Vec3f( 1, 0, 0), Vec3f( 1, 1, 0), Vec3f( 0, 1, 0) },
71 // Cubetex Default TCs
72 { Vec3f( 1,-1, 1), Vec3f(-1,-1, 1), Vec3f(-1, 1, 1), Vec3f( 1, 1, 1) },
73 { Vec3f(-1,-1,-1), Vec3f( 1,-1,-1), Vec3f( 1, 1,-1), Vec3f(-1, 1,-1) },
75 { Vec3f(-1,-1, 1), Vec3f( 1,-1, 1), Vec3f( 1,-1,-1), Vec3f(-1,-1,-1) },
76 { Vec3f(-1, 1,-1), Vec3f( 1, 1,-1), Vec3f( 1, 1, 1), Vec3f(-1, 1, 1) },
78 { Vec3f(-1,-1, 1), Vec3f(-1,-1,-1), Vec3f(-1, 1,-1), Vec3f(-1, 1, 1) },
79 { Vec3f( 1,-1,-1), Vec3f( 1,-1, 1), Vec3f( 1, 1, 1), Vec3f( 1, 1,-1) }
82 const Vec3f SkyBackground::_defaultVRMLTexCoords[7][4] =
84 // 2D default TCs
85 { Vec3f( 0, 0, 0), Vec3f( 1, 0, 0), Vec3f( 1, 1, 0), Vec3f( 0, 1, 0) },
87 // Cubetex Default TCs
88 { Vec3f(-1, 1,-1), Vec3f( 1, 1,-1), Vec3f( 1,-1,-1), Vec3f(-1,-1,-1) },
89 { Vec3f( 1, 1, 1), Vec3f(-1, 1, 1), Vec3f(-1,-1, 1), Vec3f( 1,-1, 1) },
91 { Vec3f( 1, 1,-1), Vec3f(-1, 1,-1), Vec3f(-1, 1, 1), Vec3f( 1, 1, 1) },
92 { Vec3f( 1,-1, 1), Vec3f(-1,-1, 1), Vec3f(-1,-1,-1), Vec3f( 1,-1,-1) },
94 { Vec3f( 1, 1,-1), Vec3f( 1, 1, 1), Vec3f( 1,-1, 1), Vec3f( 1,-1,-1) },
95 { Vec3f(-1, 1, 1), Vec3f(-1, 1,-1), Vec3f(-1,-1,-1), Vec3f(-1,-1, 1) }
98 /*----------------------- constructors & destructors ----------------------*/
100 SkyBackground::SkyBackground(void) :
101 Inherited(),
102 _cosval(),
103 _sinval()
107 SkyBackground::SkyBackground(const SkyBackground &source) :
108 Inherited(source ),
109 _cosval (source._cosval),
110 _sinval (source._sinval)
114 SkyBackground::~SkyBackground(void)
118 /*----------------------------- class specific ----------------------------*/
120 void SkyBackground::initMethod(InitPhase ePhase)
122 Inherited::initMethod(ePhase);
125 void SkyBackground::changed(ConstFieldMaskArg whichField,
126 UInt32 origin,
127 BitVector details)
129 Inherited::changed(whichField, origin, details);
132 void SkyBackground::dump( UInt32 ,
133 const BitVector) const
135 SLOG << "Dump SkyBackground NI" << std::endl;
138 /*-------------------------- drawing ---------------------------------*/
140 void SkyBackground::drawFace( DrawEnv * pEnv,
141 TextureBaseChunk * tex,
142 StateChunk *&oldtex,
143 const Pnt3f &p1,
144 const Pnt3f &p2,
145 const Pnt3f &p3,
146 const Pnt3f &p4,
147 const Vec3f * texCoord)
149 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
150 if(tex != NULL)
152 if(oldtex != NULL)
154 tex->changeFrom(pEnv, oldtex);
156 else
158 tex->activate(pEnv);
161 if(tex->isTransparent())
163 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
164 glEnable(GL_BLEND);
167 // ENRICO: this part holds the informations about
168 // custom texture coordinates
169 // Mess with the best, die like the rest
170 glBegin(GL_QUADS);
172 #if 0 // strange crash with par drawers.
173 glTexCoord3fv(static_cast<const GLfloat *>(
174 texCoord[0].getValues()));
175 glVertex3fv (static_cast<const GLfloat *>(p1.getValues()));
177 glTexCoord3fv(static_cast<const GLfloat *>(
178 texCoord[1].getValues()));
179 glVertex3fv (static_cast<const GLfloat *>(p2.getValues()));
181 glTexCoord3fv(static_cast<const GLfloat *>(
182 texCoord[2].getValues()));
183 glVertex3fv (static_cast<const GLfloat *>(p3.getValues()));
185 glTexCoord3fv(static_cast<const GLfloat *>(
186 texCoord[3].getValues()));
187 glVertex3fv (static_cast<const GLfloat *>(p4.getValues()));
188 #else
189 glTexCoord3fv(static_cast<const GLfloat *>(
190 texCoord[0].getValues()));
191 glVertex3f (p1[0], p1[1], p1[2]);
193 glTexCoord3fv(static_cast<const GLfloat *>(
194 texCoord[1].getValues()));
195 glVertex3f (p2[0], p2[1], p2[2]);
197 glTexCoord3fv(static_cast<const GLfloat *>(
198 texCoord[2].getValues()));
199 glVertex3f (p3[0], p3[1], p3[2]);
201 glTexCoord3fv(texCoord[3].getValues());
202 glVertex3f (p4[0], p4[1], p4[2]);
203 #endif
205 glEnd();
207 if(tex->isTransparent())
209 glDisable(GL_BLEND);
212 oldtex = tex;
214 #endif
217 void SkyBackground::clear(DrawEnv *pEnv)
219 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
220 glPushAttrib(GL_POLYGON_BIT | GL_DEPTH_BUFFER_BIT |
221 GL_LIGHTING_BIT);
223 glDisable(GL_LIGHTING);
224 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
225 glDisable(GL_DEPTH_TEST);
227 glMatrixMode(GL_MODELVIEW);
228 glPushMatrix();
230 Matrix m,t;
233 action->getCamera()->getViewing(m, viewport->getPixelWidth(),
234 viewport->getPixelHeight());
235 action->getCamera()->getProjectionTranslation(t,
236 viewport->getPixelWidth(),
237 viewport->getPixelHeight());
240 m = pEnv->getCameraViewing();
241 t = pEnv->getCameraProjectionTrans();
243 m.multLeft(t);
245 if(getBeacon() != NULL)
247 getBeacon()->getToWorld(t);
248 m.mult(t);
251 m[3][0] = m[3][1] = m[3][2] = 0;
252 glLoadMatrixf(m.getValues());
254 glMatrixMode(GL_PROJECTION);
255 glPushMatrix();
257 glLoadIdentity();
258 glTranslatef(0.f, 0.f, 0.5);
259 glScalef(1.f, 1.f, 0.f);
262 action->getCamera()->getProjection(m, viewport->getPixelWidth(),
263 viewport->getPixelHeight());
265 m = pEnv->getCameraProjection();
267 glMultMatrixf(m.getValues());
269 UInt32 i, j;
270 UInt32 sr = _sfSphereRes.getValue() + 1; // sphere resolution
272 if(_cosval.size() != sr)
274 Real32 da = 2 * Pi / (sr - 1);
276 _cosval.resize(sr);
277 _sinval.resize(sr);
279 for(i = 0; i < sr; ++i)
281 _cosval[i] = osgCos(i * da);
282 _sinval[i] = osgSin(i * da);
286 Real32 vcos1,vsin1,vcos2,vsin2;
288 // better always clear and set a defined color...
289 glColor3f(1, 1, 1);
291 if(_mfSkyColor.size() > 0)
293 glClearColor(_mfSkyColor[0][0], _mfSkyColor[0][1],
294 _mfSkyColor[0][2], _mfSkyColor[0][2]);
296 else
298 glClearColor(0, 0, 0, 1);
301 glClear(GL_COLOR_BUFFER_BIT);
303 if(_mfSkyAngle.size() > 0)
305 vcos1 = osgCos(_mfSkyAngle[0]);
306 vsin1 = osgSin(_mfSkyAngle[0]);
308 glBegin(GL_TRIANGLE_FAN);
309 glColor4fv(
310 static_cast<const GLfloat *>(_mfSkyColor[0].getValuesRGBA()));
311 glVertex3f(0, 1, 0);
312 glColor4fv(
313 static_cast<const GLfloat *>(_mfSkyColor[1].getValuesRGBA()));
315 for(i = 0; i < sr; ++i)
317 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
320 glEnd();
323 for(j = 0; j < _mfSkyAngle.size() - 1; ++j)
325 Color4f c1, c2;
327 c1 = _mfSkyColor[j+1];
328 c2 = _mfSkyColor[j+2];
330 vcos1 = osgCos(_mfSkyAngle[j ]);
331 vsin1 = osgSin(_mfSkyAngle[j ]);
332 vcos2 = osgCos(_mfSkyAngle[j+1]);
333 vsin2 = osgSin(_mfSkyAngle[j+1]);
335 glBegin(GL_TRIANGLE_STRIP);
337 for(i = 0; i < sr; ++i)
339 glColor4fv(static_cast<const GLfloat *>(c1.getValuesRGBA()));
340 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
341 glColor4fv(static_cast<const GLfloat *>(c2.getValuesRGBA()));
342 glVertex3f(vsin2 * _sinval[i], vcos2, vsin2 * _cosval[i]);
344 glEnd();
347 //if(osgAbs(_mfSkyAngle[j] - Pi) > TypeTraits<Real32>::getDefaultEps())
349 glBegin(GL_TRIANGLE_FAN);
350 glColor4fv(
351 static_cast<const GLfloat *>(_mfSkyColor[j+1].getValuesRGBA()));
352 glVertex3f(0, -1, 0);
353 vcos1 = osgCos(_mfSkyAngle[j]);
354 vsin1 = osgSin(_mfSkyAngle[j]);
356 for(i = 0; i < sr; ++i)
358 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
361 glEnd();
365 // Draw the ground.
366 // It's possible to be smarter about this, but for now just overdraw.
368 if(_mfGroundAngle.size() > 0)
370 vcos1 = -osgCos(_mfGroundAngle[0]);
371 vsin1 = osgSin(_mfGroundAngle[0]);
373 glBegin(GL_TRIANGLE_FAN);
375 if(_mfGroundColor.size())
377 glColor4fv(
378 static_cast<const GLfloat* >(
379 _mfGroundColor[0].getValuesRGBA()));
382 glVertex3f(0, -1, 0);
384 if(_mfGroundColor.size() > 1)
386 glColor4fv(
387 static_cast<const GLfloat *>(
388 _mfGroundColor[1].getValuesRGBA()));
391 for(i = 0; i < sr; ++i)
393 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
396 glEnd();
399 for(j = 0; j < _mfGroundAngle.size() - 1; ++j)
401 Color4f c1, c2;
403 if (_mfGroundColor.size() > j+2)
405 c1 = _mfGroundColor[j+1];
406 c2 = _mfGroundColor[j+2];
409 vcos1 = -osgCos(_mfGroundAngle[j ]);
410 vsin1 = osgSin(_mfGroundAngle[j ]);
411 vcos2 = -osgCos(_mfGroundAngle[j+1]);
412 vsin2 = osgSin(_mfGroundAngle[j+1]);
414 glBegin(GL_TRIANGLE_STRIP);
416 for(i = 0; i < sr; ++i)
418 glColor4fv(static_cast<const GLfloat *>(c1.getValuesRGBA()));
419 glVertex3f(vsin1 * _sinval[i], vcos1, vsin1 * _cosval[i]);
420 glColor4fv(static_cast<const GLfloat *>(c2.getValuesRGBA()));
421 glVertex3f(vsin2 * _sinval[i], vcos2, vsin2 * _cosval[i]);
423 glEnd();
427 // now draw the textures, if set
428 StateChunk *tchunk = NULL;
429 const Vec3f *pTexCoords;
431 pTexCoords = selectTexCoords(
432 getMFBackTexCoord()->size() ? &getMFBackTexCoord()->front() : NULL,
433 getBackTexture(), 1);
435 drawFace(pEnv, getBackTexture(), tchunk,
436 Pnt3f( 0.5, -0.5, 0.5),
437 Pnt3f(-0.5, -0.5, 0.5),
438 Pnt3f(-0.5, 0.5, 0.5),
439 Pnt3f( 0.5, 0.5, 0.5),
440 pTexCoords );
442 pTexCoords = selectTexCoords(
443 getMFFrontTexCoord()->size() ? &getMFFrontTexCoord()->front() : NULL,
444 getFrontTexture(), 2);
446 drawFace(pEnv, getFrontTexture(), tchunk,
447 Pnt3f(-0.5, -0.5, -0.5),
448 Pnt3f( 0.5, -0.5, -0.5),
449 Pnt3f( 0.5, 0.5, -0.5),
450 Pnt3f(-0.5, 0.5, -0.5),
451 pTexCoords );
453 pTexCoords = selectTexCoords(
454 getMFBottomTexCoord()->size() ? &getMFBottomTexCoord()->front() : NULL,
455 getBottomTexture(), 3);
457 drawFace(pEnv, getBottomTexture(), tchunk,
458 Pnt3f(-0.5, -0.5, 0.5),
459 Pnt3f( 0.5, -0.5, 0.5),
460 Pnt3f( 0.5, -0.5, -0.5),
461 Pnt3f(-0.5, -0.5, -0.5),
462 pTexCoords );
464 pTexCoords = selectTexCoords(
465 getMFTopTexCoord()->size() ? &getMFTopTexCoord()->front() : NULL,
466 getTopTexture(), 4);
468 drawFace(pEnv, getTopTexture(), tchunk,
469 Pnt3f(-0.5, 0.5, -0.5),
470 Pnt3f( 0.5, 0.5, -0.5),
471 Pnt3f( 0.5, 0.5, 0.5),
472 Pnt3f(-0.5, 0.5, 0.5),
473 pTexCoords );
475 pTexCoords = selectTexCoords(
476 getMFLeftTexCoord()->size() ? &getMFLeftTexCoord()->front() : NULL,
477 getLeftTexture(), 5);
479 drawFace(pEnv, getLeftTexture(), tchunk,
480 Pnt3f(-0.5, -0.5, 0.5),
481 Pnt3f(-0.5, -0.5, -0.5),
482 Pnt3f(-0.5, 0.5, -0.5),
483 Pnt3f(-0.5, 0.5, 0.5),
484 pTexCoords );
486 pTexCoords = selectTexCoords(
487 getMFRightTexCoord()->size() ? &getMFRightTexCoord()->front() : NULL,
488 getRightTexture(), 6);
490 drawFace(pEnv, getRightTexture(), tchunk,
491 Pnt3f( 0.5, -0.5, -0.5),
492 Pnt3f( 0.5, -0.5, 0.5),
493 Pnt3f( 0.5, 0.5, 0.5),
494 Pnt3f( 0.5, 0.5, -0.5),
495 pTexCoords );
496 if(tchunk != NULL)
497 tchunk->deactivate(pEnv);
499 Int32 bit = getClearStencilBit();
501 glClearDepth(1.f);
503 if(bit >= 0)
505 glClearStencil(bit);
506 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
508 else
510 glClear(GL_DEPTH_BUFFER_BIT);
513 glPopMatrix();
514 glMatrixMode(GL_MODELVIEW);
515 glPopMatrix();
517 glPopAttrib();
519 glColor3f(1.0, 1.0, 1.0);
520 #endif