增加我的openGL太阳系的FPS

Increasing the FPS of my openGL Solar System

本文关键字:FPS 太阳系 openGL 我的 增加      更新时间:2023-10-16

我有一个运行在超低fps上的太阳系(大约26帧)。当我运行带有行星注释的程序时,我得到了大约1500-2000帧/秒。当我添加任何行星时,它会下降到每秒400帧。然后随着我添加的行星越来越多,它就开始走下坡路了。图片的大小并不是那么大。我最大的一个大约有150kb。即使我减少它,fps仍然以同样的方式下降。这是太阳系的代码。

使用vector

的更新版本
#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

  1. 有一个太阳系行星的列表(例如std::vector<Planet>),并且只创建一次行星(如果你想让你的太阳系有运动,你需要移动它们)。
  2. 只读取纹理作为结构的一部分,因为(我期望)纹理是"恒定的"。
  3. 在SolarSystem::display()使用行星列表调用displayPlanet

Images::readImageFile做文件I/O吗?如果是这样,那就不要称之为每一次抽搐。一次将其加载到某个缓冲区/数组中,然后在显示时每隔一秒抓取指针。你可以在显示中完成剩下的功能,而不是加载。

你正在重新创造你的星球的每一个显示。在Create函数中创建它们一次,将它们放入vector或数组中,然后调用这些缓冲对象。

你也没有提到测试电脑的规格是什么。没有真正显卡的低端计算机可能难以处理稍微复杂的纹理模型。

编辑注释

// 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);
}