Converting D3D vertex buffers to OpenGL vertex arrays...

Started by
11 comments, last by ElectroDruid 16 years, 9 months ago
Hi, Firstly, apologies if this is in the wrong forum - moderators, feel free to move it if it is, but given that it mentions Direct3D and OpenGL, I didn't know which forum to put it in, and I didn't want to cross-post. Secondly, apologies if this seems like a really obvious question. It kinda does, to me, but I couldn't find any threads which answered when I searched the forums. So, I've got a bit of code I found online which I'm playing around with, which renders stuff using DirectX 8, and I'm trying to abstract the rendering calls to a common interface so I can have the same code run on an OpenGL renderer. I'm pretty comfortable with OpenGL, not so comfortable with DirectX, although I do know a little bit. The interface has methods, which do roughly the following. This isn't the exact or complete code because I've just tried to show the interesting stuff - I can post the full source if it'll help.

void CreateVertexBuffer(UINT nLength, UINT nVertexSize, DWORD FVF, D3DPOOL Pool)
{
    // m_pD3DDevice being an IDirect3DDevice8
    m_pD3DDevice->CreateVertexBuffer(nLength*nVertexSize,
		D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY, FVF,
		Pool, &m_pVertexBuffer);

    m_pD3DDevice->SetStreamSource(0, m_pVertexBuffer, nVertexSize);
    m_pD3DDevice->SetVertexShader(FVF);
}

void LockVertexBuffer(UINT nVertices, BYTE **ppVertices)
{
    m_pVertexBuffer->Lock(0, nVertices*m_nVertexSize, ppVertices, D3DLOCK_DISCARD);
}

void UnlockVertexBuffer()
{
    m_pVertexBuffer->Unlock();
}

// ... Similar functions for the index buffers - you get the picture ...

void DrawVertexBuffer(int numVerts, int numIndeces)
{
    m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, numVerts, 0, numIndeces/3);
}
The program calls CreateVertexBuffer once on initialisation, and then several times a frame it calls LockVertexBuffer, fills the resulting buffer with triangle data, calls UnLockVertexBuffer then DrawVertexBuffer, then locks the buffers again to generate more vertices, and so on. So, how to port this to OpenGL? I've got CreateVertexBuffer just setting up an array of vertices, LockVertexBuffer trying to point *ppVertices at that array and calling glLockArraysEXT(0, nVertices). UnlockVertexBuffer does nothing but call glUnlockArraysEXT(), and DrawVertexBuffer sets up the client states, does a glDrawElements, and disables the client states again. This doesn't seem to work, it crashes in annoying ways which don't give me a callstack I understand or the source code for the point where the crash occurs. I suspect this is because I'm misunderstanding the differences between D3D vertex buffers and OpenGL vertex arrays. If I understand correctly, locking a vertex buffer in D3D with the D3DLOCK_DISCARD allocates memory for a new buffer to write to every time you call it (giving you a pointer to that memory), and then automagically cleaning that memory up somehow at a later date when it's not being used (or is it done in the m_pVertexBuffer->Unlock?). Whereas, my OpenGL implementation sets up one vertex array to rule them all, and just lets the program write to it, render it, write to it again, render it... I'm not sure what needs to change in my OpenGL implementation to get the same behaviour as the D3D version. What do Direct3D vertex buffers do that OpenGL vertex arrays don't? And how would I go about adding that functionality to my OpenGL implementation?
"We two, the World and I, are stubborn fellows at loggerheads, and naturally whichever has the thinner skull will get it broken" - Richard Wagner
Advertisement
Hi ElectroDruid,

I don't know much about DX so i'm shooting in the dark here. Here is how i think it should be done :

1) CreateVertexBuffer should create a VBO or allocate a chunk of system memory depending on the value of the pool variable. Also, usage (which is hard-coded in your case) can be given as a hint to the driver when creating the VBO.

2) LockVertexBuffer should map the VBO using glMapBuffer. glLockArrays() is from the GL_EXT_compiled_vertex_arrays extension which is deprecated iirc. In D3D when you lock a VB, you are requesting a pointer in order to write to it, as you described. The equivalent functionality in GL is given by glMapBuffer.

