Torus.cpp

00001 #include "Torus.h"
00002 #include "Geometry.h"
00003 #include "Textures.h"
00004 
00005 Torus::Torus (double i_r, double o_r, int n_s, int n_r)
00006 {
00007         inner_radius = i_r;
00008         outer_radius = o_r;
00009         n_sides = n_s;
00010         n_rings = n_r;
00011 }
00012 
00013 Torus::~Torus ()
00014 {
00015         // nothing to do
00016 }
00017 
00018 void Torus::Draw ()
00019 {
00020         // use spherical texture mapping (which does not look very good).
00021         glEnable (GL_TEXTURE_2D);
00022 
00023         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); 
00024         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);  
00025 
00026         glEnable(GL_TEXTURE_GEN_S);                          
00027         glEnable(GL_TEXTURE_GEN_T); 
00028 
00029         glBindTexture (GL_TEXTURE_2D, Textures::GetId (T_PERLIN));
00030 
00031         glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emission);
00032 
00033         glColor4fv (color);
00034         glutSolidTorus (inner_radius, outer_radius, n_sides, n_rings);
00035 
00036         glDisable(GL_TEXTURE_GEN_S);
00037         glDisable(GL_TEXTURE_GEN_T);
00038 
00039         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); 
00040         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);  
00041 
00042         glDisable (GL_TEXTURE_2D);
00043 }
00044 
00045 double Torus::GetBoundingRadius ()
00046 {
00047         return inner_radius + outer_radius;
00048 }
00049 
00050 BoundingBox Torus::GetBoundingBox ()
00051 {
00052         BoundingBox result;
00053 
00054         result.xmin = - inner_radius - outer_radius;
00055         result.xmax = inner_radius + outer_radius;
00056         result.ymin = - inner_radius - outer_radius;
00057         result.ymax = inner_radius + outer_radius;
00058         result.zmin = - inner_radius;
00059         result.zmax = inner_radius;
00060 
00061         return result;
00062 }
00063 
00064 Torus2::Torus2 (double radius_t, double radius_p, int n_sides_, int n_rings_)
00065 {
00066         r_t = radius_t;
00067         r_p = radius_p;
00068         n_sides = n_sides_;
00069         n_rings = n_rings_;
00070 
00071         data = NULL;
00072         indices = NULL;
00073 
00074         Construct ();
00075 }
00076 
00077 Torus2::~Torus2 ()
00078 {
00079         // nothing to do.
00080 }
00081 
00082 void Torus2::SetTexture (GLuint texture_id)
00083 {
00084         ComputeSphericalTextureCoordinates (texture_id);
00085 }
00086 
00087 double Torus2::GetBoundingRadius ()
00088 {
00089         return r_t + r_p;
00090 }
00091 
00092 BoundingBox Torus2::GetBoundingBox ()
00093 {
00094         BoundingBox result;
00095 
00096         result.xmax = r_t + r_p;
00097         result.xmin = -r_t - r_p;
00098         result.ymax = r_t + r_p;
00099         result.ymin = -r_t - r_p;
00100         result.zmax = r_p;
00101         result.zmin = -r_p;
00102 
00103         return result;
00104 }
00105 
00106 void Torus2::Construct ()
00107 {
00108         int index;
00109         double t, p;
00110 
00111         // allocate memory
00112         data = new float[n_sides * n_rings * 3];
00113         indices = new GLuint[n_sides * n_rings * 6];
00114 
00115         // fill data
00116         index = 0;
00117         for (int ring_index = 0; ring_index < n_rings; ring_index++)
00118         {
00119                 for (int side_index = 0; side_index < n_sides; side_index++)
00120                 {
00121                         t = 2.0 * MY_PI * ring_index / n_rings;
00122                         p = 2.0 * MY_PI * side_index / n_sides;
00123 
00124                         data[index++] = (r_t + r_p * cos (p)) * cos (t);
00125                         data[index++] = (r_t + r_p * cos (p)) * sin (t);
00126                         data[index++] =  r_p * sin (p);
00127                 }
00128         }
00129 
00130         // fill indices
00131         index = 0;
00132         for (int ring_index = 0; ring_index < n_rings; ring_index++)
00133         {
00134                 for (int side_index = 0; side_index < n_sides; side_index++)
00135                 {
00136                         // first_triangle
00137                         indices[index++] = (ring_index * n_sides) + side_index;
00138                         indices[index++] = (ring_index * n_sides) + (side_index + 1)%n_sides;
00139                         indices[index++] = (((ring_index+1)%n_rings) * n_sides) + side_index;
00140 
00141                         // second triangle
00142                         indices[index++] = (ring_index * n_sides) + (side_index + 1)%n_sides;
00143                         indices[index++] = (((ring_index+1)%n_rings) * n_sides) + (side_index + 1)%n_sides;
00144                         indices[index++] = (((ring_index+1)%n_rings) * n_sides) + side_index;
00145                 }
00146         }
00147         // set pointers
00148         SetVertexData (data, n_sides * n_rings);
00149         SetVertexIndices (indices, n_sides * n_rings * 2);
00150 
00151         // compute normals
00152         ComputeNormals (true);
00153 }

Generated on Sun Jul 2 13:20:39 2006 for Demo by  doxygen 1.4.6-NO