Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

node/skybox.cpp

00001 #include "skybox.h"
00002 
00003 #include <math/matrix4.h>
00004 #include <math/umatrix.h>
00005 
00006 #include <glt/rgb.h>
00007 #include <glt/glu.h>
00008 #include <glt/error.h>
00009 #include <glt/viewport.h>
00010 
00011 GltSkyBox::GltSkyBox()
00012 : _fov(65.0)
00013 {
00014     color() = white;
00015 
00016     _positiveX.setWrap(GL_CLAMP,GL_CLAMP);
00017     _negativeX.setWrap(GL_CLAMP,GL_CLAMP);
00018     _positiveY.setWrap(GL_CLAMP,GL_CLAMP);
00019     _negativeY.setWrap(GL_CLAMP,GL_CLAMP);
00020     _positiveZ.setWrap(GL_CLAMP,GL_CLAMP);
00021     _negativeZ.setWrap(GL_CLAMP,GL_CLAMP);
00022 }
00023 
00024 GltSkyBox::~GltSkyBox()
00025 {
00026     clear();
00027 }
00028 
00029 void 
00030 GltSkyBox::clear()
00031 {
00032     _positiveX.clear();
00033     _negativeX.clear();
00034     _positiveY.clear();
00035     _negativeY.clear();
00036     _positiveZ.clear();
00037     _negativeZ.clear();
00038 }
00039 
00040 void 
00041 GltSkyBox::draw() const
00042 {
00043     if (!visible())
00044         return;
00045         
00046     GLERROR
00047 
00048     GltViewport viewport(true);
00049 
00050     //
00051     // Setup perspective camera mode,
00052     // orthogonal doesn't really work...
00053     //
00054 
00055     glMatrixMode(GL_PROJECTION);
00056     glPushMatrix();
00057     glLoadIdentity();
00058 
00059     gluPerspective(_fov,double(viewport.width())/double(viewport.height()), 0.1, 200.0);
00060 
00061     // Estimate vertical angle based on viewers distance from
00062     // on-screen window.  TODO - Make use of maximum screen size...
00063 
00064 //  const double alpha = atan(double(viewport.height())/1600.0)*M_DEG_PI;
00065 //  gluPerspective(alpha,double(viewport.width())/double(viewport.height()),0.1,5.0);
00066 
00067     //
00068     // Twiddle the current modelview matrix
00069     // to cancel out translation and scale.
00070     //
00071 
00072     glMatrixMode(GL_MODELVIEW);
00073     glPushMatrix();
00074 
00075     Matrix      matrix(GL_MODELVIEW_MATRIX);
00076 
00077     // No translation
00078 
00079     matrix[12] = 0.0;
00080     matrix[13] = 0.0;
00081     matrix[14] = 0.0;
00082 
00083     // No scale
00084 
00085     const real sf = sqrt( SQ(matrix[0]) + SQ(matrix[1]) + SQ(matrix[2]) ); 
00086     matrix *= matrixScale(1.0/sf);
00087 
00088     //
00089 
00090     matrix.glLoadMatrix();
00091 
00092     //
00093     //
00094     //
00095 
00096         GLERROR
00097 
00098         glPushAttrib(GL_ENABLE_BIT|GL_POLYGON_BIT);
00099 
00100             glDisable(GL_DEPTH_TEST);
00101             glDisable(GL_LIGHTING);
00102             glEnable(GL_CULL_FACE);
00103 
00104             if (solid())
00105             {
00106                 glEnable(GL_TEXTURE_2D);
00107                 glEnable(GL_BLEND);
00108                 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00109                 glColor();
00110                 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
00111             }
00112             else
00113             {
00114                 white.glColor();
00115                 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
00116             }
00117 
00118             // +X
00119 
00120             _positiveX.set();
00121             glBegin(GL_QUADS);
00122                 glTexCoord2f(0,0);  glVertex3f( 1,-1,-1);
00123                 glTexCoord2f(1,0);  glVertex3f( 1,-1, 1);
00124                 glTexCoord2f(1,1);  glVertex3f( 1, 1, 1);
00125                 glTexCoord2f(0,1);  glVertex3f( 1, 1,-1);
00126             glEnd();
00127 
00128             // -X
00129 
00130             _negativeX.set();
00131             glBegin(GL_QUADS);
00132                 glTexCoord2f(0,0);  glVertex3f(-1,-1, 1);
00133                 glTexCoord2f(1,0);  glVertex3f(-1,-1,-1);
00134                 glTexCoord2f(1,1);  glVertex3f(-1, 1,-1);
00135                 glTexCoord2f(0,1);  glVertex3f(-1, 1, 1);
00136             glEnd();
00137 
00138             // +Y
00139 
00140             _positiveY.set();
00141             glBegin(GL_QUADS);
00142                 glTexCoord2f(0,0);  glVertex3f(-1, 1,-1);
00143                 glTexCoord2f(1,0);  glVertex3f( 1, 1,-1);
00144                 glTexCoord2f(1,1);  glVertex3f( 1, 1, 1);
00145                 glTexCoord2f(0,1);  glVertex3f(-1, 1, 1);
00146             glEnd();
00147 
00148             // -Y
00149 
00150             _negativeY.set();
00151             glBegin(GL_QUADS);
00152                 glTexCoord2f(0,0);  glVertex3f(-1,-1, 1);
00153                 glTexCoord2f(1,0);  glVertex3f( 1,-1, 1);
00154                 glTexCoord2f(1,1);  glVertex3f( 1,-1,-1);
00155                 glTexCoord2f(0,1);  glVertex3f(-1,-1,-1);
00156             glEnd();
00157 
00158             // +Z
00159 
00160             _positiveZ.set();
00161             glBegin(GL_QUADS);
00162                 glTexCoord2f(0,0);  glVertex3f( 1,-1, 1);
00163                 glTexCoord2f(1,0);  glVertex3f(-1,-1, 1);
00164                 glTexCoord2f(1,1);  glVertex3f(-1, 1, 1);
00165                 glTexCoord2f(0,1);  glVertex3f( 1, 1, 1);
00166             glEnd();
00167 
00168             // -Z
00169 
00170             _negativeZ.set();
00171             glBegin(GL_QUADS);
00172                 glTexCoord2f(0,0);  glVertex3f(-1,-1,-1);
00173                 glTexCoord2f(1,0);  glVertex3f( 1,-1,-1);
00174                 glTexCoord2f(1,1);  glVertex3f( 1, 1,-1);
00175                 glTexCoord2f(0,1);  glVertex3f(-1, 1,-1);
00176             glEnd();
00177 
00178         glPopAttrib();
00179 
00180         GLERROR
00181 
00182     glPopMatrix();
00183     glMatrixMode(GL_PROJECTION);
00184     glPopMatrix();
00185     glMatrixMode(GL_MODELVIEW);
00186 
00187     GLERROR
00188 }
00189 
00190 GltTexture &GltSkyBox::positiveX() { return _positiveX; }
00191 GltTexture &GltSkyBox::negativeX() { return _negativeX; }
00192 GltTexture &GltSkyBox::positiveY() { return _positiveY; }
00193 GltTexture &GltSkyBox::negativeY() { return _negativeY; }
00194 GltTexture &GltSkyBox::positiveZ() { return _positiveZ; }
00195 GltTexture &GltSkyBox::negativeZ() { return _negativeZ; }
00196 
00197       GLdouble &GltSkyBox::fov()        { return _fov; }
00198 const GLdouble  GltSkyBox::fov() const  { return _fov; }

Generated on Tue Nov 5 11:11:05 2002 for GLT by doxygen1.2.18