Sign in to follow this  
azjata

Mesh vertices edit

Recommended Posts

Hello,
I am trying to edit mesh which is 4 sided polygon (a square), which contains 4 faces, each one should have 3 vertexes, I have a function that gets a CustomVertex.PositionColored[], where i have 5 vertexes, I was wondering if it's possible to get 3 vertexes for each mesh face? which should give me 12 vertexes,
here is code i use


public CustomVertex.PositionColored[] getVerticles(Mesh mesh)
{
verts =(CustomVertex.PositionColored[])mesh.VertexBuffer.Lock(0,typeof(CustomVertex.PositionColored),LockFlags.None,mesh.NumberVertices);
return verts;
}

Share this post


Link to post
Share on other sites
Do you mean something like this ?
  +------+
|\ /|
| \ / |
| \/ |
| /\ |
| / \ |
|/ \|
+------+
If yes, then there are only five vertices, which is actually the value you get from mesh.NumberVertices.

You could treat them as "isolated" vertices by checking the index buffer: three subsequent indices from the index buffer define the vertices of a face, i.e. a triangle , but in the above case vertices are shared (e.g. the center is shared for all four faces). Changing a shared vertex will change all faces that share it. What do you want to achieve ? Treat the faces individually ? Then you actually have to create a new mesh and duplicate vertices.

Share this post


Link to post
Share on other sites
Hey, ok that's true, but when I try to change position of any of them except the 0 which is the center I can't really see any changes, I have 3 trackbars which are changing the x,y,z of selected vertex, and it doesn't seem to work with the vertexes how it should look like, I am assuming this is because of that they all share the center, I really don't know

Share this post


Link to post
Share on other sites
Hmmm, well haven't used MDX a long time now and I locked buffers rather through GraphicsStream than with arrays. Anyway, I think you are missing a unlock after you have actually changed the vertices in the array. At least you haven't shown code that does.

Still: Have I understand correctly ? You can change the center but not the other vertices ? This sounds weird. Can you show us all relevant code ? I can't judge yet why this is happening.

Share this post


Link to post
Share on other sites
here is the code i use for changes

