Using ManualObject to re-Create MeshFile.mesh

MatrixAlgebra

10-12-2009 17:37:48

Hi, to avoid using unsafe code to update the color of a loaded Mesh. I try to use ManualObject for changing color and position

a) Question 1: There is a ManaualObjectToMesh Converter. Is there a similar method (MeshToManualObject method?)
b) If not, I tried to get vertexList and IndexList from GetMeshInformation. Can I assume that the IndexList are arranged to an order of every 3 index for each triangle. I shall be able to draw using ManualObject

Assuming that all triangle are List.
MO.begin
for(i < IndexList.Length)
{ //for each triangle
MO.Position(VertexList[IndexList])
MO.Position(VertexList[IndexList])
MO.Position(VertexList[IndexList])
i +=3;
}
MO.end

My problem. I did not get all the triangle. About 50% are missing.
Do I need to calculate Normal for each each triangle.
What are the additional steps I need to do.

c) I am confuse here. Shall one use the EdgeData to get triangleList (is this feasible, right track) instead of GetMeshInformation method?

PS. Using pointcloud method, all vertices are there.

I guess, there is lot of gap in my understanding. Any help pls. Being searching and still no answer yet.

WarehouseJim

11-12-2009 09:30:22

I haven't really looked into your method, but I'm guessing your issue with not having all the triangles is due to shared vertices. This is where you use the same vertex in several triangles. If you have two triangles, which share an edge, you only need 4 vertices to define it, not 6 - three for the first triangle, one for the next. If you look at http://www.ogre3d.org/docs/api/html/classOgre_1_1RenderOperation.html#7a272218dae54048c87f31176c19e10d (which you use in manualObject.Begin()) there are three ways of defining your triangle:


  1. OT_TRIANGLE_LIST A list of triangles, 3 vertices per triangle.
    OT_TRIANGLE_STRIP A strip of triangles, 3 vertices for the first triangle, and 1 per triangle after that.
    OT_TRIANGLE_FAN A fan of triangles, 3 vertices for the first triangle, and 1 per triangle after that.
    [/list:u]

    http://en.wikipedia.org/wiki/Polygon_mesh will probably help you understand them.

    The normals you should be able to get from the mesh information somehow, but if you are adding points, you will have to define yourself. A good starting point is the direction perpendicular to a triangle:
    If the triangle vertices a,b,c then two vectors along the edges are (a-b), (b-c), so the normal is (a-b).CrossProduct((b-c)); Depending on your a,b,c it might be pointing away from, or into the face, so maybe multiply by -1.

    If you want a smooth surface, then you will probably want to do something like set the normal value for a vertex as the average of the normals to the triangles it is part of. There might be more fancy ways of doing it, but that should be a good first start.

    It's not unlikely that Ogre comes with a built in function for creating the normals, but I don't know of it.

    Don't forget to normalise your normals at every stage!


    Keep posting questions and solutions, and I'll eventually write them up. I know it's poorly documented.

MatrixAlgebra

11-12-2009 17:54:45

Hi WarehouseJim, Appreciate very much for your encouragement.

To extract triangle information from VertexBuffer and IndexBuffer provided by the hmoraldo's GetMeshInformation
I attempted to following codes. I must admit that I am a total newbie, however, I am here to learn from others.

#region Extract Triangle
TriangleList = new List<MogreTriangle>();
MogreTriangle mTriangle;
for (ushort mm = 0; i < mesh.NumSubMeshes; ++i)
{
submesh = mesh.GetSubMesh(mm);
#region Extract Triangle from Each SubMesh
for (int k = 0; k < submesh.indexData.indexCount; ++k)
{
mTriangle = new MogreTriangle();
mTriangle.TriangleType = submesh.operationType;

if (k == 0)
{
mTriangle.Vertex1 = indices[k];
mTriangle.Vertex2 = indices[k + 1];
mTriangle.Vertex3 = indices[k + 2];
k += 3;
}
else if (k > 0)
{

if (submesh.operationType == RenderOperation.OperationTypes.OT_TRIANGLE_LIST)
{
mTriangle.Vertex1 = indices[k];
mTriangle.Vertex2 = indices[k + 1];
mTriangle.Vertex3 = indices[k + 2];

k += 3;
}
if (submesh.operationType == RenderOperation.OperationTypes.OT_TRIANGLE_STRIP)
{
mTriangle.Vertex3 = indices[k];
mTriangle.Vertex2 = mTriangle.Vertex3;
mTriangle.Vertex1 = mTriangle.Vertex2;
k += 1;

}
if (submesh.operationType == RenderOperation.OperationTypes.OT_TRIANGLE_FAN)
{
mTriangle.Vertex3 = indices[k];
mTriangle.Vertex2 = mTriangle.Vertex3;
//mTriangle.Vertex1 = indices[k-1];
k += 1;

}
}

TriangleList.Add(mTriangle);
} #endregion //Extract Triangle from Each SubMesh

} //Go through each submesh
#endregion






The normals you should be able to get from the mesh information somehow

Could you pls show me how?


If the triangle vertices a,b,c then two vectors along the edges are (a-b), (b-c), so the normal is (a-b).CrossProduct((b-c));



public void CalculateNormalForTriAngleList(ref List<MogreTriangle> TriangleList, Vector3[] vertices)
{
foreach (MogreTriangle tri in TriangleList)
{
Vector3 p1 = vertices[tri.Vertex1];
Vector3 p2 = vertices[tri.Vertex2];
Vector3 p3 = vertices[tri.Vertex3];

Vector3 v1 = p1 - p2;
Vector3 v2 = p2 - p3;
Vector3 normal = v1.CrossProduct(v2);
normal.Normalise();

tri.Normal = normal;
}
}


Depending on your a,b,c it might be pointing away from, or into the face, so maybe multiply by -1.


HOW CAN I FIND OUT THIS INFORMATION?? can I get this by extending the GetMeshInformation? HOW?


If you want a smooth surface, then you will probably want to do something like set the normal value for a vertex as the average of the normals to the triangles it is part of.

Help needed :cry:


Don't forget to normalise your normals at every stage!

Help needed :roll: