Skip to main content
gamedev.net
🔒 Locked

VBOs

Started by THACO Apr 8, 2005 at 8:34 PM 6 replies 1.2k views
Original Post
THACO
THACO
Ok, I have used vertex arrays but I want to move on to Vertex Buffer Objects. I currently have information for the vertices, texture verts, and the normals for each vertex. How do I convert all that into VBOs? here is what im thinking glGenBuffersARB(3, vboid); 3 buffers glEnableClientState(GL_VERTEX_ARRAY); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]); glBufferDataARB(GL_ARRAY_BUFFER_ARB, ( numVertex * 3 )*sizeof(GLfloat), vertexData, GL_STATIC_DRAW_ARB); then 2 more for normals and texture vertices. then to display I got this web site http://www.devmaster.net/forums/lofiversion/index.php?t360.html glEnableClientState(GL_VERTEX_ARRAY); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]); glVertexPointer(3,GL_FLOAT, 0, 0); // Why is the last zero a zero, the first is where to start, but what about the second? I would assume do the same as above for normals, and texture vertex but change it accordingly. Last just call glDrawArrays. Anything I am missing? Also I would be hopefully using a vertex program to deform/animate the verticies can i still use GL_STATIC_DRAW_ARB ? cuz in the main program I will not be changing the vertices at all, just after they are sent down the pipeline. Thanks in advance -Thaco
Muncher
Muncher
Hi,

here is the code that i use to create a VBO from a vertex array. I think the same would go for a normal/texcorrd array. To draw a VBo, its the same as with a regular array :) hope it helps

// Check for the extension first	if (GLEE_ARB_vertex_buffer_object)	{		// Create the primary VBO		m_bUseVBO = true;		glGenBuffersARB(1,&m_VBOHandle);		glBindBuffer(GL_ARRAY_BUFFER,m_VBOHandle);		glBufferDataARB(GL_ARRAY_BUFFER,sizeof(GLfloat)* m_numElements * 3,m_vertArray,GL_STATIC_DRAW);		// Check for a valid buffer		if (glIsBuffer(m_VBOHandle))		{			// Output sucess			std::cout << "SUCCESS: initVBO() Primary VBO created \n";			std::cout << "VBO using: " << (sizeof(GLfloat)* m_numElements * 3) << " bytes of video memory \n"; 						// Release the vertex array because its			// all on the video card now			if (m_vertArray) {				delete []m_vertArray;				m_vertexData = NULL;			}		}		else		{			std::cout << "ERROR: initVBO() Primary VBO creating failed \n";			m_bUseVBO = false;		}	}	else	{		// VBO's are not supported, so print error and set flag to false.		std::cout << "\nERROR: initVBO() \"GL_ARB_vertex_buffer_object extension\" not supported \n";		m_bUseVBO = false;	}
swiftcoder
swiftcoder
Quote:
Original post by THACO
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]);
glVertexPointer(3,GL_FLOAT, 0, 0); // Why is the last zero a zero, the first is where to start, but what about the second?


If I remember correctly, the first zero is the stride (the distance between individual vertices), and the second zero is the pointer to where to begin (i.e. zero for the beggining of the VBO).

Quote:
Original post by THACO
Also I would be hopefully using a vertex program to deform/animate the verticies can i still use GL_STATIC_DRAW_ARB ? cuz in the main program I will not be changing the vertices at all, just after they are sent down the pipeline. Thanks in advance

-Thaco


AFAIK, GL_STATIC_DRAW_ARB just means that the app is not going to write to the VBO again, the vertices still pass through the standard GL pipeline (but from super-fast video memory).

Hope this helps,

SwiftCoder

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
_the_phantom_
_the_phantom_
Quote:
Original post by swiftcoder
Quote:
Original post by THACO
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]);
glVertexPointer(3,GL_FLOAT, 0, 0); // Why is the last zero a zero, the first is where to start, but what about the second?


If I remember correctly, the first zero is the stride (the distance between individual vertices), and the second zero is the pointer to where to begin (i.e. zero for the beggining of the VBO).


