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

Draw and Position Multiple Models help

Started by sk8er754 Sep 30, 2008 at 12:18 AM 3 replies 2.9k views
Original Post
sk8er754
sk8er754
Im new and I won't get ahold of a book on XNA till tommorow if my bookstore has one in stock so I thought Ide post my question up here since it might be awhile. Im trying to draw and position 2 models on screen using these two bits of code. I was hoping someone could write up a working example of taking two models and drawing/rendering them beside each other. Saying the models are p1_wedge and p2_wedge. Code Courtesy of MSDN Tutorial ------------------------------------------------------------------------------- // Set the 3D model to draw. Model myModel; // The aspect ratio determines how to scale 3d to 2d projection. float aspectRatio; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); myModel = Content.Load("Models\\p1_wedge"); aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height; } -------------------------------------------------------------------- // Set the position of the model in world space, and set the rotation. Vector3 modelPosition = Vector3.Zero; float modelRotation = 0.0f; // Set the position of the camera in world space, for our view matrix. Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f); protected override void Draw(GameTime gameTime) { graphics.GraphicsDevice.Clear(Color.CornflowerBlue); // Copy any parent transforms. Matrix[] transforms = new Matrix[myModel.Bones.Count]; myModel.CopyAbsoluteBoneTransformsTo(transforms); // Draw the model. A model can have multiple meshes, so loop. foreach (ModelMesh mesh in myModel.Meshes) { // This is where the mesh orientation is set, as well as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation) * Matrix.CreateTranslation(modelPosition); effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up); effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); } // Draw the mesh, using the effects set above. mesh.Draw(); } base.Draw(gameTime); } ------------------------------------------------------------------------------
DarkRyder
DarkRyder
You can do it several ways but the idea is simple if you have to draw two models then you need two model variables and you need to load them both and draw them both. I have made some changes to your code see if it helps.


// Set the 3D model to draw.
Model[] myModel; ///making an array of models
int NumModels; /// store the number of models we'll keep it 2 later

// The aspect ratio determines how to scale 3d to 2d projection.
float aspectRatio;

protected override void LoadContent()
{
NumModels = 2;
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

myModel = new Models[NumModels];
for(int i =0; i < numModels; i++)
myModel = Content.Load("Models\\p1_wedge"); // this will store p1_wedge in both the models you store defferent ones. I am sure you can figure tht bit on your own.

aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
(float)graphics.GraphicsDevice.Viewport.Height;
}

--------------------------------------------------------------------

// Set the position of the model in world space, and set the rotation.
Vector3 modelPosition = Vector3.Zero;
float modelRotation = 0.0f;

// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);


for(int i = 0; i < NumModels; i++)
{
// Copy any parent transforms.
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);

// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in myModel.Meshes)
{
// This is where the mesh orientation is set, as well as our camera and projection.
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation)
* Matrix.CreateTranslation(modelPosition);
effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
aspectRatio, 1.0f, 10000.0f);
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
}
base.Draw(gameTime);
}



Hope this helps you. I have'nt tried this very bit of code. I think this should work though.
DarkRyder
DarkRyder
oops sorry i forgot about the position and rotation. You just store each models position and orientation and pass it in while drawing.

You could use a struct or write a class that handles the whole model loading and updation and drawing which you can initialize and manage from the game class.

Again, Hope this has been useful.
sk8er754
sk8er754
Thanx for the reply. Everything worked except for acouple things

Error 1 The type or namespace name 'Models' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Gamer\Desktop\testing\ModelLoad2\ModelLoad2\Game1.cs 58 15 ModelLoad2

Error 2 Cannot implicitly convert type 'Models[]' to 'Microsoft.Xna.Framework.Graphics.Model[]' C:\Users\Gamer\Desktop\testing\ModelLoad2\ModelLoad2\Game1.cs 58 11 ModelLoad2


Ill got myself some breakfast and look it over :).
DarkRyder
DarkRyder
i think you are spelling the Model variable as Models. there is no s.
i think this might be the problem. if it does not solve your issue post the code i might be able to help.

Topic Locked

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

Sign in to reply to this topic.