tex-sphere.cpp

Go to the documentation of this file.
00001 /*
00002  * tex-sphere.cpp
00003  *
00004  * Copyright (C) 2011  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Texture-mapped sphere code
00032  *
00033  * This code adapted from the excellent tutorial here:
00034  *      http://www.swiftless.com/tutorials/opengl/sphere.html
00035  */
00036 
00037 // includes --------------------------------------------------------------------
00038 #include "opengl-effects.h"             // always include our own header first!
00039 
00040 #include "perf/perf.h"
00041 #include "wave-glut/texture.h"
00042 
00043 
00044 #include <GL/glut.h>
00045 #include <stdio.h>
00046 #include <math.h>
00047 
00048 
00049 namespace glut {
00050 
00051 
00052 // sphere coordinates: lists the xyz coordinate on the surface of the (unit)
00053 //      sphere, plus the texture uv coordinates there.
00054 struct sphere_vertex_t {
00055         // data fields
00056         float   x;
00057         float   y;
00058         float   z;
00059         float   u;
00060         float   v;
00061 };
00062 
00063 
00064 // how finely do we slice the sphere?  Want something that divides 90 evenly
00065 static const int s_degreesPerSlice = 9;
00066 
00067 static const float s_radiansPerDegree = M_PI / 180.0;
00068 
00069 // we have a map for a quarter-rotation (90 degrees), with 4 points per
00070 // basically a map of the northern hemisphere, which is easy to flip for the
00071 //      south
00072 static const int s_vertexCount =
00073                 4 * (90 / s_degreesPerSlice) * (360 / s_degreesPerSlice);
00074 
00075 
00076 
00077 ////////////////////////////////////////////////////////////////////////////////
00078 //
00079 //      static helper methods
00080 //
00081 ////////////////////////////////////////////////////////////////////////////////
00082 
00083 static const sphere_vertex_t *
00084 getSphereVertices
00085 (
00086 void
00087 )
00088 {
00089         // this is the static map that describes how to render (and map) a
00090         //      (hemi)sphere
00091         static sphere_vertex_t s_vertices[s_vertexCount];
00092 
00093         // caller is asking for the struct that maps from sphere xyz coordinates
00094         //      to texture UV coordinates.  We calculate this once, and return
00095         //      the cached (static) result from then on.
00096         static bool s_haveVertices = false;
00097         if (s_haveVertices) {
00098                 return s_vertices;
00099         }
00100 
00101         perf::Timer timer("construct-sphere-map");
00102 
00103         // first time this has been called: construct vertex array
00104         // note that we create this array for a sphere of radius = 1, with no
00105         //      translations.  Callers can scale and translate as needed
00106         sphere_vertex_t * psv = s_vertices;
00107         const float inv360 = 1.0 / 360.0;
00108         const float inv180 = 1.0 / 180.0;
00109 //      const float inv360 = 1.0 / 720.0;
00110 //      const float inv180 = 1.0 / 360.0;
00111         float sinBplus = 0.0;   // sin(0)
00112         float cosBplus = 1.0;   // cos(0)
00113 
00114         // loop through northern latitudes (90 degrees only)
00115         for (int b = 0; b < 90; b += s_degreesPerSlice) {
00116                 float sinB = sinBplus;
00117                 float cosB = cosBplus;
00118 
00119                 //DPRINTF("b=%02d degrees, sinB=%5.3f  cosB=%5.3f", b, sinB, cosB);
00120                 sinBplus = sin((b + s_degreesPerSlice) * s_radiansPerDegree);
00121                 cosBplus = cos((b + s_degreesPerSlice) * s_radiansPerDegree);
00122 
00123                 float sinAplus = 0.0;   // sin(0)
00124                 float cosAplus = 1.0;   // cos(0)
00125 
00126                 // loop through longitudes (full 360)
00127                 for (int a = 0; a < 360; a += s_degreesPerSlice) {
00128 
00129                         float sinA = sinAplus;
00130                         float cosA = cosAplus;
00131 
00132                         sinAplus = sin((a + s_degreesPerSlice) * s_radiansPerDegree);
00133                         cosAplus = cos((a + s_degreesPerSlice) * s_radiansPerDegree);
00134 
00135                         // calculate the sphere at this point, plus 3 adjacent
00136                         //      points (one slice advanced in each direction)
00137                         psv->x = sinA * sinB;
00138                         psv->y = cosA * sinB;
00139                         psv->z = cosB;
00140                         psv->u = a * inv360;
00141                         psv->v = b * inv180;
00142         //              DPRINTF("a=%02d,b=%02d  u=%5.3f,v=%5.3f", a, b, psv->u, psv->v);
00143                         ++psv;
00144 
00145                         psv->x = sinA * sinBplus;
00146                         psv->y = cosA * sinBplus;
00147                         psv->z = cosBplus;
00148                         psv->u = a * inv360;
00149                         psv->v = (b + s_degreesPerSlice) * inv180;
00150                         ++psv;
00151 
00152                         psv->x = sinAplus * sinB;
00153                         psv->y = cosAplus * sinB;
00154                         psv->z = cosB;
00155                         psv->u = (a + s_degreesPerSlice) * inv360;
00156                         psv->v = b * inv180;
00157                         ++psv;
00158 
00159                         psv->x = sinAplus * sinBplus;
00160                         psv->y = cosAplus * sinBplus;
00161                         psv->z = cosBplus;
00162                         psv->u = (a + s_degreesPerSlice) * inv360;
00163                         psv->v = (b + s_degreesPerSlice) * inv180;
00164                         ++psv;
00165                 }
00166         }
00167 
00168         // verify we counted correctly
00169         ASSERT(psv - s_vertices == s_vertexCount,
00170            "Incorrect vertex count?");
00171 
00172         // all done!
00173         s_haveVertices = true;
00174         return s_vertices;
00175 }
00176 
00177 
00178 
00179 class TexSphere : public Renderable {
00180 public:
00181         // constructor, destructor ---------------------------------------------
00182         ~TexSphere(void) throw() { }
00183 
00184         // public class methods ------------------------------------------------
00185         void initialize(IN const tex_sphere_t& ts);
00186 
00187         // glut::Renderable class interface methods ----------------------------
00188         void render(IN const render_context_t& rc,
00189                                 IN RenderQueue * rq);
00190         rect3d_t getBoundingBox(void) const throw();
00191 
00192 private:
00193         // private typedefs ----------------------------------------------------
00194         // private helper methods ----------------------------------------------
00195         // private member data -------------------------------------------------
00196         float           m_radius;
00197         int             m_textureId;
00198 };
00199 
00200 
00201 void
00202 TexSphere::initialize
00203 (
00204 IN const tex_sphere_t& ts
00205 )
00206 {
00207         ASSERT(ts.radius > 0, "bad sphere radius: %f", ts.radius);
00208         ASSERT(ts.textureId > -1, "bad texture id: %d", ts.textureId);
00209 
00210         m_radius = ts.radius;
00211         m_textureId = ts.textureId;
00212 }
00213 
00214 
00215 
00216 void
00217 TexSphere::render
00218 (
00219 IN const render_context_t& rc,
00220 IN RenderQueue * rq
00221 )
00222 {
00223         perf::Timer timer("TexSphere::render");
00224         ASSERT(m_radius > 0, "bad radius");
00225 
00226         // set up texture
00227         glEnable(GL_TEXTURE_2D);
00228         glBindTexture(GL_TEXTURE_2D, m_textureId);
00229 
00230 //      glDisable(GL_CULL_FACE);
00231 
00232         // update modelview matrix for radius
00233         glMatrixMode(GL_MODELVIEW);
00234         glPushMatrix();
00235 //      glRotatef(90, 1, 0, 0);
00236         glScalef(m_radius, m_radius, m_radius);
00237 
00238         const sphere_vertex_t * v0 = getSphereVertices();
00239 
00240         // render the top and bottom hemispheres as triangle strips
00241         glBegin(GL_TRIANGLE_STRIP);
00242         const sphere_vertex_t * psv = v0;
00243         for (int b = 0; b < s_vertexCount; ++b, ++psv) {
00244                 glTexCoord2f(psv->u, 1.0 - psv->v);
00245                 glVertex3f(psv->x, psv->z, psv->y);
00246         }
00247         glEnd();
00248 
00249         // reset for second hemisphere
00250         glBegin(GL_TRIANGLE_STRIP);
00251         psv = v0;
00252         for (int b = 0; b < s_vertexCount; ++b, ++psv) {
00253                 glTexCoord2f(-psv->u, psv->v);
00254                 glVertex3f(-psv->x, -psv->z, psv->y);
00255         }    
00256         glEnd();
00257 
00258         // clean up OpenGL context
00259         glPopMatrix();
00260         glDisable(GL_TEXTURE_2D);
00261 }
00262 
00263 
00264 
00265 rect3d_t
00266 TexSphere::getBoundingBox
00267 (
00268 void
00269 )
00270 const
00271 throw()
00272 {
00273         rect3d_t r;
00274         r.clear();
00275         r.inflate(m_radius);
00276         return r;
00277 }
00278 
00279 
00280 
00281 ////////////////////////////////////////////////////////////////////////////////
00282 //
00283 //      public API
00284 //
00285 ////////////////////////////////////////////////////////////////////////////////
00286 
00287 smart_ptr<Renderable>
00288 createMappedSphere
00289 (
00290 IN const tex_sphere_t& ts
00291 )
00292 {
00293         smart_ptr<TexSphere> local = new TexSphere;
00294         ASSERT(local, "out of memory");
00295 
00296         local->initialize(ts);
00297 
00298         return local;
00299 }
00300 
00301 
00302 
00303 };      // glut namespace
00304