2011-08-29 6 views
0

렌더 타겟이 작동하는 방식을 근본적으로 오해하고 있습니다. RenderTargets는 Spritebatch draw draw가 그리는 텍스처 일뿐입니다.RenderTargets XNA에서 순서 그리기

그래서이 코드를 사용하여 GUI 창을 렌더링하여 클라이언트 영역을 그릴 수 있고 그 밖에 잘릴 수 있는지 확인했습니다.

for (int i = Controls.Count - 1; i >= 0; i--) 
     { 
      RenderTarget2D oldTarget; 
      if (graphics.GetRenderTargets().Count() == 0) oldTarget = null; 
      else oldTarget = (RenderTarget2D)graphics.GetRenderTargets()[0].RenderTarget; // Get the old target being used. 
      graphics.SetRenderTarget(canvas); //set the target to a temporary RT 
      graphics.Clear(Color.Black); // Clear it 
      Control c = Controls[i]; // Get the current control (a form in this case) 
      c.Draw(spriteBatch, gameTime); // Draw it to the temp RT 
      graphics.SetRenderTarget(oldTarget); // Set the RT back to the main RT 
      Vector2 dest = c.DrawCoOrds(); // Gets the draw coordinates of the control 
      spriteBatch.Begin(); 
      spriteBatch.Draw(canvas, new Rectangle((int)dest.X, (int)dest.Y, c.Bounds.Width, c.Bounds.Height), new Rectangle((int)dest.X, (int)dest.Y, c.Bounds.Width, c.Bounds.Height), Color.White); 
// take the rect from the temp RT and draw it to the main RT. 
      spriteBatch.End(); 
     } 

그러나이 코드는 어떻게 든 주요 RT를 삭제해야하지만 난 이유를 이해 해달라고 의미 목록의 마지막 형태를 그립니다. RT가 임시 캔버스로 설정되었을 때만 명확하게 호출합니다.

답변

0

gui 컨트롤을 그리는 가장 좋은 방법은 ScissorRectangle을 사용하는 것입니다. 왜냐하면 gui 컨트롤의 클라이언트 영역 일 수있는 사각형 내부 만 그릴 수 있기 때문입니다.

MSDN: GraphicsDevice.ScissorRectangle

당신은 RasterizerState 통해이 funcionality를 활성화해야합니다.

RasterizerState ScissorState = new RasterizerState() 
{ 
    ScissorTestEnabled = true; 
} 

그리기 전에 SpriteBatch.Begin을이 상태로 호출하십시오.

A video of my own gui running in a xbox360 :)

0

어떻게 당신이 당신의 렌더링 타겟과 백 버퍼를 생성 않았다 ? 기본적으로 다른 렌더 대상으로 변경 한 후에는 여러 번 렌더링 대상에 쓸 수 없습니다.

http://blogs.msdn.com/b/shawnhar/archive/2007/11/21/rendertarget-changes-in-xna-game-studio-2-0.aspx

당신은 RenderTargetUsage.PreserveContents.로 렌더링 타겟을 작성하면 기본 동작을 변경하고, 링크에 설명 된대로 GraphicsDeviceInformation.PresentationParameters.RenderTargetUsage을 변경, GraphicsDeviceManager.PrepareDeviceSettings.를 오버라이드 (override)하여 백 버퍼 수 :이 이유입니다. 이러한 설정을 재정의하는 것이 XNA 4에서 다르게 수행된다고 생각합니다.

기본적으로 기본 동작에서 벗어나는 것은 성능 고려 사항이므로 권장하지 않습니다. 이것을 다르게하는 방법을 찾아야합니다. 한 가지 가능성은 각각의 윈도우에 대해 별도의 렌더 타겟을 생성하고, 모두 그려서, 백 버퍼로 전환하고 렌더 타겟을 렌더링하는 것입니다.

더 나은 옵션은 @Blau가 제안한 가위 사각형 래스터 라이저 상태를 사용하는 것입니다.