예제 #1
0
        public override WriteableBitmap Initialize(Device device)
		{
            const int width = 600;
			const int height = 400;

			// Create device and swap chain.
            var swapChainPresenter = new WpfSwapChainPresenter();
            _swapChain = device.CreateSwapChain(width, height, swapChainPresenter);
			_deviceContext = device.ImmediateContext;

			// Create RenderTargetView from the backbuffer.
			var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
			_renderTargetView = device.CreateRenderTargetView(backBuffer);

			// Create the depth buffer
			var depthBuffer = device.CreateTexture2D(new Texture2DDescription
			{
				ArraySize = 1,
				MipLevels = 1,
				Width = width,
				Height = height,
				BindFlags = BindFlags.DepthStencil
			});

			// Create the depth buffer view
			_depthView = device.CreateDepthStencilView(depthBuffer);

            // Load model.
		    var modelLoader = new ModelLoader(device, TextureLoader.CreateTextureFromStream);
            _model = modelLoader.Load("Modules/SampleBrowser/Samples/ModelLoading/Car/car.obj");

            // Compile Vertex and Pixel shaders
            var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/ModelLoading/ModelLoading.fx", "VS", "vs_4_0");
            var vertexShader = device.CreateVertexShader(vertexShaderByteCode);

            var pixelShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/ModelLoading/ModelLoading.fx", "PS", "ps_4_0");
            var pixelShader = device.CreatePixelShader(pixelShaderByteCode);

		    _model.SetInputLayout(device, vertexShaderByteCode.InputSignature);

			var sampler = device.CreateSamplerState(new SamplerStateDescription
			{
				Filter = Filter.MinMagMipPoint,
				AddressU = TextureAddressMode.Wrap,
				AddressV = TextureAddressMode.Wrap,
				AddressW = TextureAddressMode.Wrap,
				BorderColor = Color4.Black,
				ComparisonFunction = Comparison.Never,
				MaximumAnisotropy = 16,
				MipLodBias = 0,
				MinimumLod = 0,
				MaximumLod = 16,
			});

            // Create constant buffers.
            _vertexConstantBuffer = device.CreateBuffer(new BufferDescription
            {
                SizeInBytes = Utilities.SizeOf<VertexShaderData>(),
                BindFlags = BindFlags.ConstantBuffer
            });
            _pixelConstantBuffer = device.CreateBuffer(new BufferDescription
            {
                SizeInBytes = Utilities.SizeOf<PixelShaderData>(),
                BindFlags = BindFlags.ConstantBuffer
            });

			// Prepare all the stages
            _deviceContext.VertexShader.SetConstantBuffers(0, _vertexConstantBuffer);
            _deviceContext.VertexShader.Shader = vertexShader;
            _deviceContext.PixelShader.SetConstantBuffers(0, _pixelConstantBuffer);
            _deviceContext.PixelShader.Shader = pixelShader;
			_deviceContext.PixelShader.SetSamplers(0, sampler);

            _vertexShaderData = new VertexShaderData();

            _pixelShaderData = new PixelShaderData
            {
                LightDirection = new Vector4(Vector3.Normalize(new Vector3(0.5f, 0, -1)), 1)
            };
            _deviceContext.SetBufferData(_pixelConstantBuffer, ref _pixelShaderData);

		    // Setup targets and viewport for rendering
			_deviceContext.Rasterizer.SetViewports(new Viewport(0, 0, width, height, 0.0f, 1.0f));
			_deviceContext.OutputMerger.SetTargets(_depthView, _renderTargetView);

            //_deviceContext.Rasterizer.State = device.CreateRasterizerState(new Pipeline.Rasterizer.RasterizerStateDescription
            //{
            //    FillMode = Pipeline.Rasterizer.FillMode.Wireframe
            //});

			// Prepare matrices
			_projection = Matrix.PerspectiveFovLH(MathUtil.Pi / 3.0f, 
				width / (float) height, 1f, 1000.0f);

            return swapChainPresenter.Bitmap;
		}
