PDA

View Full Version : vertex buffer objects



stanigator
03-10-2009, 03:55 PM
After looking at this tutorial (http://www.songho.ca/opengl/gl_vbo.html) and even importing the useful parts of the tutorial code, I am lost on the part with the vertex coordinates and indices buffer. Is it absolutely essential in terms of ease to have a buffer for the indices? I have two separate arrays that are stored as [x1 y1 z1 ... xn yn zn], but I don't have any indices of each node. What would be your recommendations if I don't have an index buffer? Make something up?

Thanks.

RGHP
03-10-2009, 04:48 PM
Indices are not necessary. When you have a mesh where many triagles share a vertex plus texture coordinates, normals and other per-vertex attributte, indices can save you a lot of memory. Also if, for example, you are animating your mesh, with inidices you just have to modify one vertex and all the triangles connected to it will be updated.
Of course if you only have vertices and you are not editing them, and no connected primitives are used, just send the vertexs :).

stanigator
03-11-2009, 12:58 AM
I ended up didn't use this tutorial other than for references on the functions only. I followed this (http://steinsoft.net/index.php?site=Programming/Code%20Snippets/OpenGL/no11) instead. I used vbo without the indices, but I can find out the indexing information if it's helpful on reducing significant memory usage. However, I didn't have anything rendered in the image.

I have uploaded the relevant source code, libraries, and binary files to http://svn.xp-dev.com/svn/ssylee-sdl-needle/ as a public repository for now in case if you want to make any comments on what I did that may have caused the project not to render the points. Meanwhile, it would also be helpful to hear the most common issues with using vertex buffer objects in order to render arrays that contain all the vertices. Thanks.

Ilian Dinev
03-11-2009, 06:33 AM
You use
glVertexPointer(3, GL_FLOAT, 0, 0);
but I don't see you enable that pointer:
glEnableClientState(GL_VERTEX_ARRAY);

Rosario Leonardi
03-11-2009, 08:01 AM
I used vbo without the indices, but I can find out the indexing information if it's helpful on reducing significant memory usage. However, I didn't have anything rendered in the image.
Take a sphere with n vertical division and m horizontal division.
You will have n triangle on the top, n on the bottom and a grid of m*(n-1) quad = 2 * m(n-1) triangle and n*m+2 vertices.
Without indexing you have to memorize
3 * (2 * n + m(n-1)) triangle vertices
with indexing you have to memorize
m*n+2 vertices and 3 * (2 * n + m(n-1)) index
Every vertices (if you memorize only the position) is tree float, every index is an integer (but you can use short or even byte).
With n = 10; m = 10 you have
3 * (2* 10 + 10 * 9) * 3 * sizeof(float) = 3960 byte with non index mode and
3 * (2* 10 + 10 * 9) * sizeof(int) + (10*10+2)*3*sizeof(float) = 2544 byte with index mode.
the memory consumption become much worse for non index mode if you have normal, texture coordinates and colour in your vertex structures.
The only bad thing about index mode is that you have to split vertices if triangle don't share some attribute (like normal in sharp edge or texCoord on texture cut) that's why I choose a sphere for the example. :D

