1

我在用 VBO 绘图时遇到问题。使用即时模式,我可以轻松地绘制一个立方网格点,但速度很慢。我想使用 VBO 来尝试加快绘图速度。我正在使用 SDL/glew。我的输出是一个黑点。相同的 sdl / glew 设置代码适用于纹理三角形,但没有使用结构来存储数据 - 我想要一个用于缓存目的的结构,因此顶点颜色接近顶点位置。

设置 sdl / glew:

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    lm->Log(LogManager::Error, "Video initialization failed: " + std::string(SDL_GetError()));
    return false;
}

const SDL_VideoInfo *videoInfo;
videoInfo = SDL_GetVideoInfo();

if (!videoInfo) {
    lm->Log(LogManager::Error, "Video query failed: " + std::string(SDL_GetError()));
    return false;
}

int videoFlags;
videoFlags = SDL_OPENGL;
videoFlags |= SDL_GL_DOUBLEBUFFER;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_RESIZABLE;
videoFlags |= SDL_FULLSCREEN;

if (videoInfo->hw_available) {
    videoFlags |= SDL_HWSURFACE;
} else {
    videoFlags |= SDL_SWSURFACE;
}

if (videoInfo->blit_hw) {
    videoFlags |= SDL_HWACCEL;
}

SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

int scrWidth = 1280, scrHeight = 800;
GLfloat aspectRatio = (GLfloat) scrWidth / (GLfloat) scrHeight;
*surface = SDL_SetVideoMode(scrWidth, scrHeight, 32, videoFlags);

if (!*surface) {
    lm->Log(LogManager::Error, "Video mode set failed: " + std::string(SDL_GetError()));
    return false;
}

glewInit();

if (!glewIsSupported("GL_VERSION_2_0")) {
    lm->Log(LogManager::Error, "OpenGL 2.0 not available");
    return false;
}

glViewport(0, 0, (GLsizei) scrWidth, (GLsizei) scrHeight); // Set the viewport
glMatrixMode(GL_PROJECTION); // Set the Matrix mode
glLoadIdentity();
gluPerspective(75, aspectRatio, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glPointSize(5);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

制作 VBO:

struct Vertex {
    GLfloat position[3];
    GLfloat colour[3];
};
... // then in the init function
verts = new Vertex[numVerts];
... // fill verts
vboId = 0;
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof (verts), &verts[0], GL_STATIC_DRAW);

glVertexPointer(3, GL_FLOAT, sizeof (Vertex), (GLvoid*) offsetof(Vertex, position));
glColorPointer(3, GL_FLOAT, sizeof (Vertex), (GLvoid*) offsetof(Vertex, colour));
delete [] verts;

画:

glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(3, GL_FLOAT, sizeof (Vertex), (GLvoid*) offsetof(Vertex, position));
glColorPointer(3, GL_FLOAT, sizeof (Vertex), (GLvoid*) offsetof(Vertex, colour));

glDrawArrays(GL_POINTS, 0, numVerts);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
4

1 回答 1

2

这个:

glBufferData(GL_ARRAY_BUFFER, sizeof (verts), &verts[0], GL_STATIC_DRAW);

应该:

glBufferData(GL_ARRAY_BUFFER, sizeof (Vertex)*numVerts, &verts[0], GL_STATIC_DRAW);

原来只是给你指针的大小,而不是数组的大小。这解释了单点,但我不确定您为什么会看到速度变慢。

于 2010-12-26T05:03:39.107 回答