示例#1
0
        /// <summary>
        /// Setup the matrices
        /// </summary>
        private void SetupMatrices()
        {
            // move the object
            MatrixFixed temp = new MatrixFixed(
                Matrix.Translation(-(bufferSize * 0.5f), 0,
                                   -(bufferSize * 0.5f)));

            // For our world matrix, we will just rotate the object
            // about the indexY-axis.
            device.Transform.WorldFixed = MatrixFixed.Multiply(temp,
                                                               new MatrixFixed(Matrix.RotationAxis(new Vector3(0,
                                                                                                               (float)Environment.TickCount / 2150.0f, 0),
                                                                                                   Environment.TickCount / 3000.0f)));

            // Set up our view matrix. A view matrix can be defined given
            // an eye point, a point to lookat, and a direction for which
            // way is up. Here, we set the eye five units back along the
            // z-axis and up three units, look at the origin, and define
            // "up" to be in the indexY-direction.
            device.Transform.ViewFixed = new MatrixFixed(
                Matrix.LookAtLH(new Vector3(0.0f, 25.0f, -30.0f),
                                new Vector3(0.0f, 0.0f, 0.0f),
                                new Vector3(0.0f, 1.0f, 0.0f)));

            // For the projection matrix, we set up a perspective
            // transform (which
            // transforms geometry from 3D view space to 2D viewport space,
            // with a perspective divide making objects smaller in the
            // distance). To build a perpsective transform, we need the
            // field of view (1/4 pi is common), the aspect ratio, and the
            // near and far clipping planes (which define at what distances
            // geometry should be no longer be rendered).
            device.Transform.ProjectionFixed = new MatrixFixed(
                Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 1.0f, 1.0f,
                                        100.0f));
        }
示例#2
0
        /// <summary>
        /// Called once per frame, the call is the entry point for 3d
        /// rendering. This function sets up render states, clears the
        /// viewport, and renders the scene.
        /// </summary>
        public void Render()
        {
            MatrixFixed matWorld;
            MatrixFixed matTrans;
            MatrixFixed matRotate;

            fpsTimer.StartFrame();

            // Clear the viewport to a blue color (0x000000ff = blue)
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer,
                         0x000000ff, 1.0f, 0);

            device.BeginScene();

            // Turn on light #0 and #2, and turn off light #1
            device.LightsFixed[0].Enabled = true;
            device.LightsFixed[1].Enabled = false;
            device.LightsFixed[2].Enabled = true;

            // Draw the floor
            matTrans = new MatrixFixed(Matrix.Translation(-5.0f,
                                                          -5.0f, -5.0f));
            matRotate = new MatrixFixed(Matrix.RotationZ(0.0f));
            matWorld  = MatrixFixed.Multiply(matRotate, matTrans);
            device.Transform.WorldFixed = matWorld;
            wallMesh.DrawSubset(0);

            // Draw the back wall
            matTrans =
                new MatrixFixed(Matrix.Translation(5.0f, -5.0f, -5.0f));
            matRotate = new MatrixFixed(Matrix.RotationZ((float)Math.PI / 2));
            matWorld  = MatrixFixed.Multiply(matRotate, matTrans);
            device.Transform.WorldFixed = matWorld;
            wallMesh.DrawSubset(0);

            // Draw the side wall
            matTrans =
                new MatrixFixed(Matrix.Translation(-5.0f, -5.0f, 5.0f));
            matRotate =
                new MatrixFixed(Matrix.RotationX((float)-Math.PI / 2));
            matWorld = MatrixFixed.Multiply(matRotate, matTrans);
            device.Transform.WorldFixed = matWorld;
            wallMesh.DrawSubset(0);

            // Turn on light #1, and turn off light #0 and #2
            device.LightsFixed[0].Enabled = false;
            device.LightsFixed[1].Enabled = true;
            device.LightsFixed[2].Enabled = false;

            // Draw the mesh representing the light
            if (lightData.Type == LightType.Point)
            {
                // Just position the point light -- no need to orient it
                matWorld =
                    new MatrixFixed(Matrix.Translation(lightData.Position.X,
                                                       lightData.Position.Y, lightData.Position.Z));
                device.Transform.WorldFixed = matWorld;
                sphereMesh.DrawSubset(0);
            }
            else
            {
                // Position the light and point it in the light's direction
                Vector3 vecFrom = new Vector3(lightData.Position.X,
                                              lightData.Position.Y,
                                              lightData.Position.Z);
                Vector3 vecAt = new Vector3(
                    lightData.Position.X + lightData.Direction.X,
                    lightData.Position.Y + lightData.Direction.Y,
                    lightData.Position.Z + lightData.Direction.Z);
                Vector3 vecUp = new Vector3(0, 1, 0);
                Matrix  matWorldInv;
                matWorldInv = Matrix.LookAtLH(vecFrom, vecAt, vecUp);
                matWorld    = new MatrixFixed(Matrix.Invert(matWorldInv));
                device.Transform.WorldFixed = matWorld;
                coneMesh.DrawSubset(0);
            }

            // Output statistics
            fpsTimer.Render();

            device.EndScene();
            device.Present();

            fpsTimer.StopFrame();
        }