Jump to content
  • Advertisement
Sign in to follow this  
Anddos

modify vertex data at runtime..

This topic is 2938 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

basically i am doing picking and so far so good , ive manage to pick the triangle of the mesh fine and intersect works, now i want to change the face vertexs at run time so if i click on a triangle it would dent in abit each time probably modifying .z for all 3 vectors...does anyone know how todo it?, its sort of like a map editor effect where you lower or raise the terrain with mouse etc...


BYTE* lpbVb;
int numIndices;
BYTE *lpIB;
DWORD idxVbVert0, idxVbVert1, idxVbVert2;
D3DINDEXBUFFER_DESC ibdesc;
LPDIRECT3DINDEXBUFFER9 lpIndexBuffer = NULL;
meshTeapot->LockVertexBuffer(D3DLOCK_READONLY, (VOID**)&lpbVb);
meshTeapot->LockIndexBuffer ( D3DLOCK_READONLY, (VOID**)&lpIB);
meshTeapot->GetIndexBuffer( &lpIndexBuffer );
lpIndexBuffer->GetDesc( &ibdesc );
if(ibdesc.Format == D3DFMT_INDEX32)
{
//MessageBox(NULL,"ibdesc.Format 32","",0);
numIndices = ibdesc.Size / sizeof(DWORD);
}
else
{
//MessageBox(NULL,"ibdesc.Format 16","",0);
numIndices = ibdesc.Size / sizeof(WORD);
}

DWORD numBytesPerVertex = meshTeapot->GetNumBytesPerVertex();

for(int i=0; i<numIndices; i += 3 )
{
idxVbVert0 = ((WORD*)lpIB)[ i ];
idxVbVert1 = ((WORD*)lpIB)[ i+1 ];
idxVbVert2 = ((WORD*)lpIB)[ i+2 ];

D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ];
D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ];
D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ];

if(faceIndex == 369) //just 1 face for now ..
{
sprintf(Buffer,"%s %f %s %f %s %f\n","v0.x",v0.x,"v0.y",v0.y,"v0.z",v0.z);
WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);

//ive tried
v0.z -= 1.0f;
v1.z -= 1.0f;
v2.z -= 1.0f;
but no effect changes on screen?, unsure how todo this...
}

}

meshTeapot->UnlockVertexBuffer();
meshTeapot->UnlockVertexBuffer();



Share this post


Link to post
Share on other sites
Advertisement
Quote:
//ive tried
v0.z -= 1.0f;
v1.z -= 1.0f;
v2.z -= 1.0f;

You might load those changes back into the vertexbuffer! [smile] You copied 3 vectors, made some changes to them, but left the vertexbuffer as it was originally.

Share this post


Link to post
Share on other sites
Just put them back where you got them from.

*(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ] = v0;
*(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ] = v1;
*(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ] = v2;

By the way:
if(faceIndex == 369) //just 1 face for now ..

Won't that be true for every index you loop through?

Maybe something like:

if( i/3 == 369 ) // just the index for face 369

If you're only going to edit a couple of vertices, there's no need to loop through every index. Don't you just want to calculate the required index, grab the vertices, modify them and put them back in the vertex buffer?

What I mean is, rather than loop through all the indices looking for the one you want:

// NO LOOP, just...
// face to edit is faceIndex = 369
DWORD requiredIndex = faceIndex*3;
idxVbVert0 = ((WORD*)lpIB)[ requiredIndex ];
idxVbVert1 = ((WORD*)lpIB)[ requiredIndex +1 ];
idxVbVert2 = ((WORD*)lpIB)[ requiredIndex +2 ];
// grab the vertices and modify them

Share this post


Link to post
Share on other sites
hmm when i pick the face now it just seems todo this..

http://img716.imageshack.us/img716/1038/tepick.jpg

its like its setting it to that all the time and not moving back on all 3 vectors etc...


for(int i=0; i<numIndices; i += 3 )
{


if(faceIndex == 369)
{

//idxVbVert0 = ((WORD*)lpIB)[ i ];
//idxVbVert1 = ((WORD*)lpIB)[ i+1 ];
//idxVbVert2 = ((WORD*)lpIB)[ i+2 ];

// face to edit is faceIndex = 369
DWORD requiredIndex = faceIndex*3;
idxVbVert0 = ((WORD*)lpIB)[ requiredIndex ];
idxVbVert1 = ((WORD*)lpIB)[ requiredIndex +1 ];
idxVbVert2 = ((WORD*)lpIB)[ requiredIndex +2 ];



D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ] = v0;
D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ] = v1;
D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ] = v2;

