Direct3D,部分三角形在某个角度会被掩盖。求高手帮忙。

vv_victor 2018-03-21 11:35:15
(求高手帮忙,谢谢)
我用Direct3D画出高程的图像,但是图像在旋转到一定角度的时候,某些图像会被背后的图像覆盖。如下图:
正常显示:

部分图像被覆盖:


代码:
/--------------------------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------------------------
HINSTANCE g_hInst = nullptr;
HWND g_hWnd = nullptr;
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11Device1* g_pd3dDevice1 = nullptr;
ID3D11DeviceContext* g_pImmediateContext = nullptr;
ID3D11DeviceContext1* g_pImmediateContext1 = nullptr;
IDXGISwapChain* g_pSwapChain = nullptr;
IDXGISwapChain1* g_pSwapChain1 = nullptr;
ID3D11RenderTargetView* g_pRenderTargetView = nullptr;
ID3D11Texture2D* g_pDepthStencil = nullptr;
ID3D11DepthStencilView* g_pDepthStencilView = nullptr;
ID3D11VertexShader* g_pVertexShader = nullptr;
ID3D11PixelShader* g_pPixelShader = nullptr;
ID3D11PixelShader* g_pPixelShaderSolid = nullptr;
ID3D11InputLayout* g_pVertexLayout = nullptr;
ID3D11Buffer* g_pTriangleVertexBuffer = nullptr;
ID3D11Buffer* g_pTriangleIndexBuffer = nullptr;
ID3D11Buffer* g_pTriangleIndexRvsBuffer = nullptr;
ID3D11Buffer* g_pLineVertexBuffer = nullptr;
ID3D11Buffer* g_pLineIndexBuffer = nullptr;
ID3D11Buffer* g_pConstantBuffer = nullptr;
XMMATRIX g_World;
XMMATRIX g_View;
XMMATRIX g_Projection;
ID3D11RasterizerState* g_pRasterizerState;
ID3D11RasterizerState* g_pRasterizerStateNet;
ID3D11ShaderResourceView* g_pTextureRV = nullptr;
ID3D11SamplerState* g_pSamplerLinear = nullptr;

ID3D11Buffer* g_pVertexBuffer = nullptr;
ID3D11Buffer* g_pIndexBuffer = nullptr;

HRESULT Grid3DView::InitDevice()
{
InitGridData();
//RefreshData();
HRESULT hr = S_OK;

RECT rc;
GetClientRect(g_hWnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;

UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = ARRAYSIZE(driverTypes);

D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);

for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{
g_driverType = driverTypes[driverTypeIndex];
hr = D3D11CreateDevice(nullptr, g_driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext);

if (hr == E_INVALIDARG)
{
// DirectX 11.0 platforms will not recognize D3D_FEATURE_LEVEL_11_1 so we need to retry without it
hr = D3D11CreateDevice(nullptr, g_driverType, nullptr, createDeviceFlags, &featureLevels[1], numFeatureLevels - 1,
D3D11_SDK_VERSION, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext);
}

if (SUCCEEDED(hr))
break;
}
if (FAILED(hr))
return hr;

// Obtain DXGI factory from device (since we used nullptr for pAdapter above)
IDXGIFactory1* dxgiFactory = nullptr;
{
IDXGIDevice* dxgiDevice = nullptr;
hr = g_pd3dDevice->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&dxgiDevice));
if (SUCCEEDED(hr))
{
IDXGIAdapter* adapter = nullptr;
hr = dxgiDevice->GetAdapter(&adapter);
if (SUCCEEDED(hr))
{
hr = adapter->GetParent(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&dxgiFactory));
adapter->Release();
}
dxgiDevice->Release();
}
}
if (FAILED(hr))
return hr;

// Create swap chain
IDXGIFactory2* dxgiFactory2 = nullptr;
hr = dxgiFactory->QueryInterface(__uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgiFactory2));
if (dxgiFactory2)
{
TRACE(_T("DirectX 11.1 or later\n"));
hr = g_pd3dDevice->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void**>(&g_pd3dDevice1));
if (SUCCEEDED(hr))
{
(void)g_pImmediateContext->QueryInterface(__uuidof(ID3D11DeviceContext1), reinterpret_cast<void**>(&g_pImmediateContext1));
}

DXGI_SWAP_CHAIN_DESC1 sd;
ZeroMemory(&sd, sizeof(sd));
sd.Width = width;
sd.Height = height;
sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;

hr = dxgiFactory2->CreateSwapChainForHwnd(g_pd3dDevice, g_hWnd, &sd, nullptr, nullptr, &g_pSwapChain1);
if (SUCCEEDED(hr))
{
hr = g_pSwapChain1->QueryInterface(__uuidof(IDXGISwapChain), reinterpret_cast<void**>(&g_pSwapChain));
}

