2013-06-20 22 views
0

我正在研究一個繪製太陽系的項目。我在主要功能中只創建一次行星。更改已創建的矢量

int main(int argc,char* argv[]) 
{ 

SolarSystem app(argc, argv);// Create 1 Solar System 
app.createPlanets();// Create the planets in the Solar System 
app.run(); 

return 0; 
} 

這裏是我的createPlanets()功能設定值的vector<Planet>planets;載體。

void SolarSystem::createPlanets() const 
{ 
        //File for texture  / Radius //  Orbit  //Tilt angle 
planets.push_back(Planet("images/Sun.jpg",  696, 696, 2500.0, 0.0,  0.0 ));//0 
planets.push_back(Planet("images/mercury.jpg", 2.44, 2.44, 57910.0, 45740.0, 0.0 ));//1 
planets.push_back(Planet("images/venus.jpg", 6.05, 6.05, 108200.0, 107464.0, 177.3));//2 
planets.push_back(Planet("images/earth.jpg", 6.37, 6.34, 149600.0, 147102.0, 23.5));//3 
planets.push_back(Planet("images/moon.jpg", 1.74, 1.73, 384.0,  383.0,  5.145));//4 
planets.push_back(Planet("images/mars.jpg", 3.39, 3.37, 227940.0, 207425.0, 25.2));//5 
planets.push_back(Planet("images/Jupiter.jpg", 69.90, 65.24, 778330.0, 740734.0, 3.1 ));//6 
planets.push_back(Planet("images/neptune.jpg", 24.63, 24.08, 4504300.0, 4460608.0, 29.6));//7 
planets.push_back(Planet("images/pluto.jpg", 1.15, 1.15, 5913520.0, 4475140.0, 29.6));//8 
} 

我創建一個菜單,用戶可以改變行星的規模,使他們很容易被察覺(真正形成規模,使行星真的很難找到)。爲了改變尺寸,我不得不進入矢量並改變那裏的每個行星物體的半徑。

有誰知道我該怎麼做? 下面的顯示功能每幀畫出行星。

void SolarSystem::display(GLContextData& contextData) const 
{ 


if(showOrbitPath) 
    displayOrbitPath(); 


//These variables set each planet's orbit and rotation speed 
       //Sun Mer Ven Ear Moon Mar Jup Nep Plu 
double orbitS[] = {0.0, 4.15, 1.600, 1.00, 13.0, 0.40, 0.08, 0.006, 0.004}; 
double rotatS[] = {1.0, 0.50, 0.125, 30.0, 1.00, 30.0, 75.0, 40.00, 5.000}; 

//index counter for variables above 
int i = 0; 

//Vector iteration 
for(std::vector<Planet>::iterator it = planets.begin(); it != planets.end(); ++it) 
{ 
    //If the planet being displayed is the sun 
    if (i == 0) 
    //Turn off shading 
     glDisable(GL_LIGHTING); 
    else //Turn the spot where the Sun is into a light Source 
    { 
     GLfloat light_diffuse[] = { 1.5, 1.5, 1.5, 1.5 }; 
     GLfloat pos[] = { 2500.0, 0.0, 0.0, 1.0}; 
     glEnable(GL_LIGHTING); 
     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 
     glLightfv(GL_LIGHT0, GL_POSITION, pos); 

    } 
    if (i == 4)//moon 
    { 
     it->displayPlanet(orbitCounter*orbitS[i], orbitCounter*rotatS[i], 
       planets[3].getMajorAxis(),planets[3].getMinorAxis()); 
    } 
    else //Planets 
     it->displayPlanet(orbitCounter*orbitS[i]*0, orbitCounter*rotatS[i], 0.0,0.0); 

    i++; 
} 
//base counter used to calculate orbit and rotation speed 
orbitCounter+=0.05; 
//89750.0 is what's necessary for the Pluto to orbit around the Sun once. 
if (orbitCounter > 89750.0) 
orbitCounter = 0.0; 
} 

我想添加一個if語句,我可以說

int scale = 1; 
if(changeScale) 
    scale == 100; 

,那麼我會通過規模乘以半徑。如果沒有設置changeScale,行星應保持相同的大小。如果選擇changeScale將會使行星的大小增加100. 有沒有辦法在不必每幀畫出行星的情況下做到這一點?如果這是不可能的,我怎樣才能在每一幀重繪行星?我只是不確定如何修改我的向量中的對象。

謝謝!

+0

我建議您保持模型數據的完整性並將該比例單獨應用於該對象的查看變換。 – legends2k

回答

2

您可以在您的vector中修改Planet。這將是這個樣子:

planets[index].member = new_val; 

或者,如果變量是private,你有存取方法,

planets[index].setMember(new_val); 

所以,如果你想改變半徑,它看起來像一個以下兩個語句:

if (changeScale) { 
    for (int i = 0; i < planets.size(); ++i) { 
     planets[i].radius *= 100; // this 
     planets[i].setRadius(planets[i].getRadius() * 100); // or this 
    } 
} 

或者,你可以使用iterator S:

if (changeScale) { 
    for (std::vector<Planet>::iterator it = planets.begin(); it != planets.end(); ++it) { 
     (*it).radius *= 100; // this 
     (*it).setRadius((*it).getRadius() * 100); // or this 
    } 
}