예제 #2
0
        public void CanImport3dsModel()
        {
            // Arrange.
            var device = new Device();
            var modelLoader = new ModelLoader(device, TextureLoader.CreateTextureFromStream);
            const string file = "Assets/Sponza/sponza.3ds";

            // Act.
            var model = modelLoader.Load(file);

            // Assert.
            Assert.That(model, Is.Not.Null);
            Assert.That(model.Meshes, Is.Not.Null);
            Assert.That(model.Meshes, Has.Count.EqualTo(37));

            var expectedIndexCounts = new[]
            {
                576,
                3456,
                10320,
                1440,
                144,
                384,
                2736,
                2544,
                192,
                3456,
                4320,
                2304,
                210,
                2712,
                312,
                1920,
                48,
                1260,
                3300,
                144,
                5100,
                690,
                25056,
                31110,
                6324,
                3060,
                4368,
                12096,
                14931,
                24144,
                16704,
                7515,
                12,
                2439,
                870,
                726,
                2439
            };
            for (int i = 0; i < model.Meshes.Count; i++)
                Assert.That(model.Meshes[i].IndexCount, Is.EqualTo(expectedIndexCounts[i]));

            var expectedVertexCounts = new[]
            {
                200,
                950,
                6761,
                1024,
                64,
                263,
                2224,
                1849,
                144,
                880,
                1530,
                1216,
                76,
                988,
                208,
                1276,
                18,
                436,
                1338,
                56,
                1989,
                274,
                7227,
                9135,
                1760,
                1772,
                2753,
                5477,
                6694,
                7343,
                4805,
                2594,
                6,
                718,
                320,
                340,
                722
            };
            for (int i = 0; i < model.Meshes.Count; i++)
                Assert.That(model.Meshes[i].VertexCount, Is.EqualTo(expectedVertexCounts[i]));

            var expectedFaceCounts = new[]
            {
                192,
                1152,
                3440,
                480,
                48,
                128,
                912,
                848,
                64,
                1152,
                1440,
                768,
                70,
                904,
                104,
                640,
                16,
                420,
                1100,
                48,
                1700,
                230,
                8352,
                10370,
                2108,
                1020,
                1456,
                4032,
                4977,
                8048,
                5568,
                2505,
                4,
                813,
                290,
                242,
                813
            };
            for (int i = 0; i < model.Meshes.Count; i++)
                Assert.That(model.Meshes[i].PrimitiveCount, Is.EqualTo(expectedFaceCounts[i]));

            var expectedTextureWidths = new[]
            {
                1021,
                1021,
                903,
                -1,
                1024,
                1024,
                155,
                423,
                1024,
                207,
                1021,
                903,
                640,
                -1,
                1024,
                423,
                294,
                1021,
                207,
                640,
                1021,
                1021,
                1021,
                -1,
                1021,
                903,
                903,
                423,
                155,
                1021,
                1021,
                640,
                305,
                423,
                305,
                410,
                423
            };
            for (int i = 0; i < model.Meshes.Count; i++)
                if (model.Meshes[i].DiffuseTexture != null)
                    Assert.That(model.Meshes[i].DiffuseTexture.Description.Width, Is.EqualTo(expectedTextureWidths[i]));
        }
        public override WriteableBitmap Initialize(Device device)
		{
			// Create device and swap chain.
            var swapChainPresenter = new WpfSwapChainPresenter();
            _swapChain = device.CreateSwapChain(Width, Height, swapChainPresenter);
			_deviceContext = device.ImmediateContext;

			// Create RenderTargetView from the backbuffer.
			var backBuffer = Texture2D.FromSwapChain(_swapChain, 0);
			_renderTargetView = device.CreateRenderTargetView(backBuffer);

			// Create the depth buffer
			var depthBuffer = device.CreateTexture2D(new Texture2DDescription
			{
				ArraySize = 1,
				MipLevels = 1,
				Width = Width,
				Height = Height,
				BindFlags = BindFlags.DepthStencil
			});

			// Create the depth buffer view
			_depthView = device.CreateDepthStencilView(depthBuffer);

            // Create cube map resources.
            var cubeMapTexture = device.CreateTexture2D(new Texture2DDescription
            {
                Width = CubeMapSize,
                Height= CubeMapSize,
                ArraySize = 6,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                MipLevels = 1
            });
            _renderTargetViewCube = device.CreateRenderTargetView(cubeMapTexture);
            _resourceViewCube = device.CreateShaderResourceView(cubeMapTexture);
            var depthBufferCube = device.CreateTexture2D(new Texture2DDescription
            {
                ArraySize = 6,
                MipLevels = 1,
                Width = CubeMapSize,
                Height = CubeMapSize,
                BindFlags = BindFlags.DepthStencil
            });
            _depthViewCube = device.CreateDepthStencilView(depthBufferCube);

            // Load model.
		    var modelLoader = new ModelLoader(device, TextureLoader.CreateTextureFromStream);
            _model = modelLoader.Load("Modules/SampleBrowser/Samples/EnvironmentMapping/teapot.obj");

            // Compile Vertex and Pixel shaders
            var vertexShaderByteCode = ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "VS_CubeMap", "vs_4_0");
            _vertexShaderCubeMap = device.CreateVertexShader(vertexShaderByteCode);
            _vertexShaderStandard = device.CreateVertexShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "VS_Standard", "vs_4_0"));

            _geometryShaderCubeMap = device.CreateGeometryShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "GS_CubeMap", "gs_4_0"));

            _pixelShaderCubeMap = device.CreatePixelShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "PS_CubeMap", "ps_4_0"));
            _pixelShaderStandard = device.CreatePixelShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "PS_Standard", "ps_4_0"));
            _pixelShaderReflective = device.CreatePixelShader(ShaderCompiler.CompileFromFile("Modules/SampleBrowser/Samples/EnvironmentMapping/EnvironmentMapping.fx", "PS_Reflective", "ps_4_0"));

		    _model.SetInputLayout(device, vertexShaderByteCode.InputSignature);

            var sampler = device.CreateSamplerState(SamplerStateDescription.Default);

            // Create constant buffers.
            _vertexConstantBuffer = device.CreateBuffer(new BufferDescription
            {
                SizeInBytes = Utilities.SizeOf<VertexShaderData>(),
                BindFlags = BindFlags.ConstantBuffer
            });
            _geometryConstantBuffer = device.CreateBuffer(new BufferDescription
            {
                SizeInBytes = Utilities.SizeOf<GeometryShaderData>(),
                BindFlags = BindFlags.ConstantBuffer
            });
            var pixelConstantBuffer = device.CreateBuffer(new BufferDescription
            {
                SizeInBytes = Utilities.SizeOf<PixelShaderData>(),
                BindFlags = BindFlags.ConstantBuffer
            });

			// Prepare all the stages
            _deviceContext.VertexShader.SetConstantBuffers(0, _vertexConstantBuffer);
            _deviceContext.GeometryShader.SetConstantBuffers(0, _geometryConstantBuffer);
            _deviceContext.PixelShader.SetConstantBuffers(0, pixelConstantBuffer);
			_deviceContext.PixelShader.SetSamplers(0, sampler);

            _vertexShaderData = new VertexShaderData();

            _view1 = Matrix.LookAtLH(Vector3.Zero, new Vector3(1, 0, 0), Vector3.UnitY);
            _view2 = Matrix.LookAtLH(Vector3.Zero, new Vector3(-1, 0, 0), Vector3.UnitY);
            _view3 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, 1, 0), -Vector3.UnitZ);
            _view4 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, -1, 0), Vector3.UnitZ);
            _view5 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, 0, 1), Vector3.UnitY);
            _view6 = Matrix.LookAtLH(Vector3.Zero, new Vector3(0, 0, -1), Vector3.UnitY);

            var from = new Vector3(0, 30, 70);
            var to = new Vector3(0, 0, 0);

            // Prepare matrices
            var view = Matrix.LookAtLH(from, to, Vector3.UnitY);
            
			_projectionCube = Matrix.PerspectiveFovLH(MathUtil.PiOverTwo, 1.0f, 1f, 10000.0f);
            _projection = Matrix.PerspectiveFovLH(MathUtil.Pi / 3.0f, Width / (float) Height, 1f, 10000.0f);

            _viewProjectionCube = Matrix.Multiply(view, _projectionCube);
            _viewProjection = Matrix.Multiply(view, _projection);

            var pixelShaderData = new PixelShaderData
            {
                LightDirection = new Vector4(Vector3.Normalize(new Vector3(0.5f, 0, -1)), 1),
                EyePosition = new Vector4(from, 1)
            };
            _deviceContext.SetBufferData(pixelConstantBuffer, ref pixelShaderData);

            // Setup targets and viewport for rendering
            _deviceContext.Rasterizer.SetViewports(new Viewport(0, 0, Width, Height, 0.0f, 1.0f));
            _deviceContext.OutputMerger.SetTargets(_depthView, _renderTargetView);

            return swapChainPresenter.Bitmap;
		}