dxgiFactory2->Release();
}
else
{
TRACE(_T("DirectX 11.0 systems\n"));
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;

hr = dxgiFactory->CreateSwapChain(g_pd3dDevice, &sd, &g_pSwapChain);
}

// Note this tutorial doesn't handle full-screen swapchains so we block the ALT+ENTER shortcut
dxgiFactory->MakeWindowAssociation(g_hWnd, DXGI_MWA_NO_ALT_ENTER);

dxgiFactory->Release();

if (FAILED(hr))
return hr;

// Create a render target view
ID3D11Texture2D* pBackBuffer = nullptr;
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pBackBuffer));
if (FAILED(hr))
return hr;

hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_pRenderTargetView);
pBackBuffer->Release();
if (FAILED(hr))
return hr;

// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = g_pd3dDevice->CreateTexture2D(&descDepth, nullptr, &g_pDepthStencil);
if (FAILED(hr))
return hr;

// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);
if (FAILED(hr))
return hr;


g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, nullptr);

// Setup the viewport
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)width;
vp.Height = (FLOAT)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
g_pImmediateContext->RSSetViewports(1, &vp);

// Compile the vertex shader
ID3DBlob* pVSBlob = nullptr;
hr = CompileShaderFromFile(SHADER_FILENAME, "VS", "vs_4_0", &pVSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}

// Create the vertex shader
hr = g_pd3dDevice->CreateVertexShader(pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), nullptr, &g_pVertexShader);
if (FAILED(hr))
{
pVSBlob->Release();
return hr;
}

// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE(layout);

// Create the input layout
hr = g_pd3dDevice->CreateInputLayout(layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &g_pVertexLayout);
pVSBlob->Release();
if (FAILED(hr))
return hr;

// Set the input layout
g_pImmediateContext->IASetInputLayout(g_pVertexLayout);

// Compile the pixel shader
ID3DBlob* pPSBlob = nullptr;
hr = CompileShaderFromFile(SHADER_FILENAME, "PS", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}

// Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), nullptr, &g_pPixelShader);
pPSBlob->Release();
if (FAILED(hr))
return hr;

// Compile the pixel shader
pPSBlob = nullptr;
hr = CompileShaderFromFile(SHADER_FILENAME, "PSSolid", "ps_4_0", &pPSBlob);
if (FAILED(hr))
{
MessageBox(nullptr,
L"The FX file cannot be compiled. Please run this executable from the directory that contains the FX file.", L"Error", MB_OK);
return hr;
}

// Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader(pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), nullptr, &g_pPixelShaderSolid);
pPSBlob->Release();
if (FAILED(hr))
return hr;

...全文
445 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
IONPhantom 2018-11-11
  • 打赏
  • 举报
回复
你没开 Z-enable 啊,你整个程序都没设置 Depth State,当然会这样嘛
WJN92 2018-03-28
  • 打赏
  • 举报
回复
可以具体到某一个点,选中某一个点后,就可以看到shader是怎么运行的。 不过我只用过DX12,不知道dx11能不能这样
vv_victor 2018-03-27
  • 打赏
  • 举报
回复
多谢回复,能看到哪个三角形具体没有画出来吗?抱歉我这块还在摸索中。谢谢。
WJN92 2018-03-26
  • 打赏
  • 举报
回复
要学会调试啊,visualstudio已经附带了dx的调试工具了,很简单也很强大
vv_victor 2018-03-21
  • 打赏
  • 举报
