我在使用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);
答案 0 :(得分:2)
这:
glBufferData(GL_ARRAY_BUFFER, sizeof (verts), &verts[0], GL_STATIC_DRAW);
应该是:
glBufferData(GL_ARRAY_BUFFER, sizeof (Vertex)*numVerts, &verts[0], GL_STATIC_DRAW);
原始只是给你指针的大小,而不是数组的大小。这解释了单点,但我不确定你为什么会看到减速。