Converting a DrawModel() using BasicEffect to one using Effect

Posted by Fibericon on Game Development See other posts from Game Development or by Fibericon
Published on 2012-04-09T07:31:56Z Indexed on 2012/04/09 11:50 UTC
Read the original article Hit count: 232

Filed under:
|
|

Take this DrawModel() provided by MSDN:

private void DrawModel(Model m)
{
    Matrix[] transforms = new Matrix[m.Bones.Count];
    float aspectRatio = graphics.GraphicsDevice.Viewport.Width / 
        graphics.GraphicsDevice.Viewport.Height;
    m.CopyAbsoluteBoneTransformsTo(transforms);
    Matrix projection = 
        Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
        aspectRatio, 1.0f, 10000.0f);
    Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom), 
        Vector3.Zero, Vector3.Up);

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

            effect.View = view;
            effect.Projection = projection;
            effect.World = gameWorldRotation * 
                transforms[mesh.ParentBone.Index] * 
                Matrix.CreateTranslation(Position);
        }
        mesh.Draw();
    }
}

How would I apply a custom effect to a model with that? Effect doesn't have View, Projection, or World members. This is what they recommend replacing the foreach loop with:

foreach (ModelMesh mesh in terrain.Meshes)
{
    foreach (Effect effect in mesh.Effects)
    {
        mesh.Draw();
    }
}

Of course, that doesn't really work. What else needs to be done?

© Game Development or respective owner

Related posts about XNA

Related posts about 3d