回复
//-------------------------------------------------------------------------------------- // Render a frame //-------------------------------------------------------------------------------------- void Grid3DView::Render() { // // Clear the back buffer // g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, Colors::MidnightBlue); g_pImmediateContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); { TRACE(_T("m_radRotation = #%f\n"), RAD2DEG(m_radRotation)); //m_radRotation = 90.714188; //m_radRotation = XM_PI / 2; g_World = XMMatrixIdentity(); g_World = g_World * XMMatrixRotationY(m_radRotation); g_World = g_World * XMMatrixRotationX(m_radRotationX); g_World = g_World * XMMatrixRotationZ(m_radRotationZ); RECT rc; GetClientRect(g_hWnd, &rc); UINT width = rc.right - rc.left; UINT height = rc.bottom - rc.top; g_Projection = XMMatrixPerspectiveFovLH(XM_PIDIV4 * 1.8 / m_ZoomRatio, width / (FLOAT)height, 0.000001f, 1000.0f); // Initialize the view matrix XMVECTOR Eye = XMVectorSet(0.0f + m_viewMoveWidX, 1.5f, -1.2f + m_viewMoveDepZ, 0.0f); XMVECTOR At = XMVectorSet(0.0f + m_viewMoveWidX, -0.0f, -0.0f + m_viewMoveDepZ, 0.0f); XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); //XMVECTOR Eye = XMVectorSet(0.0f, 5.0f, -10.0f, 0.0f); //XMVECTOR At = XMVectorSet(0.0f, 2.0f, 0.0f, 0.0f); //XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); g_View = XMMatrixLookAtLH(Eye, At, Up); // Setup our lighting parameters XMFLOAT4 vLightSrcDir = XMFLOAT4(0.0f, 0.577f, 0.0f, 1.0f); //XMFLOAT4 vLightSrcDir = XMFLOAT4(0.0f + m_viewMoveWidX, 1.5f, -1.2f + m_viewMoveDepZ, 1.0f); XMFLOAT4 vLightSrcColor = XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f); // Rotate the second light around the origin //XMMATRIX mRotate = XMMatrixRotationY(-2.0f * m_radRotation); //XMVECTOR vLightDir = XMLoadFloat4(&vLightSrcDir); //vLightDir = XMVector3Transform(vLightDir, mRotate); //XMStoreFloat4(&vLightDirs, vLightDir); ConstantBuffer cb; cb.mWorld = XMMatrixTranspose(g_World); cb.mView = XMMatrixTranspose(g_View); cb.mProjection = XMMatrixTranspose(g_Projection); cb.vLightDir = vLightSrcDir; cb.vLightColor = vLightSrcColor; cb.vOutputColor = XMFLOAT4(0, 1.0f, 0, 1.0f); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb, 0, 0); // // Renders a triangle // g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0); g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); //g_pImmediateContext->PSSetShader(g_pPixelShaderSolid, nullptr, 0); g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0); g_pImmediateContext->PSSetConstantBuffers(0, 1, &g_pConstantBuffer); g_pImmediateContext->PSSetSamplers(0, 1, &g_pSamplerLinear); } if (true) { if (m_bNetView) g_pImmediateContext->RSSetState(g_pRasterizerStateNet); else g_pImmediateContext->RSSetState(g_pRasterizerState); } if (true) //if (false) {///draw the target ConstructViewBuf(m_maxHeight, m_bFlatView); g_pImmediateContext->UpdateSubresource(g_pTriangleVertexBuffer, 0, nullptr, m_vertices_grid, 0, 0); // Set vertex buffer UINT stride = sizeof(SimpleVertex); UINT offset = 0; //g_pImmediateContext->PSSetShader(g_pPixelShaderSolid, nullptr, 0); // Set vetex buffer g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pTriangleVertexBuffer, &stride, &offset); // Set index buffer //g_pImmediateContext->IASetIndexBuffer(g_pTriangleIndexBuffer, DXGI_FORMAT_R16_UINT, 0); g_pImmediateContext->IASetIndexBuffer(g_pTriangleIndexRvsBuffer, DXGI_FORMAT_R16_UINT, 0); // Set primitive topology g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); g_pImmediateContext->DrawIndexed(VIEW_ANGLE_VETEXIDX_CNT, 0, 0); //g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_16_CONTROL_POINT_PATCHLIST); //g_pImmediateContext->Draw(VIEW_GRID_PINTS_WID * VIEW_GRID_PINTS_DEPTH, 0); } if (true) //if (false) {///draw the lines // Set vertex buffer UINT stride = sizeof(SimpleVertex); UINT offset = 0; // Set vetex buffer g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pLineVertexBuffer, &stride, &offset); // Set index buffer g_pImmediateContext->IASetIndexBuffer(g_pLineIndexBuffer, DXGI_FORMAT_R16_UINT, 0); // Set primitive topology g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST); g_pImmediateContext->DrawIndexed(18, 0, 0); } if (false) //if (true) {///draw the cube // Set vertex buffer UINT stride = sizeof(SimpleVertex); UINT offset = 0; // Set vetex buffer g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // Set index buffer g_pImmediateContext->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); // Set primitive topology g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); g_pImmediateContext->DrawIndexed(36, 0, 0); } // // Present our back buffer to our front buffer // g_pSwapChain->Present(0, 0); }
vv_victor 2018-03-21
  • 打赏
  • 举报
