//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Satallite.h" #include TSatallite::TSatallite() { // initalise all the variables m_CurrentRotation=0.0f; m_RotationSpeed=0.0f; m_Distance=0.0f; m_Radius=0.0f; m_Color[0]=0.0f; m_Color[1]=0.0f; m_Color[2]=0.0f; m_Color[3]=0.0f; // initalise the moon list m_Moons=new TList; m_Moons->Clear(); } TSatallite::~TSatallite() { TSatallite *pSat; // delete all the moons (this will also // call the delete functions all the way down // the heiracy). while(m_Moons->Count!=0) { pSat=(TSatallite *)m_Moons->Last(); delete pSat; m_Moons->Delete(m_Moons->Count-1); } m_Moons->Clear(); } void TSatallite::CreateGLObject( bool IsSun ) { // Create the quadric object: m_SatalliteObject=gluNewQuadric(); // A quadric is a complex 3d shape in OpenGL, such as // a cylinder, disk or sphere (as used here). // It holds no actual geometric information, but is used // to store all the rest of the draw info about the shape, // such as: // draw style: GLU_FILL, GLU_LINE, GLU_SILHOUETTE or GLU_POINT gluQuadricDrawStyle(m_SatalliteObject, GLU_FILL); // how to shade: GLU_SMOOTH, GLU_FLAT gluQuadricNormals(m_SatalliteObject, GLU_SMOOTH); // whether normals are to face out (GLU_OUTSIDE) or in (GLU_INSIDE) if(IsSun) // if we are the sun then the light is going to be inside us, // therefore, if normals point outwards, then the sun will appear // unlit and hence dark, so make them point inwards gluQuadricOrientation(m_SatalliteObject, GLU_INSIDE); else gluQuadricOrientation(m_SatalliteObject, GLU_OUTSIDE); // whether the object is to be texture mapped or not gluQuadricTexture(m_SatalliteObject, GL_FALSE); } void TSatallite::AddMoon( float RotationSpeed, float Distance, float Radius, float Color[4] ) { TSatallite *pSat; // create a new TSatallite for our moon pSat=new TSatallite; // initalise the moons parameters pSat->m_RotationSpeed=RotationSpeed; pSat->m_Distance=Distance; pSat->m_Radius=Radius; pSat->m_Color[0]=Color[0]; pSat->m_Color[1]=Color[1]; pSat->m_Color[2]=Color[2]; pSat->m_Color[3]=Color[3]; // create its object, it isn't the sun pSat->CreateGLObject(false); // add the moon to the list m_Moons->Add((void *)pSat); } TSatallite *TSatallite::GetMoon( int moon ) { if(moon>m_Moons->Count) return(NULL); // return a casted pointer to the moon return((TSatallite *)m_Moons->Items[moon]); } void TSatallite::Draw( void ) { int i; // push the transform matrix because we will want it // back later glPushMatrix(); // rotate to the orbital rotation of the satallite, // around the y axis glRotatef(m_CurrentRotation, 0.0f, 1.0f, 0.0f); // tranlate out to the orbital distance along the // newly rotated x axis glTranslatef(m_Distance, 0.0f, 0.0f); // set to the color of the satallite glColor4fv(m_Color); // draw the quadric sphere gluSphere(m_SatalliteObject, m_Radius, SATALLITE_SLICES, SATALLITE_STACKS); // loop through all this satallites moons and draw them too for(i=0; iCount; i++) { (GetMoon(i))->Draw(); } // get the original transform matrix back glPopMatrix(); } void TSatallite::DrawOrbit( void ) { int i; float angle, x, z; // we don't want the orbit paths lit glDisable(GL_LIGHTING); // save the transform matrix glPushMatrix(); // set an appropriate color (the same as the satallite itself) glColor4fv(m_Color); // draw the orbit in a line strip glBegin(GL_LINE_STRIP); // go though a full 2PI arch for(angle=0; angle<=(2.0f*M_PI)+0.1f; angle+=0.1f) { // work out the x and z coords to connect the next line to x=m_Distance*sin(angle); z=m_Distance*cos(angle); // connect the line glVertex3f(x, 0.0f, z); } glEnd(); // rotate and translate out to the satallite's actual position glRotatef(m_CurrentRotation, 0.0f, 1.0f, 0.0f); glTranslatef(m_Distance, 0.0f, 0.0f); // draw the orbits for all the moons for(i=0; iCount; i++) { (GetMoon(i))->DrawOrbit(); } // get the matrix back glPopMatrix(); // reenable the lighting glEnable(GL_LIGHTING); } void TSatallite::UpdateOrbit( float Multiplier ) { int i; // update the current rotation by the rotationspeed multiplied by // the multiplier m_CurrentRotation+=m_RotationSpeed*Multiplier; if(m_CurrentRotation>=360.0f) m_CurrentRotation-=360.0f; else if(m_CurrentRotation<0.0f) m_CurrentRotation+=360.0f; // update the orbits for all the moons for(i=0; iCount; i++) { (GetMoon(i))->UpdateOrbit(Multiplier); } } //--------------------------------------------------------------------------- #pragma package(smart_init)