2012-08-17 74 views
0

我想創建一個紋理並渲染到它的表面,但是我遇到了一個問題:當紋理大小變大(超過263x263像素ARGB)時,沒有原始渲染在表面上(我只能看到清除操作期間使用的顏色)。使用紋理作爲渲染目標時的大小限制

這裏是我的代碼:

private void Form1_Load(object sender, EventArgs e) 
    { 
     // Setting up DirectX 
     PresentParameters pp = new PresentParameters() 
     { 
      BackBufferCount = 1, 
      Windowed = true, 
      SwapEffect = SwapEffect.Discard, 
      BackBufferWidth = ClientSize.Width, 
      BackBufferHeight = ClientSize.Height, 
     }; 

     d3d = new Direct3D(); 
     device = new Device(d3d, 0, DeviceType.Hardware, Handle, CreateFlags.HardwareVertexProcessing, pp); 

     TexturedVertex.SetVertexDeclaration(device); 

     // Creating a triangle 
     triangleBuffer = new VertexBuffer(device, 3 * Marshal.SizeOf(typeof(TexturedVertex)), Usage.WriteOnly, VertexFormat.None, Pool.Managed); 

     var stream1 = triangleBuffer.Lock(0, 0, LockFlags.None); 
     stream1.WriteRange(new[] { 
          new TexturedVertex(-2, -2, 0, 0, 0), 
          new TexturedVertex(0, 2, 0, 1, 0), 
          new TexturedVertex(2, -2, 0, 1, 1), 
        }); 

     triangleBuffer.Unlock(); 

     // Creating a rect 
     rectBuffer = new VertexBuffer(device, 4 * Marshal.SizeOf(typeof(TexturedVertex)), Usage.WriteOnly, VertexFormat.None, Pool.Managed); 

     var stream2 = rectBuffer.Lock(0, 0, LockFlags.None); 
     stream2.WriteRange(new[] { 
          new TexturedVertex(-2, -2, 0, 0, 0), 
          new TexturedVertex(-2, 2, 0, 0, 1), 
          new TexturedVertex(2, -2, 0, 1, 0), 
          new TexturedVertex(2, 2, 0, 1, 1), 
        }); 

     TexturedVertex.SetVertexDeclaration(device); 
     rectBuffer.Unlock(); 

     // Creating a render texture 
     int w = 262; 
     Texture texture = new Texture(device, w, w, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default); 

     Surface surface = texture.GetSurfaceLevel(0); 
     Surface buffer = device.GetRenderTarget(0); 

     device.SetRenderTarget(0, surface); 

     device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(-4, 4, -4, 4, 0, 100)); 
     device.SetTransform(TransformState.View, Matrix.Identity); 
     device.SetTransform(TransformState.World, Matrix.Identity); 

     device.Clear(ClearFlags.Target, new Color4(Color.Green), 0, 0); 

     device.BeginScene(); 

     device.SetStreamSource(0, triangleBuffer, 0, Marshal.SizeOf(typeof(TexturedVertex))); 
     device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 3); 

     device.EndScene(); 
     device.Present(); 

     device.SetRenderTarget(0, buffer); 

     // Setting current texture and material 
     device.SetTexture(0, texture); 
     Material mat = new Material(); 
     mat.Ambient = mat.Diffuse = mat.Emissive = Color.White; 
     device.Material = mat; 

     // Setting the projection, view and transform matrix 
     device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(-4, 4, -4, 4, 0, 100)); 
     device.SetTransform(TransformState.View, Matrix.Identity); 
     device.SetTransform(TransformState.World, Matrix.Identity); 


     // Setting handler 
     this.KeyDown += new KeyEventHandler(Form1_KeyDown); 
     this.Paint += new PaintEventHandler(Form1_Paint); 
    } 

    void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     device.Clear(ClearFlags.Target, new Color4(Color.Purple), 0, 0); 

     device.BeginScene(); 

     device.SetStreamSource(0, rectBuffer, 0, Marshal.SizeOf(typeof(TexturedVertex))); 
     //device.SetStreamSource(0, triangleBuffer, 0, Marshal.SizeOf(typeof(TexturedVertex))); 

     device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 4); 

     device.EndScene(); 

     device.Present(); 
    } 

回答

1

我猜你的窗體的客戶端大小爲262像素寬或高。

在表示參數中,您將backbuffer和z buffer限制爲此大小。您需要另一個Z緩衝區,與要渲染的紋理大小相匹配,並且需要將其設置爲使用,然後切換回原始。

Surface oldDepthBuffer = device.DepthStencilSurface; 
Texture db = new Texture(device, w, w, 1, Usage.DepthStencil, oldDepthBuffer.Description.Format, Pool.Default); 

Surface myDepthBuffer = db.GetSurfaceLevel(0); 

device.SetRenderTarget(0, surface); 
device.DepthStencilSurface = myDepthBuffer; 

// RENDER to texture 

device.DepthStencilSurface = oldDepthBuffer; 
+0

這是正確的! – 2012-08-17 11:46:24

+0

我找到了另一種方法:刪除我不使用的z緩衝區的技巧:* pp.EnableAutoDepthStencil = false * – 2012-08-17 12:16:31

+0

我嘗試編輯它,它也工作:我可以保留使用z緩衝區,謝謝! – 2012-08-17 12:24:49