【问题标题】:XNA: Orthographic Projection that matches Screen CoordinatesXNA:匹配屏幕坐标的正交投影
【发布时间】:2011-11-07 11:17:12
【问题描述】:

我将 XNA 与 SpriteBatch 和自定义绘制的顶点并行使用。目标是为两种技术使用相同的坐标系。 这意味着我需要一个映射到屏幕坐标的投影矩阵: (0, 0) 位于屏幕左上角,而宽度和高度由屏幕分辨率决定。

Matrix.CreateOrthographicOffCenter(0, width, 0, height, -1, 1);

效果很好,但中心位于 底部-左角。

Matrix.CreateOrthographicOffCenter(0, width, height, 0, -1, 1);

根本不显示任何东西。

尝试将第一个投影矩阵与平移相结合并将 y 缩放 -1 也不会显示任何内容。按正值缩放效果很好,翻译也一样。但是,一旦我按负值缩放,我就根本没有任何输出。

有什么想法吗?

PS:出于测试目的,我绘制的顶点远远超出屏幕坐标,所以如果翻译出现错误,我至少会看到一些东西。

【问题讨论】:

  • 您应该使用 View 矩阵和 Projection。

标签: c# xna projection orthogonal


【解决方案1】:

我使用此代码初始化我的 2D 相机以绘制线条,并使用基本的自定义效果进行绘制。

    Vector2 center;
    center.X = Game.GraphicsDevice.Viewport.Width * 0.5f;
    center.Y = Game.GraphicsDevice.Viewport.Height * 0.5f;

    Matrix View = Matrix.CreateLookAt( new Vector3( center, 0 ), new Vector3( center, 1 ), new Vector3( 0, -1, 0 ) );
    Matrix Projection = Matrix.CreateOrthographic( center.X * 2, center.Y * 2, -0.5f, 1 );

效果

uniform float4x4 xWorld;
uniform float4x4 xViewProjection;

void VS_Basico(in float4 inPos : POSITION,  in float4 inColor: COLOR0,  out float4      outPos: POSITION,    out float4 outColor:COLOR0 )
{
    float4 tmp = mul (inPos, xWorld);
    outPos = mul (tmp, xViewProjection);
    outColor = inColor; 
}

technique Lines
{
    pass Pass0
    {   
        VertexShader = compile vs_2_0 VS_Basico();
        FILLMODE = SOLID;
        CULLMODE = NONE;        
    }  
}

【讨论】:

  • 感谢这项工作。可能我的一些实验也奏效了,但效果文件中似乎有一个错误阻止它绘制。当我用 BasicEffect 绘制图元时,一切都很好,谢谢! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2020-07-16
  • 1970-01-01
相关资源
最近更新 更多