Example #1
0
        /// <summary>
        /// This event will be fired immediately after the Direct3D device has been
        /// created, which will happen during application initialization and windowed/full screen
        /// toggles. This is the best location to create Pool.Managed resources since these
        /// resources need to be reloaded whenever the device is destroyed. Resources created
        /// here should be released in the Disposing event.
        /// </summary>
        private void OnCreateDevice(object sender, DeviceEventArgs e)
        {
            // Initialize the stats font
            statsFont = ResourceCache.GetGlobalInstance().CreateFont(e.Device, 15, 0, FontWeight.Bold, 1, false, CharacterSet.Default,
                                                                     Precision.Default, FontQuality.Default, PitchAndFamily.FamilyDoNotCare | PitchAndFamily.DefaultPitch
                                                                     , "Arial");

            // Define DEBUG_VS and/or DEBUG_PS to debug vertex and/or pixel shaders with the
            // shader debugger. Debugging vertex shaders requires either REF or software vertex
            // processing, and debugging pixel shaders requires REF.  The
            // ShaderFlags.Force*SoftwareNoOptimizations flag improves the debug experience in the
            // shader debugger.  It enables source level debugging, prevents instruction
            // reordering, prevents dead code elimination, and forces the compiler to compile
            // against the next higher available software target, which ensures that the
            // unoptimized shaders do not exceed the shader model limitations.  Setting these
            // flags will cause slower rendering since the shaders will be unoptimized and
            // forced into software.  See the DirectX documentation for more information about
            // using the shader debugger.
            ShaderFlags shaderFlags = ShaderFlags.None;

#if (DEBUG_VS)
            shaderFlags |= ShaderFlags.ForceVertexShaderSoftwareNoOptimizations;
#endif
#if (DEBUG_PS)
            shaderFlags |= ShaderFlags.ForcePixelShaderSoftwareNoOptimizations;
#endif
            // Read the D3DX effect file
            string path = Utility.FindMediaFile("CustomUI.fx");
            effect = ResourceCache.GetGlobalInstance().CreateEffectFromFile(e.Device,
                                                                            path, null, null, shaderFlags, null);

            // Create the mesh
            mesh = new FrameworkMesh(e.Device, "misc\\cell.x");

            // Setup the camera's view parameters
            Vector3 eye = new Vector3(0.0f, 1.5f, -7.0f);
            Vector3 at  = new Vector3(0.0f, 0.2f, 0.0f);
            camera.SetViewParameters(eye, at);
            viewMatrix = Matrix.LookAtLH(eye, at, Camera.UpDirection);
        }
Example #2
0
        /// <summary>
        /// This event will be fired immediately after the Direct3D device has been
        /// created, which will happen during application initialization and windowed/full screen
        /// toggles. This is the best location to create Pool.Managed resources since these
        /// resources need to be reloaded whenever the device is destroyed. Resources created
        /// here should be released in the Disposing event.
        /// </summary>
        private void OnCreateDevice(object sender, DeviceEventArgs e)
        {
            // Initialize the stats font
            statsFont = ResourceCache.GetGlobalInstance().CreateFont(e.Device, 15, 0, FontWeight.Bold, 1, false, CharacterSet.Default,
                                                                     Precision.Default, FontQuality.Default, PitchAndFamily.FamilyDoNotCare | PitchAndFamily.DefaultPitch
                                                                     , "Arial");

            // Load the mesh
            mesh = new FrameworkMesh(e.Device, "dwarf\\dwarf.x");

            // Find the mesh's center, then generate a centering matrix
            using (VertexBuffer vb = mesh.SystemMesh.VertexBuffer)
            {
                using (GraphicsStream stm = vb.Lock(0, 0, LockFlags.None))
                {
                    try
                    {
                        objectRadius = Geometry.ComputeBoundingSphere(stm,
                                                                      mesh.SystemMesh.NumberVertices, mesh.SystemMesh.VertexFormat, out objectCenter);

                        worldCenter = Matrix.Translation(-objectCenter);
                    }
                    finally
                    {
                        vb.Unlock();
                    }
                }
            }

            // Try to find the D3DX Effect file
            try
            {
                string path = Utility.FindMediaFile("CompiledEffect.fxo");

                // Since we are loading a binary file here and this effect has already been compiled,
                // you can not pass compiler flags here (for example to debug the shaders).
                // To debug the shaders, you must pass these flags to the compiler that generated the
                // binary (for example fxc.exe).  In this sample, there is an extra Visual Studio configuration
                // called "Debug Shaders" that pass the debug shader flags to fxc.exe.
                effect = ResourceCache.GetGlobalInstance().CreateEffectFromFile(e.Device,
                                                                                path, null, null, ShaderFlags.None, null);
            }
            catch (MediaNotFoundException)
            {
                // Inform the user
                System.Windows.Forms.MessageBox.Show("Could not locate \"CompiledEffect.fxo\".\n\n" +
                                                     "This file is created as part of the project build process,\n" +
                                                     "so the associated project must be compiled inside Visual\n" +
                                                     "Studio before attempting to run this sample.\n\n" +
                                                     "If receiving this error even after compiling the project,\n" +
                                                     "it's likely there was a problem compiling the effect file.\n" +
                                                     "Check the build log to verify the custom build step was\n" +
                                                     "run and to look for possible fxc compile errors.", "File Not Found",
                                                     System.Windows.Forms.MessageBoxButtons.OK,
                                                     System.Windows.Forms.MessageBoxIcon.Error);

                // Rethrow
                throw;
            }

            // Setup the camera's view parameters
            camera.SetViewParameters(new Vector3(0.0f, 0.0f, -5.0f), Vector3.Empty);
        }