Esempio n. 1
0
        /// <summary>
        /// The device exists, but may have just been Reset().  Resources
        /// and any other device state that persists during
        /// rendering should be set here.  Render states, matrices, textures,
        /// etc., that don't change during rendering can be set once here to
        /// avoid redundant state setting during Render() or FrameMove().
        /// </summary>
        void RestoreDeviceObjects(
            System.Object sender, System.EventArgs e)
        {
            // Create the tree textures
            Assembly asm = Assembly.GetExecutingAssembly();

            for (int i = 0; i < treeTextureFileNames.Length; i++)
            {
                treeTextures[i] = TextureLoader.FromStream(device,
                                                           asm.GetManifestResourceStream(
                                                               "Billboard.Content." + treeTextureFileNames[i]));
            }

            // This function could be called before the buffer is created
            // or after it is destroyed... check for that
            if ((treeVertexBuffer == null) || (treeVertexBuffer.Disposed))
            {
                Pool vertexBufferPool;
                Caps caps;

                // Get the device capabilities

                caps = device.DeviceCaps;

                if (caps.SurfaceCaps.SupportsVidVertexBuffer)
                {
                    vertexBufferPool = Pool.VideoMemory;
                }
                else
                {
                    vertexBufferPool = Pool.SystemMemory;
                }

                // Create a quad for rendering each tree
                treeVertexBuffer = new VertexBuffer(
                    typeof(CustomVertex.PositionColoredTextured),
                    numberTrees * 4, device, Usage.WriteOnly,
                    CustomVertex.PositionColoredTextured.Format,
                    vertexBufferPool);

                treeVertexBuffer.Created +=
                    new System.EventHandler(this.CreateTreeData);
                this.CreateTreeData(treeVertexBuffer, null);
            }

            // Restore the device objects for the meshes and fonts
            terrainMesh.RestoreDeviceObjects(device, null);
            skyBoxMesh.RestoreDeviceObjects(device, null);

            // Add some "hilliness" to the terrain
            VertexBuffer tempVertexBuffer;

            tempVertexBuffer = terrainMesh.Mesh.VertexBuffer;
            CustomVertex.PositionTextured[] pVertices;
            int numberVertices = terrainMesh.Mesh.NumberVertices;

            pVertices = (CustomVertex.PositionTextured[])
                        tempVertexBuffer.Lock(0,
                                              typeof(CustomVertex.PositionTextured), 0, numberVertices);
            for (int i = 0; i < numberVertices; i++)
            {
                pVertices[i].Y = HeightField(pVertices[i].X, pVertices[i].Z);
            }
            tempVertexBuffer.Unlock();


            // Set the transform matrices (view and world are updated per
            // frame)

            Matrix matProj;
            // create a perspective view with aspect ratio 1:1,
            // field of view as Pi/4 and near/far clipping planes at 1 and 100
            float fAspect = 1.0f;

            matProj = Matrix.PerspectiveFovLH((float)Math.PI / 4,
                                              fAspect, 1.0f, 100.0f);
            device.Transform.Projection = matProj;

            // Set up the default texture states
            device.TextureState[0].ColorOperation =
                TextureOperation.Modulate;
            device.TextureState[0].ColorArgument1 =
                TextureArgument.TextureColor;
            device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
            device.TextureState[0].AlphaOperation =
                TextureOperation.SelectArg1;
            device.TextureState[0].AlphaArgument1 =
                TextureArgument.TextureColor;
            device.TextureState[0].MinFilter = TextureFilter.Linear;
            device.TextureState[0].MagFilter = TextureFilter.Linear;
            device.TextureState[0].MipFilter = TextureFilter.Linear;
            device.TextureState[0].AddressU  = TextureAddress.Clamp;
            device.TextureState[0].AddressV  = TextureAddress.Clamp;

            device.RenderState.DitherEnable  = true;
            device.RenderState.ZBufferEnable = true;
            device.RenderState.Lighting      = false;

            // Turn on perspective correction for textures
            // This provides a more accurate visual at the cost
            // of a small performance overhead
            device.RenderState.TexturePerspective = true;
        }
