【问题标题】:C++/Cx can't define vertex struct in DirectXBase class, could do it in child classesC++/Cx 不能在 DirectXBase 类中定义顶点结构,可以在子类中定义
【发布时间】:2013-10-26 13:44:06
【问题描述】:

这是我的 DirectXBase 类头:

#pragma once

#include "pch.h"

struct Vertex
{   
XMFLOAT3 pos;
XMFLOAT3 color;
};

struct ModelViewProjectionConstantBuffer
{
DirectX::XMFLOAT4X4 model;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
};

ref class DirectXBase abstract
{
internal:
DirectXBase();

virtual void Initialize(
    Windows::UI::Core::CoreWindow^ window,
    Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ swapChainPanel,
    float dpi);

virtual void HandleDeviceLost();
virtual void CreateDeviceIndependentResources();
virtual void CreateDeviceResources();
virtual void SetDpi(float dpi);
virtual void UpdateForWindowSizeChanged();
virtual void CreateWindowSizeDependentResources();
virtual void Render() = 0;
virtual void Present();
void ValidateDevice();  
virtual float ConvertDipsToPixels(float dips);

protected private:
Windows::UI::Core::CoreWindow^ m_window;
Windows::UI::Xaml::Controls::SwapChainBackgroundPanel^ m_swapChainPanel;

// Transforms used for display orientation.
D2D1::Matrix3x2F                                m_rotationTransform2D;
DirectX::XMFLOAT4X4                             m_rotationTransform3D;
DXGI_MODE_ROTATION ComputeDisplayRotation();

//Directx Core Objects
Microsoft::WRL::ComPtr<ID3D11Device1>           m_d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext1>    m_d3dContext;
Microsoft::WRL::ComPtr<IDXGISwapChain1>         m_swapChain;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView>  m_d3dRenderTargetView;

// Cached renderer properties.
D3D_FEATURE_LEVEL                               m_featureLevel;
Windows::Foundation::Size                       m_renderTargetSize;
Windows::Foundation::Rect                       m_windowBounds;
float                                           m_dpi;
Windows::Graphics::Display::DisplayOrientations m_orientation;
bool                                            m_stereoEnabled;

// Direct3D Rendering Objects. Required for 3D.
Microsoft::WRL::ComPtr<ID3D11DepthStencilView>  m_d3dDepthStencilView;

// Transform used for display orientation.
DirectX::XMFLOAT4X4 m_orientationTransform3D;
};

我收到以下错误消息:

syntax error : missing ';' before identifier 'pos'

我不知道为什么它不能识别我的结构。有趣的是,我只是从继承自该类的 CubeRenderer 类的标头中复制并粘贴了它,这样我就可以从任何继承类访问这些结构……代码运行在大约五分钟前复制。

如果有帮助,这里是我的两个子类头文件,其中的结构已移至这些头文件,可以正常编译和运行。

CubeRenderer.h:

#pragma once

#include "DirectXBase.h"
#include <DirectXMath.h>

using namespace DirectX;

#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

struct CubeVertex
{   
XMFLOAT3 pos;
XMFLOAT3 color;
};

struct CubeMVPBuffer
{
DirectX::XMFLOAT4X4 model;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
};

ref class CubeRenderer : public DirectXBase
{
internal:
CubeRenderer();

virtual void CreateDeviceResources() override;
virtual void CreateWindowSizeDependentResources() override;
virtual void Render() override;
void Update(float timeTotal, float timeDelta);
private:
bool m_loadingComplete;

Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_constantBuffer;

uint32 m_indexCount;
CubeMVPBuffer m_constantBufferData;
};

HillRenderer.h:

#pragma once

#include "DirectXBase.h"
#include <DirectXMath.h>

using namespace DirectX;

#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

struct HillVertex
{   
    XMFLOAT3 pos;
    XMFLOAT3 color;
};

struct HillMVPBuffer
{
    DirectX::XMFLOAT4X4 model;
    DirectX::XMFLOAT4X4 view;
    DirectX::XMFLOAT4X4 projection;
};

ref class HillRenderer : public DirectXBase
{
internal:
    HillRenderer();

    virtual void CreateDeviceResources() override;
    virtual void CreateWindowSizeDependentResources() override;
    virtual void Render() override;
    void Update(float timeTotal, float timeDelta);
private:
    bool m_loadingComplete;

    Microsoft::WRL::ComPtr<ID3D11InputLayout> m_inputLayout;
    Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;
    Microsoft::WRL::ComPtr<ID3D11Buffer> m_indexBuffer;
    Microsoft::WRL::ComPtr<ID3D11VertexShader> m_vertexShader;
    Microsoft::WRL::ComPtr<ID3D11PixelShader> m_pixelShader;
    Microsoft::WRL::ComPtr<ID3D11Buffer> m_constantBuffer;

    uint32 m_indexCount;
    HillMVPBuffer m_constantBufferData;

    float GetHeight(float x, float z) const
    {
        return 0.3f*(z*sinf(0.1f*x) + x*cosf(0.1f*z));
    }
};

【问题讨论】:

    标签: struct directx c++-cx


    【解决方案1】:

    您的结构类型不“工作”是因为当您声明该结构时,编译器(尚)不知道XMFLOAT3 的含义。

    在您可以使用XMFLOAT3 类型之前,必须在某处声明它。我的猜测是它在DirectXBase.h 开头的DirectXMath.h #include DirectXMath.h 内声明,它应该可以工作。

    【讨论】:

    • 废话,不敢相信我盯着它看了那么久却错过了。谢谢你的额外眼睛。
    • 这是因为您在标题中使用了using namespace DirectX;!永远不要在标头中使用 using 命名空间,顺便说一下,这在 cpp 中也不是一个好习惯。
    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 2015-12-18
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多