/// <summary> /// This event will be fired immediately after the Direct3D device has /// been destroyed, which generally happens as a result of application termination or /// windowed/full screen toggles. Resources created in the OnCreateDevice event /// should be released here, which generally includes all Pool.Managed resources. /// </summary> private void OnDestroyDevice(object sender, EventArgs e) { // Update the direction widget DirectionWidget.OnDestroyDevice(); if (mesh != null) { mesh.Dispose(); } }
/// <summary> /// This event function will be called fired after the Direct3D device has /// entered a lost state and before Device.Reset() is called. Resources created /// in the OnResetDevice callback should be released here, which generally includes all /// Pool.Default resources. See the "Lost Devices" section of the documentation for /// information about lost devices. /// </summary> private void OnLostDevice(object sender, EventArgs e) { if (textSprite != null) { textSprite.Dispose(); textSprite = null; } // Update the direction widget DirectionWidget.OnLostDevice(); }
/// <summary> /// This event will be fired immediately after the Direct3D device has /// been destroyed, which generally happens as a result of application termination or /// windowed/full screen toggles. Resources created in the OnCreateDevice event /// should be released here, which generally includes all Pool.Managed resources. /// </summary> private void OnDestroyDevice(object sender, EventArgs e) { // Update the direction widget DirectionWidget.OnDestroyDevice(); if (nif != null) { nif.Dispose(); nif = null; nifPath = null; currentSubset = -1; } }
/// <summary>Initializes the application</summary> public void InitializeApplication() { isUsingPreshader = true; for (int i = 0; i < MaxNumberLights; i++) { lightControl[i] = new DirectionWidget(); lightControl[i].LightDirection = new Vector3((float)Math.Sin((float)Math.PI * 2 * i / MaxNumberLights - (float)Math.PI / 6), 0, -(float)Math.Cos((float)Math.PI * 2 * i / MaxNumberLights - (float)Math.PI / 6)); } activeLight = 0; numberActiveLights = 1; lightScale = 1.0f; int y = 10; // Initialize the dialogs Button fullScreen = hud.AddButton(ToggleFullscreen, "Toggle full screen", 35, y, 125, 22); Button toggleRef = hud.AddButton(ToggleReference, "Toggle reference (F3)", 35, y += 24, 125, 22); Button changeDevice = hud.AddButton(ChangeDevice, "Change Device (F2)", 35, y += 24, 125, 22); // Hook the button events for when these items are clicked fullScreen.Click += new EventHandler(OnFullscreenClicked); toggleRef.Click += new EventHandler(OnRefClicked); changeDevice.Click += new EventHandler(OnChangeDevicClicked); // Now add the sample specific UI y = 10; sampleUi.AddStatic(NumberLightsStatic, string.Format("# Lights: {0}", numberActiveLights), 35, y += 24, 125, 22); Slider lightSlider = sampleUi.AddSlider(NumberLights, 50, y += 24, 100, 22, 1, MaxNumberLights, numberActiveLights, false); y += 24; sampleUi.AddStatic(LightScaleStatic, string.Format("Light scale: {0}", lightScale.ToString("f2", System.Globalization.CultureInfo.CurrentUICulture)), 35, y += 24, 125, 22); Slider scaleSlider = sampleUi.AddSlider(LightScaleControl, 50, y += 24, 100, 22, 0, 20, (int)(lightScale * 10.0f), false); y += 24; Button activeLightControl = sampleUi.AddButton(ActiveLightControl, "Change active light (K)", 35, y += 24, 125, 22, System.Windows.Forms.Keys.K, false); Checkbox preShader = sampleUi.AddCheckBox(EnablePreshader, "Enable preshaders", 35, y += 24, 125, 22, isUsingPreshader); // Hook the events preShader.Changed += new EventHandler(OnPreshaderClick); lightSlider.ValueChanged += new EventHandler(OnNumberLightsChanged); activeLightControl.Click += new EventHandler(OnActiveLightClick); scaleSlider.ValueChanged += new EventHandler(OnLightScaleChanged); }
/// <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); }
/// <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); }
/// <summary>Initializes the application</summary> public void InitializeApplication() { lightControl = new DirectionWidget(); lightControl.LightDirection = new Vector3((float)Math.Sin((float)Math.PI * 2 - (float)Math.PI / 6), 0, -(float)Math.Cos((float)Math.PI * 2 - (float)Math.PI / 6)); lightScale = 1.0f; ambLightScale = 0.1f; int y = 10; // Initialize the dialogs Button fullScreen = hud.AddButton(ToggleFullscreen, "Toggle full screen", 35, y, 125, 22); Button changeDevice = hud.AddButton(ChangeDevice, "Change Device (F2)", 35, y += 24, 125, 22); // Hook the button events for when these items are clicked fullScreen.Click += new EventHandler(OnFullscreenClicked); changeDevice.Click += new EventHandler(OnChangeDevicClicked); // Now add the sample specific UI y = 10; //lighting sampleUi.AddStatic(LightScaleStatic, string.Format("Light scale: {0}", lightScale.ToString("f2", System.Globalization.CultureInfo.CurrentUICulture)), 35, y += 24, 125, 22); Slider scaleSlider = sampleUi.AddSlider(LightScaleControl, 50, y += 24, 100, 22, 0, 20, (int)(lightScale * 10.0f), false); Button lightButton = sampleUi.AddButton(LightColorPicker, "Change light colour", 35, y += 24, 125, 22); sampleUi.AddStatic(AmbLightScaleStatic, string.Format("Ambient light scale: {0}", ambLightScale.ToString("f2", System.Globalization.CultureInfo.CurrentUICulture)), 35, y += 24, 125, 22); Slider ambScaleSlider = sampleUi.AddSlider(AmbLightScaleControl, 50, y += 24, 100, 22, 0, 20, (int)(ambLightScale * 10.0f), false); Button ambLightButton = sampleUi.AddButton(AmbLightColorPicker, "Change ambient colour", 35, y += 24, 125, 22); y += 19; ComboBox shaderPicker = sampleUi.AddComboBox(ChangeShader, 35, y += 24, 125, 22); shaderPicker.AddItem("Standard", null); shaderPicker.AddItem("Color map", "ColorMap"); shaderPicker.AddItem("Alpha/height map", "HeightMap"); shaderPicker.AddItem("Normal map", "NormalMap"); shaderPicker.AddItem("Specular map", "SpecularMap"); shaderPicker.AddItem("Glow map", "GlowMap"); shaderPicker.AddItem("Ambient material", "AmbMat"); shaderPicker.AddItem("Diffuse material", "DifMat"); shaderPicker.AddItem("Specular material", "SpecMat"); shaderPicker.AddItem("Emissive material", "GlowMat"); shaderPicker.AddItem("Vertex position", "vPosition"); shaderPicker.AddItem("Vertex normals", "vNormal"); shaderPicker.AddItem("Vertex colours", "vColor"); shaderPicker.AddItem("Texture coords", "vUV"); shaderPicker.AddItem("Vertex tangents", "vTangent"); shaderPicker.AddItem("Vertex binormals", "vBinormal"); Checkbox wireframeCheckbox = sampleUi.AddCheckBox(WireFrame, "Wireframe", 35, y += 24, 125, 22, false); Checkbox cullCheckBox = sampleUi.AddCheckBox(ToggleCulling, "Backface culling", 35, y += 24, 125, 22, true); y += 19; ComboBox subsetControl = sampleUi.AddComboBox(SubsetControl, 35, y += 24, 125, 22); y += 19; Button loadButton = sampleUi.AddButton(LoadMeshButton, "Load mesh", 35, y += 24, 125, 22); // Hook the events scaleSlider.ValueChanged += new EventHandler(OnLightScaleChanged); lightButton.Click += new EventHandler(lightButton_Click); ambScaleSlider.ValueChanged += new EventHandler(ambScaleSlider_ValueChanged); ambLightButton.Click += new EventHandler(ambLightButton_Click); shaderPicker.Changed += new EventHandler(shaderPicker_Changed); wireframeCheckbox.Changed += new EventHandler(wireframeCheckbox_Changed); subsetControl.Changed += new EventHandler(subsetControl_Changed); loadButton.Click += new EventHandler(loadButton_Click); cullCheckBox.Changed += new EventHandler(cullCheckBox_Changed); }