Трафарет глубины не работает — DirectX 10 Stack Overflow

У меня проблема с DirectX10 + C ++.

По сути, мы находимся на ранних стадиях рендеринга, и по какой-то причине наш шаблон глубины, похоже, не понимает нашу модель. По сути, вот все, что мы делаем:

  1. Загрузить шейдер, модель и текстуру
  2. Инициализировать DirectX
  3. Рисовать

Модель, шейдер и текстура загружаются и работают правильно, однако (как показано на скриншоте ниже) трафарет глубины явно не выполняет свою работу, а шейдер используется в неправильных местах. Я также включил наш метод инициализации на случай, если вам понадобится его выяснить. Мы считаем, что перепробовали почти все, но, зная свою удачу, мы, вероятно, пропустили 1 строку важного кода ^. ^

Мы также видели, что у кого-то еще была такая же проблема, однако их исправление не работало (их проблема заключалась в том, что они установили ближнюю плоскость отсечения на 0,0, однако у нас нет 0,0, так что это не проблема)

Заранее спасибо!

Скриншот проблемы

void GraphicsDeviceDirectX::InitGraphicsDevice(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;    // create a struct to hold various swap chain information

ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));    // clear out the struct for use

scd.BufferCount = 2;    // create two buffers, one for the front, one for the back
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
scd.BufferDesc.Height = 600;
scd.BufferDesc.Width = 600;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    // tell how the chain is to be used
scd.OutputWindow = hWnd;    // set the window to be used by Direct3D
scd.SampleDesc.Count = 1;    // set the level of multi-sampling
scd.SampleDesc.Quality = 0;    // set the quality of multi-sampling
scd.Windowed = true;    // set to windowed or full-screen mode

//set scan line ordering and scaling
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

//discard back buffer dontents
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

//dont set advanced flags
scd.Flags = 0;

// create a device class and swap chain class using the information in the scd struct
if(FAILED(D3D10CreateDeviceAndSwapChain(NULL,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
D3D10_CREATE_DEVICE_DEBUG,
D3D10_SDK_VERSION,
&scd,
&swapchain,
&device)))
{
throw EngineException("Error creating graphics device");
}

//Push graphics device to Persistant Object Manager
//PerObjMan::Push(device);
//Push swapchain to Peristant Object Manager
PerObjMan::Push(swapchain);

// get the address of the back buffer and use it to create the render target
ID3D10Texture2D* pBackBuffer;
swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);

/*D3D10_TEXTURE2D_DESC descBack;
pBackBuffer->GetDesc(&descBack);*/
pBackBuffer->Release();
pBackBuffer = NULL;

//Push graphics device to Persistant Object Manager
PerObjMan::Push(rtv);

ID3D10Texture2D* pDepthStencil = NULL;
D3D10_TEXTURE2D_DESC descDepth;

ZeroMemory(&descDepth, sizeof(descDepth));

descDepth.Width = 600;
descDepth.Height = 600;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
HRESULT hr;
hr = GetGraphicsDevice()->CreateTexture2D( &descDepth, NULL, &pDepthStencil );
if(FAILED(hr))
throw EngineException("FAIL");

PerObjMan::Push(pDepthStencil);

D3D10_DEPTH_STENCIL_DESC dsDesc;

ZeroMemory(&dsDesc, sizeof(dsDesc));
// Depth test parameters
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK::D3D10_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D10_COMPARISON_FUNC::D3D10_COMPARISON_LESS;

// Stencil test parameters
dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing.
dsDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing.
dsDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

// Create depth stencil state
hr = device->CreateDepthStencilState(&dsDesc, &dss);
if(FAILED(hr))
throw EngineException("FAIL");

// Bind depth stencil state
device->OMSetDepthStencilState(dss, 1);PerObjMan::Push(dss);

D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;

ZeroMemory(&descDSV, sizeof(descDSV));

descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION::D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;

// Create the depth stencil view
hr = device->CreateDepthStencilView( pDepthStencil, // Depth stencil texture
&descDSV, // Depth stencil desc
&dsv );  // [out] Depth stencil view

if(FAILED(hr))
throw EngineException("FAIL");

PerObjMan::Push(dsv);

// Bind the depth stencil view
device->OMSetRenderTargets( 1,          // One rendertarget view
&rtv,      // Render target view, created earlier
dsv);     // Depth stencil view for the render targetD3D10_VIEWPORT viewport;    // create a struct to hold the viewport data

ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT));    // clear out the struct for use

GameToImplement::GameInfo::Info info = GameToImplement::GameInfo::GetGameInfo();

RECT rect;
int width = 0;
int height = 0;
if(GetClientRect(hWnd, &rect))
{
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
else
{
throw EngineException("");
}

viewport.TopLeftX = 0;    // set the left to 0
viewport.TopLeftY = 0;    // set the top to 0
viewport.Width = 600;    // set the width to the window's width
viewport.Height = 600;    // set the height to the window's height
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;

device->RSSetViewports(1, &viewport);    // set the viewport

}

4

Решение

Я исправил это, благодаря кивку кошки в правильном направлении. Оказывается, я на самом деле высвобождаю состояние растеризатора слишком рано, чтобы использовать трафарет глубины.

Я оставлю этот ответ здесь для тех, у кого такая же проблема.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector