Example #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)
        {
            // Set the transform matrices
            Vector3 vEyePt    = new Vector3(0.0f, 0.0f, -5.0f);
            Vector3 vLookatPt = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 vUpVec    = new Vector3(0.0f, 1.0f, 0.0f);
            float   fAspect   = ((float)device.PresentationParameters.BackBufferWidth) / device.PresentationParameters.BackBufferHeight;

            worldMatrix      = Matrix.Identity;
            viewMatrix       = Matrix.LookAtLH(vEyePt, vLookatPt, vUpVec);
            projectionMatrix = Matrix.PerspectiveFovLH((float)Math.PI / 3, fAspect, 1.0f, 10000.0f);

            // Set default render states
            device.SamplerState[0].MinFilter = TextureFilter.Linear;
            device.SamplerState[0].MagFilter = TextureFilter.Linear;
            device.RenderState.ZBufferEnable = true;
            device.RenderState.FogEnable     = true;
            device.RenderState.FogColor      = WaterColor;

            // Create vertex shader for the dolphin
            VertexElement[] dolphinVertexDecl = new VertexElement[]
            {
                // First stream is first mesh
                new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
                new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                // Second stream is second mesh
                new VertexElement(1, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 1),
                new VertexElement(1, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 1),
                new VertexElement(1, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
                // Third stream is third mesh
                new VertexElement(2, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 2),
                new VertexElement(2, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 2),
                new VertexElement(2, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 2),
                VertexElement.VertexDeclarationEnd
            };

            dolphinVertexDeclaration = new VertexDeclaration(device, dolphinVertexDecl);
            dolphinVertexShader      = GraphicsUtility.CreateVertexShader(device, "DolphinTween.vsh");
            dolphinVertexShader2     = GraphicsUtility.CreateVertexShader(device, "DolphinTween2.vsh");


            // Create vertex shader for the seafloor
            VertexElement[] seaFloorVertexDecl = new VertexElement[]
            {
                new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
                new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                VertexElement.VertexDeclarationEnd
            };

            seaFloorVertexDeclaration = new VertexDeclaration(device, seaFloorVertexDecl);
            seaFloorVertexShader      = GraphicsUtility.CreateVertexShader(device, "SeaFloor.vsh");
            seaFloorVertexShader2     = GraphicsUtility.CreateVertexShader(device, "SeaFloor2.vsh");
        }
Example #2
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;
        }
Example #3
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)
        {
            // Set the transform matrices
            Vector3 vEyePt    = new Vector3(0.0f, 400.0f, -1650.0f);
            Vector3 vLookatPt = new Vector3(0.0f, 0.0f, 0.0f);
            Vector3 vUpVec    = new Vector3(0.0f, 1.0f, 0.0f);

            Matrix matWorld = Matrix.Identity;
            Matrix matView  = Matrix.LookAtLH(vEyePt, vLookatPt, vUpVec);
            Matrix matProj  = Matrix.PerspectiveFovLH(1.00f, 1.0f, 1.0f, 10000.0f);

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

            // Set any appropiate state
            device.RenderState.Ambient            = System.Drawing.Color.White;
            device.RenderState.DitherEnable       = true;
            device.RenderState.SpecularEnable     = false;
            device.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
            device.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
            device.TextureState[0].ColorOperation = TextureOperation.Modulate;
            device.SamplerState[0].MinFilter      = TextureFilter.Linear;
            device.SamplerState[0].MagFilter      = TextureFilter.Linear;
            device.SamplerState[1].MinFilter      = TextureFilter.Linear;
            device.SamplerState[1].MagFilter      = TextureFilter.Linear;

            if (isUsingVertexShader)
            {
                Matrix matCamera, matFinal;

                // Create our declaration
                VertexElement[] decl = new VertexElement[] { new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                                                             new VertexElement(0, 12, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                                                             VertexElement.VertexDeclarationEnd };

                vertexDecl = new VertexDeclaration(device, decl);

                vertexShader = GraphicsUtility.CreateVertexShader(device, "bumpwaves.vsh");

                matCamera = matWorld * matView;
                matFinal  = matCamera * matProj;
                matCamera.Transpose(matCamera);
                matFinal.Transpose(matFinal);
                float[][] camera = new float[][] { new float[] { matCamera.M11, matCamera.M12, matCamera.M13, matCamera.M14 }, new float[] { matCamera.M21, matCamera.M22, matCamera.M23, matCamera.M24 }, new float[] { matCamera.M31, matCamera.M32, matCamera.M33, matCamera.M34 } };

                camera[0][0] *= 0.8f;
                camera[0][1] *= 0.8f;
                camera[0][2] *= 0.8f;
                camera[0][3] *= 0.8f;
                camera[1][0] *= 0.8f;
                camera[1][1] *= 0.8f;
                camera[1][2] *= 0.8f;
                camera[1][3] *= 0.8f;
                for (int i = 0; i < 3; i++)
                {
                    device.SetVertexShaderConstant(i, camera[i]);
                }

                device.SetVertexShaderConstant(3, new Matrix[] { matFinal });

                float[] data = new float[] { 0.5f, -0.5f, 0, 0 };
                device.SetVertexShaderConstant(8, data);
            }
        }