Example #1
0
		public void Attach(Device hostDevice, PointD2D hostSize)
		{
			if (hostDevice == null)
				throw new ArgumentNullException(nameof(hostDevice));

			_hostDevice = hostDevice;
			_hostSize = hostSize;

			Device device = _hostDevice;

			using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Altaxo.CompiledShaders.Effects.Lighting.cso"))
			{
				if (null == stream)
					throw new InvalidOperationException(string.Format("Compiled shader resource not found: {0}", "Altaxo.CompiledShaders.Effects.Lighting.cso"));

				using (var shaderBytes = ShaderBytecode.FromStream(stream))
				{
					_lightingEffect = new Effect(device, shaderBytes);
				}
			}

			int i;

			for (i = 0; i < _layoutNames.Length; ++i)
			{
				string techniqueName = "Shade_" + _layoutNames[i];
				_renderLayouts[i].technique = this._lightingEffect.GetTechniqueByName(techniqueName);
				_renderLayouts[i].pass = _renderLayouts[i].technique.GetPassByIndex(0);

				if (null == _renderLayouts[i].technique || !_renderLayouts[i].technique.IsValid)
					throw new InvalidProgramException(string.Format("Technique {0} was not found or is invalid", techniqueName));
				if (null == _renderLayouts[i].pass || !_renderLayouts[i].pass.IsValid)
					throw new InvalidProgramException(string.Format("Pass[0] of technique {0} was not found or is invalid", techniqueName));
			}

			i = 0;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0)
																								});

			i = 1;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
																																new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
																								});

			i = 2;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
																																new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0)
																								});

			i = 3;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
																																new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0)
																								});

			i = 4;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
																																new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
																																new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 32, 0)
																								});

			i = 5;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
																																new InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 16, 0),
																																new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0)
																								});

			i = 6;
			_renderLayouts[i].VertexLayout = new InputLayout(device, _renderLayouts[i].pass.Description.Signature, new[] {
																																new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0),
																																new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
																																new InputElement("TEXCOORD", 0, Format.R32G32_Float, 24, 0)
																								});

			// Create Constant Buffers
			//_constantBuffer = new Buffer(device, Utilities.SizeOf<Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None);
			//_constantBufferForColor = new Buffer(device, Utilities.SizeOf<Vector4>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None);
			//_constantBufferForSixPlanes = new Buffer(device, Utilities.SizeOf<Vector4>() * 6, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None);

			// View transformation variables
			_cbViewTransformation = this._lightingEffect.GetConstantBufferByName("cbViewTransformation");
			_evWorldViewProj = _cbViewTransformation.GetMemberByName("WorldViewProj").AsMatrix();
			_evEyePosition = _cbViewTransformation.GetMemberByName("EyePosition").AsVector();

			_cbMaterial = this._lightingEffect.GetConstantBufferByName("cbMaterial");
			_evMaterialDiffuseColor = _cbMaterial.GetMemberByName("MaterialDiffuseColor").AsVector();
			_evMaterialSpecularExponent = _cbMaterial.GetMemberByName("MaterialSpecularExponent").AsScalar();
			_evMaterialSpecularExponent.Set(4.0f);
			_evMaterialSpecularIntensity = _cbMaterial.GetMemberByName("MaterialSpecularIntensity").AsScalar();
			_evMaterialSpecularIntensity.Set(1.0f);
			_evMaterialDiffuseIntensity = _cbMaterial.GetMemberByName("MaterialDiffuseIntensity").AsScalar();
			_evMaterialMetalnessValue = _cbMaterial.GetMemberByName("MaterialMetalnessValue").AsScalar();
			_evMaterialMetalnessValue.Set(0.75f);

			// Color providers
			BindTextureFor1DColorProviders();

			// Clip plane variables
			_cbClipPlanes = this._lightingEffect.GetConstantBufferByName("cbClipPlanes");
			for (i = 0; i < 6; ++i)
			{
				_evClipPlanes[i] = _cbClipPlanes.GetMemberByName("ClipPlane" + i.ToString(System.Globalization.CultureInfo.InvariantCulture)).AsVector();
			}

			// Lighting variables

			_lighting.Initialize(_lightingEffect);
			_lighting.SetDefaultLighting();

			// --------------------
			if (_drawing != null)
			{
				BringDrawingIntoBuffers(_drawing);
			}

			if (null != _markerGeometry)
			{
				BringMarkerGeometryIntoDeviceBuffers(_markerGeometry);
			}

			if (null != _overlayGeometry)
			{
				BringOverlayGeometryIntoDeviceBuffers(_overlayGeometry);
			}
		}