Beispiel #1
0
        /// <summary>
        /// The device exists, but may have just been Reset().  Resources in
        /// Pool.Default 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>
        protected override void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
        {
            // Restore mesh's local memory objects
            blendObject.RestoreDeviceObjects(device, null);

            // Get access to the mesh vertex and index buffers
            vertexBuffer = blendObject.LocalMesh.VertexBuffer;
            indexBuffer  = blendObject.LocalMesh.IndexBuffer;
            numVertices  = blendObject.LocalMesh.NumberVertices;
            numFaces     = blendObject.LocalMesh.NumberFaces;

            if (BehaviorFlags.SoftwareVertexProcessing ||
                Caps.VertexShaderVersion.Major >= 1)
            {
                // Setup the vertex declaration
                VertexElement[] decl = VertexInformation.DeclaratorFromFormat(BlendVertex.Format);
                declaration = new VertexDeclaration(device, decl);

                // Create vertex shader from a file
                try
                {
                    shader = GraphicsUtility.CreateVertexShader(device, "blend.vsh");
                }
                catch
                {
                    SampleException d3de = new MediaNotFoundException();
                    HandleSampleException(d3de, ApplicationMessage.ApplicationMustExit);
                    throw d3de;
                }
            }
            // Set miscellaneous render states
            renderState.ZBufferEnable = true;
            renderState.Ambient       = System.Drawing.Color.FromArgb(0x00404040);

            // Set the projection matrix
            float fAspect = device.PresentationParameters.BackBufferWidth / (float)device.PresentationParameters.BackBufferHeight;

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

            // Set the app view matrix for normal viewing
            Vector3 vEyePt    = new Vector3(0.0f, -5.0f, -10.0f);
            Vector3 vLookatPt = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 vUpVec    = new Vector3(0.0f, 1.0f, 0.0f);

            device.Transform.View = Matrix.LookAtLH(vEyePt, vLookatPt, vUpVec);

            // Create a directional light. (Use yellow light to distinguish from
            // vertex shader case.)
            GraphicsUtility.InitLight(device.Lights[0], LightType.Directional, -0.5f, -1.0f, 1.0f);
            device.Lights[0].Diffuse = System.Drawing.Color.Yellow;
            device.Lights[0].Commit();
            device.Lights[0].Enabled = true;
            renderState.Lighting     = true;
        }