The 2nd 0 is the offset in bytes from the start of the VBO to the data you want. the VBO spec has a macro in it which most people use to set it right.
swiftcoder
swiftcoder
Quote:
Original post by _the_phantom_
Quote:
Original post by swiftcoder
Quote:
Original post by THACO
glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]);
glVertexPointer(3,GL_FLOAT, 0, 0); // Why is the last zero a zero, the first is where to start, but what about the second?


If I remember correctly, the first zero is the stride (the distance between individual vertices), and the second zero is the pointer to where to begin (i.e. zero for the beggining of the VBO).


The 2nd 0 is the offset in bytes from the start of the VBO to the data you want. the VBO spec has a macro in it which most people use to set it right.


My intention was to show that it is a pointer argument, not an integer.

From the spec:

When the default buffer object (object zero) is
bound, all pointers behave as real pointers. When any other
object is bound, all pointers are treated as offsets.
Conceptually, one can imagine that buffer object zero is a buffer
object sitting at base NULL and with an extent large enough that
it covers all of the system's virtual address space.
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
V-man
V-man
Quote:
Original post by THACO
Ok, I have used vertex arrays but I want to move on to Vertex Buffer Objects. I currently have information for the vertices, texture verts, and the normals for each vertex. How do I convert all that into VBOs?

here is what im thinking

glGenBuffersARB(3, vboid); 3 buffers

glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, ( numVertex * 3 )*sizeof(GLfloat), vertexData, GL_STATIC_DRAW_ARB);

then 2 more for normals and texture vertices.

then to display I got this web site
http://www.devmaster.net/forums/lofiversion/index.php?t360.html

glEnableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboid[0]);
glVertexPointer(3,GL_FLOAT, 0, 0); // Why is the last zero a zero, the first is where to start, but what about the second?

I would assume do the same as above for normals, and texture vertex but change it accordingly.

Last just call glDrawArrays.

Anything I am missing?

Also I would be hopefully using a vertex program to deform/animate the verticies can i still use GL_STATIC_DRAW_ARB ? cuz in the main program I will not be changing the vertices at all, just after they are sent down the pipeline. Thanks in advance

-Thaco


If you have vertex, normals and texcoords, I suggest you interlace them like this :

struct MyVertex
{
float x, y, z;
float nx, ny, nz;
float s, t;
};


Then upload that will glBufferData
Then call these :
glBindBuffer(GL_ARRAY_BUFFER, vboid[0]);
glVertexPointer(3, GL_FLOAT, sizeof(MyVertex), 0);
glNormalPointer(GL_FLOAT, sizeof(MyVertex), 12);
glTexCoordPointer(2, GL_FLOAT, sizeof(MyVertex), 24);

and enable the arrays, and call glDrawElements or whatever you like.
GL_STATIC_DRAW is good if you won't be modifying the VBO. Using a vertex_program is not a problem whatsoever.
THACO
THACO
Thanks Vman, I completely forgot about the interleaved arrays. This leaves me with one more questions. This would be more CQ, but I need to send a matrix to the vertex program also. I am just learning shaders so I am kinda lost when it comes to something this advanced. But I have a group of matrices, right now 1 of matrix has influence on many vertices, but its just 1:x. Is there a way I could send all the matricies to the program then for each vertex also send an index into an array of matrices? My main problem is I don't have the best understanding of how to actually send the information from OpenGl to the vertex program. I have done simple programs which send a value that is the same for each vertex but never send extra info which is different for each vertex. I know you can send arrays but are they constant like i need for my array of matrices, or do they match up with the verts, or can you do both, and how to set them up? I'll be searching the net to hopefully find answers, and probably repost this in a couple of days anyway. Again thanks for the help

-Thaco
V-man
V-man
THACO, not sure I understand. If the matrix is for the entire object, you can upload it as a program environment parameter or program local parameter.
If it's a 4x4 matrix, then you need to store each row in a single parameter.

The program address registers can be used as an index.
I think the procedure is to pass a per vertex value, copy that value to the address register and then use that register.

You will need functions like
glProgramEnvParameter4fvARB
glProgramLocalParameter4fvARB

to load the matrices.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.