DirectX 和 DirectXTK 转换限制

DirectX and DirectXTK translation limits

我使用 DirectX Toolkit to display a 3d model, following the 'Rendering the model' 并显示我的金字塔:

尝试变换对象时,缩放和旋转效果很好,但我不确定如何移动(平移)对象。基本上,我正在寻找一种算法,在给定当前相机位置、焦点、视口和渲染模型(DirectX 工具包为我提供边界框,因此它是“大小”)的情况下,确定 XYZ 平移的最小值和最大值,因此对象仍然可见。

无论视口大小如何,边界框始终相同,那么如何将它的大小与我的视口进行比较?

请原谅我的新手,我不是 3D 开发人员,至少现在还不是。

绘制三角形的“简单渲染”示例:

Matrix proj = Matrix::CreateScale( 2.f/float(backBufferWidth),
   -2.f/float(backBufferHeight), 1.f)
   * Matrix::CreateTranslation( -1.f, 1.f, 0.f );
m_effect->SetProjection(proj);

说归一化的三角形大小是 [1,1,1] 但这里的归一化值不起作用。

TL:DR: 要在世界范围内移动模型,请为平移创建一个矩阵并将其设置为 SetWorld.[=26 的世界矩阵=]

Matrix world = Matrix::CreateTranslation( 2.f, 1.f, 3.f);
m_effect->SetWorld(world);

// Also be sure you have called SetView and SetProjection for the 3D camera setup
//covered in the 3D shapes / Rendering a model tutorial

您应该从 3D 转换的基本回顾开始,尤其是 world -> view -> 投影 转换管道。

  1. world 变换执行仿射变换以使您正在渲染的模型进入它的 'world' 位置。 (a.k.a.'local coordinates to world coordinates transformation').

  2. 视图变换执行变换以将世界位置转换为相机的视角(即位置和方向)(a.k.a。'world coordinates to view coordinates transformation').

  3. projection 转换执行转换,使视图位置进入实际硬件使用的规范“-1 到 1”范围,包括任何透视projection(a.k.a.'查看坐标到'clip'坐标的变换).

  4. 硬件本身执行最后一步,根据 Direct3D SetViewport 信息 (a.k.a 将“-1 到 1”转换为渲染目标中的像素位置。 'clip'坐标到像素坐标的转换)。

This Direct3D 9 era article is a bit dated, but it covers the overall idea well.

DirectX 工具包 BasicEffect 系统中,每个矩阵都有不同的方法:SetWorldSetViewSetProjection.如果你想一次设置所有三个,还有一个助手 SetMatrices

simple rendering 教程涉及最简单的渲染形式,即 2D 渲染,其中您希望提供的坐标自然 'pixel coordinates'

Matrix proj = Matrix::CreateScale( 2.f/float(backBufferWidth),
   -2.f/float(backBufferHeight), 1.f)
   * Matrix::CreateTranslation( -1.f, 1.f, 0.f );
m_effect->SetProjection(proj);

此矩阵的目的基本上是 'undo' SetViewport 将执行的操作,以便您可以用简单的像素坐标进行思考。 适合3D模型。

3D shapes 教程中,我介绍了基本相机模型,但我保留了 world 矩阵作为标识,因此形状位于世界原点。

m_view = Matrix::CreateLookAt(Vector3(2.f, 2.f, 2.f),
    Vector3::Zero, Vector3::UnitY);
m_proj = Matrix::CreatePerspectiveFieldOfView(XM_PI / 4.f,
    float(backBufferWidth) / float(backBufferHeight), 0.1f, 10.f);

Rendering a model tutorial, I also leave the world matrix as identity. I get into the basics of this in Basic game math 教程中。

One of the nice properties of affine transformations is that you can perform them all at once by transforming by the concatenation of the individual transforms. Point p transformed by matrix W, then transformed by matrix V, then transformed by matrix P is the same as point p transformed by matrix W * V * P.