回复
D3D11_BUFFER_DESC bd; D3D11_SUBRESOURCE_DATA InitData; ZeroMemory(&bd, sizeof(bd)); ZeroMemory(&InitData, sizeof(InitData)); { bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(SimpleVertex) * MAX_LINE_IDXCNT; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = m_LineVertices; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pLineVertexBuffer); if (FAILED(hr)) return hr; bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(WORD) * MAX_LINE_IDXCNT; bd.BindFlags = D3D11_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = m_pLineIndicesList; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pLineIndexBuffer); if (FAILED(hr)) return hr; } { bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(SimpleVertex) * VIEW_GRID_PINTS_DEPTH * VIEW_GRID_PINTS_DEPTH; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = m_vertices_grid; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTriangleVertexBuffer); if (FAILED(hr)) return hr; bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = VIEW_BYTES_ANGLE_VETEXIDXBUF; bd.BindFlags = D3D11_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = m_pTriangleIndicesList; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTriangleIndexBuffer); if (FAILED(hr)) return hr; } { bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = VIEW_BYTES_ANGLE_VETEXIDXBUF; bd.BindFlags = D3D11_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = m_pTriangleIndicesRvsList; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pTriangleIndexRvsBuffer); if (FAILED(hr)) return hr; } { SimpleVertex vertices[] = { { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f) }, { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f) }, { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT4(0.0f, 1.0f, 1.0f, 1.0f) }, { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }, { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT4(1.0f, 0.0f, 1.0f, 1.0f) }, { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT4(1.0f, 1.0f, 0.0f, 1.0f) }, { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f) }, { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f) }, }; D3D11_BUFFER_DESC bd; ZeroMemory(&bd, sizeof(bd)); bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(SimpleVertex) * 8; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; D3D11_SUBRESOURCE_DATA InitData; ZeroMemory(&InitData, sizeof(InitData)); InitData.pSysMem = vertices; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer); if (FAILED(hr)) return hr; // Set vertex buffer UINT stride = sizeof(SimpleVertex); UINT offset = 0; //g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); // Create index buffer WORD indices[] = { 3, 1, 0, 2, 1, 3, 0, 5, 4, 1, 5, 0, 3, 4, 7, 0, 4, 3, 1, 6, 5, 2, 6, 1, 2, 7, 6, 3, 7, 2, 6, 4, 5, 7, 4, 6, }; bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(WORD) * 36; // 36 vertices needed for 12 triangles in a triangle list bd.BindFlags = D3D11_BIND_INDEX_BUFFER; bd.CPUAccessFlags = 0; InitData.pSysMem = indices; hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pIndexBuffer); if (FAILED(hr)) return hr; } // Set primitive topology g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // Create the constant buffer bd.Usage = D3D11_USAGE_DEFAULT; bd.ByteWidth = sizeof(ConstantBuffer); bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bd.CPUAccessFlags = 0; hr = g_pd3dDevice->CreateBuffer(&bd, nullptr, &g_pConstantBuffer); if (FAILED(hr)) return hr; // Create the sample state D3D11_SAMPLER_DESC sampDesc; ZeroMemory(&sampDesc, sizeof(sampDesc)); sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; sampDesc.MinLOD = 0; sampDesc.MaxLOD = D3D11_FLOAT32_MAX; hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &g_pSamplerLinear); if (FAILED(hr)) return hr; // Initialize the world matrix g_World = XMMatrixIdentity(); // Initialize the view matrix XMVECTOR Eye = XMVectorSet(0.0f, 3.5f, -1.2f, 0.0f); XMVECTOR At = XMVectorSet(0.0f, 0.0f, -0.0f, 0.0f); XMVECTOR Up = XMVectorSet(0.0f, 2.0f, 0.0f, 0.0f); g_View = XMMatrixLookAtLH(Eye, At, Up); // Initialize the projection matrix g_Projection = XMMatrixPerspectiveFovLH(XM_PIDIV4 * 1.5, width / (FLOAT)height, 0.0000001f, 500.0f); if (true) { D3D11_RASTERIZER_DESC rsDesc; ZeroMemory(&rsDesc, sizeof(D3D11_RASTERIZER_DESC)); rsDesc.FillMode = D3D11_FILL_SOLID; rsDesc.CullMode = D3D11_CULL_BACK; rsDesc.FrontCounterClockwise = false; //rsDesc.DepthBiasClamp rsDesc.SlopeScaledDepthBias = false; rsDesc.DepthClipEnable = true; rsDesc.ScissorEnable = false; rsDesc.MultisampleEnable = true; rsDesc.AntialiasedLineEnable = true; g_pd3dDevice->CreateRasterizerState(&rsDesc, &g_pRasterizerState); rsDesc.FillMode = D3D11_FILL_WIREFRAME; g_pd3dDevice->CreateRasterizerState(&rsDesc, &g_pRasterizerStateNet); g_pImmediateContext->RSSetState(g_pRasterizerState); } //g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); return S_OK; }

456

社区成员

发帖
与我相关
我的任务
社区描述
其它游戏引擎
社区管理员
  • 其它游戏引擎社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