DirectX 11 Vertices Coordinates

依然范特西╮ 提交于 2019-12-12 01:01:36

问题


I'm working on my first C++ and DirectX 11 project where my current goal is to draw a colored triangle on the screen. This has worked well without any problems. However, there's one part that I would like to change, but I don't know how. I've tried searching for a solution but I havn't found any yet, and I guess that the reason is because I don't really know what I should search for.

Currently I set up my triangles 3 vertices like this:

VertexPos vertices[] =
{
    { XMFLOAT3(  0.5f,  0.5f, 1.0f )},
    { XMFLOAT3(  0.5f, -0.5f, 1.0f )},
    { XMFLOAT3( -0.5f, -0.5f, 1.0f )},
}

Where VertexPos is defined like this:

struct VertexPos
{
    XMFLOAT3 pos;
};

Currenty, my vertices position is set up by the range -1.0F to 1.0F, where 0.0F is the center. How can I change this so that I can position my vertices by using "real" coordinates like this:

VertexPos vertices[] =
{
    { XMFLOAT3(  100.0f,  300.0f, 1.0f )},
    { XMFLOAT3(  200.0f,  200.0f, 1.0f )},
    { XMFLOAT3(  200.0f,  300.0f, 1.0f )},
}

回答1:


  1. Usual way:

    • create orthogonal projection matrix (XMMatrixOrthographicLH() if you using XMMATH)
    • create constant buffer with this matrix on CPU side (C++ code) and on GPU size (vertex shader)
    • multiply vertex positions by orthogonal projection matrix in vertex shader
  2. Simpler way (from F.Luna book):

    XMFLOAT3 SpriteBatch::PointToNdc(int x, int y, float z)
    {
        XMFLOAT3 p;
    
        p.x = 2.0f*(float)x/mScreenWidth - 1.0f;
        p.y = 1.0f - 2.0f*(float)y/mScreenHeight;
        p.z = z;
    
        return p;
    }
    

    Almost the same, but on CPU side. Of course, you can move this code to shader too.

P.S. Probably your book/manual/tutorials will learn you about it a little later. So, you better trust it and flow step-by step.

Happy coding! =)



来源:https://stackoverflow.com/questions/19226156/directx-11-vertices-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!