Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Sphere.cpp

Go to the documentation of this file.
00001 
00007 #include "Sphere.h"
00008 
00009 Sphere::Sphere(float cx, float cy, float cz, float r, int p) {
00010         m_pSphereVertices = NULL;
00011         m_nNumSphereVertices = 0;
00012 
00013         createSphereGeometry(cx, cy, cz, r, p);
00014 
00015         if (m_pSphereVertices != NULL) {
00016 
00017                 addFormat(ATT_VERTEX, ATT_FLOAT, 3, sizeof(float)*5);
00018                 addFormat(ATT_NORMAL, ATT_FLOAT, 3, sizeof(float)*2);
00019                 addFormat(ATT_TEX, ATT_FLOAT, 2, 0);
00020                 setPrimitive(PRIM_QUAD_STRIP);
00021                 setVertices(m_pSphereVertices, m_nNumSphereVertices, sizeof(Vertex));
00022 
00023                 //delete [] m_pSphereVertices;
00024                 //m_pSphereVertices = NULL;
00025                 //m_nNumSphereVertices = 0;
00026 
00027                 LOG_SUCCESS(("Object sphere loaded"));
00028         }
00029         else
00030                 LOG_ERROR(("Unable to load object Sphere"));
00031 }
00032 
00033 Sphere::~Sphere() {
00034         m_pSphereVertices = NULL;               // deletion handled by Object.cpp
00035 }
00036 
00037 void Sphere::setVertData(int index, float tu, float tv, 
00038                                                 float nx, float ny, float nz, 
00039                                                 float vx, float vy, float vz)
00040 {
00041         (m_pSphereVertices+index)->tu = tu;
00042         (m_pSphereVertices+index)->tv = tv;
00043         (m_pSphereVertices+index)->nx = nx;
00044         (m_pSphereVertices+index)->ny = ny;
00045         (m_pSphereVertices+index)->nz = nz;
00046         (m_pSphereVertices+index)->vx = vx;
00047         (m_pSphereVertices+index)->vy = vy;
00048         (m_pSphereVertices+index)->vz = vz;
00049 }
00050 
00051 void Sphere::createSphereGeometry(float cx, float cy, float cz, float r, int p)
00052 {
00053     const float PI = 3.14159265358979f;
00054     const float TWOPI = 6.28318530717958f;
00055     const float PIDIV2 = 1.57079632679489f;
00056 
00057     float theta1 = 0.0;
00058     float theta2 = 0.0;
00059     float theta3 = 0.0;
00060 
00061     float ex = 0.0f;
00062     float ey = 0.0f;
00063     float ez = 0.0f;
00064 
00065     float px = 0.0f;
00066     float py = 0.0f;
00067     float pz = 0.0f;
00068 
00069     float tu  = 0.0f;
00070     float tv  = 0.0f;
00071 
00072     //-------------------------------------------------------------------------
00073     // If sphere precision is set to 4, then 20 verts will be needed to 
00074     // hold the array of GL_TRIANGLE_STRIP(s) and so on...
00075     //
00076     // Example:
00077     //
00078     // total_verts = (p/2) * ((p+1)*2)
00079     // total_verts = (4/2) * (  5  *2)
00080     // total_verts =   2   *  10
00081     // total_verts =      20
00082     //-------------------------------------------------------------------------
00083 
00084     m_nNumSphereVertices = (p/2) * ((p+1)*2);
00085 
00086     if( m_pSphereVertices != NULL )
00087     {
00088         delete []m_pSphereVertices;
00089         m_pSphereVertices = NULL;
00090         m_pSphereVertices = new Vertex[m_nNumSphereVertices];
00091     }
00092     else
00093     {
00094         m_pSphereVertices = new Vertex[m_nNumSphereVertices];
00095     }
00096 
00097     // Disallow a negative number for radius.
00098     if( r < 0 )
00099         r = -r;
00100 
00101     // Disallow a negative number for precision.
00102     if( p < 4 ) 
00103         p = 4;
00104 
00105     int k = -1;
00106 
00107     for( int i = 0; i < p/2; ++i )
00108     {
00109         theta1 = i * TWOPI / p - PIDIV2;
00110         theta2 = (i + 1) * TWOPI / p - PIDIV2;
00111 
00112         for( int j = 0; j <= p; ++j )
00113         {
00114             theta3 = j * TWOPI / p;
00115 
00116             ex = cosf(theta2) * cosf(theta3);
00117             ey = sinf(theta2);
00118             ez = cosf(theta2) * sinf(theta3);
00119             px = cx + r * ex;
00120             py = cy + r * ey;
00121             pz = cz + r * ez;
00122             tu  = -(j/(float)p);
00123             tv  = 2*(i+1)/(float)p;
00124 
00125             ++k;
00126             setVertData( k, tu, tv, ex, ey, ez, px, py, pz );
00127 
00128             ex = cosf(theta1) * cosf(theta3);
00129             ey = sinf(theta1);
00130             ez = cosf(theta1) * sinf(theta3);
00131             px = cx + r * ex;
00132             py = cy + r * ey;
00133             pz = cz + r * ez;
00134             tu  = -(j/(float)p);
00135             tv  = 2*i/(float)p;
00136 
00137             ++k;
00138             setVertData( k, tu, tv, ex, ey, ez, px, py, pz );
00139         }
00140     }
00141 }

Generated on Sun Jun 5 15:47:05 2005 for Defacto by  doxygen 1.4.3