case "trackXx":
{
if (inEditMode)
{
Vector3 t = editedMeshVertex[num].Position;
t.X = (float)trk.Value / 100;
editedMeshVertex[num].Position = t;
graphicsEngine.getMeshByName(listMesh.SelectedItem.ToString()).ConvertVerticlesToMesh(editedMeshVertex);
}
break;
}
case "trackYy":
{
if (inEditMode)
{
Vector3 t = editedMeshVertex[num].Position;
t.Y = (float)trk.Value / 100;
editedMeshVertex[num].Position = t;
graphicsEngine.getMeshByName(listMesh.SelectedItem.ToString()).ConvertVerticlesToMesh(editedMeshVertex);
int i = graphicsEngine.getMeshByName(listMesh.SelectedItem.ToString()).mesh.NumberVertices;

}
break;
}
case "trackZz":
{
if (inEditMode)
{
Vector3 t = editedMeshVertex[num].Position;
t.Z = (float)trk.Value / 100;
editedMeshVertex[num].Position = t;
graphicsEngine.getMeshByName(listMesh.SelectedItem.ToString()).ConvertVerticlesToMesh(editedMeshVertex);

}

break;


graphicsengine.getmeshbyname returns the customvertex.positioncolored, and method convertverticlestomesh, pushes the data back to the mesh, I can make actually some changes to maybe 2 vertexes, but it still works quite strange


public void ConvertVerticlesToMesh(CustomVertex.PositionColored[] vertex)
{
this._mesh.VertexBuffer.SetData(vertex, 0, LockFlags.None);
}

Share this post


Link to post
Share on other sites
First, as stated, if you get/set data through a lock, you need a unlock (later), too.
Second, and I think you ran into the same problem I ran into when working with meshes in SlimDX. DON'T lock/unlock through the exposed VertexBuffer/IndexBuffer properties but rather with the functions from the (Base)Mesh class directly, i.e. LockVertexBuffer, UnlockVertexBuffer or SetVertexBufferData.

Share this post


Link to post
Share on other sites
Hey, I made the following changes as you mentioned before here is how the code looks right now


public void ConvertVerticlesToMesh(CustomVertex.PositionColored[] vertex)
{
this._mesh.LockVertexBuffer(LockFlags.None);
this._mesh.VertexBuffer.SetData(vertex, 0, LockFlags.None);
this._mesh.UnlockVertexBuffer();
}




public CustomVertex.PositionColored[] getVerticles(Mesh mesh)
{

GraphicsStream gs = mesh.LockVertexBuffer(LockFlags.None);
verts = new CustomVertex.PositionColored[mesh.NumberVertices];
for (int i = 0; i < mesh.NumberVertices; i++)
{
verts[i]=(CustomVertex.PositionColored)gs.Read(typeof(CustomVertex.PositionColored));
}

mesh.UnlockVertexBuffer();


return verts;
}


but nothing really changed, what I have noticed that with some of the vertices, I can change only one coordinate, and for example for the vertex number 1 (still talking about the same geometry), when I change the Z position, it actually acts like X coordinate, and it goes from left to right, I can move the camera around the scene, and It really doesn't go on the Z axis, with the X and Y I can't see no changes at all, even better when I select vertex 2, it still moves the same vertex but now only X works but It acts like a Z axis, the 3rd vertex works like it should I can move it around X, Y, and Z like it should work, the next vertex again doesn't work like I was expecting...
Anyway, I think that there is some strange relationship between the vertexes
created with the Mesh class, like I am using Mesh.polygon function... help me out I am out of clues

Share this post


Link to post
Share on other sites
I don't know if it will help but here is my render loop


public void RenderGraphics()
{
_device.Clear(ClearFlags.Target, Color.Gray, 1.0f, 0);
_device.RenderState.CullMode = Cull.None;
_device.RenderState.Ambient = Color.White;
_device.RenderState.Lighting = true;
_device.RenderState.FillMode = fillMode;



_device.BeginScene();
foreach (MeshView mesh in MeshList)
{
mesh.DrawMe();
}
drawGrid();

_device.EndScene();
_device.Present();
}


here is the DrawMe from MeshList class

public void DrawMe()
{
device.Transform.World = Matrix.Translation(this._positionXYZ);
this._mesh.DrawSubset(0);
}

Share this post


Link to post
Share on other sites
More code is always better, thanks. The drawing code looks fine, it's rather bare (DrawSubset), I don't suspect the issue coming from there.

Quote:
...what I have noticed that with some of the vertices, I can change only one coordinate, and for example for the vertex number 1 (still talking about the same geometry), when I change the Z position, it actually acts like X coordinate...


Hmmmm, that sounds like you got a mixup with the vertex structure. Have you actually checked the mesh uses a vertex that matches PositionColored. How do you get that mesh at all ? Did you load it or create it from buttom up (constructor) ? Really check the mesh's vertex format (BaseMesg.VertexFormat) against the CustomVertex.PositionColored.Format (which is probably Format.Diffuse|Format.Position).

You can also always save a mesh - in this case preferably in text format - to check what's going on.

Apart from that: It's essential to use the debugging capabilities the DX SDK offers, especially when dealing with such weird behaviour: Run your app through PIX and/or try to setup the DirectX debug runtimes (The latter is something I unfortunately never got running in MDX but you might be luckier). If you don't know how to do that: refer to the forum FAQ and the sticky threads to learn more about.

Share this post


Link to post
Share on other sites
Hey,
Yes, thanks a lot, you were right, I checked the VertexFormat and it is PositionNormal, not PositionColored, sinced I changed that everything works great!
Big big big thanks!! that was very helpfull

Share this post


Link to post
Share on other sites
[smile] Glad it's working.

I'd like to add something though: MDX is no longer supported and has some lacks now (e.g. it's still .NET 1, no generics, an event logic which is rather cumbersome, no DX 10/11 support etc. ). If you're not forced to use it (legacy) consider using alternatives like SlimDX, or - if you are very new to Direct X - even XNA. It should also increase your chances to get answers here.

Share this post


Link to post
Share on other sites
Thanks, yeah I tried before to use littlebit of SlimDX, but the implementation there is bit different, I also bought MDX Kickstart book, which goes through the DX9, and in my application, I don't really need loads of 3D, I am making a application where you will be able to map videos, into non rectangular objects, or make a projections on not flat surfaces, and everything I need is in MDX9, I need to create and edit simple mesh objects basing on polygons, and then apply video textures to them, and do it all in real time... so mdx9 should do the job in here, and i think it's not a bad start with pure directx

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