我已经学习了几周的维吾尔族,我被告知here维多利亚州立大学可以渲染“约100万个顶点,数百fps”。但是,我目前的VBO测试程序只能获得大约50 FPS,并且需要渲染一百万个顶点。有没有办法优化VBO效率?或者,更有可能的是,我做错了什么?我的测试程序在这里:
编辑:根据反馈改进代码。
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <glew.h>
#include <gl/gl.h>
#include <gl/glu.h>
using namespace std;
float cube_vertices[] = {-1, -1, 1,
1, -1, 1,
1, 1, 1,
-1, 1, 1,
-1, -1, -1,
-1, 1, -1,
1, 1, -1,
1, -1, -1,
-1, 1, -1,
-1, 1, 1,
1, 1, 1,
1, 1, -1,
-1, -1, -1,
1, -1, -1,
1, -1, 1,
-1, -1, 1,
1, -1, -1,
1, 1, -1,
1, 1, 1,
1, -1, 1,
-1, -1, -1,
-1, -1, 1,
-1, 1, 1,
-1, 1, -1};
float cube_normals[] = {0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 0, -1,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
0, -1, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0,
-1, 0, 0};
class Scene {
public:
void setup_projection( int w, int h ) {
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 50, (GLdouble)w/(GLdouble)h, 1, 5000.0 );
glMatrixMode( GL_MODELVIEW );
}
};
int main() {
///Number of models to render
int NumberOfCubes = 0;
cout << "Enter number of cubes to render: ";
cin >> NumberOfCubes;
system("cls");
///Create vectors for mesh data
//6 faces * 4 verts * x, y, z * number of cubes
std::vector<float> vertices; vertices.resize(6*4*3*NumberOfCubes);
std::vector<float> normals; normals.resize(6*4*3*NumberOfCubes);
for(int i = 0; i < NumberOfCubes; i++)
{
for(int j = 0; j < 6*4*3; j++)
{
vertices[(i*6*4*3) + j] = cube_vertices[j] + i;
normals[(i*6*4*3) + j] = cube_normals[j];
}
}
///Store size of the vectors
int SizeOfVertices = vertices.size() * sizeof(float);
int SizeOfNormals = normals.size() * sizeof(float);
///Window setup, lighting setup
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");
Scene scene;
scene.setup_projection(window.getSize().x,window.getSize().y);
glewInit();
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
float XL = .5, YL = .1, ZL = 1;
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat lightpos[] = {XL, YL, ZL, 0.};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
///Generate the VBO
GLuint VBOID;
glGenBuffers(1, &VBOID);
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
glBufferData(GL_ARRAY_BUFFER, SizeOfVertices + SizeOfNormals, 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, SizeOfVertices, &vertices[0]);
glBufferSubData(GL_ARRAY_BUFFER, SizeOfVertices, SizeOfNormals + SizeOfVertices, &normals[0]);
glBindBuffer(GL_ARRAY_BUFFER, 0);
///FPS Stuff
sf::Clock FPS;
sf::Clock ShowFPS;
float fps;
///Start loop
cout << "Rendering " << NumberOfCubes * 8 << " vertices." << endl;
cout << "Using graphics card: " << glGetString(GL_RENDERER) << endl;
while( window.isOpen() ) {
sf::Event event;
while( window.pollEvent( event ) ) {
if( event.type == sf::Event::Closed )
window.close();
}
fps = FPS.getElapsedTime().asSeconds();
fps = 1 / fps;
FPS.restart();
if(ShowFPS.getElapsedTime().asSeconds() > 1)
{
cout << "FPS: " << fps << "\t FrameTime: " << 1000 / fps << endl;
ShowFPS.restart();
}
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
scene.setup_projection(window.getSize().x,window.getSize().y);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-25, -25, 150, 50, 50, 50, 0, 1, 0);
glBindBuffer(GL_ARRAY_BUFFER, VBOID);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glColor3f(1, 0, 0);
glNormalPointer(GL_FLOAT, 0, 0);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, 6*4*NumberOfCubes);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
window.display();
}
return 1;
}
答案 0 :(得分:1)
对您的代码进行一些评论:
void Scene::resize( int w, int h ) {
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 50, (GLdouble)w/(GLdouble)h, 1, 5000.0 );
glMatrixMode( GL_MODELVIEW );
}
请注意,设置视口和投影不是某种“调整大小”操作。它们是绘图过程的一部分,因此应该这样对待。好处是,您可以在每次绘制迭代时调用此函数。但我不称它为resize
。更好的名称是setup_projection
或类似名称,以明确这个函数做什么,而不是它做了什么反应。总是通过它的功能来调用函数!
这个
cout << endl << endl << "Close program when finished." << endl;
bool ProgramRunning(true);
while(ProgramRunning == true) {}
可能无法正常工作。你关闭的是控制台窗口/终端;这使您的程序松开其标准输入和进程负责人,从而终止它。 while循环之后的所有代码都不会被执行。您可以安装信号处理程序,将 - 迄今为止无功能 - ProgrammRunning标志设置为false。
然而,处理此问题的规范方法是等待用户暂停直到用户点击回车键:
cout << "Program execution finished, hit the ENTER key to terminate" << endl;
cin.get();
现在说明为什么你只获得50 FPS:最可能的原因是你启用了V-Sync并且你的显示器的刷新频率为50Hz。 50Hz是不寻常的,但不是没有听到的。也可能是你的显示器以60Hz运行,但由于某些原因你没有为每次回扫制作刷新截止日期,所以有效地使你的代码平均每隔6帧丢失一次。
另一个原因可能是,您没有在GeForce上运行,但可能在笔记本电脑的芯片组GPU上运行。如果您有混合图形系统,请确保在执行程序之前已正确安装和的所有驱动程序切换到GeForce GPU。
打印glGetString(GL_RENDERER);
的输出以确保。打开窗口后,创建OpenGL上下文添加
cout << glGetString(GL_RENDERER) << endl;
答案 1 :(得分:0)
您使用的是双缓冲吗?是的,那么您的驱动程序中可能已启用sync to vblank
。这意味着,每个使用双缓冲的OpengGL应用程序最多都会以显示器的刷新率渲染(通常大约为50 - 60Hz)。
您可以尝试使用(显着)较小型号的相同代码来查看您的FPS是否超过此值。
答案 2 :(得分:0)
在做了进一步的研究之后,我发现了VBO Indexing并且能够使用它来获得具有一百万个顶点的数百个FPS。