Speedup rendering models

For a person like me who has only one mesh per model I find doing this to be a waste of processing power:

foreach (ModelMesh mesh in model.Meshes)
 foreach (BasicEffect effect in mesh.Effects)
 //set effect parameters here

That was the rendering code i saw on the demos from the msdn site. It has its purposes, some exporters split meshes based on the material that gets assigned to the triangles, or when exporting entire schenes the fbx file would contain more than one meshes. But that’s never the case on my part, and probably wont be for you either. Why waste a foreach loop when i only have one model and one mesh? Second reason is that i have to do this whenever i want to render a model with a new effect:

foreach (ModelMesh m in model.Meshes)
 foreach (ModelMeshPart p in m.MeshParts)
 p.Effect = effect;

which is also a waste of time. I dont want to bind the effect from my materials to my models and waste precious cpu cycles applying an effect to my model. Here’s my new approach:

public void setModel(Model model)
 this.model = model;
if (model == null)
 meshPart = null;
 //assumes the model has only one mesh
 meshPart =  model.Meshes[0].MeshParts[0];

i cache the meshPart whenever the model is set. When rendering i just render it manually(set the vertex and index buffers):


 device.Indices = meshPart.IndexBuffer;
 0, 0,

that was one thing i did to speedup rendering, i’ll show you a few more techniques later


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s