Skip to main content
GameDev.net gamedev.net
🔒 Locked

VBO Map-Unmap query

Started by WillPash Dec 21, 2008 at 5:52 AM 1 replies 1.7k views
Original Post
WillPash
WillPash
I am working with VBO's for a new project, I want to use flexible maaped VBO's which are similar to the Direct3D Lock() Unlock() mechanism. I have written some code below, which Maps and Unmaps the vertex data on the fly in the same way as the D3D lock, but it overwrites over the previous VBO vertex data... Here is the rough brief example of what i'm implementing...
//Vertex structure
struct Vertex3D
{
	float x;
	float y;
	float z;
	float u;
	float v;
};

class VertexBuffer
{
	public:
	VertexBuffer(){}
	~VertexBuffer(){}
	
	void InitialiseBuffer(Vuint buffer_size)
	{
		//NOW INTIAILISING VERTEX DATA FOR VBO
		glGenBuffers(1, &ID);
		glBindBuffer(GL_ARRAY_BUFFER,ID);
		glBufferData(GL_ARRAY_BUFFER,buffer_size, NULL, V_VERT_BUF_STATIC);

		glEnableClientState(GL_VERTEX_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glVertexPointer(3, GL_FLOAT, sizeof(Vertex3D), BUFFER_OFFSET(0));
		glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex3D), BUFFER_OFFSET(12));
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
		glDisableClientState(GL_VERTEX_ARRAY);
	}

	//MAPPING FUNCTION MEANT TO WORK LIKE LOCK FUNCTION IN DIRECT3D, BUT SHORTENED TO MAKE IT ONLY WRITE TO VERTEX BUFFER
	void* MapBuffer()
	{
		glBindBufferARB(GL_ARRAY_BUFFER_ARB,ID);
		return glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY_ARB);
	}

	void UnmapBuffer()
	{
		glBindBuffer(GL_ARRAY_BUFFER,ID);
		glUnmapBuffer(GL_ARRAY_BUFFER);
		glBindBufferARB(GL_ARRAY_BUFFER,0);
	}
	
	void BindBuffer()
	{
		glBindBuffer(GL_ARRAY_BUFFER, ID);
	}
	
	void Destroy()
	{
		glDeleteBuffers(1, &ID);
	}

	GLuint ID;
};



Now I am going to use the class to bind buffers

//PLACE VERTICES INTO VECTOR CONTAINER
vector<Vertex3D>	points_one;
vector<Vertex3D>	points_two;

VertexBuffer	*buffer_one;
VertexBuffer	*buffer_two;

buffer_one = new VertexBuffer();
buffer_two = new VertexBuffer();

Vuint buffer_size_one = points_one.size()*sizeof(Vertex3D);
Vuint buffer_size_two = points_two.size()*sizeof(Vertex3D);

buffer_one->InitialiseBuffer(buffer_size_one);
VertexBuffer* membuffer_one = (VertexBuffer*)buffer_one->MapBuffer();
for(vector<Vertex3D>::iterator it = points_one.begin(); it!=points_one.end(); it++)
{
membuffer_one->x	=	it->x;
membuffer_one->y	=	it->y;
membuffer_one->z	=	it->z;
membuffer_one->u	=	it->u;
membuffer_one->v	=	it->v;
membuffer_one++;
}
buffer_one->UnmapBuffer();


buffer_two->InitialiseBuffer(buffer_size_two);
VertexBuffer* membuffer_two = (VertexBuffer*)buffer_two->MapBuffer();
for(vector<Vertex3D>::iterator it = points_two.begin(); it!=points_two.end(); it++)
{
	membuffer_two->x	=	it->x;
	membuffer_two->y	=	it->y;
	membuffer_two->z	=	it->z;
	membuffer_two->u	=	it->u;
	membuffer_two->v	=	it->v;
	membuffer_two++;
}
buffer_two->UnmapBuffer();




buffer_one->BindBuffer();
//RENDER VERTEX BUFFER THROUGH GL DRAWARRAYS

buffer_two->BindBuffer();
//AGAIN RENDER VERTEX BUFFER THROUGH GL DRAWARRAYS



Now if I render and bind VBOs, the only vertex buffer that renders is the second binded vertex mesh during both instances... Could anyone give me advice on how to map it the same way as D3D. [Edited by - WillPash on December 21, 2008 6:56:22 AM]
Dave 'Kit' Wilson - Reliant Code
jonathanjansson
jonathanjansson
Try set the pointers each time in BindBuffer().

glVertexPointer(3, GL_FLOAT, sizeof(Vertex3D), BUFFER_OFFSET(0));glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex3D), BUFFER_OFFSET(12);


In the current code you will leave the pointers set to the buffer that was initialized last so that explains why you see only the second object.

I am also pretty sure that the client states must be left enabled while rendering:

glEnableClientState(GL_TEXTURE_COORD_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);

WillPash
WillPash
Thanks Jon as i've already tried glEnableClientState, but its same outlook, with the overwriting the first buffer. I want to write onto the buffer and keep the data on the graphics card and not on the system memory. I dont want to keep on updating the mapping data on a per frame basis...
Dave 'Kit' Wilson - Reliant Code

Topic Locked

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

Sign in to reply to this topic.