Esempio n. 2
0
        /// <summary>
        /// The device exists, but may have just been Reset().  Resources
        /// and any other device state that persists during
        /// rendering should be set here.  Render states, matrices, textures,
        /// etc., that don't change during rendering can be set once here to
        /// avoid redundant state setting during Render() or FrameMove().
        /// </summary>
        void RestoreDeviceObjects(System.Object sender,
                                  System.EventArgs e)
        {
            // Build the device objects for the file-based objecs
            airplane.RestoreDeviceObjects(device, null);
            terrainObject.RestoreDeviceObjects(device, null);

            // Tweak the terrain vertices to add some bumpy terrain
            if (terrainObject != null)
            {
                // Get access to the mesh vertices
                VertexBuffer vertBuffer  = null;
                MeshVertex[] vertices    = null;
                int          numVertices = terrainObject.Mesh.NumberVertices;
                vertBuffer = terrainObject.Mesh.VertexBuffer;
                vertices   = (MeshVertex[])vertBuffer.Lock(0,
                                                           typeof(MeshVertex), 0, numVertices);

                Random r = new Random();
                // Add some more bumpiness to the terrain object
                for (int i = 0; i < numVertices; i++)
                {
                    Vector3 p = vertices[i].Point;
                    p.Y += 1 * (float)r.NextDouble();
                    p.Y += 2 * (float)r.NextDouble();
                    p.Y += 1 * (float)r.NextDouble();
                    vertices[i].Point = p;
                }

                vertBuffer.Unlock();
                vertBuffer.Dispose();
            }

            // Create and set up the shine materials w/ textures
            Material mtrl = new Material();

            mtrl.Ambient = mtrl.Diffuse = System.Drawing.Color.White;

            // Set up textures
            device.TextureState[0].ColorArgument1 =
                TextureArgument.TextureColor;
            device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
            device.TextureState[0].ColorOperation = TextureOperation.Modulate;
            device.TextureState[0].MinFilter      = TextureFilter.Linear;
            device.TextureState[0].MagFilter      = TextureFilter.Linear;

            // Set up misc render states
            device.RenderState.ZBufferEnable  = true;
            device.RenderState.DitherEnable   = true;
            device.RenderState.SpecularEnable = false;

            // Set the transform matrices
            Vector3 vEyePt = new Vector3(0.0f, 10.0f, -20.0f);
            Vector3 vLookatPt = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 vUpVec = new Vector3(0.0f, 1.0f, 0.0f);
            Matrix  matWorld, matView, matProj;

            matWorld = Matrix.Identity;
            matView  = Matrix.LookAtLH(vEyePt, vLookatPt, vUpVec);
            float fAspect = device.PresentationParameters.BackBufferWidth /
                            (float)device.PresentationParameters.BackBufferHeight;

            matProj = Matrix.PerspectiveFovLH((float)Math.PI / 4, fAspect,
                                              1.0f, 1000.0f);

            device.Transform.World      = matWorld;
            device.Transform.View       = matView;
            device.Transform.Projection = matProj;

            // Turn on fog
            float fFogStart = 30.0f;
            float fFogEnd   = 80.0f;

            device.RenderState.FogEnable      = true;
            device.RenderState.FogColor       = fogColor;
            device.RenderState.FogTableMode   = FogMode.None;
            device.RenderState.FogVertexMode  = FogMode.Linear;
            device.RenderState.RangeFogEnable = false;
            device.RenderState.FogStart       = fFogStart;
            device.RenderState.FogEnd         = fFogEnd;

            device.Lights[0].Type         = LightType.Point;
            device.Lights[0].Ambient      = System.Drawing.Color.White;
            device.Lights[0].Diffuse      = System.Drawing.Color.White;
            device.Lights[0].Range        = 1000.0f;
            device.Lights[0].Attenuation0 = 0.9f;
            device.Lights[0].Attenuation1 = 0.0f;
            device.Lights[0].Enabled      = true;
            device.RenderState.Lighting   = true;
            device.RenderState.Ambient    =
                System.Drawing.Color.FromArgb(0x00303030);

            // Turn on perspective correction for textures
            // This provides a more accurate visual at the cost
            // of a small performance overhead
            device.RenderState.TexturePerspective = true;
        }