RICH525234

Hello im trying to load the same model many times at differnt locations
first i create an array of models then i load the the models content location info
then i created each model to draw in the draw method.
however doing this simply is transforming the same model instead of drawing 10 seperate models


here is m code if anyone can figure it out

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Model[] models;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
protected override void Initialize()
{ models = new Model[10];
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
for(int i=0;i<10;i++)
{
modelsIdea = content.Load<Model>(@"Contents\Models\asteroid1");
}
}
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);


foreach(Model mods in models)
{
LoadModel(mods);
objectPosition.X +=+2f;
}
base.Draw(gameTime);
}
Vector3 cameraPosition=new Vector3(0.0f,0.0f,2000.0f);
Vector3 cameraTarget = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 cameraUP = Vector3.Up;
float FOV = MathHelper.ToRadians(45f);
float aspectRatio=640f/480f;
float clipNear=1.0f;
float clipFar=50000f;
Vector3 objectPosition = new Vector3(0.0f, 0.0f, 0.0f);

protected void LoadModel(Model model)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = Matrix.Identity * Matrix.CreateRotationX(45f) * Matrix.CreateTranslation(objectPosition);
effect.View = Matrix.CreateLookAt(cameraPosition, cameraTarget, cameraUP);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(FOV, aspectRatio, clipNear, clipFar);
}
mesh.Draw();
}
}
}




Re: XNA Framework Models

RICH525234

I've been looking on the forums for this occurcance and discovered i shoudl change my code as below but still sdoesnt work this is the updated code if anyone can figure out how to load these meshes i just want to load ten meshes on the screen of the model.
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Model[]MODELS;
Model models;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
protected override void Initialize()
{
MODELS = new Model[10];
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
models =content.Load<Model>(@"Contents\Models\asteroid1");
}
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();


base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
LoadModel();
base.Draw(gameTime);
}
Vector3 cameraPosition=new Vector3(0.0f,0.0f,2000.0f);
Vector3 cameraTarget = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 cameraUP = Vector3.Up;
float FOV = MathHelper.ToRadians(45f);
float aspectRatio=640f/480f;
float clipNear=1.0f;
float clipFar=50000f;
Model mod;
Vector3 objectPosition = new Vector3(0.0f, 0.0f, 0.0f);

protected void LoadModel()
{
for (int i=0; i < 10; i++)
{
MODELSIdea = models;
}
for(int i=0; i<10;i++)
{
mod=(Model)MODELSIdea;
foreach (ModelMesh mesh in mod.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = Matrix.Identity * Matrix.CreateRotationX(45f) * Matrix.CreateTranslation(objectPosition);
effect.View = Matrix.CreateLookAt(cameraPosition, cameraTarget, cameraUP);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(FOV, aspectRatio, clipNear, clipFar);
}
mesh.Draw();
}

objectPosition.X += +20f;
}
}
}





Re: XNA Framework Models

Kyle_W

To me, it looks like your code should work. Although if you simply want to create dupicates of an object it would make more sense to store an array of positions rather than an array of models. You can then simply draw the same Model ten times per frame with each of the unique stored positions. Besides, when you create an array of 10 Models from the same asset name, I believe you end up with 10 references to the same object instead of 10 unique copies. Even if this is not the case, storing an array of redundant models would be a waste of memory.

I'm not seeing anything at the moment in your code that looks like a problem. What happens when you try to run it





Re: XNA Framework Models

RICH525234

when i run this it moves the model over 2f in the x direction so its just one object moving in the x direction. i tried your suggesting and my result is now that the position doesnt update;
i just did what you suggested. why isnt it drawing; in the new locations
this time i m just drawing the one time loaded object in different locations.

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Model[]MODELS;
Model models;
Vector3[] objectPosition;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
protected override void Initialize()
{
objectPosition = new Vector3[10];
MODELS = new Model[10];
base.Initialize();
}
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{

models =content.Load<Model>(@"Contents\Models\asteroid1");

}
}
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent == true)
{
content.Unload();
}
}
protected override void Update(GameTime gameTime)
{
// Allows the default game to exit on Xbox 360 and Windows
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();


base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);


MakeModels(models);




base.Draw(gameTime);
}
Vector3 cameraPosition=new Vector3(0.0f,0.0f,5000.0f);
Vector3 cameraTarget = new Vector3(0.0f, 0.0f, 0.0f);
Vector3 cameraUP = Vector3.Up;
float FOV = MathHelper.ToRadians(45f);
float aspectRatio=640f/480f;
float clipNear=1.0f;
float clipFar=50000f;
Vector3 Pos;
protected void MakeModels(Model models)
{

for (int i = 0; i < 10; i++)
{
if (i == 0)
{
objectPositionIdea = new Vector3(0.0f, 0.0f, 0.0f);
Pos = (Vector3)objectPositionIdea;
Pos.X += +10f;

LoadModel(models, Pos);
}
else
{
objectPositionIdea = Pos;

Pos = (Vector3)objectPositionIdea;
Pos.X += +10f;
LoadModel(models, Pos);
}

}
}

protected void LoadModel(Model mod,Vector3 pos)
{
foreach (ModelMesh mesh in mod.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = Matrix.Identity * Matrix.CreateRotationX(45f) * Matrix.CreateTranslation(pos);
effect.View = Matrix.CreateLookAt(cameraPosition, cameraTarget, cameraUP);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(FOV, aspectRatio, clipNear, clipFar);
}
mesh.Draw();
}


}
}





Re: XNA Framework Models

Kyle_W

So are you saying that the model is drawn on the screen in only one position and it remains in that one position (does not move)

One thing I did notice is that in your MakeModels() method you are re-initializing the objectPosition array on every frame. You should initialize the values in the array once in the Initialize() method and then simply read back through the array on each frame to retrieve the values for drawing.

However, it still seems to me that the model should be getting successfully drawn 10 times with a gap of 10 units between each, even with your existing code. I noticed that your camera is positioned at Z = 5000. That's a fairly large distance. If you are even able to see your model at that distance, then the model must be fairly large in world space units, so therefore a movement of only 10 units between instances of the model might not be noticeable at that distance, and the different instances of the model might closely overlap each other such that it appears as if only one instance was drawn.





Re: XNA Framework Models

RICH525234

You rock bro thanks i new that code was right. I updated to update to100f and can clearly see the change only reason i made it so small in the first place was cause the object loaded huge but thanks much.
best