Vertex Buffers in OpenGL

Started by
2 comments, last by FreOzgur 11 years, 2 months ago

Actually, It's not an OpenGL question.

This is how I draw things in OpenGL:


glEnableClientState (GL_VERTEX_ARRAY);	
glEnableClientState (GL_NORMAL_ARRAY);	
glEnableClientState (GL_TEXTURE_COORD_ARRAY);		
glVertexPointer(3, GL_FLOAT, sizeof(VertexBuffer), obj->m_vertices[0].vertex);	
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexBuffer), obj->m_vertices[0].texCoord);	
glNormalPointer(GL_FLOAT, sizeof(VertexBuffer), obj->m_vertices[0].normal);			
glDrawElements (GL_TRIANGLES, obj->indicesCount, GL_UNSIGNED_INT, obj->indices);					glDisableClientState (GL_VERTEX_ARRAY);	
glDisableClientState (GL_NORMAL_ARRAY);	
glDisableClientState (GL_TEXTURE_COORD_ARRAY);



As you see here, I don't use any memcpy..

And this is how I have to draw in DirectX: (Pseudo)


D3DVERTEX *ptr = NULL;
app.getDevice()->CreateVertexBuffer(sizeof(quad),                                    
D3DUSAGE_WRITEONLY,                                    
0,                                    
D3DPOOL_MANAGED,                                    
&vertex_buffer,                                    
NULL);
vertex_buffer->Lock(0, 0, (void**)&ptr, 0);
memcpy((void*)ptr, (void*)vetices_of_quad, sizeof(vetices_of_quad));
vertex_buffer->Unlock();




As you see here, I have to use a "memcpy" each frame.. Why each frame, because I can't keep my LPDIRECT3DVERTEXBUFFER9 object..

I just want to hold my vertex data in my own vertex buffer class (VertexBuffer), and be able to use it in both OpenGL and DirectX, without copying the data each frame..

I don't know DirectX much.. I'm a newbie.
Please help sad.png

(BTW, sorry for my bad English)

Advertisement
because I can't keep my LPDIRECT3DVERTEXBUFFER9 object..
And why not?


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Something seriously wrong if that's the way you're drawing in D3D; the equivalent D3D code should look more like this:


device->SetStreamSource (...);
device->SetVertexDeclaration (...);
device->SetIndices (...);
device->DrawIndexedPrimitive (...);

However, from the look of things, you're not even using vertex buffers at all, but system memory pointers instead, so the following may be more appropriate if so:


device->SetVertexDeclaration (...);
device->DrawIndexedPrimitiveUP (...);

What's more, if you're just using the fixed pipeline, you don't even need to bother creating a vertex declaration at all, so it becomes:


device->SetFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1);
device->DrawIndexedPrimitiveUP (...);

As you can see, all of these cases are substantially simpler than the GL code.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Actually I won't need DirectX codes. I'm just writing a renderer, I will implement it using OpenGL, but I want it to become flexible. So I just want things to be correct, and won't be a problem when I decide to write a DirectX renderer.

My VertexBuffer class keeps the system pointers, and it seems that I can use them both in OpenGL and DirectX..

Thank you so much mhagain.

This topic is closed to new replies.

Advertisement