我已经学习 VBO 几个星期了,我在这里被告知VBO 可以渲染“以数百 fps 的速度渲染约 100 万个顶点”。但是,我当前的 VBO 测试程序只能获得大约 50 FPS 和 100 万个顶点来渲染。有没有办法优化 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;
}