Cube.cpp

00001 #include "Cube.h"
00002 
00003 Cube::Cube (double s)
00004 {
00005         size = s;
00006 }
00007 
00008 Cube::~Cube ()
00009 {
00010         // nothing to do
00011 }
00012 
00013 void Cube::Draw ()
00014 {
00015         glColor4fv (color);
00016         glutSolidCube (size);
00017 }
00018 
00019 double Cube::GetBoundingRadius ()
00020 {
00021         // corners have the distance of size * sqrt (3) / two.
00022         return size * 1.732051 / 2;
00023 }
00024 
00025 BoundingBox Cube::GetBoundingBox ()
00026 {
00027         BoundingBox result;
00028 
00029         result.xmin = -size / 2;
00030         result.xmax = size / 2;
00031         result.ymin = -size / 2;
00032         result.ymax = size / 2;
00033         result.zmin = -size / 2;
00034         result.zmax = size / 2;
00035 
00036         return result;
00037 }
00038 
00039 Cube2::Cube2 (float s)
00040 {
00041         size = s;
00042         
00043         Construct (s);
00044         ComputeNormals (true);
00045 
00046         PrintToConsole ();
00047 }
00048 
00049 Cube2::~Cube2 ()
00050 {
00051         if (v_data != NULL) delete v_data;
00052         if (indices != NULL) delete indices;
00053 }
00054 
00055 void Cube2::Construct (float size)
00056 {
00057         // cube has 8 vertices, construct them
00058         v_data = new float[3*8];
00059 
00060         v_data [0] = size/2;
00061         v_data [1] = size/2;
00062         v_data [2] = size/2;
00063  
00064         v_data [3] = size/2;
00065         v_data [4] = -size/2;
00066         v_data [5] = size/2;
00067 
00068         v_data [6] = -size/2;
00069         v_data [7] = -size/2;
00070         v_data [8] = size/2;
00071 
00072         v_data [9] = -size/2;
00073         v_data [10] = size/2;
00074         v_data [11] = size/2;
00075  
00076         v_data [12] = size/2;
00077         v_data [13] = size/2;
00078         v_data [14] = -size/2;
00079 
00080         v_data [15] = size/2;
00081         v_data [16] = -size/2;
00082         v_data [17] = -size/2;
00083  
00084         v_data [18] = -size/2;
00085         v_data [19] = -size/2;
00086         v_data [20] = -size/2;
00087 
00088         v_data [21] = -size/2;
00089         v_data [22] = size/2;
00090         v_data [23] = -size/2;
00091 
00092         // cube consists of 6 sides composed of 2 triangles each, construct them
00093         indices = new GLuint[12 * 3];
00094 
00095         indices [0] = 0;
00096         indices [1] = 4;
00097         indices [2] = 5;
00098 
00099         indices [3] = 0;
00100         indices [4] = 5;
00101         indices [5] = 1;
00102 
00103         indices [6] = 1;
00104         indices [7] = 5;
00105         indices [8] = 6;
00106 
00107         indices [9] = 1;
00108         indices [10] = 6;
00109         indices [11] = 2;
00110 
00111         indices [12] = 2;
00112         indices [13] = 6;
00113         indices [14] = 7;
00114 
00115         indices [15] = 2;
00116         indices [16] = 7;
00117         indices [17] = 3;
00118 
00119         indices [18] = 3;
00120         indices [19] = 7;
00121         indices [20] = 4;
00122 
00123         indices [21] = 3;
00124         indices [22] = 4;
00125         indices [23] = 0;
00126 
00127         indices [24] = 0;
00128         indices [25] = 1;
00129         indices [26] = 2;
00130 
00131         indices [27] = 0;
00132         indices [28] = 2;
00133         indices [29] = 3;
00134 
00135         indices [30] = 4;
00136         indices [31] = 6;
00137         indices [32] = 5;
00138 
00139         indices [33] = 4;
00140         indices [34] = 7;
00141         indices [35] = 6;
00142 
00143         SetVertexData (v_data, 8);
00144         SetVertexIndices (indices, 12);
00145 
00146 }
00147 
00148 TexturedCube::TexturedCube (float size_, GLuint texture_id_, bool allow_lighting_)
00149 {
00150         half_size = size_/2;
00151         texture_id = texture_id_;
00152         allow_lighting = allow_lighting_;
00153 
00154         ConstructDisplayList ();
00155 }
00156 
00157 TexturedCube::~TexturedCube ()
00158 {
00159         glDeleteLists (list_id, 1);
00160 }
00161 
00162 double TexturedCube::GetBoundingRadius ()
00163 {
00164         return half_size * 1.732051;
00165 }
00166 
00167 BoundingBox TexturedCube::GetBoundingBox ()
00168 {
00169         BoundingBox result;
00170 
00171         result.xmin = -half_size;
00172         result.xmax = half_size;
00173         result.ymin = -half_size;
00174         result.ymax = half_size;
00175         result.zmin = -half_size;
00176         result.zmax = half_size;
00177 
00178         return result;
00179 }
00180 
00181 void TexturedCube::ConstructDisplayList ()
00182 {
00183         GLboolean lighting_state;
00184         GLboolean texture_state;
00185 
00186         list_id =  glGenLists (1);
00187 
00188         glNewList (list_id, GL_COMPILE);
00189 
00190         // get states (note: states are taken at compile time of the display list !!!)
00191         glGetBooleanv (GL_TEXTURE_2D, &texture_state);
00192         glEnable(GL_TEXTURE_2D);        
00193         
00194         if (!allow_lighting)
00195         {
00196                 glGetBooleanv (GL_LIGHTING, &lighting_state);
00197                 glDisable (GL_LIGHTING);
00198         }
00199 
00200         glBindTexture (GL_TEXTURE_2D, texture_id);
00201         glColor4fv (color);
00202 
00203         glBegin (GL_QUADS);
00204                 
00205                 // top
00206                 glNormal3f (0.0f, 0.0f, 1.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (half_size, half_size, half_size);
00207                 glNormal3f (0.0f, 0.0f, 1.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (half_size, -half_size, half_size);
00208                 glNormal3f (0.0f, 0.0f, 1.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (-half_size, -half_size, half_size);
00209                 glNormal3f (0.0f, 0.0f, 1.0f);glTexCoord2f (0.0f, 1.0f); glVertex3f (-half_size, half_size, half_size);
00210 
00211                 // bottom
00212                 glNormal3f (0.0f, 0.0f, -1.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (half_size, half_size, -half_size);
00213                 glNormal3f (0.0f, 0.0f, -1.0f);glTexCoord2f (0.0f, 1.0f); glVertex3f (-half_size, half_size, -half_size);
00214                 glNormal3f (0.0f, 0.0f, -1.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (-half_size, -half_size, -half_size);
00215                 glNormal3f (0.0f, 0.0f, -1.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (half_size, -half_size, -half_size);
00216 
00217                 // front
00218                 glNormal3f (1.0f, 0.0f, 0.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (half_size, half_size, -half_size);
00219                 glNormal3f (1.0f, 0.0f, 0.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (half_size, -half_size, -half_size);
00220                 glNormal3f (1.0f, 0.0f, 0.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (half_size, -half_size, half_size);
00221                 glNormal3f (1.0f, 0.0f, 0.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (half_size, half_size, half_size);
00222 
00223                 // back
00224                 glNormal3f (-1.0f, 0.0f, 0.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (-half_size, half_size, -half_size);
00225                 glNormal3f (-1.0f, 0.0f, 0.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (-half_size, half_size, half_size);
00226                 glNormal3f (-1.0f, 0.0f, 0.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (-half_size, -half_size, half_size);
00227                 glNormal3f (-1.0f, 0.0f, 0.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (-half_size, -half_size, -half_size);
00228 
00229                 // right
00230                 glNormal3f (0.0f, 1.0f, 0.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (-half_size, half_size, -half_size);
00231                 glNormal3f (0.0f, 1.0f, 0.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (half_size, half_size, -half_size);
00232                 glNormal3f (0.0f, 1.0f, 0.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (half_size, half_size, half_size);
00233                 glNormal3f (0.0f, 1.0f, 0.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (-half_size, half_size, half_size);
00234 
00235                 // left
00236                 glNormal3f (0.0f, -1.0f, 0.0f); glTexCoord2f (0.0f, 0.0f); glVertex3f (-half_size, -half_size, -half_size);
00237                 glNormal3f (0.0f, -1.0f, 0.0f); glTexCoord2f (1.0f, 0.0f); glVertex3f (-half_size, -half_size, half_size);
00238                 glNormal3f (0.0f, -1.0f, 0.0f); glTexCoord2f (1.0f, 1.0f); glVertex3f (half_size, -half_size, half_size);
00239                 glNormal3f (0.0f, -1.0f, 0.0f); glTexCoord2f (0.0f, 1.0f); glVertex3f (half_size, -half_size, -half_size);
00240 
00241         glEnd ();
00242 
00243         // restore states
00244         if (!allow_lighting && lighting_state) glEnable (GL_LIGHTING);
00245         if (!texture_state) glDisable (GL_TEXTURE_2D);
00246 
00247         glEndList ();
00248 }
00249 
00250 void TexturedCube::Draw ()
00251 {
00252         glCallList (list_id);
00253 }

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