• Advertisement
Sign in to follow this  

Unable to draw triangle...

This topic is 2056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone.

I've been stuck at this nonsense error for a week now and I can't seem to find the problem. Okay I simplyfied it, because I can't draw even a triangle. Must be some initialization problem but I can't find it even with compare tools. In my other project I could succesfully render my scenes.
Here're the correspoding code snipets:
[CODE]
bool DXManager::Initialize(const HINSTANCE hInstance, const HWND hWnd)
{
m_hInstance = hInstance;
m_hWnd = hWnd;
assert(DriverTypes);
assert(DriverTypes_COUNT);
assert(FeatureLevels);
assert(FeatureLevels_COUNT);
RECT ClientRect;
ZeroMemory(&ClientRect, sizeof(RECT));
GetClientRect(m_hWnd, &ClientRect);
UINT ClientWidth = ClientRect.right - ClientRect.left;
UINT ClientHeight = ClientRect.bottom - ClientRect.top;
DXGI_SWAP_CHAIN_DESC SwapChain_DESCRIPTOR;
ZeroMemory(&SwapChain_DESCRIPTOR, sizeof(DXGI_SWAP_CHAIN_DESC));
SwapChain_DESCRIPTOR.BufferCount = 1;
SwapChain_DESCRIPTOR.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
SwapChain_DESCRIPTOR.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SwapChain_DESCRIPTOR.BufferDesc.Width = ClientWidth;
SwapChain_DESCRIPTOR.BufferDesc.Height = ClientHeight;
SwapChain_DESCRIPTOR.BufferDesc.RefreshRate.Numerator = 60;
SwapChain_DESCRIPTOR.BufferDesc.RefreshRate.Denominator = 1;
SwapChain_DESCRIPTOR.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
SwapChain_DESCRIPTOR.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
SwapChain_DESCRIPTOR.Flags = 0;
SwapChain_DESCRIPTOR.OutputWindow = m_hWnd;
SwapChain_DESCRIPTOR.SampleDesc.Count = 1;
SwapChain_DESCRIPTOR.SampleDesc.Quality = 0;
SwapChain_DESCRIPTOR.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
SwapChain_DESCRIPTOR.Windowed = true;
HRESULT result = E_FAIL;
for (UINT Driver_INDEX = 0; Driver_INDEX < DriverTypes_COUNT; ++Driver_INDEX)
{
result = D3D11CreateDeviceAndSwapChain( 0,
DriverTypes[Driver_INDEX],
0,
D3D11_CREATE_DEVICE_DEBUG,
FeatureLevels,
FeatureLevels_COUNT,
D3D11_SDK_VERSION,
&SwapChain_DESCRIPTOR,
&m_SwapChain,
&m_Device,
&m_FeatureLevel,
&m_Context);
if (SUCCEEDED(result))
{
m_DriverType = DriverTypes[Driver_INDEX];
break;
}
}
if (FAILED(result))
{
const WCHAR* ErrorString = DXGetErrorStringW(result);
const WCHAR* ErrorDescription = DXGetErrorDescriptionW(result);
MessageBox(m_hWnd, ErrorDescription, ErrorString, MB_ICONERROR | MB_OK);
return false;
}
result = E_FAIL;
ID3D11Texture2D* BackBufferPTR;
result = m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast<LPVOID*>(&BackBufferPTR));
if (FAILED(result))
{
if (BackBufferPTR != 0)
BackBufferPTR->Release();
DXTRACE_MSG(L"Failed to retrieve backbuffer!");
return false;
}

result = E_FAIL;
result = m_Device->CreateRenderTargetView(BackBufferPTR, 0, &m_RenderTargetView);
if (BackBufferPTR)
BackBufferPTR->Release();
if (FAILED(result))
{
DXTRACE_MSG(L"Failed to create RenderTargetView!");
return false;
}
D3D11_VIEWPORT Viewport;
ZeroMemory(&Viewport, sizeof(D3D11_VIEWPORT));
Viewport.Width = static_cast<float>(ClientWidth);
Viewport.Height = static_cast<float>(ClientHeight);
Viewport.TopLeftX = 0.0f;
Viewport.TopLeftY = 0.0f;
Viewport.MinDepth = 0.0f;
Viewport.MaxDepth = 1.0f;
m_Context->RSSetViewports(1, &Viewport);
return true;
}
[/CODE]

