Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » VBO under DirectX

This thread is locked; no one can reply to it. rss feed Print
VBO under DirectX
Billybob
Member #3,136
January 2003

I can get a nice tutorial on VBOs for OpenGL (NeHe), but I have trouble finding something for DirectX. How do I go about doing the same thing (if it's under a different name) under Direct3D?

And I don't code D3D, I just got to slap this into Irrlicht and be on my merry way.

Thanks!

Thomas Harte
Member #33
April 2000
avatar

VBO is... vertex buffer object? If so then this is an area where OpenGL has played catchup with DirectX. Check out MSDN for DirectX vertex buffer information.

Krzysztof Kluczek
Member #4,191
January 2004
avatar

DirectX 9 SDK->DirectX Graphics->Tutorials and Samples->Tutorials->Direct3D Tutorials->Tutorial 2: Rendering Vertices

This should get you started. Follow links in the tutorial to get descriptions what functions do, follow links in these functions' descriptions, etc. :)

Billybob
Member #3,136
January 2003

So, DirectX will automatically put the vertex buffer on the video card?
Pfff, OpenGL is behind ;)

So, let's see here. Will this work (the way I want):

1#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX2)
2 
3IDirect3DVertexBuffer9* g_pVB = NULL;
4if( FAILED( g_pd3dDevice->CreateVertexBuffer( vertexCount * sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &g_pVB, NULL ) ) )
5 return E_FAIL;
6 
7VOID* pVertices;
8if( FAILED( g_pVB->Lock( 0, sizeof(CUSTOMVERTEX) * vertexCount, (void**)&pVertices, 0 ) ) )
9 return E_FAIL;
10
11memcpy( pVertices, vertices, sizeof(S3DVertex) * vertexCount );
12
13g_pVB->Unlock();
14 
15 
16// Render
17g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
18g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
19g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, vertexCount / 3 );

Obviously "Render" will occur in a different function.

So, If I understand this correctly "D3DUSAGE_WRITEONLY" should tell it that video memory is the best place for the buffer, right? Or something like that.

Bob
Free Market Evangelist
September 2000
avatar

Quote:

So, DirectX will automatically put the vertex buffer on the video card?
Pfff, OpenGL is behind ;)

Behold, the same code in OpenGL:

1 
2#ifndef GL_BUFFER_OFFSET
3# define GL_BUFFER_OFFSET(x) ((char *)NULL + (x))
4#endif
5 
6GLuint vb;
7glGenBuffer(1, &vb);
8glBindBuffer(GL_ARRAY_BUFFER, vb);
9 
10/* Either this, if you already have a copy of the data */
11glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
12 
13/* Or this, if you want to build the data in place */
14do {
15 GLvoid *ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
16 if (!ptr) {
17 return failure;
18 }
19 
20 memcpy(ptr, vertices, sizeof(vertices));
21} while (!glUnmapBuffer(GL_ARRAY_BUFFER)); // XXX You may want to handle this case a little better than I did.
22 
23 
24/* Now set up your vertex array */
25glEnableVertexAttribArray(0);
26glVertexAttribPointer(index, size, type, normalized, stride, GL_BUFFER_OFFSET(0));
27 
28/* Render */
29glDrawArray(GL_TRIANGLES, 0, vertex_count/3);

Edit: Fixed code.

--
- Bob
[ -- All my signature links are 404 -- ]

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Quote:

GLvoid *ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(ptr, vertices, sizeof(vertices));

Won't it crash when glMapBuffer returns NULL?

Quote:

glUnmapBuffer()

I guess it should be:
glUnmapBuffer(GL_ARRAY_BUFFER);

Quote:

glVertexAttribPointer(index, size, type, normalized, stride, GL_BUFFER_OFFSET(0));

Is it OpenGL 2.0? There is no such function in OpenGL 1.5, it's ARB_vertex_program and requires *ARB suffix as such (and probably won't work without vertex program set up).

Also, doesn't it require EnableVertexAttribArrayARB?

Anyway, if anybody wants easy to use OpenGL VBO wrapper class, just PM me. :)

Billybob
Member #3,136
January 2003

So is my code right?

Krzysztof Kluczek
Member #4,191
January 2004
avatar

Quote:

So is my code right?

Does it work? If it does, then it's probably right (at least looks like it should). :)

Bob
Free Market Evangelist
September 2000
avatar

Quote:

Won't it crash when glMapBuffer returns NULL?

Sure.

Quote:

I guess it should be:
glUnmapBuffer(GL_ARRAY_BUFFER);

Yup.

Quote:

Is it OpenGL 2.0?

Yes. Alternatively, you can use any of the ARB or NV vertex program extensions for the similar functionality.

Quote:

Also, doesn't it require EnableVertexAttribArrayARB?

Sure, you may want to do that too :)

--
- Bob
[ -- All my signature links are 404 -- ]

Go to: