2013-06-20 4 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; 
} 

여기에 vector<Planet>planets; 벡터 값을 설정하는 제 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; 
} 

은 내가

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의를 사용할 수 있습니다

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 
    } 
} 
관련 문제