RTodd

I'm trying to wrap my head around the use of effects and their use in rendering models and primitives. The sample code to render a model "m" looks like the following:

foreach (ModelMesh mesh in m.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();

effect.View = view;
effect.Projection = projection;
effect.World = MyWorldRotation * mesh.ParentBone.Transform *
Matrix.CreateTranslation(Position);
}
mesh.Draw();
}

While the sample code to render a primitive looks like this:

basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();

graphics.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0,
VertexPositionNormalTexture.SizeInBytes);

graphics.GraphicsDevice.DrawPrimitives(
PrimitiveType.TriangleList,
0,
12
);

pass.End();
}
basicEffect.End();

A couple of questions:

  • Does the call to "mesh.Draw" in the first example contain an EffectPass loop like the one shown for rendering a primitive
  • When a model is loaded, is it normally associated with a BasicEffect effect (it would appear so from the sample) Are they separate instances of the BasicEffect class Would/should you share the same effect instance among more than one model so that effect properties only have to be set once
  • If you're rendering both primitives and models, is there a way to render the models as part of the EffectPass loop used by the primitives Or would you render them separately

Thank you!



Re: XNA Framework Rendering models and primitives

Leaf.

RTodd wrote:

A couple of questions:

  • Does the call to "mesh.Draw" in the first example contain an EffectPass loop like the one shown for rendering a primitive
  • When a model is loaded, is it normally associated with a BasicEffect effect (it would appear so from the sample) Are they separate instances of the BasicEffect class Would/should you share the same effect instance among more than one model so that effect properties only have to be set once
  • If you're rendering both primitives and models, is there a way to render the models as part of the EffectPass loop used by the primitives Or would you render them separately



Yes, ModelMesh.Draw() does some form of EffectPass loop.

The Model loading code creates seperate Effect instances but they should all use the same EffectPool.

You don't have to use ModelMesh.Draw(), it doesn't do anything that you can't do yourself - You can get the VertexBuffer and IndexBuffer from each ModelMesh and then the Effect, VertexDeclaration, index offset etc. from each ModelMeshPart in ModelMesh.MeshParts.

Cheers,
Leaf.





Re: XNA Framework Rendering models and primitives

Snoitkever

I have got some code working to draw models without using the draw method:

foreach( ModelMesh mesh in _model.Meshes ){
foreach( ModelMeshPart meshPart in mesh.MeshParts )
{
graphics.VertexDeclaration = meshPart.VertexDeclaration;
graphics.Vertices[ 0 ].SetSource( mesh.VertexBuffer , meshPart.StreamOffset , meshPart.VertexStride );
graphics.Indices = mesh.IndexBuffer;
graphics.DrawIndexedPrimitives( PrimitiveType.TriangleList , meshPart.BaseVertex , 0 , meshPart.NumVertices , meshPart.StartIndex , meshPart.PrimitiveCount );
}
}

I've still got some questions however, to which I could not find the answers in the documentation:

As you can see I am using PrimitiveType.TriangleList as the primitive type for the model. Will this always be the case, or are there situation where it could for example be a TriangleStrip And if that is the case, how can I find out It is not listed in either ModelMesh or ModelMeshPart as far as I can see.

Another thing I'm not sure of is the 0 I currently use as "minVertexIndex" in the DrawIndexedPrimitives call. Should it always be 0, or is there some property in ModelMesh or ModelMeshPart I can set it to

The last thing is the use of indices. Are all Models indexed

I hope someone knows the answers to these questions.




Re: XNA Framework Rendering models and primitives

Leaf.

You should be fine using indexed drawing, TriangleList primitives and 0 for the minimum vertex index until/unless Microsoft change the Model types.

Just assuming that those factors will work is a little ugly and is one reason why some people create their own types to represent models/meshes/etc. instead of using the Model types.

Cheers,
Leaf.