Skip to main content
GameDev.net gamedev.net
🔒 Locked

Dynamically Create ModelMesh (MeshContent->ModelMesh)

Started by Lloydg Oct 3, 2012 at 10:02 AM 0 replies 2.5k views
Original Post
Lloydg
Lloydg
I am having a problem, I want to Dynamically Create a ModelMesh at runtime rather then loading a file.


public static ModelMesh BuildMesh(String name)
{
MeshBuilder builder = MeshBuilder.StartMesh(name);
// build the mesh
MeshContent finishedMesh = builder.FinishMesh();

return finishedMesh as a ModelMesh?
}


But I don't know how to convert a MeshContent to a ModelMesh?
Does anyone have an idea?

Thanks
AmzBee
AmzBee
Hi there, MeshBuilder is a part of the content pipeline, which depends on MsBuild. And due the way in which the mechanism works, it is only supposed to be used within the content pipeline (or a pipeline extension library), the information that MeshBuilder creates is intended for either the built in model processor, or your own custom processor. If you wish to be able to build the equivalent of the functionality ModelMesh provides, you may have to do this yourself, however there are plenty of tutorials on the internet that will show you how to do this.

Just incase this is new to you, here is a quick checklist for achieving this:

- Create an array of vertices (VertexPositionColor is one, but you can use other presets, and even create your own type).
- Create an array of indices for faces, it's normal to have them in multiples of 3, and each index refers to a vertex.
- In your draw call, call one of the appropriate Draw... methods, here's a quick example:


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace FastTriExample
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
VertexBuffer vbo;
BasicEffect shader;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void LoadContent()
{
vbo = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 3, BufferUsage.WriteOnly);
vbo.SetData<VertexPositionColor>(new VertexPositionColor[]{
new VertexPositionColor(Vector3.Up, Color.White),
new VertexPositionColor(Vector3.Down + Vector3.Right, Color.Blue),
new VertexPositionColor(Vector3.Down + Vector3.Left, Color.Red)
});
float aspectRatio = GraphicsDevice.Viewport.AspectRatio;
shader = new BasicEffect(GraphicsDevice);
shader.World = Matrix.CreateScale(0.5f);
shader.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up);
shader.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver2, aspectRatio, 0.1f, 2);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
shader.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.SetVertexBuffer(vbo);
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}

}
}


Have fun!
We are now on Tumblr, so please come and check out our site!    http://xpod-games.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.