我刚刚开始学习 D3D,我的代码运行良好,直到我在我的代码中实现了 D3D Shader Compiler Stuff。
我正在使用DirectXTutorials上的教程。如果我只是将代码从那里复制粘贴到一个新项目中,则程序编译得很好。
但是,与教程不同,我将代码放在不同的类中。当我尝试编译我的说法时,它给了我错误:语法错误:“TextMetrica”(编译 Direct3DRenderer.cpp)。
这是 Direct3DRenderer 文件:
#include "Window.h"
#include "Direct3DRenderer.h"
#include "Vertex.h"
Renderer::Renderer(HWND hw)
{
OutputDebugString("Direct3D Initializing\n");
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC)); // ZERO OUT SCD
scd.BufferCount = 1; // HOW MANY BACKBUFFERS WE WANT
scd.OutputWindow = hw; // HANDLE TO THE OUTPUT WINDOW
scd.Windowed = true; // SHOULD WINDOW BE IN WINDOWED MODE BY DEFAULT
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // BUFFER FORMAT
scd.BufferDesc.Width = 800; // BUFFER WIDTH
scd.BufferDesc.Height = 600; // BUFFER HEIGHT
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // USE SWAP CHAIN AS OUTPUT TARGET
scd.SampleDesc.Count = 4; // MSAA COUNT
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // FLAGS
if (D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapchain,
&dev,
NULL,
&context
) == SEVERITY_SUCCESS)
{
OutputDebugString("SUCCESS\n");
// Get The Address of BackBuffer
ID3D11Texture2D* pbuffer;
swapchain->GetBuffer(0, _uuidof(ID3D11Texture2D), (LPVOID*)& pbuffer);
// Create a Render Target COM Object from the buffer
dev->CreateRenderTargetView(pbuffer, NULL, &RenderTarget);
pbuffer->Release();
// Set Our RenderTarget as the back buffer
context->OMSetRenderTargets(1, &RenderTarget, NULL);
// Create Our Viewport
viewport.Height = 800;
viewport.Width = 600;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
context->RSSetViewports(1, &viewport);
InitPipeline();
InitGraphics();
}
else
{
OutputDebugString("ERROR\n");
}
}
Renderer::~Renderer()
{
OutputDebugString("Direct3D Cleanup Phase Started.\n");
swapchain->SetFullscreenState(FALSE, NULL);
swapchain->Release();
context->Release();
RenderTarget->Release();
VS->Release();
PS->Release();
dev->Release();
OutputDebugString("Direct3D Cleanup Phase Completed.\n");
}
void Renderer::InitPipeline()
{
// Compile Shaders from file
D3DX11CompileFromFile("shaders.shader", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &compiled_vs, 0, 0);
D3DX11CompileFromFile("shaders.shader", 0, 0, "PShader", "ps_4_0", 0, 0, 0, &compiled_ps, 0, 0);
// Convert Compiled Shaders to COM Shader Objects
dev->CreateVertexShader(compiled_vs->GetBufferPointer(), compiled_vs->GetBufferSize(), NULL, &VS);
dev->CreatePixelShader(compiled_ps->GetBufferPointer(), compiled_ps->GetBufferSize(), NULL, &PS);
// Sets the shaders to the device / Activates the shader
context->VSSetShader(VS, 0, 0);
context->PSSetShader(PS, 0, 0);
// Create the Input Layout
D3D11_INPUT_ELEMENT_DESC VertexElementDesc[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0}
};
dev->CreateInputLayout(VertexElementDesc, 2, compiled_vs->GetBufferPointer(), compiled_vs->GetBufferSize(), &InputLayout);
context->IASetInputLayout(InputLayout);
}
void Renderer::InitGraphics() {
// Create Buffer so we can duplicate data from system memory to graphics memory
ZeroMemory(&VBufferDesc, sizeof(VBufferDesc));
VBufferDesc.ByteWidth = sizeof(Vertex) * 3;
VBufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
VBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
VBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
dev->CreateBuffer(&VBufferDesc, NULL, &VBuffer);
Vertex OurVertices[] =
{
{0.0f, 0.5f, 0.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)},
{0.45f, -0.5, 0.0f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f)},
{-0.45f, -0.5f, 0.0f, D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f)}
};
// we need to map to avoid issues
D3D11_MAPPED_SUBRESOURCE mapRes;
context->Map(VBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &mapRes);
memcpy(mapRes.pData, OurVertices, sizeof(OurVertices));
context->Unmap(VBuffer, NULL);
}
void Renderer::RenderFrame()
{
context->ClearRenderTargetView(RenderTarget, D3DXCOLOR(0.2, 0.4, 0.6, 1.0));
// We can do the rendering here
UINT stride = sizeof(Vertex);
UINT offset = 0;
context->IASetVertexBuffers(0, 1, &VBuffer, &stride, &offset);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
context->Draw(3, 0);
// Swap Buffer
swapchain->Present(0, 0);
}
TEXTMETRICA 标识符错误实际上是在 D3DX10Core.h 中。我已经查看了定义,该文件似乎给了我错误。这个标识符的定义应该在 gdi 文件中。
我已将包含路径和 lib 路径添加到 directx(2010 年 6 月)SDK,并尝试在项目对所有配置设置的附加依赖项中指定 d3dx10.lib、d3dx11.lib、d3d11.lib。我是新手,所以我不知道我做错了什么。如果需要更多代码,请对此发表评论。