PDA

View Full Version : VBO Map-Unmap query



DWilson
12-21-2008, 06:25 AM
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.

Jan
12-21-2008, 07:04 AM
Hm, i just quickly glanced at it, but one thing that could be a problem, is that you call glVertexPointer as the first thing to set up your arrays. Make it the LAST function to be called, most drivers do all the vertex array setup in that function call, so maybe that's the reaseon, that it only works properly the second time.

Jan.

dletozeun
12-21-2008, 03:14 PM
I did not look deeply in your code, but I do not understand why you see something with the 2nd VBO. Does this one uses texture mapping?
I have seen something that is wrong to me, you use texture coordinates buffer objects but you do not set which vertex array client state to be modified by glTexCoordPointer. You have to call glClientActiveTexture, this way, before drawing something:



glClientActiveTexture( GL_TEXTURE0_ARB );
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// bind a vbo that contains texture coordinates
// or call glTexCoordPointer with the array size and address
// to use as a simple vertex array
glBindBuffer( GL_ARRAY_BUFFER, texCoordBuffer );
glTexCoordPointer(2, GL_FLOAT, 0, NULL);

glActiveTexture( GL_TEXTURE0_ARB );
glEnable( GL_TEXTURE_2D );

glBindTexture( ... );

// draw something
...
// disable client states
...

_NK47
12-21-2008, 03:35 PM
i had similar problem where i was binding the vertex -index buffers after all gl pointer calls. glBind them first and then specify the vertex stream solved the problem. maybe something similar here.

DWilson
12-22-2008, 07:15 AM
Thanks guys, its all sorted... Happy Xmas to you all

dletozeun
12-22-2008, 12:46 PM
Great! Thanks you too! <8D

_NK47
12-22-2008, 02:14 PM
still would like to know what the problem was. ;D merry xmas!