【问题标题】:No suitable constructor exists to convert from "float" to "_D3DCOLORVALUE"不存在合适的构造函数来将“float”转换为“_D3DCOLORVALUE”
【发布时间】:2020-06-24 06:26:44
【问题描述】:

我目前正在尝试在 Visual Studio 2017 中使用 C++ 学习 DirectX 11,但遇到了问题。

#include <Windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3d12.h>
#include <d3dcompiler.h>

// include D3D Librararies
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3d12.lib")
#pragma comment (lib, "D3DCompiler.lib")

// Definitions
#define SCREEN_WIDTH    800
#define SCREEN_HEIGHT   600

// Global Declarations
IDXGISwapChain *swapchain;              // Pointer to Swap Chain Buffer
ID3D11DeviceContext *devcon;            // Pointer to managing Device for GPU
ID3D11Device *dev;                      // Pointer to GPU
ID3D11RenderTargetView *backBuffer;     // A pointer to an object that holds info about the render target
ID3D11VertexShader *pVS;                // The Vertex shader
ID3D11PixelShader *pPS;                 // The Pixel Shader


struct VERTEX {
   FLOAT X, Y, Z;           // Position
   D3DCOLORVALUE Color; // Color
};

// float color2[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
VERTEX OurVertex = { 0.0f, 0.5f, 0.0f,  D3DCOLORVALUE(1.0f, 0.0f, 0.0f, 1.0f)}; // No suitable constructor exists to convert from "float" to "_D3DCOLORVALUE"

OurVertex 内部存在错误,C++ 似乎无法将 D3DCOLORVALUE() 中的浮点数转换为 D3DCOLORVALUE 项。我尝试查看其他解决方案,但找不到任何解决方案。

有解决办法吗?

【问题讨论】:

  • VERTEX OurVertex = {0.0f, 0.5f, 0.0f, D3DCOLORVALUE{1.0f, 0.0f, 0.0f, 1.0f}}; 呢?
  • 谢谢你,成功了。因此,大括号用于 D3DCOLORVALUE。我会记住这一点。
  • 检查文档,_D3DCOLORVALUEstruct,所以你可能需要像D3DCOLORVALUE{1.0f, 0.0f, 0.0f, 1.0f} 那样做

标签: c++ visual-studio-2017 directx directx-11 directx-12


【解决方案1】:

D3DCOLORVALUE 没有构造函数占用 4 个floats。你应该使用list initialization:

VERTEX OurVertex = {0.0f, 0.5f, 0.0f, D3DCOLORVALUE{1.0f, 0.0f, 0.0f, 1.0f}}; 

或者更简单:

VERTEX OurVertex = {0.0f, 0.5f, 0.0f,  {1.0f, 0.0f, 0.0f, 1.0f}};

【讨论】:

    【解决方案2】:

    @TedLyngmo Ted 基本上都说了你应该怎么做。

    但是根据您之前的评论,

    花括号用于 D3DCOLORVALUE。我会记住这一点

    您似乎认为这是一个特例。相反,理解的方式应该是您是在调用函数还是在创建对象。

    在现代 C++ 中,大多数情况下首选列表初始化。检查这个问题:Why is list initialization (using curly braces) better than the alternatives?

    基本上,您通常可以使用myClass objectName { myValues }; 来声明一个对象,或者如果您要创建一个临时对象,则使用myClass { myValues };。这样可以为函数调用或带有std::initializer_list ctors 的对象保留括号。

    【讨论】:

      【解决方案3】:

      请记住,D3DCOLORVALUE 是专门的 Direct3D 9 构造。许多人会为每个顶点的颜色使用四个浮点数(至少在开始时),但如果您想使用压缩颜色格式,DirectX::PackedVector 命名空间中的DirectXMath 类型XMCOLOR 匹配旧的D3DCOLORVALUE

      对于采用四个浮点数的数组作为颜色的 Direct3D 11 函数(例如 ClearRenderTargetView,DirectXMath 还在 DirectXColors.h 标头中定义了许多颜色常量。

      由于您是 DirectX 11 的新手,您应该看看 DirectX Tool Kit

      【讨论】:

        猜你喜欢
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多