DirectX 11顶点坐标 [英] DirectX 11 Vertices Coordinates

查看:391
本文介绍了DirectX 11顶点坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个C ++和DirectX 11项目,其中我的目标是在屏幕上绘制一个彩色三角形。这工作良好没有任何问题。但是,有一部分我想改变,但我不知道如何。我试图寻找一个解决方案,但我还没有发现任何,我想这是因为我真的不知道我应该搜索什么。

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.

目前我设置了我的三角形3个顶点,如下所示:

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 )},
}



其中VertexPos定义如下:

Where VertexPos is defined like this:

struct VertexPos
{
    XMFLOAT3 pos;
};

当前,我的顶点位置设置范围为-1.0F到1.0F,其中0.0F是中心。我如何改变这个,使我可以通过使用真正的坐标定位我的顶点:

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:


  • 创建正交投影矩阵( XMMatrixOrthographicLH ) 如果使用XMMATH)

  • 使用CPU矩阵(C ++代码)和GPU大小(顶点着色器)创建常量缓冲区

  • 通过顶点着色器中的正交投影矩阵乘以顶点位置

  • 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

F.Luna book):

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;
}

几乎相同,但在CPU端。当然,您也可以将此代码移动到着色器。

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.

快乐编程! =)

这篇关于DirectX 11顶点坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