예제 #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)
        {
            // Setup direction widget
            DirectionWidget.OnCreateDevice(e.Device);

            // 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");

            // Read the D3DX effect file
            string path = "NifViewer.fx";
            string errors;

            effect = ResourceCache.GetGlobalInstance().CreateEffectFromFile(e.Device, path, null, null, ShaderFlags.NotCloneable, null, out errors);

            if (effect == null)
            {
                MessageBox.Show("Effects.fx Shader compilation failed.\n" + errors, "Error");
            }

            ehLightDir = effect.GetParameter(null, "g_LightDir");
            ehLightCol = effect.GetParameter(null, "g_LightDiffuse");
            egAmbCol   = effect.GetParameter(null, "g_LightAmbient");
            ehViewProj = effect.GetParameter(null, "viewProjection");
            ehEyePos   = effect.GetParameter(null, "eyePos");
            ehEyeVec   = effect.GetParameter(null, "eyeVec");
            ehHalfVec  = effect.GetParameter(null, "g_LightHalfVec");

            NifFile.SetEffect(effect);

            // Setup the camera's view parameters
            camera.SetViewParameters(new Vector3(0.0f, 0.0f, -15.0f), Vector3.Empty);
            camera.IsPositionMovementEnabled = true;

            NifFile.SetCamera(camera);

            lightControl.Radius = 10;
            camera.SetRadius(30.0f, 0, 100.0f);
        }
예제 #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 = LoadMesh(e.Device, "tiny\\tiny.x");

            // Calculate a bounding sphere
            float radius = 0.0f;

            using (GraphicsStream data = mesh.LockVertexBuffer(LockFlags.None))
            {
                Vector3 center;
                radius = Geometry.ComputeBoundingSphere(data, mesh.NumberVertices, mesh.VertexFormat, out center);

                worldFix  = Matrix.Translation(-center);
                worldFix *= Matrix.RotationY((float)Math.PI);
                worldFix *= Matrix.RotationX((float)Math.PI / 2.0f);

                // Setup direction widget
                DirectionWidget.OnCreateDevice(e.Device);
                for (int i = 0; i < MaxNumberLights; i++)
                {
                    lightControl[i].Radius = radius;
                }

                // Finally unlock the vertex buffer
                mesh.UnlockVertexBuffer();
            }

            // 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
            // Preshaders are parts of the shader that the effect system pulls out of the
            // shader and runs on the host CPU. They should be used if you are GPU limited.
            // The ShaderFlags.NoPreShader flag disables preshaders.
            if (!isUsingPreshader)
            {
                shaderFlags |= ShaderFlags.NoPreShader;
            }

            // Read the D3DX effect file
            string path = Utility.FindMediaFile("BasicHLSL.fx");
            effect = ResourceCache.GetGlobalInstance().CreateEffectFromFile(e.Device,
                                                                            path, null, null, shaderFlags, null);

            // Create the mesh texture from a file
            path        = Utility.FindMediaFile("tiny\\tiny_skin.bmp");
            meshTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(e.Device, path);

            // Set effect variables as needed
            effect.SetValue("g_MaterialAmbientColor", new ColorValue(0.35f, 0.35f, 0.35f, 0));
            effect.SetValue("g_MaterialDiffuseColor", WhiteColor);
            effect.SetValue("g_MeshTexture", meshTexture);

            // Setup the camera's view parameters
            camera.SetViewParameters(new Vector3(0.0f, 0.0f, -15.0f), Vector3.Empty);
            camera.SetRadius(radius * 3.0f, radius * 0.5f, radius * 10.0f);
        }