Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mesh.DrawSubset()

22 views
Skip to first unread message

qlyqlyqly

unread,
Feb 27, 2014, 10:24:59 AM2/27/14
to
Hello,

I have this code below that renders my mesh correctly with
Device.DrawUserPrimitives() calls, but wouldn't render properly with the
Mesh.DrawSubset() call which is commented out.

I am trying to populate the mesh object dynamically using CustomVertex objects,
but seem to be getting something wrong somewhere. I will appreciate all the
help
I can get, preferably with explanations.

public static void DrawScene()
{
int textureCount = 0;
IList<CustomVertex.PositionNormalTextured> vertices = new
List<CustomVertex.PositionNormalTextured>();
IList<int> indexes = new List<int>();
AttributeRange[] attributeRanges = new
AttributeRange[ProjectSystem.ObjectList.Count];

foreach (RigidBody body in ProjectSystem.ObjectList)
{
Object3D obj = body.GetFrameCount() > 0 ?
body.GetFrame(body.GetCurrentFrame()) : body.StationaryObject;

DrawTextureStates(obj, textureCount);

Device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
Device.SetStreamSource(0, vb, 0);
Device.Transform.World =
Matrix.RotationX((float)((RigidBody)obj.Parent).GetRotation().X) *
Matrix.RotationY((float)((RigidBody)obj.Parent).GetRotation().Y) *
Matrix.RotationZ((float)((RigidBody)obj.Parent).GetRotation().Z) *
Matrix.Translation(ConvertToVector3(((RigidBody)obj.Parent).GetTranslation()))
*
Matrix.RotationZ((float)ProjectSystem.Camera.Angle.Z) *
Matrix.RotationY((float)ProjectSystem.Camera.Angle.Y) *
Matrix.RotationX((float)ProjectSystem.Camera.Angle.X);

foreach (Face face in obj.Faces)
{
CustomVertex.PositionNormalTextured[] v = new
CustomVertex.PositionNormalTextured[face.VertexInformation.Vertex.Length];

for (int j = 0; j < face.VertexInformation.Vertex.Length; j++)
{
v[j].X = (float)face.VertexInformation.Vertex[j].X;
v[j].Y = (float)face.VertexInformation.Vertex[j].Y;
v[j].Z = (float)face.VertexInformation.Vertex[j].Z;

v[j].Tu = (float)(face.VertexInformation.VertexTexture[j].X);
v[j].Tv = (float)(face.VertexInformation.VertexTexture[j].Y);

v[j].Normal = ConvertToVector3(face.VertexInformation.VertexNormal[j]);

/*
//alter the lighting effects on the surface to indicate selection
if (textureCount == Selection)
{
v[j].Normal = new Vector3(1, 1, 1) -
ConvertToVector3(face.VertexInformation.VertexNormal[j]);
}
*/

vertices.Add(v[j]);

indexes.Add(face.VertexInformation.VertexIndex[j]);
}

if (face.VertexInformation.Vertex.Length == 3)
{
Device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, v);
}
else
{
if (face.TriangleFan)
Device.DrawUserPrimitives(PrimitiveType.TriangleFan,
(face.VertexInformation.Vertex.Length - 2), v);
else
Device.DrawUserPrimitives(PrimitiveType.TriangleStrip,
(face.VertexInformation.Vertex.Length - 2), v);
}

}

if (textureCount == Selection)
{
DrawBoundingBox(obj); //give the selected object a bounding box
}

AttributeRange attributeRange = new AttributeRange();
attributeRange.AttributeId = textureCount;
attributeRange.FaceStart = 0;
attributeRange.FaceCount = vertices.Count;
attributeRange.VertexStart = 0;
attributeRange.VertexCount = indexes.Count;

attributeRanges[textureCount] = attributeRange;

textureCount++;
}

if (vertices.Count > 0)
{
CustomVertex.PositionNormalTextured[] arrayVertices = new
CustomVertex.PositionNormalTextured[vertices.Count];
int[] arrayIndices = new int[indexes.Count];

vertices.CopyTo(arrayVertices, 0);
indexes.CopyTo(arrayIndices, 0);

mesh = new Mesh(arrayIndices.Length, arrayVertices.Length,
MeshFlags.SystemMemory, CustomVertex.PositionNormalTextured.Format, Device);
mesh.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
mesh.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
mesh.SetAttributeTable(attributeRanges);

for (int k = 0; k < mesh.NumberAttributes; k++)
{
/*mesh.DrawSubset(k);*/
}
}
}
0 new messages