3) UnlockVertexBuffer should unmap the VBO in order to be able to use it for rendering.

4) DrawVertexBuffer should set all the glXXXPointer variables and enable all the required client state. I'm not really sure, but i suspect that in the D3D case you posted, this happens in the CreateVertexBuffer function. Maybe because the vertex format is needed to be known when locking the buffer? I'm not sure.
Finally you can safely call glDrawElements.

Hope that helps. As i said i don't know much about DX, so if anyone find any mistakes in the above descriptions, please correct me.

HellRaiZer

EDIT : I think, until now, there is no flag in GL equivalent to D3DLOCK_DISCARD, and other similar flags. I think this is because you can't specify the size of the memory you are going to modify when locking/mapping the VB. IIRC this will be added in Long Peaks. Check the latest OGL Pipeline article for more info.
HellRaiZer
Quote:Original post by HellRaiZer
EDIT : I think, until now, there is no flag in GL equivalent to D3DLOCK_DISCARD, and other similar flags.
You can perform a discard lock in OGL by calling BufferData with a NULL pointer.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
hey

Quote:The program calls CreateVertexBuffer once on initialisation, and then several times a frame it calls LockVertexBuffer, fills the resulting buffer with triangle data, calls UnLockVertexBuffer then DrawVertexBuffer, then locks the buffers again to generate more vertices, and so on.


I too know very little about Direct3D. But I wonder, are you rendering static objects? and if so then why must you keep filling the Vertex Buffer several times each frame? Do you use a single Vertex Buffer to render multiple objects?

Anyhows... with Vertex Buffer Objects in OpenGL, for static objects you only need to generate the Vertex Buffer Object, and load in the Vertex Data once, which is stored in Video Memory. You then can render them very much alike regular Vertex Arrays in OpenGL.

Note, you must use a separate, and different target/type of Vertex Buffer Object for Vertex Indices (ELEMENT_ARRAY_BUFFER, rather than ARRAY_BUFFER).

You allocate/load in the Vertex Data with glBufferData(), and at this time you specify the data usage, whether it be STATIC/DYNAMIC/STREAM along with READ/COPY/DRAW flags. You can then update the data via glBufferSubData().

You can also gain direct access to data via glMapBuffer()/glUnmapBuffer(), which returns a pointer to the Vertex Data.

BTW, it's recommended that you use glDrawRangeElements(), rather than glDrawElements() when drawing Vertex Buffer Objects, which you can read about in the article I link to below.

Finally, rather than try and learn/understand from my brief explainations, I recommend you checkout THIS.

edit
To conclude, I made some pesudo code...

glGenBuffers...
glBindBuffer...
glBufferData...

I think the official OpenGL API is HERE.

// init vertex buffer object...// note, this would be done when you load your geometry// first you generate your vertex buffer object via...void glGenBuffers(GLsizei n, GLuint * buffers)// then you bind this buffer via... void glBindBuffer(GLenum target, GLuint buffer)void glBindBuffer(GLenum target, GLuint buffer)// then you allocate/load data into this buffer via...// note, "GLenum target" set to GL_ARRAY_BUFFER for vertex data, and GL_ELEMENT_ARRAY_BUFFER for vertex indicesvoid glBufferData(GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage)// and remember to unbind your vertex buffer object via passing 0 for "uint buffer"...void glBindBuffer(GLenum target, GLuint buffer)// and later on when it comes to drawing...// you draw alike regular opengl vertex arrays, except you specify offset relative to each vertex buffer object// note, this would obviously be done in a separate function// enable vertex arraysglEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_TEXTURE_COORD_ARRAY);// bind your vertex data VBOvoid glBindBuffer(GL_ARRAY_BUFFER, GLuint buffer);// set your vertex array pointers// note, instead of pointer you use offset relative to origin of VBO dataglVertexPointer(3, GL_FLOAT, 0, (const GLvoid*)0);glNormalPointer(GL_FLOAT, 0, (const GLvoid*)normalOffset);glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid*)texCoordOffset);// remember to unbind your VBOglBindBuffer(GL_ARRAY_BUFFER_ARB, 0);// now bind your vertex indices VBOvoid glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, GLuint buffer)// draw vertex array// note, again instead of pointer you use offset relative to origin of VBO data// note, use glDrawRangeElements() for best performancevoid glDrawRangeElements (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, (const GLvoid*)indexOffset);// again, remember to unbind your VBOglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);// disable your vertex arraysglDisableClientState(GL_VERTEX_ARRAY);glDisableClientState(GL_NORMAL_ARRAY);glDisableClientState(GL_TEXTURE_COORD_ARRAY);


Please correct me if I have made any mistakes. I hope this helps :).

cya

[Edited by - yosh64 on July 11, 2007 10:08:29 AM]
Wow... Okay, I have to hold my hands up and admit that I'm a bit out of my depth here. I've not used any of the advanced (read: not immediate mode) stuff in OpenGL since version 1.2 (hence the use of the deprecated extensions in my code). I don't know what's new since then and I'm kinda lost.

VBOs, glMapBuffer, glBufferData, glBufferSubData, glMapBuffer, glUnmapBuffer... All of these things are new to me. I've been working as a gameplay coder on consoles for nearly 5 years now, and haven't done any graphics programming in that time, so I'm clearly pretty out of touch with how the APIs work nowadays. This is a pet project on the PC, so I need to play catch-up.

I looked on NeHe but didn't see anything which might be of use - can someone point me to any kind of resource/tutorial/example code/whatever which demonstrates this new (to me at least) OpenGL functionality in action, so I can see what makes it tick?

Oh, and:

Quote:But I wonder, are you rendering static objects? and if so then why must you keep filling the Vertex Buffer several times each frame? Do you use a single Vertex Buffer to render multiple objects?


You hit the nail on the head, kinda. I'm not rendering static objects, I'm rendering very dynamic ones (metaballs, specifically), so I'm needing to construct new meshes every frame. I mean meshes as a plural in the sense that I want to make sure that (for example) if two metaballs seperate, and form seperate unconnected meshes, I want to render both of them rather than have one of them suddenly vanish. I'm trying to use a single vertex buffer for everything, even though I'm aware that it's probably wrong, because that's the only way I know about how to do this kind of thing. It strikes me that the most efficient thing would be to do the maths to calculate a bunch of triangles (connected or not), and then just pipe all the triangles to get rendered at the same time. I'm aware that this might not work, and that I might need to render each mesh seperately, but right now I'm just trying to get *something* onscreen without the program crashing.

