Пример #1
0
        public void Dispose()
        {
            System.Console.WriteLine("Renderer3d.Dispose");
            if (context != null)
            {
                DiscardDeviceResources();

                textureCollection?.Dispose();
                textureLoader?.Dispose();

                cb_shader_flags?.Dispose();
                cb_mat?.Dispose();
                cb_wvp?.Dispose();

                foreach (NiFile nif in nifs)
                {
                    foreach (Mesh mesh in nif.meshes)
                    {
                        mesh.Dispose();
                    }
                }

                context.ClearState();
                context.Flush();
                context.Dispose();
                context = null;
            }
            skeleton = null;
            device   = null;
        }
Пример #2
0
        public void CreateDeviceIndependentResources(hkaSkeleton skeleton)
        {
            this.skeleton = skeleton;

            d2dFactory    = new SharpDX.Direct2D1.Factory();
            dwriteFactory = new SharpDX.DirectWrite.Factory();
        }
Пример #3
0
        public bool InitializeGraphics(Control control)
        {
            this.control = control;

            AttachMouseEventHandler(control);
            control.Resize += new System.EventHandler(form_Resize);

            System.Drawing.Size clientSize = control.ClientSize;

            // Create Device and SwapChain
            device = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 });

            factory1  = new SharpDX.DXGI.Factory1();
            swapChain = new SwapChain(factory1, device, new SwapChainDescription()
            {
                ModeDescription   = new ModeDescription(clientSize.Width, clientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = DetectSampleDescription(device, Format.D32_Float_S8X24_UInt),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = control.Handle,
                IsWindowed        = true,
                SwapEffect        = SwapEffect.Discard
            });
            // Ignore all windows events
            factory1.MakeWindowAssociation(control.Handle, WindowAssociationFlags.IgnoreAll);

            CreateViewportAndProjection(ref clientSize);

            string skel_file = Path.Combine(Application.StartupPath, @"resources\skeleton.bin");

            skeleton = new hkaSkeleton();
            skeleton.Load(skel_file);

            string anim_file = Path.Combine(Application.StartupPath, @"resources\idle.bin");

            anim = new hkaAnimation();
            if (anim.Load(anim_file))
            {
                string source_file = Path.ChangeExtension(anim_file, ".hkx");
                LoadAnimationSuccessful(source_file);
            }

            renderer3d.InitializeGraphics(device, skeleton);
            renderer3d.CreateDeviceResources(swapChain, ref viewport);

            renderer2d.CreateDeviceIndependentResources(skeleton);

            return(true);
        }
Пример #4
0
        public void InitializeGraphics(Device device, hkaSkeleton skeleton)
        {
            this.device   = device;
            this.skeleton = skeleton;

            string meshes_path   = Path.Combine(Application.StartupPath, @"data\meshes");
            string textures_path = Path.Combine(Application.StartupPath, @"data\textures");
            string shader_path   = Path.Combine(Application.StartupPath, @"shader.fx");

            foreach (string file in Directory.GetFiles(meshes_path, "*.nif"))
            {
                string path = Path.Combine(meshes_path, file);
                nifs.Add(new NiFile(device, path));
            }

            textureLoader     = new TextureLoader(textures_path);
            textureCollection = new TextureCollection(device, textureLoader);
            foreach (NiFile nif in nifs)
            {
                foreach (Mesh mesh in nif.meshes)
                {
                    textureCollection.LoadTexture(mesh.albedoMap_path);
                }
            }

            context = device.ImmediateContext;

            using (var bytecode = ShaderBytecode.CompileFromFile(shader_path, "VS", "vs_4_0"))
            {
                DefineInputLayout(bytecode);
                using (var vertexShader = new VertexShader(device, bytecode))
                    context.VertexShader.Set(vertexShader);
            }
            using (var bytecode = ShaderBytecode.CompileFromFile(shader_path, "PS", "ps_4_0"))
            {
                using (var pixelShader = new PixelShader(device, bytecode))
                    context.PixelShader.Set(pixelShader);
            }

            cb_wvp          = new Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
            cb_mat          = new Buffer(device, Utilities.SizeOf <Matrix>() * 40, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, Utilities.SizeOf <Matrix>());
            cb_shader_flags = new Buffer(device, Utilities.SizeOf <uint>() * 4, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, Utilities.SizeOf <uint>());

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            context.VertexShader.SetConstantBuffer(0, cb_wvp);
            context.VertexShader.SetConstantBuffer(1, cb_mat);
            context.PixelShader.SetConstantBuffer(2, cb_shader_flags);

            var rasterizerStateDescription = RasterizerStateDescription.Default();

            rasterizerStateDescription.IsFrontCounterClockwise = true;
            using (var rasterizerState = new RasterizerState(device, rasterizerStateDescription))
                context.Rasterizer.State = rasterizerState;

            var blendStateDescription = new BlendStateDescription();

            blendStateDescription.RenderTarget[0] = new RenderTargetBlendDescription()
            {
                IsBlendEnabled        = true,
                SourceBlend           = BlendOption.SourceAlpha,
                DestinationBlend      = BlendOption.InverseSourceAlpha,
                BlendOperation        = BlendOperation.Add,
                SourceAlphaBlend      = BlendOption.One,
                DestinationAlphaBlend = BlendOption.Zero,
                AlphaBlendOperation   = BlendOperation.Add,
                RenderTargetWriteMask = ColorWriteMaskFlags.All
            };
            using (var blendState = new BlendState(device, blendStateDescription))
                context.OutputMerger.SetBlendState(blendState);

            // from skeleton
            // name to idx
            var nameMap = new Dictionary <string, int>();

            for (int i = 0; i < skeleton.bones.Length; i++)
            {
                nameMap[skeleton.bones[i].name] = i;
            }

            boneMapCollection = new BoneMapCollection(nameMap);
            foreach (NiFile nif in nifs)
            {
                foreach (Mesh mesh in nif.meshes)
                {
                    boneMapCollection.SetBoneMap(mesh);
                }
            }
        }