c# - XNA:匹配屏幕坐标的正交投影

标签 c# xna projection orthogonal

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

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

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

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

根本不显示任何内容。

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

有什么想法吗?

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

最佳答案

我使用此代码初始化我的 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;        
    }  
}

关于c# - XNA:匹配屏幕坐标的正交投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7254946/

相关文章:

C# 从末尾删除整个字符串

c# - 网络 API : Configure JSON serializer settings on action or controller level

c# - 获取 UIElement 的高度和宽度?

c# - 得到一个没有很多 if 语句的类

c# - XNA 可以处理多少(低多边形)模型?

c# - 一次在十个图片框上显示多张图片

c# - 无法修改 XNA Vector 组件

spring - "java.lang.IllegalArgumentException: Projection type must be an interface"错误

python - 从 3d 到 2d 的 Project Scipy Voronoi 图

mongodb - 如何仅在特定字段存在时投影对象?