我正在做一个绘制太阳系的项目。我在我的主要功能中只创建了一次行星。
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。有没有一种方法可以做到这一点,而不必在每一帧都绘制行星?如果这是不可能的,我怎么能做到这一点每帧重绘行星?我只是不确定如何修改向量中的对象。
谢谢!