1

I am working to generate terrain for our project, something that will be contained in the Model class that I can draw, but I new class would be alright since I may need to look inside for specific data often, and then I would just need the basic function to work with the Game class. Anyway, I have a fair amount of knowledge of the XNA framework, but because of how convoluted it handles anything. So my problem is I can't just make a Model, I can't instantiate that class or anything. I have what I believe the proper data to form a model's geometry, which is all I need right now, and later possibly have it textured. I don't know where to go from here.

XNA you usually use Content.Load, to have their content pipeline read in a file and parse it specifically, but I want to avoid that because I want my terrain generated. I can compute an array of Vertex data and indices for the triangles I want to make-up a mesh, but so far my efforts have tried to instantiate any object like Model or those it contains, have failed. If there is some factory class I can use to build it, I have no idea what that is, so if someone else can point me in the right direction there and give me a rough outline on how to build a model, that would help. If that's not the answer, maybe I need to do something completely different, either centered on using Content.Load or not, but basically I don't want my terrain sitting in a file, consistent between executions, I want to control the mesh data on load and randomize it, etc.

So how can I get a model generated completely programmatically, to show up on the screen, and still have its data exposed?

Pysis
  • 1,452
  • 2
  • 17
  • 31

1 Answers1

4

Model and its associated classes (eg: ModelMesh), are convenience classes. They are not the only way to draw models. It is expected that sometimes, particularly when doing something "special", you will have to re-implement them entirely, using the same low-level methods that Model uses.

Here's the quick version of what you should do:

First of all, at load time, create a VertexBuffer and an IndexBuffer and use SetData on each to fill each with the appropriate data.

Then, at draw time, do this:

GraphicsDevice.SetVertexBuffer(myVertexBuffer);
GraphicsDevice.Indices = myIndexBuffer;

// Set up your effect. Use a BasicEffect here, if you don't have something else.
myEffect.CurrentTechnique.Passes[0].Apply();

GraphicsDevice.Textures[0] = myTexture; // From Content.Load<Texture2D>("...")

GraphicsDevice.DrawIndexedPrimitives(...);
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • Yes, one solution can be using low-level commands, I didn't know what was at my disposal since I can't look at the source code for anything. That leaves the convenience objects you mentioned, and I'd like to learn more about why they're there, which would help in determining when I should and shouldn't use them. All the MSDN has is a very lacking Javadoc of methods and such, unlike the Java tutorial center which is much more helpful. Now could I just get by using the last command? I also want to draw other normally loaded models and I imagine managing the gfx device buffers being difficult. – Pysis Dec 11 '11 at 18:44
  • Don't confuse low-level with difficult. It's actually pretty easy to do this - just set what you want on the device and then call a `Draw*` method. The MSDN documentation of XNA is a bit lacking. But the examples, and the samples on App Hub are excellent - so I recommend looking at those when in doubt. It's usually experience that tells you when not to use these built-in convenience classes. But needing to create an object that lacks a public constructor is a good indicator. – Andrew Russell Dec 12 '11 at 01:16