And the triangle file:
[CODE]
bool Triangle::LoadContent(void)
{
bool CompilationResult = false;
HRESULT Result = E_FAIL;

// Vertex Shader
ID3DBlob* vsBuffer = 0;
CompilationResult = CompileShaderFromFile(L"./Shaders/BasicHLSL.fx", "VS_Main", "vs_4_0", &vsBuffer);
if (!CompilationResult)
return false;
Result = m_Device->CreateVertexShader( vsBuffer->GetBufferPointer(),
vsBuffer->GetBufferSize(),
0,
&m_VertexShader);
if (FAILED(Result))
{
DXTRACE_ERR_MSGBOX(L"Error loading vertex shader", Result);
if (vsBuffer != 0)
vsBuffer->Release();

return false;
}
// Vertex Shader END
Result = m_Device->CreateInputLayout( SimpleVertex_InputLayout,
1,
vsBuffer->GetBufferPointer(),
vsBuffer->GetBufferSize(),
&m_InputLayout);
if (FAILED(Result))
{
DXTRACE_ERR_MSGBOX(L"Error while creating input layout", Result);
vsBuffer->Release();
return false;
}
if (vsBuffer != 0)
vsBuffer->Release();
vsBuffer = 0;
// PIXEL SHADER
ID3DBlob* psBuffer = 0;
CompilationResult = CompileShaderFromFile(L"./Shaders/BasicHLSL.fx", "PS_Main", "ps_4_0", &psBuffer);
if (!CompilationResult)
return false;
Result = E_FAIL;
Result = m_Device->CreatePixelShader( psBuffer->GetBufferPointer(),
psBuffer->GetBufferSize(),
0,
&m_PixelShader);
if (FAILED(Result))
{
DXTRACE_ERR_MSGBOX(L"Error loading pixel shader", Result);
psBuffer->Release();
return false;
}
if (psBuffer != 0)
psBuffer->Release();
psBuffer = 0;
// PIXEL SHADER END
// Load Vertices to array
SimpleVertex Vertices[] =
{
XMFLOAT3( 0.0f, 0.5f, -0.25f),
XMFLOAT3( 0.5f,-0.5f, -0.25f),
XMFLOAT3(-0.5f,-0.5f, -0.25f)
};
D3D11_BUFFER_DESC VertexBuffer_DESCRIPTOR;
ZeroMemory(&VertexBuffer_DESCRIPTOR, sizeof(D3D11_BUFFER_DESC));
VertexBuffer_DESCRIPTOR.Usage = D3D11_USAGE_DEFAULT;
VertexBuffer_DESCRIPTOR.BindFlags = D3D11_BIND_VERTEX_BUFFER;
VertexBuffer_DESCRIPTOR.ByteWidth = sizeof(SimpleVertex) * 3;

// setup vertex data
D3D11_SUBRESOURCE_DATA InitialData;
ZeroMemory(&InitialData, sizeof(D3D11_SUBRESOURCE_DATA));
InitialData.pSysMem = Vertices;
Result = m_Device->CreateBuffer(&VertexBuffer_DESCRIPTOR, &InitialData, &m_VertexBuffer);
if (FAILED(Result))
return false;
// TODO
// FIX: - causes assertion!
// delete/[]/ Vertices;
return true;
}
void Triangle::Render(void)
{
assert(m_Context);
float BgColor[4] = { 0.0f, 0.0f, 0.15f, 1.0f };
m_Context->ClearRenderTargetView(m_RenderTargetView, BgColor);
UINT Stride = sizeof(SimpleVertex);
UINT Offset = 0;
assert(m_InputLayout);
assert(m_VertexBuffer);
m_Context->IASetInputLayout(m_InputLayout);
m_Context->IASetVertexBuffers(0, 1, &m_VertexBuffer, &Stride, &Offset);
m_Context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
assert(m_VertexShader);
assert(m_PixelShader);
m_Context->VSSetShader(m_VertexShader, 0, 0);
m_Context->PSSetShader(m_PixelShader, 0, 0);
m_Context->Draw(3, 0);
assert(m_SwapChain);
m_SwapChain->Present(0, 0);
}
[/CODE]

Could someone tell me what am I missing? HLSL in this simple case:
[CODE]
float4 VS_Main(float4 Position : POSITION) : SV_POSITION
{
return Position;
}
float4 PS_Main(float4 Position : SV_POSITION) : SV_TARGET
{
return float4( 1.0f, 1.0f, 1.0f, 1.0f);
}
[/CODE]

Only the blank d3d client area is visible but no triangle. Vertex buffers are loaded and the fx file is compiled so I have no idea where is that error.

Share this post


Link to post
Share on other sites
Advertisement
Your vertex positions have a negative z value, which will cause the triangle to get clipped. The visible range is [-1, 1] for X and Y and [0, 1] for Z. Edited by MJP

Share this post


Link to post
Share on other sites
Ah thanks, but changing those to positive 0.25 doesn't change anything. Only the blue d3d client area is visible but the triangle is nowhere.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement