• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

Modfiying a d3dxmesh vertexbuffer

5 posts in this topic

Hi,

I have the following situation:

 

1. A mesh is loaded into a D3DXMESH using D3DXLoadMeshFromX

2. A IDirect3DVertexBuffer9 is part of the mes class

3. The vertexbuffer is retrieved from the mesh to the vertexbuffer variable, by:

mMesh->GetVertexBuffer(&mVtxBuffer)

4. I access the vertex buffer to calculate boundingsphere, box etc.. Accessing the vertices is done by this code:

	TVERTEX *verticesPointer;
	TVERTEX *vertices = new TVERTEX[pMesh.mMesh->GetNumVertices()];

	pMesh.mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, D3DLOCK_READONLY);		// ,0 = default
	memcpy(vertices, verticesPointer, pMesh.mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0)); 
	// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

// do stuff, read only with vertices

	pMesh.mVtxBuffer->Unlock();
	delete[] vertices;

That's basically my current situation.

Now my goal is to transform the vertices of the meshes to their child modelspace's, instead of parent.

 

The only thing I don't know how to do is, how can I change the data in the vertexbuffer?

 

This is what I've tried to test out:

	// TESTING VERTEX BUFFERS COPYING //
	Crealysm_dxmath::TVERTEX *verticesPointer;
	Crealysm_dxmath::TVERTEX *vertices = new Crealysm_dxmath::TVERTEX[mMesh->GetNumVertices()];

	mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0);		// ,0 = default
	memcpy(vertices, verticesPointer, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(Crealysm_dxmath::vtxdecl, 0)); 
	// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

	for(DWORD vtx=0;vtx<mMesh->GetNumVertices();++vtx)
	{
		vertices[vtx].position.z = 5.0f;
	}

	mVtxBuffer->Unlock();

This test would make my world 'float',  by giving all meshes vertices Z = 5.0.

But the result is that nothing changes at all.

 

Do you have any idea how to solve this?

(is it possible at all to change the data, or do I need to make a new vertexbuffer and 'link' that/ use that for rendering, instead of the one retrieved from the d3dxmesh)

0

Share this post


Link to post
Share on other sites

Regarding this code:

TVERTEX *verticesPointer;
TVERTEX *vertices = new TVERTEX[pMesh.mMesh->GetNumVertices()];

pMesh.mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, D3DLOCK_READONLY); // ,0 = default
memcpy(vertices, verticesPointer, pMesh.mMesh->GetNumVertices()*D3DXGetDeclVertexSize(vtxdecl, 0));
// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

// do stuff, read only with vertices

pMesh.mVtxBuffer->Unlock();
delete[] vertices;

Just to check some things out:

1. Have you have ensured that your TVERTEX structure is identical to the pMesh vertex structure? If TVERTEX differs from the pMesh vertex structure, things could go really badly.

2. You can unlock the mesh vertex buffer immediately after the memcpy if desired.

 

In your second bit of code, nothing changes because all you do it copy the data out. You never put it back in the buffer.

The order should be:

 

0. Lock the buffer.

1. Read the data.

2. Modify the data.

3. Copy the modified data back into the buffer.

4. Unlock the buffer.

EDIT: 5. Release the buffer pointer.

 

Once again, your TVERTEX structure must be identical to the mesh's vertex structure!

 

EDIT: IIRC, you need to release the mesh vertexbuffer when you're done with it.

 

EDIT2: If your modification (setting all z's to some number) is only for testing, that's fine. Just be aware that you may not know whether you're successful or not. That will likely change the winding order of triangles and other "features" such as degenerate triangles, etc. You may just see a mess.

Edited by Buckeye
0

Share this post


Link to post
Share on other sites

Maybe

D3DLOCK_READONLY

is a problem. You lock the vertices only for reading.

And I think the system will ignore the result while unlocking.

0

Share this post


Link to post
Share on other sites

Maybe

D3DLOCK_READONLY

is a problem. You lock the vertices only for reading.

And I think the system will ignore the result while unlocking.

 

In his second bit of code, he doesn't specify D3DLOCK_READONLY, so that section of code is fine.

1

Share this post


Link to post
Share on other sites

Thanks all, I got it working now.

See the paste below.

 

Next step is do determine the spherecenter of the 'childs' and transform the vertices of the childs to that position. So I'm able to transform childs on their own (their vertices belong to their own origin). It is a bit slow though, about half a second loading time longer for a relatively simple scene.

 

Might have to consider to use a file format (saving / loading) where the renderables/ childs's vertices are already in their 'own space'.

bool CD3dmesh::UpdateVtxBufferRenderables()
{
	// TESTING VERTEX BUFFERS COPYING //
	Crealysm_dxmath::TVERTEX *verticesPointer;
	Crealysm_dxmath::TVERTEX *vertices = new Crealysm_dxmath::TVERTEX[mMesh->GetNumVertices()];

	mVtxBuffer->Lock(0, 0, (void**)&verticesPointer, 0);		// ,0 = default
	memcpy(vertices, verticesPointer, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(Crealysm_dxmath::vtxdecl, 0)); 
	// VERTICES ARE NOT SCALED/ROTATED/TRANSLATED, AND ARE IN MODEL SPACE

	for(DWORD vtx=0;vtx<mMesh->GetNumVertices();++vtx)
	{
		vertices[vtx].position.x += 1.0f;
		vertices[vtx].position.y += 1.0f;
		vertices[vtx].position.z += 1.0f;
	}
	memcpy(verticesPointer, vertices, mMesh->GetNumVertices()*D3DXGetDeclVertexSize(Crealysm_dxmath::vtxdecl, 0)); 

	mVtxBuffer->Unlock();

	delete[] vertices;
	return true;
}

Edited by cozzie
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0