I guess what I really need (and I will thank my lucky stars if such a thing exists, although I suspect it doesn't) is something like a piece of source code (a "Rosetta Stone", if you will) which draws something very simple (a sphere, or a cube) using Direct3D vertex buffers, and can do the same thing with the same results in OpenGL using vertex arrays, or VBOs, or whatever the best equivalent is. Does such a thing exist?
"We two, the World and I, are stubborn fellows at loggerheads, and naturally whichever has the thinner skull will get it broken" - Richard Wagner
I am not sure what your OpenGL implementation looks like, but I have some comments about your original post:

If you allocate the vertex buffer in D3DPOOL_DEFAULT, Direct3D tries to put it in video memory or AGP memory (unless you're using software vertex processing, then it has to be in system memory). Either way, the memory is not directly accessible by you.

When you call Lock() on the buffer, Direct3D maps the vertex buffer to a special part of memory; when you perform a write, it doesn't go to system memory but is routed through an I/O bus and to the actual location of the buffer. When you call Unlock(), it unmaps the memory so that you no longer have direct access to it. I think that if you're getting a crash, it might be because you're keeping around this pointer.

With D3DLOCK_DISCARD, Direct3D discards the entire contents of the vertex buffer. The old vertex buffer could be floating around somewhere; your video card could be rendering from it right now. You would never know, because it's not in your control anymore. Instead, you get mapped a new vertex buffer that has nothing to do with your old buffer. This means all the data you uploaded before is gone, too.
Quote:Original post by ElectroDruid
I looked on NeHe but didn't see anything which might be of use - can someone point me to any kind of resource/tutorial/example code/whatever which demonstrates this new (to me at least) OpenGL functionality in action, so I can see what makes it tick?


Can't point you to any direct turotials, but whenever i'm interested in seeing whats new or getting to grips with an opengl extension i usually check out Delphi3D first and its extension list each with full spec. Although i guess its no different to opengl.org list. They canbe a bit hard to get into, but after a while they begin to make sense.

I guess you could also search on thos sites for more specific information such as tutorials.
hey

Quote:I'm trying to use a single vertex buffer for everything, even though I'm aware that it's probably wrong, because that's the only way I know about how to do this kind of thing.

I don't see anything wrong with doing this, and are sure it's quite possible. When loading the scene, I would just allocate one Vertex Buffer Object, large enough to hold the maximum amount of vertex data you will need.

Ohh, if you would prefer to update your vertex data in system memory, then when it comes to rendering you can just bind your VBO, and use glBufferSubData() to copy and update your Vertex Buffer Object.

Remember you will also need to update your vertex indices, or maybe just change the number of elements to draw, if I make any sense?

Finally, remember for getting started you may prefer not even to use Vertex Buffer Objects, and/or even Vertex Arrays, and go back to the basics of glBegin()/glEnd(), hehe.

Here are a couple of tutorials I goog'd on Vertex Buffer Objects, THIS, and NeHe lesson 45. But I think you may need to refer back to that original link on Vertex Buffer Objects I posted, or the OpenGL API or something.

edit
You can find an example of both Vertex Arrays, and Vertex Buffer Objects (refered to as AGP Memory) on page 5 of the OpenGL section of ultimategameprogramming.com. You can probably also find a simular example for DirectX at ultimategameprogramming.com also.

cyas

[Edited by - yosh64 on July 12, 2007 1:10:25 AM]
Hi,

Thanks for all the replies and links, I think I'm starting to get a handle on how VBOs work now - although I'm clearly not quite there yet :) I've got code which compiles and runs without crashing, but it's not drawing anything. I've tried the usual testing stuff (setting a non-black glClearColour, disabling culling, lighting, texturing and alpha blending, triple-checking my camera position, orientation, clip planes and FOV to make sure it matches what the Direct3D renderer is doing, and drawing an immediate mode test quad to prove that I can see it), and all of that seems to work fine. The rest of the program hasn't changed, and it seems to be filling the vertex and index arrays with sensible values, and telling the renderer to draw about the number of polygons I'd expect. I just don't see any polys, and I'm at a bit of a loss as to what could be going wrong.

I've ended up trying to use glDrawArrays for the rendering, because glDrawElements was crashing, and I can't get my compiler to accept glDrawRangedElements for some reason. There's also potentially some weirdness with the way I'm using glBindBufferARB in my Lock/Unlock functions, but I'm still not 100% sure how glBindBufferARB works. If someone could take a quick look at what my code is doing, and give me an idea about what might be going wrong, that would be great.

GLuint vboVerts = 0;GLuint vboIndeces = 0;SVertex*				mpVertexBuffer;UINT*					mpIndexBuffer;struct SVertex{	Vector3f v;	// Vertex	Vector3f n;	// Normal	Vector2f t; // Texture coordinates (currently not used)};void Initialize(){	// ... Set up the window, the openGL extensions, and set some default GL states	// (lighting, texturing and backface culling turned off for testing, etc) ...		CreateVertexBuffer(MAX_VERTICES, sizeof(SVertex));	CreateIndexBuffer(MAX_INDICES);}void Uninitialize(){	delete[] mpVertexBuffer;	delete[] mpIndexBuffer;	 glDeleteBuffersARB(1, &vboVerts);	 glDeleteBuffersARB(1, &vboIndeces);}void CreateVertexBuffer(UINT nLength, UINT nVertexSize){	mpVertexBuffer = new SVertex[nLength];	glGenBuffersARB(1, &vboVerts);	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	glBufferDataARB(GL_ARRAY_BUFFER_ARB, nLength * nVertexSize, mpVertexBuffer, GL_DYNAMIC_DRAW_ARB);}void LockVertexBuffer(UINT nVertices, BYTE **ppVertices){	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	// Not sure if this should be here	(BYTE*)(*ppVertices) = (BYTE*)glMapBufferARB(GL_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);	mpVertexBuffer = (SVertex*)(*ppVertices);}void UnlockVertexBuffer(){	glUnmapBufferARB(GL_ARRAY_BUFFER_ARB);	glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);	// Not sure if this should be here}void CreateIndexBuffer(UINT nLength){	mpIndexBuffer = new UINT[nLength];	glGenBuffersARB(1, &vboIndeces);	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, nLength * sizeof(UINT), mpIndexBuffer, GL_DYNAMIC_DRAW_ARB);}void LockIndexBuffer(UINT nIndices, BYTE **ppIndices){	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	// Not sure if this should be here	(BYTE*)(*ppIndices) = (BYTE*)glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_READ_WRITE_ARB);	mpIndexBuffer = (UINT*)(*ppIndices);}void UnlockIndexBuffer(){	glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB);	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);	// Not sure if this should be here}void DrawVertexBuffer(int numVerts, int numIndeces){	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	glNormalPointer(GL_FLOAT, sizeof(SVertex), &mpVertexBuffer[0].n);	glVertexPointer(3, GL_FLOAT, sizeof(SVertex), &mpVertexBuffer[0].v);	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	glIndexPointer(GL_UNSIGNED_SHORT, 0, 0);		glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_NORMAL_ARRAY);	//	glDrawElements(GL_TRIANGLES, numIndeces, GL_UNSIGNED_INT, mpIndexBuffer); // This crashes the program//	glDrawRangeElements(GL_TRIANGLES, 0, numIndeces, numIndeces, GL_UNSIGNED_INT, mpIndexBuffer); // I couldn't get this to compile	glDrawArrays( GL_TRIANGLES, 0, numIndeces);	glDisableClientState(GL_VERTEX_ARRAY);	glDisableClientState(GL_NORMAL_ARRAY);}


Usage: The rest of the code is doing stuff in the following order, about half a dozen times per frame...

LockVertexBuffer();
LockIndexBuffer();
... Generate a bunch of vertex/index data ...
UnlockVertexBuffer();
UnlockIndexBuffer();
DrawVertexBuffer();

Any ideas?
"We two, the World and I, are stubborn fellows at loggerheads, and naturally whichever has the thinner skull will get it broken" - Richard Wagner
I think there are a couple of problems with the code you posted.

1) You don't have to allocate memory for passing it to the glBufferData() function. You can pass NULL in there. This way, when you Lock the VB/IB you won't overwrite the pointer value in 'mpVertexBuffer' and 'mpIndexBuffer'.

2) About your comments/questions in the Lock functions. Yes you have to bind the buffer before mapping it. OTOH there is no need to bind a zero buffer when unmapping, but that's ok if you re-bind it later for using it.

