Jump to content
  • Advertisement
Sign in to follow this  
Matt Aufderheide

Newb question: writing to vertex buffers(D3D C++)

This topic is 4507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, here is newbish quesion about vertex buffers.. I normally just use D3DXMESHES in my engine, and I am ashamed to admit I have mostly avoided vertex/index buffers for a long time :( All I want to do is change some things to an existing vertex buffer. I get the vertex buffer from a D3DXMESH->GetVertexBuffer() my vertex format has several texture coords. In my code I want to dynamically write to one of the texture coords for each vertx, which is a FLOAT4. I dont want to modify anything else in the vertex data, like position or whatnot, just the texture coord. I understand i need to lock the buffer first, then iterate through the vertices somehow and write to the specifed coord float4. I admit I find vertex buffers confusing, I am wondering if there anyone has a smiple method for doing this, or a tutorial/sample.

Share this post


Link to post
Share on other sites
Advertisement
this thread might be of interest to you.

You can use ID3DXMesh::LockVertexBuffer() directly rather than retrieving an IDirect3DVertexBuffer9 and then locking it.

Working from the declaration should be the best way of doing what you want. Call ID3DXMesh::GetDeclaration() and search for an element who's usage is D3DDECLUSAGE_TEXCOORD and UsageIndex is 0 (or whatever). You can then get the byte offset for each vertex (ID3DXBaseMesh::GetNumBytesPerVertex() tells you the size of each vertex).

Using some pointer reinterpret_cast<>()'ing you should be able to jump directly to the texture coordinate and drop in your modified value.

hth
Jack

Share this post


Link to post
Share on other sites
Ok i got it to work thanks much. I used a void cast for a struct matching the vertex declaration, and just interated through the vertices and set the variables, works perfect.

In case you are wondering, this is for a new way to do mesh instancing, in which i discoverd a way to do pure vertex shader-based instancing wihtout an additional vertex buffer.. the secret is to have your mesh object actually made up of a given numbers of copies with the exact same known vertex count.. then you just store the vertex postion intot he texture coords, then you can have what appears to be a large number of meshes with one draw call.

Works great now, though it's not yet as flexible as real instancing. But messing with this has given me a whole new way of thinking about meshes and vertex shaders.

Concievably you could do this copying of meshes in code, from one base mesh, and then you can have perfect instancing, with only one draw call for any number of instances, and for any version of vertex shader. This seemes to me to be better than the constants instancing techchnique.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!