增加我的openGL太阳能系统的FPS

时间:2013-06-05 16:00:00

标签: c++ opengl

我有一个以超低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

更新

planet.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;
};

planet.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

2 个答案:

答案 0 :(得分:8)

  1. 在solarsystem中有一个列表(例如std::vector<Planet>个行星,并且只创建一次行星(如果你希望你的solarsystem有移动,你需要移动它们。
  2. 只读取纹理作为构造的一部分,因为(我期望)纹理是“常量”。
  3. SolarSystem中的
  4. :: display()使用行星列表来调用displayPlanet

答案 1 :(得分:3)

Images :: readImageFile是否执行文件I / O?如果是这样,请不要称之为每个抽搐。将它一次加载到某个缓冲区/数组中,然后在显示时每次tic抓取指针。您可以在显示中完成该功能的其余部分,而不是负载。

您正在重新创建行星,每次显示时都会显示。在create函数中创建一次,将它们放在向量或数组中,然后调用那些缓冲的对象。

您也没有提到测试计算机的规格。没有真正显卡的低端计算机可能会遇到一些复杂的纹理模型。

根据评论进行修改

// Within your Planet class add this where you have your texture stored:
Images::RGBImage surfaceImage;

// When the texture is setup in your constructor, add
surfaceImage=Images::readImageFile(this->texture);
// And remove it from the drawPlanet call

// Global or in a high visibility scope location
std::vector<Planet> planets;  

// Call this outside of your main run loop
void createPlanets()
{   
  planets.push_back(Planet("images/pluto.jpg", 10000.15, 10000.15, 5913520.0, 4475140.0, 29.6));
  ... // Each planet
}

// And in your main draw loop:
for(std::vector<Planet>::iterator iter = planets.begin(); 
    iter != planets.end(); 
    ++iter)
{
  double plOrbS = orbitSpeed;
  double plRotS = rotatSpeed;

  iter->displayPlanet(plOrbS, plRotS, 0.0,0.0);
}