3) What's the value of 'mpVertexBuffer' when specifying glXXXPointer in DrawVertexBuffer? If it holds the last value from the Lock function, then this is wrong. If it's NULL then it should work. The point is that when using VBOs you don't specify a true pointer in memory, but an offset in the VB.

4) glIndexPointer() isn't responsible for setting the triangle indices pointer. It is related to per-vertex indexed color, which you don't need. In order for the glDrawElements to work you have to bind the index buffer (as you do) and then pass an offset in the IBO as the last parameter in the glDrawElements() call.

The DrawVertexBuffer() function should look something like this :
void DrawVertexBuffer(int numVerts, int numIndeces){	glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboVerts);	glNormalPointer(GL_FLOAT, sizeof(SVertex), (void*)(0 + sizeof(Vector3f)));	glVertexPointer(3, GL_FLOAT, sizeof(SVertex), (void*)(0));	glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboIndeces);	glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_NORMAL_ARRAY);		glDrawElements(GL_TRIANGLES, numIndeces, GL_UNSIGNED_INT, 0);	glDisableClientState(GL_VERTEX_ARRAY);	glDisableClientState(GL_NORMAL_ARRAY);}


I hope i haven't done any mistakes, and the above makes sense.

HellRaiZer
HellRaiZer

This topic is closed to new replies.

Advertisement