C++ 更改已创建的向量

C++ 更改已创建的向量,c++,vector,C++,Vector,我正在做一个画太阳系的项目。在我的主要功能中,我只创造了一次行星 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()函数,它为矢量行星设置值向量 void

我正在做一个画太阳系的项目。在我的主要功能中,我只创造了一次行星

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()
函数,它为
矢量行星设置值向量

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; 
}
然后我将半径乘以刻度。如果未设置
changeScale
,行星的大小应保持不变。如果选择了
changeScale
,将使行星的大小增加100。 有没有一种方法可以让我不用每一帧都画行星?如果不可能的话,我怎么能在每一帧重新绘制行星呢?我只是不确定如何修改向量中的对象


谢谢

您可以修改
向量中的
行星
s。它看起来像这样:

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
    }
}

我建议您保持模型数据的灵活性,并将比例分别应用于该对象的查看变换。
if (changeScale) {
    for (int i = 0; i < planets.size(); ++i) {
        planets[i].radius *= 100; // this
        planets[i].setRadius( planets[i].getRadius() * 100 ); // or this
    }
}
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
    }
}