OpenGL VBO not displaying
I'm having trouble drawing with VBOs. Using immediate mode, I can easily draw a cubic grid of points, but its quite slow. I wanted to use a VBO to try and speed up the drawing. I'm using SDL/glew. My output is a single black point. The same sdl / glew setup code worked for textured triangles, but didn't use a struct to store the data - I want a struct for caching purposes, so the vertex colour is close to the vertex position.
to set up sdl / glew:
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
lm->Log(LogManager::Error, "Video initialization failed: " + std::stri开发者_Python百科ng(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);
make the 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;
to draw:
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);
This:
glBufferData(GL_ARRAY_BUFFER, sizeof (verts), &verts[0], GL_STATIC_DRAW);
Should be:
glBufferData(GL_ARRAY_BUFFER, sizeof (Vertex)*numVerts, &verts[0], GL_STATIC_DRAW);
The original just gave you the size of the pointer, not the size of the array. This explains the single dot, but I'm not sure why you are seeing a slow down.
精彩评论