コード例 #1
0
        public void Draw(camera3d cam, directionalLightSource light)
        {
            if (!visible)
            {
                return;                                               // dont render hidden meshes
            }
            foreach (ModelMesh mesh in mesh.Meshes)                   // loop through the mesh in the 3d model, drawing each one in turn.
            {
                foreach (BasicEffect effect in mesh.Effects)          // This loop then goes through every effect in each mesh.
                {
                    effect.World = transforms[mesh.ParentBone.Index]; // begin dealing with transforms to render the object into the game world
                                                                      // The following effects allow the object to be drawn in the correct place, with the correct rotation and scale.

                    ///////////////////////////////////////////////////////////////////
                    //
                    // CODE FOR TASK 1 SHOULD BE ENTERED HERE
                    // -----------------------------------------------------------------
                    //
                    // To render we need three things - world matrix, view matrix and projection matrix
                    // But we actually start in model space - this is when our world starts before transforms

                    //--------------------------------------
                    // MESH BASE MATRIX
                    //--------------------------------------
                    // Our meshes start with world - model space, so we use our transforms array
                    effect.World = transforms[mesh.ParentBone.Index];

                    //--------------------------------------
                    // WORLD MATRIX
                    //--------------------------------------
                    // Transform from model space to world space in order - scale, rotation, translation.

                    // 1. Scale
                    //Scale our model by multiplying the world matrix by a scale matrix
                    //XNA does this for us using CreateScale()

                    effect.World *= Matrix.CreateScale(scale);

                    // 2. Rotation
                    // Rotate our model in the game world
                    effect.World *= Matrix.CreateRotationX(rotation.X); // Rotate the x axis
                    effect.World *= Matrix.CreateRotationY(rotation.Y); // Rotate the y axis
                    effect.World *= Matrix.CreateRotationZ(rotation.Z); // Rotate the z axis

                    // 3. Translation / Position
                    // Move our model to the correct place in the game world
                    effect.World *= Matrix.CreateTranslation(position);

                    //--------------------------------------
                    // VIEW MATRIX
                    //--------------------------------------
                    // This puts the model in relation to where our cam is, and direction of our cam.
                    effect.View = Matrix.CreateLookAt(cam.position, cam.target, cam.whichWayIsUp);

                    //--------------------------------------
                    // PROJECTION MATRIX
                    //--------------------------------------
                    // Projection changes from view space (3D) to screen space (2D)

                    // Can be either orthographic or perspective

                    // Perspective
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(cam.fieldOfView, cam.aspectRatio, cam.nearPlane, cam.farPlane);

                    //Orthographic
                    //effect.Projection = Matrix.CreateOrthographic(1600f, 900f, 1f, 10000f);

                    ///////////////////////////////////////////////////////////////////



                    // the following effects are related to lighting and texture  settings, feel free to tweak them to see what happens.
                    effect.LightingEnabled   = true;
                    effect.Alpha             = Alpha;              //  amount of transparency
                    effect.AmbientLightColor = new Vector3(0.25f); // fills in dark areas with a small amount of light
                    effect.DiffuseColor      = new Vector3(0.1f);
                    // Diffuse is the standard colour method
                    effect.DirectionalLight0.Enabled       = true;                // allows a directional light
                    effect.DirectionalLight0.DiffuseColor  = light.diffuseColor;  // the directional light's main colour
                    effect.DirectionalLight0.SpecularColor = light.specularColor; // the directional light's colour used for highlights
                    effect.DirectionalLight0.Direction     = light.direction;     // the direction of the light
                    effect.EmissiveColor = new Vector3(0.15f);
                }
                mesh.Draw(); // draw the current mesh using the effects.
            }
        }