Пример #1
0
        private MtlMaterial GetMaterial(string mtlFile)
        {
            CultureInfo infos    = CultureInfo.InvariantCulture;
            MtlMaterial material = new MtlMaterial();

            using (StreamReader reader = new StreamReader(mtlFile))
            {
                while (true)
                {
                    string l = reader.ReadLine();
                    if (reader.EndOfStream)
                    {
                        break;
                    }
                    if (l.Contains("map_Ka "))
                    {
                        break;
                    }
                    if (l.Contains("Ns "))
                    {
                        material.Ns_SpecularPower = float.Parse(l.Replace("Ns ", "").Trim(), infos);
                    }
                    if (l.Contains("Ni "))
                    {
                        material.Ni_OpticalDensity = float.Parse(l.Replace("Ni ", "").Trim(), infos);
                    }

                    if (l.Contains("\td "))
                    {
                        material.d_Transparency = float.Parse(l.Replace("d ", "").Trim(), infos);
                    }

                    if (l.Contains("Tr "))
                    {
                        material.Tr_Transparency = float.Parse(l.Replace("Tr ", "").Trim(), infos);
                    }

                    if (l.Contains("Tf "))
                    {
                        var val = l.Replace("Tf ", "").Trim().Split(' ').Select(s => float.Parse(s, infos)).ToArray();
                        material.Tf_TransmissionFilter = new Vector3(val[0], val[1], val[2]);
                    }
                    if (l.Contains("Ka "))
                    {
                        var val = l.Replace("Ka ", "").Trim().Split(' ').Select(s => float.Parse(s, infos)).ToArray();
                        material.Ka_AmbientColor = new Color4(val[0], val[1], val[2], 1);
                    }
                    if (l.Contains("Kd "))
                    {
                        var val = l.Replace("Kd ", "").Trim().Split(' ').Select(s => float.Parse(s, infos)).ToArray();
                        material.Kd_DiffuseColor = new Color4(val[0], val[1], val[2], 1);
                    }
                    if (l.Contains("Ks "))
                    {
                        var val = l.Replace("Ks ", "").Trim().Split(' ').Select(s => float.Parse(s, infos)).ToArray();
                        material.Ks_SpecularColor = new Color4(val[0], val[1], val[2], 1);
                    }
                    if (l.Contains("Ke "))
                    {
                        var val = l.Replace("Ke ", "").Trim().Split(' ').Select(s => float.Parse(s, infos)).ToArray();
                        material.Ke_EmissiveColor = new Color4(val[0], val[1], val[2], 0);
                    }
                }
            }
            return(material);
        }
Пример #2
0
        public EarthFromOBJ(DeviceContext dx11Context)
        {
            _dx11Context = dx11Context;
            World        = Matrix.Translation(0, 100, -5000);
            _light.Color = Color4.White;

            const string obj         = "3DModelsFiles\\Earth\\earth.obj";
            const string mtl         = "3DModelsFiles\\Earth\\earth.mtl";
            const string jpg         = "3DModelsFiles\\Earth\\earthmap.jpg";
            const string shadersFile = "Shaders\\EarthT.hlsl";
            Tuple <List <Face>, List <uint> > tuple = GetFaces(obj);

            _facesCount = tuple.Item2.Count;

            _vertexBuffer  = Buffer.Create(dx11Context.Device, BindFlags.VertexBuffer, tuple.Item1.ToArray());
            _indexBuffer   = Buffer.Create(dx11Context.Device, BindFlags.IndexBuffer, tuple.Item2.ToArray());
            _vertexBinding = new VertexBufferBinding(_vertexBuffer, Utilities.SizeOf <Face>(), 0);

            MtlMaterial material = GetMaterial(mtl);

            _materialsBuffer = Buffer.Create(_dx11Context.Device, BindFlags.ConstantBuffer, ref material);

            _constantBuffer = new Buffer(_dx11Context.Device, Utilities.SizeOf <Matrices>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            _lightBuffer    = new Buffer(_dx11Context.Device, Utilities.SizeOf <Light>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            _textureResourse  = _dx11Context.LoadTextureFromFile(jpg);
            _textureResourse1 = _dx11Context.LoadTextureFromFile("3DModelsFiles\\Earth\\map.jpg");
            _textureResourse2 = _dx11Context.LoadTextureFromFile("3DModelsFiles\\Earth\\map1.jpg");


            SamplerStateDescription description = SamplerStateDescription.Default();

            description.Filter   = Filter.MinMagMipLinear;
            description.AddressU = TextureAddressMode.Wrap;
            description.AddressV = TextureAddressMode.Wrap;
            description.AddressW = TextureAddressMode.Wrap;
            _samplerState        = new SamplerState(_dx11Context.Device, description);

            //Загружаем шейдеры из файлов
            InputElement[] inputElements = new InputElement[]
            {
                new InputElement("SV_Position", 0, Format.R32G32B32_Float, 0, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32B32_Float, 24, 0)
            };
            ShaderFlags shaderFlags = ShaderFlags.None;

#if DEBUG
            shaderFlags = ShaderFlags.Debug;
#endif

            using (var vertexShaderByteCode = ShaderBytecode.CompileFromFile(shadersFile, "VS", "vs_5_0", shaderFlags))
            {
                //Синатура храянящая сведения о том какие входные переменные есть у шейдера
                _inputSignature = ShaderSignature.GetInputSignature(vertexShaderByteCode);
                _vertexShader   = new VertexShader(_dx11Context.Device, vertexShaderByteCode);
            }
            using (var pixelShaderByteCode = ShaderBytecode.CompileFromFile(shadersFile, "PS", "ps_5_0", shaderFlags))
            {
                _pixelShader = new PixelShader(_dx11Context.Device, pixelShaderByteCode);
            }
            using (var pixelShaderByteCode = ShaderBytecode.CompileFromFile(shadersFile, "HS", "hs_5_0", shaderFlags))
            {
                _hShader = new HullShader(_dx11Context.Device, pixelShaderByteCode);
            }

            using (var pixelShaderByteCode = ShaderBytecode.CompileFromFile(shadersFile, "DS", "ds_5_0", shaderFlags))
            {
                _dShader = new DomainShader(_dx11Context.Device, pixelShaderByteCode);
            }

            _inputLayout = new InputLayout(_dx11Context.Device, _inputSignature, inputElements);

            RasterizerStateDescription rasterizerStateDescription = RasterizerStateDescription.Default();
            rasterizerStateDescription.CullMode = CullMode.None;
            rasterizerStateDescription.FillMode = FillMode.Solid;

            DepthStencilStateDescription DStateDescripshion = DepthStencilStateDescription.Default();
            DStateDescripshion.IsDepthEnabled = true;

            _DState          = new DepthStencilState(_dx11Context.Device, DStateDescripshion);
            _rasterizerState = new RasterizerState(_dx11Context.Device, rasterizerStateDescription);

            _light.Direction = new Vector3(1f, -1f, 1f);
        }