stanigator
03-11-2009, 02:51 PM
I have added a few lines in the appropriate areas (what I think is appropriate based on what I read on relevant resources on vbo), and updated it to the repository (http://svn.xp-dev.com/svn/ssylee-sdl-needle/). However, it still doesn't render anything on the screen (i.e. pitch black). I have also tried a much simpler example of drawing a triangle using vbo on the screen, but not much luck either. I'm not sure if it would have anything to do with sdl toolkit initialization problem.

I haven't started working out on the example of the indices and the nodes yet for the sphere. I have been trying to get a simple example working, and then work on the vertex indices later.

M/\dm/\n
03-11-2009, 03:25 PM
Indices are also good because some of the vertices can be precached, but that's not always the case with all those shaders nowadays.

But if it's simple geometry, call glDrawArrays(..?..? GL_TRIANGLES ..?..?);

stanigator
03-11-2009, 08:14 PM
I did call glDrawArrays() function with the 3 parameters as I wrote in the code. I'm going to double-check if I set up the SDL toolkit on OpenGL properly.

Ilian Dinev
03-12-2009, 06:30 AM
Cross-check whether with simple arrays (no vbos) anything is drawn at all.

glVertexPointer(3, GL_FLOAT, 0, myVertices);
It could be something as simple as having foreground color = clear (background) color, or camera is not looking at object at all.

stanigator
03-12-2009, 12:53 PM
I have tried to render simple vertex arrays using the following method:



/* Vertices of a triangle (counter-clockwise winding) */
float data[] = {1.0, 0.0, 1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 1.0};


if (rInfo.vboSupported)
{
/* rInfo.vboId1 = createVBO(model.xall.nodepts, DIMENSIONS*sizeof(float)*model.xall.entries,
GL_ARRAY_BUFFER_ARB, GL_DYNAMIC_DRAW_ARB);
rInfo.vboId2 = createVBO(model.xndisp.nodepts, DIMENSIONS*sizeof(float)*model.xndisp.entries,
GL_ARRAY_BUFFER_ARB, GL_DYNAMIC_DRAW_ARB);*/
id = createVBO(&data[0], sizeof(data), GL_ARRAY_BUFFER_ARB, GL_STATIC_DRAW_ARB);
}

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, &data[0]);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_VERTEX_ARRAY);

SDL_GL_SwapBuffers();


At the same time, I have also attempted to clear the foreground color as well as attempting to set a camera view in the initialization function:



void renderInfo::initialize()
{
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}

SDL_WM_SetCaption("My first OpenGL program", NULL); // change the name later
screen = SDL_SetVideoMode(640, 480, 32, flags);
if ( screen == NULL ) {
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}

// clear foreground color
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
SDL_FillRect(screen,NULL,0x000000);

videoinfo = SDL_GetVideoInfo();

printf("%i", videoinfo->blit_hw);

// set up double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

// set up RGBA
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

// set up camera
gluPerspective(45.0f, // 45 degrees
(float) screen->w / (float) screen->h, // 1:1 aspect ratio
0.01f, // distance from the viewer to the near clipping plane
100.0f // distance from the viewer to the far clipping plane
);
gluLookAt(5, 5, 5,
1, 0, 0,
0, 0, 1);


// check if the video card and operating system supports VBO
if (strstr((char*) glGetString(GL_EXTENSIONS),"ARB_vertex_buffer_object"))
{
#ifdef _WIN32 // if running under Windows, retrieve the function pointers; otherwise, no need
//get function pointers using SDL_GL_GetProcAddress(..)
// (could be using wglGetProcAddress(..) too)
pglBindBufferARB = (PFNGLBINDBUFFERARBPROC)
SDL_GL_GetProcAddress("glBindBufferARB");
pglGenBuffersARB = (PFNGLGENBUFFERSARBPROC)
SDL_GL_GetProcAddress("glGenBuffersARB");
pglBufferDataARB = (PFNGLBUFFERDATAARBPROC)
SDL_GL_GetProcAddress("glBufferDataARB");
pglDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)
SDL_GL_GetProcAddress("glDeleteBuffersARB");
pglGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)
SDL_GL_GetProcAddress("glGetBufferParameterivARB");
#endif

vboSupported = true;
}
}


However, nothing still is rendering. Could there be other factors other than the undesired foreground color and improper setup of the camera viewpoint? Thanks. (I have also updated the changes to the repository in case if you want to take a look).

Ilian Dinev
03-12-2009, 04:00 PM
Setting the clear-color is done via glClearColor()

Start with something simpler. See it work. Add a few lines, see it work. Then jump onto VBOs (though as you see it's easy).