【问题标题】:Render a texture to a window将纹理渲染到窗口
【发布时间】:2011-10-14 09:40:36
【问题描述】:

我有一个对话框,基本上我想使用 DirectX 将其实现为纹理查看器。源纹理可以来自磁盘上的文件,也可以来自内存中的任意 D3D 纹理/表面。窗口可以调整大小,所以我需要能够相应地缩放其内容(保持纵横比,虽然不是必需的,但知道会很有用)。

实现上述内容的最佳方法是什么?

【问题讨论】:

    标签: c++ directx rendering textures direct3d


    【解决方案1】:

    恕我直言,最简单的方法是创建一个四边形(或两个三角形),其顶点包含正确的 UV 坐标。您设置为查看立方体坐标的 XYZ 坐标。这仅在单位矩阵设置为投影时才有效。您可以在 X 轴和 Y 轴上使用 -1 到 1。

    编辑:这里是一个例子:

    【讨论】:

    • 谢谢!你会碰巧知道我是否/在哪里可以找到有关该主题的一些示例材料吗?我对 DirectX 比较陌生。
    • 我用 DX8 教程的链接更新了我的答案。但是思路应该和DX 9, 10 11一样
    【解决方案2】:

    这是我用来为可调整大小的对话保留大小和缩放比例的代码。我的纹理保存在内存位图中。如果您没有内存位图,我相信您可以适应。重要的一点是我确定正确的缩放因子以保持任何客户区大小的纵横比的方式

    CRect destRect( 0, 0, frameRect.Width(), frameRect.Height() );
    
    
    if( txBitmapInfo.bmWidth <= frameRect.Width() && txBitmapInfo.bmHeight <= frameRect.Height() )
    {
        destRect.left   = ( frameRect.Width() - txBitmapInfo.bmWidth ) / 2;
        destRect.right  = destRect.left +  txBitmapInfo.bmWidth;
        destRect.top    = ( frameRect.Height() - txBitmapInfo.bmHeight ) / 2;
        destRect.bottom = destRect.top +  txBitmapInfo.bmHeight;
    } 
    else
    {
    double  hScale = static_cast<double>( frameRect.Width() ) / txBitmapInfo.bmWidth;
    double  vScale = static_cast<double>( frameRect.Height() ) / txBitmapInfo.bmHeight;
    
    if( hScale < vScale )
    {
        int height = static_cast<int>( frameRect.Width() * ( static_cast<double>(txBitmapInfo.bmHeight) / txBitmapInfo.bmWidth ) );
    
        destRect.top = ( frameRect.Height() - height ) / 2;
        destRect.bottom = destRect.top +  height;
    }
    else
    {
        int width = static_cast<int>( frameRect.Height() * ( static_cast<double>(txBitmapInfo.bmWidth) / txBitmapInfo.bmHeight ) );
    
        destRect.left = ( frameRect.Width() - width ) / 2;
        destRect.right = destRect.left + width;
    }
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多