//very slowly...
//v0.z -= 0.000000000001f;
//v1.z -= 0.000000000001f;
//v2.z -= 0.000000000001f;

sprintf(Buffer,"%s %f %s %f %s %f\n","v0.x",v0.x,"v0.y",v0.y,"v0.z",v0.z);
WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);


}

}



Share this post


Link to post
Share on other sites
You need to actually read my previous post.

I have

// NO LOOP, just...

You have

for(int i=0; i<numIndices; i += 3 ) // This is a loop!

Also, this doesn't make any sense:

D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ] = v0;
D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ] = v1;
D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ] = v2;

You had it correct before. You copied the vertices and modified them. THEN you need to put them back in the vertex buffer per the code I posted.

Share this post


Link to post
Share on other sites
ok the loop is gone ,
it seems to go in the same positon everytime,i want it to all 3 vectors make the triangle move in wards etcs and not just go in some wierd shape...


if(faceIndex == 369)
{

//idxVbVert0 = ((WORD*)lpIB)[ i ];
//idxVbVert1 = ((WORD*)lpIB)[ i+1 ];
//idxVbVert2 = ((WORD*)lpIB)[ i+2 ];

// face to edit is faceIndex = 369
DWORD requiredIndex = faceIndex*3;
idxVbVert0 = ((WORD*)lpIB)[ requiredIndex ];
idxVbVert1 = ((WORD*)lpIB)[ requiredIndex +1 ];
idxVbVert2 = ((WORD*)lpIB)[ requiredIndex +2 ];

D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ] = v0;
D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ] = v1;
D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ] = v2;

//very slowly...
v0.z += 0.1f;
v1.z += 0.1f;
v2.z += 0.1f;

sprintf(Buffer,"%s %f %s %f %s %f\n","v0.x",v0.x,"v0.y",v0.y,"v0.z",v0.z);
WriteConsole(Console,Buffer,strlen(Buffer),&Written,0);

}




Share this post


Link to post
Share on other sites
You're very difficult to help, Anddos, because you don't actually read what people post. Please try to actually READ this post!

Here's the code:

D3DXVECTOR3 v0 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ];
D3DXVECTOR3 v1 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ];
D3DXVECTOR3 v2 = *(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ];
//very slowly...
v0.z += 0.1f;
v1.z += 0.1f;
v2.z += 0.1f;
*(D3DXVECTOR3*) &lpbVb[ idxVbVert0 * numBytesPerVertex ] = v0;
*(D3DXVECTOR3*) &lpbVb[ idxVbVert1 * numBytesPerVertex ] = v1;
*(D3DXVECTOR3*) &lpbVb[ idxVbVert2 * numBytesPerVertex ] = v2;



Share this post


Link to post
Share on other sites
yeh but you have to admit this is kind of confusing thats probably why i didnt listen but i think its working how it should ,anyway i am really greatfull for you're help on this , i understand it better now. ive changed the model to a box so i can see better how the vertices are changing etc...

heres the result of you're help :)

http://img267.imageshack.us/img267/5459/boxintersect.gif

[Edited by - Anddos on August 22, 2010 2:03:50 AM]

Share this post


Link to post
Share on other sites
now i am trying picking on a terrain and i cant figure out the picking space
the terrain is not being set to any matrix at all , so i presume thats its in view space so i do


// convert origin and direction into view space
D3DXVec3TransformCoord(&origin, &origin, &(matView));
D3DXVec3TransformNormal(&direction, &direction, &(matView));
D3DXVec3Normalize(&direction, &direction);



This is the code that renders the terrain


HR(mFX->SetMatrix(mhViewProj, &gCamera->viewProj()));
HR(mFX->SetTechnique(mhTech));
UINT numPasses = 0;
HR(mFX->Begin(&numPasses, 0));
HR(mFX->BeginPass(0));

for(UINT i = 0; i < mSubGridMeshes.size(); ++i)
HR(mSubGridMeshes->DrawSubset(0));

HR(mFX->EndPass());
HR(mFX->End());




but its not , can anyone help ?

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!