我有一个以超低 fps(约 26)运行的太阳系。当我在注释掉行星的情况下运行程序时,我得到大约 1500-2000 fps。当我添加任何行星时,它会下降到大约 400 fps。然后它只会从那里走下坡路,我添加的行星越多。图片的大小没有那么大。我拥有的最大的是大约 150kb。即使我减少它,fps仍然以同样的方式下降。这是太阳系的代码。
使用向量的更新版本
#include <Vrui/Application.h>
#include <iostream>
#include <vector>
#include "solarSystem.h"
#include "drawShape.h"
#include "planet.h"
#include "skybox.h"
using namespace std;
double orbitSpeed = 0.0;
double rotatSpeed = 0.0;
vector<Planet>planets;
/****************************************************
*
****************************************************/
SolarSystem::SolarSystem(int& argc,char**& argv)
:Vrui::Application(argc,argv)
{
//Vrui::setNavigationTransformation(Vrui::Point::origin,Vrui::Scalar(15));
}
/****************************************************
*
****************************************************/
void SolarSystem::frame(void)
{
Vrui::scheduleUpdate(Vrui::getApplicationTime()); // Aim for 125 FPS
}
/****************************************************
*
****************************************************/
void SolarSystem::createPlanets() const
{
planets.push_back(Planet("images/Sun.jpg", 696, 696, 2500, 0.0, 0.0));
planets.push_back(Planet("images/mercury.jpg", 200.44, 200.44, 57910.0, 45740.0, 0.0));
planets.push_back(Planet("images/venus.jpg", 600.05, 600.05, 108200.0, 107464.0, 177.3));
planets.push_back(Planet("images/earth.jpg", 600.37, 600.34, 149600.0, 147102.0, 23.5));
//planets.push_back(Planet("images/moon.jpg", 300.4, 300.4, 384.0, 363.0, 5.145));
planets.push_back(Planet("images/mars.jpg", 30000.39, 30000.37, 227940.0, 207425.0, 25.2));
planets.push_back(Planet("images/Jupiter.jpg", 69000.9, 65000.24, 778330.0, 740734.0, 3.1));
planets.push_back(Planet("images/neptune.jpg", 24.63, 24.08, 4504300.0, 4460608.0, 29.6));
planets.push_back(Planet("images/pluto.jpg", 10000.15, 10000.15, 5913520.0, 4475140.0, 29.6));
}
/****************************************************
*
****************************************************/
void SolarSystem::displayOrbitPath() const
{
glDisable(GL_LIGHTING);
//Orbit Path
glColor3f(1.0f, 1.0f, 1.0f);
//Mercury //1.5849
drawCircle(91781.559, 72493.326, 1, 200);
//Venus
drawCircle(171486.0, 170319.6936, 1, 200);
//Earth
drawCircle(237101.04, 233141.9598, 1, 200);
//Mars
drawCircle(361262.106, 328747.8825, 1, 200);
//Jupiter
drawCircle(1233575.217, 1173994.071, 1, 200);
//Saturn
drawCircle(1429400.0*1.5849, 1349353.0*1.5849, 1, 100);
//Uranus
drawCircle(2870990.0*1.5849, 2738637.0*1.5849, 1, 100);
//Neptune
drawCircle(7138865.07, 7069617.619, 1, 200);
//Pluto
drawCircle(5913520.0*1.5849, 4475140.0*1.5849, 1, 200);
}
/****************************************************
*
****************************************************/
void SolarSystem::display(GLContextData& contextData) const
{
displayOrbitPath();
glDisable(GL_LIGHTING);
for(std::vector<Planet>::iterator it = planets.begin();
it != planets.end();
++it)
{
double plOrbS = orbitSpeed;
double plRotS = rotatSpeed;
it->displayPlanet(0, plRotS, 0.0,0.0);
}
orbitSpeed+=1;
if (orbitSpeed > 359)
orbitSpeed = 0.0;
rotatSpeed+=3;
if (rotatSpeed > 1436.0)
rotatSpeed = 0.0;
}
/****************************************************
*
****************************************************/
int main(int argc,char* argv[])
{
SolarSystem app(argc, argv);
app.createPlanets();
app.run();
return 0;
}
这就是大幅降低fps的原因
更新
行星.h
class Planet
{
public:
//Planet();
Planet(const char* fileName, double ER, double PR,
double orbitSMa, double orbitSmi, double angle);
~Planet() {};
void setOrbit(double orbitSpeed, double rotationSpeed,
double moonOrbitX, double moonOrbitY) ;
void displayPlanet(double orbitSpeed, double rotationSpeed,
double moonOrbitX, double moonOrbitY);
double getMajorAxis() {return majorAxis;};
double getMinorAxis() {return minorAxis;};
private:
const char* texture;
double equatRadius;
double polarRadius;
double orbitSemiMajor;
double orbitSemiMinor;
double majorAxis;
double minorAxis;
double orbitAngle;
Images::RGBImage surfaceImage;
};
行星.cpp 更新
#include "planet.h"
Planet::Planet(const char* fileName, double ER, double PR, double orbitSMa, double orbitSMi, double angle)
{
this->texture = fileName;
this->equatRadius = ER;
this->polarRadius = PR;
this->orbitSemiMajor = orbitSMa;
this->orbitSemiMinor = orbitSMi;
this->majorAxis = 0.0;
this->minorAxis = 0.0;
this->orbitAngle = angle;
surfaceImage=Images::readImageFile(this->texture);
}
void Planet::setOrbit(double orbitSpeed, double rotationSpeed,
double moonOrbitX, double moonOrbitY)
{
majorAxis = orbitSemiMajor * cos(orbitSpeed * 0.0055555556 * Math::Constants<double>::pi);
minorAxis = orbitSemiMinor * sin(orbitSpeed * 0.0055555556 * Math::Constants<double>::pi);
glTranslate(majorAxis+moonOrbitX, minorAxis+moonOrbitY, 0.0);
glRotatef(orbitAngle, 0.0, 1.0,1.0);
glRotatef(rotationSpeed, 0.0, 0.0, 1.0);
}
void Planet::displayPlanet(double orbitSpeed,double rotationSpeed,
double moonOrbitX, double moonOrbitY)
{
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
surfaceImage.glTexImage2D(GL_TEXTURE_2D,0,GL_RGB);
glPushMatrix();
setOrbit(orbitSpeed,rotationSpeed, moonOrbitX, moonOrbitY);
drawSolidPlanet(equatRadius, polarRadius, 1, 40, 40);
glPopMatrix();
}
如上所示,我尝试使用矢量创建行星,但是当我运行程序时,FPS 仍然很低。
以下是电脑规格:
显卡:NVIDIA Quadro FX 580 4GB
电脑:Intel Xeon(R) 3.2GHZ 12GB RAM