00001 #include "skyspher.h"
00002
00008 #include <glt/gl.h>
00009 #include <glt/glu.h>
00010
00011 #include <glt/viewport.h>
00012 #include <glt/error.h>
00013 #include <glt/rgb.h>
00014
00015 template<class T>
00016 void cosTable(T *val,const T min,const T max,const uint32 n)
00017 {
00018 assert(val);
00019 const T delta = (max-min)/n;
00020 T j = min;
00021 uint32 i;
00022 for (i=0; i<n-1; i++,j+=delta)
00023 val[i] = static_cast<T>(cos(j));
00024 val[i] = static_cast<T>(cos(max));
00025 }
00026
00027 template<class T>
00028 void sinTable(T *val,const T min,const T max,const uint32 n)
00029 {
00030 assert(val);
00031 const T delta = (max-min)/n;
00032 T j = min;
00033 uint32 i;
00034 for (i=0; i<n-1; i++,j+=delta)
00035 val[i] = static_cast<T>(sin(j));
00036 val[i] = static_cast<T>(sin(max));
00037 }
00038
00039
00040 GltSkySphere::GltSkySphere()
00041 : _fov(65.0), _slices(18)
00042 {
00043 }
00044
00045 GltSkySphere::~GltSkySphere()
00046 {
00047 }
00048
00049 void
00050 GltSkySphere::draw() const
00051 {
00052 if (!visible())
00053 return;
00054
00055 GLERROR
00056
00057 GltViewport viewport(true);
00058
00059
00060
00061
00062
00063
00064 glMatrixMode(GL_PROJECTION);
00065 glPushMatrix();
00066 glLoadIdentity();
00067
00068 gluPerspective(_fov,double(viewport.width())/double(viewport.height()), 0.1, 200.0);
00069
00070
00071
00072
00073
00074
00075 glMatrixMode(GL_MODELVIEW);
00076 glPushMatrix();
00077
00078 Matrix matrix(GL_MODELVIEW_MATRIX);
00079
00080
00081
00082 matrix[12] = 0.0;
00083 matrix[13] = 0.0;
00084 matrix[14] = 0.0;
00085
00086
00087
00088 const real sf = sqrt( SQ(matrix[0]) + SQ(matrix[1]) + SQ(matrix[2]) );
00089 matrix *= matrixScale(1.0/sf);
00090
00091
00092
00093 matrix.glLoadMatrix();
00094 transformation().glMultMatrix();
00095
00096
00097
00098
00099
00100 GLERROR
00101
00102 glPushAttrib(GL_ENABLE_BIT|GL_POLYGON_BIT|GL_LIGHTING_BIT);
00103
00104 glDisable(GL_DEPTH_TEST);
00105 glDisable(GL_LIGHTING);
00106 glDisable(GL_TEXTURE_2D);
00107 glEnable(GL_CULL_FACE);
00108
00109 if (solid())
00110 {
00111 glEnable(GL_BLEND);
00112 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00113 glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
00114 glShadeModel(GL_SMOOTH);
00115 }
00116 else
00117 {
00118 glDisable(GL_BLEND);
00119 glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
00120 glShadeModel(GL_FLAT);
00121 }
00122
00123
00124
00125 drawSphere(_map,_slices);
00126
00127
00128
00129 glPopAttrib();
00130
00131 GLERROR
00132
00133 glPopMatrix();
00134 glMatrixMode(GL_PROJECTION);
00135 glPopMatrix();
00136 glMatrixMode(GL_MODELVIEW);
00137
00138 GLERROR
00139 }
00140
00141 void
00142 GltSkySphere::drawSphere(const GltColorMap &colmap,const int slices)
00143 {
00144 if (colmap.size()<2)
00145 return;
00146
00147 glBegin(GL_QUADS);
00148
00149 float *x = new float[slices];
00150 float *z = new float[slices];
00151 sinTable<float>(x,0.0f,float(M_2PI),slices);
00152 cosTable<float>(z,0.0f,float(M_2PI),slices);
00153
00154 for (int i=0; i<colmap.size()-1; i++)
00155 {
00156 const real a1 = colmap.lookup(i);
00157 const real a2 = colmap.lookup(i+1);
00158
00159 const float y1 = cos(a1);
00160 const float y2 = cos(a2);
00161 const float s1 = sin(a1);
00162 const float s2 = sin(a2);
00163
00164 const GltColor c1 = colmap.lookup(a1);
00165 const GltColor c2 = colmap.lookup(a2);
00166
00167 for (int j=0; j<slices-1; j++)
00168 {
00169 const float &x1 = x[j];
00170 const float &x2 = x[j+1];
00171 const float &z1 = z[j];
00172 const float &z2 = z[j+1];
00173
00174 c1.glColor();
00175 glVertex3f(x1*s1,y1,z1*s1);
00176 glVertex3f(x2*s1,y1,z2*s1);
00177 c2.glColor();
00178 glVertex3f(x2*s2,y2,z2*s2);
00179 glVertex3f(x1*s2,y2,z1*s2);
00180 }
00181 }
00182
00183 delete [] x;
00184 delete [] z;
00185
00186 glEnd();
00187 }
00188
00189 void
00190 GltSkySphere::clear()
00191 {
00192 _map.clear();
00193 }
00194
00195 GltColorMap &GltSkySphere::map() { return _map; }
00196
00197 GLdouble &GltSkySphere::fov() { return _fov; }
00198 const GLdouble GltSkySphere::fov() const { return _fov; }
00199