Saturday, August 3, 2013

Duplicating and placing 3D models in XNA

                                      In this post I'm going to talk about duplicating 3d models several times using for loop. To duplicate 3d models, you have to import a 3d  model to visual studio xna gaming platform. After importing the model you have to do the basic coding of the model and the camera which I explained in my previous post.
                             After implementing the required methods, replace the draw method with following to see a series of boxes. In here I have used two constant variables.

            const int numberBoxes = 3;
            const int radiusMultiple = numberBoxes + 1;
            float radius = model.Meshes[0].BoundingSphere.Radius;
            Matrix view = Matrix.CreateLookAt(new Vector3(0, radius * radiusMultiple, radius * (radiusMultiple * radiusMultiple)), new Vector3((numberBoxes / 2) * (radius * radiusMultiple) - 1, (numberBoxes / 2) * (radius * radiusMultiple) - 1, 0), Vector3.Up);
            Matrix proj = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);
            for (int x = 0; x < numberBoxes; x++)
                for (int y = 0; y < numberBoxes; y++)
                    Vector3 pos = new Vector3((y * (radius * radiusMultiple)) - 1, (x * (radius * radiusMultiple)) - 1, -(y + x));
                    model.Draw(Matrix.CreateTranslation(pos), view, proj);

                               In here I have used two for loops for duplicating the 3d model nine times. You can use any method for this. Also you can use Orthographic view for camera projection.

Matrix proj = Matrix.CreateOrthographic(15.0f, 15.5f, 1.0f, 100.0f);

                                Note:- You have to define the variable model in the game class.


No comments:

Post a Comment