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"
48 #include "OSGCamera.h"
49 #include "OSGViewport.h"
50 #include "OSGTextureObjChunk.h"
52 #include "OSGDrawEnv.h"
55 #include "OSGSkyBackground.h"
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] =
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] =
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) :
107 SkyBackground::SkyBackground(const SkyBackground
&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
,
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
,
147 const Vec3f
* texCoord
)
149 #if !defined(OSG_OGL_COREONLY) || defined(OSG_CHECK_COREONLY)
154 tex
->changeFrom(pEnv
, oldtex
);
161 if(tex
->isTransparent())
163 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
167 // ENRICO: this part holds the informations about
168 // custom texture coordinates
169 // Mess with the best, die like the rest
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()));
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]);
207 if(tex
->isTransparent())
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
|
223 glDisable(GL_LIGHTING
);
224 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
225 glDisable(GL_DEPTH_TEST
);
227 glMatrixMode(GL_MODELVIEW
);
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();
245 if(getBeacon() != NULL
)
247 getBeacon()->getToWorld(t
);
251 m
[3][0] = m
[3][1] = m
[3][2] = 0;
252 glLoadMatrixf(m
.getValues());
254 glMatrixMode(GL_PROJECTION
);
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());
270 UInt32 sr
= _sfSphereRes
.getValue() + 1; // sphere resolution
272 if(_cosval
.size() != sr
)
274 Real32 da
= 2 * Pi
/ (sr
- 1);
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...
291 if(_mfSkyColor
.size() > 0)
293 glClearColor(_mfSkyColor
[0][0], _mfSkyColor
[0][1],
294 _mfSkyColor
[0][2], _mfSkyColor
[0][2]);
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
);
310 static_cast<const GLfloat
*>(_mfSkyColor
[0].getValuesRGBA()));
313 static_cast<const GLfloat
*>(_mfSkyColor
[1].getValuesRGBA()));
315 for(i
= 0; i
< sr
; ++i
)
317 glVertex3f(vsin1
* _sinval
[i
], vcos1
, vsin1
* _cosval
[i
]);
323 for(j
= 0; j
< _mfSkyAngle
.size() - 1; ++j
)
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
]);
347 //if(osgAbs(_mfSkyAngle[j] - Pi) > TypeTraits<Real32>::getDefaultEps())
349 glBegin(GL_TRIANGLE_FAN
);
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
]);
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())
378 static_cast<const GLfloat
* >(
379 _mfGroundColor
[0].getValuesRGBA()));
382 glVertex3f(0, -1, 0);
384 if(_mfGroundColor
.size() > 1)
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
]);
399 for(j
= 0; j
< _mfGroundAngle
.size() - 1; ++j
)
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
]);
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),
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),
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),
464 pTexCoords
= selectTexCoords(
465 getMFTopTexCoord()->size() ? &getMFTopTexCoord()->front() : NULL
,
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),
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),
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),
497 tchunk
->deactivate(pEnv
);
499 Int32 bit
= getClearStencilBit();
506 glClear(GL_DEPTH_BUFFER_BIT
| GL_STENCIL_BUFFER_BIT
);
510 glClear(GL_DEPTH_BUFFER_BIT
);
514 glMatrixMode(GL_MODELVIEW
);
519 glColor3f(1.0, 1.0, 1.0);