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 増加します。フレームごとに惑星を描画しなくても、これを行う方法はありますか? それが不可能な場合、フレームごとに惑星を再描画するにはどうすればよいですか? ベクトル内のオブジェクトを変更する方法がわかりません。

ありがとう!

4

1 に答える 1

2

内のを変更できPlanetますvector。次のようになります。

planets[index].member = new_val;

または、変数がprivateあり、アクセサ メソッドがある場合は、

planets[index].setMember(new_val);

したがって、半径を変更する場合は、次の 2 つのステートメントのいずれかのようになります。

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
    }
}
于 2013-06-20T17:59:14.413 に答える