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();
}
這是正確的! – 2012-08-17 11:46:24
我找到了另一種方法:刪除我不使用的z緩衝區的技巧:* pp.EnableAutoDepthStencil = false * – 2012-08-17 12:16:31
我嘗試編輯它,它也工作:我可以保留使用z緩衝區,謝謝! – 2012-08-17 12:24:49