Exemplo n.º 1
0
        private void CreateResource(Format format, int width, int height)
        {
            PresentParameters presentParameters = this.GetPresentParameters(width, height);

            #region 创建device

            this.device = this.isVistaOrBetter ?
                          new DeviceEx((Direct3DEx)this.direct3D, this.adapterId, DeviceType.Hardware, this.hwnd, this.createFlag, presentParameters) :
                          new Device(this.direct3D, this.adapterId, DeviceType.Hardware, this.hwnd, this.createFlag, presentParameters);

            this.device.SetRenderState(RenderState.CullMode, Cull.None);
            this.device.SetRenderState(RenderState.ZEnable, ZBufferType.DontUseZBuffer);
            this.device.SetRenderState(RenderState.Lighting, false);
            this.device.SetRenderState(RenderState.DitherEnable, true);
            this.device.SetRenderState(RenderState.MultisampleAntialias, true);
            this.device.SetRenderState(RenderState.AlphaBlendEnable, true);
            this.device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
            this.device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
            this.device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear);
            this.device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
            this.device.SetSamplerState(0, SamplerState.AddressU, TextureAddress.Clamp);
            this.device.SetSamplerState(0, SamplerState.AddressV, TextureAddress.Clamp);
            this.device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.SelectArg1);
            this.device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
            this.device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.Specular);
            this.device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate);
            this.device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture);
            this.device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.Diffuse);

            #endregion

            #region 创建RenderTarget

            //this.renderTarget = this.device.GetRenderTarget(0);
            int vertexSize = Marshal.SizeOf(typeof(VERTEX));
            this.texture        = new Texture(this.device, width, height, 1, Usage.RenderTarget, this.displayMode.Format, Pool.Default);
            this.textureSurface = this.texture.GetSurfaceLevel(0);

            VERTEX[] vertexList = new VERTEX[]
            {
                new VERTEX()
                {
                    pos = new Vector3(0, 0, 0), texPos = new Vector2(0, 0), color = 0xFFFFFFFF
                },                                                                                         // 左上
                new VERTEX()
                {
                    pos = new Vector3(width, 0, 0), texPos = new Vector2(1, 0), color = 0xFFFFFFFF
                },                                                                                             // 右上
                new VERTEX()
                {
                    pos = new Vector3(width, height, 0), texPos = new Vector2(1, 1), color = 0xFFFFFFFF
                },                                                                                                  // 右下
                new VERTEX()
                {
                    pos = new Vector3(0, height, 0), texPos = new Vector2(0, 1), color = 0xFFFFFFFF
                },                                                                                              // 左下
            };

            this.vertexBuffer = new VertexBuffer(this.device, vertexSize * 4, Usage.Dynamic | Usage.WriteOnly, CUSTOM_VERTEX, Pool.Default);
            DataStream stream = this.vertexBuffer.Lock(0, 0, LockFlags.Discard);

            // 左上
            IntPtr dataPointer = stream.DataPointer;
            Marshal.StructureToPtr(vertexList[0], dataPointer, true);

            // 右上
            dataPointer += vertexSize;
            Marshal.StructureToPtr(vertexList[1], dataPointer, true);

            // 右下
            dataPointer += vertexSize;
            Marshal.StructureToPtr(vertexList[2], dataPointer, true);

            // 左下
            dataPointer += vertexSize;
            Marshal.StructureToPtr(vertexList[3], dataPointer, true);

            this.vertexBuffer.Unlock();

            #endregion

            this.SetupMatrices(width, height);

            #region 创建input surface
            this.inputSurface = this.isVistaOrBetter ?
                                Surface.CreateOffscreenPlainEx((DeviceEx)this.device, width, height, format, Pool.Default, Usage.None) :
                                Surface.CreateOffscreenPlain(this.device, width, height, format, Pool.Default);

            this.device.ColorFill(this.inputSurface, BlackColor);

            this.SetImageSourceBackBuffer();

            #endregion
        }
        private void CreateResource(Format format, int width, int height)
        {
            PresentParameters presentParameters = this.GetPresentParameters(width, height);

            #region 创建device

            this.device = this.isVistaOrBetter ?
                new DeviceEx((Direct3DEx)this.direct3D, this.adapterId, DeviceType.Hardware, this.hwnd, this.createFlag, presentParameters) :
                new Device(this.direct3D, this.adapterId, DeviceType.Hardware, this.hwnd, this.createFlag, presentParameters);

            this.device.SetRenderState(RenderState.CullMode, Cull.None);
            this.device.SetRenderState(RenderState.ZEnable, ZBufferType.DontUseZBuffer);
            this.device.SetRenderState(RenderState.Lighting, false);
            this.device.SetRenderState(RenderState.DitherEnable, true);
            this.device.SetRenderState(RenderState.MultisampleAntialias, true);
            this.device.SetRenderState(RenderState.AlphaBlendEnable, true);
            this.device.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
            this.device.SetRenderState(RenderState.DestinationBlend, Blend.InverseSourceAlpha);
            this.device.SetSamplerState(0, SamplerState.MagFilter, TextureFilter.Linear);
            this.device.SetSamplerState(0, SamplerState.MinFilter, TextureFilter.Linear);
            this.device.SetSamplerState(0, SamplerState.AddressU, TextureAddress.Clamp);
            this.device.SetSamplerState(0, SamplerState.AddressV, TextureAddress.Clamp);
            this.device.SetTextureStageState(0, TextureStage.ColorOperation, TextureOperation.SelectArg1);
            this.device.SetTextureStageState(0, TextureStage.ColorArg1, TextureArgument.Texture);
            this.device.SetTextureStageState(0, TextureStage.ColorArg2, TextureArgument.Specular);
            this.device.SetTextureStageState(0, TextureStage.AlphaOperation, TextureOperation.Modulate);
            this.device.SetTextureStageState(0, TextureStage.AlphaArg1, TextureArgument.Texture);
            this.device.SetTextureStageState(0, TextureStage.AlphaArg2, TextureArgument.Diffuse);

            #endregion

            #region 创建RenderTarget

            //this.renderTarget = this.device.GetRenderTarget(0);
            int vertexSize = Marshal.SizeOf(typeof(VERTEX));
            this.texture = new Texture(this.device, width, height, 1, Usage.RenderTarget, this.displayMode.Format, Pool.Default);
            this.textureSurface = this.texture.GetSurfaceLevel(0);
            
            VERTEX[] vertexList = new VERTEX[]
            {
                new VERTEX(){ pos = new Vector3(0, 0, 0), texPos = new Vector2(0, 0), color = 0xFFFFFFFF}, // 左上
                new VERTEX(){ pos = new Vector3(width, 0, 0), texPos = new Vector2(1, 0), color = 0xFFFFFFFF}, // 右上
                new VERTEX(){ pos = new Vector3(width, height, 0), texPos = new Vector2(1, 1), color = 0xFFFFFFFF}, // 右下
                new VERTEX(){ pos = new Vector3(0, height, 0), texPos = new Vector2(0, 1), color = 0xFFFFFFFF}, // 左下
            };

            this.vertexBuffer = new VertexBuffer(this.device, vertexSize * 4, Usage.Dynamic | Usage.WriteOnly, CUSTOM_VERTEX, Pool.Default);
            DataStream stream = this.vertexBuffer.Lock(0, 0, LockFlags.Discard);

            // 左上
            IntPtr dataPointer = stream.DataPointer;
            Marshal.StructureToPtr(vertexList[0], dataPointer, true);

            // 右上
            dataPointer += vertexSize;
            Marshal.StructureToPtr(vertexList[1], dataPointer, true);

            // 右下
            dataPointer += vertexSize;
            Marshal.StructureToPtr(vertexList[2], dataPointer, true);

            // 左下
            dataPointer += vertexSize;
            Marshal.StructureToPtr(vertexList[3], dataPointer, true);

            this.vertexBuffer.Unlock();

            #endregion

            this.SetupMatrices(width, height);

            #region 创建input surface
            this.inputSurface = this.isVistaOrBetter ?
                Surface.CreateOffscreenPlainEx((DeviceEx)this.device, width, height, format, Pool.Default, Usage.None) :
                Surface.CreateOffscreenPlain(this.device, width, height, format, Pool.Default);

            this.device.ColorFill(this.inputSurface, BlackColor);

            this.SetImageSourceBackBuffer();

            #endregion
        }