   Square not showing
Okay I've managed to create a triangle within directx11 so I tried making a square but it seems that nothing is being show (http://puu.sh/5yiWg.png) Here's the code:#include "directx.h"#include <D3Dcompiler.h>struct Vertex {DirectX::XMFLOAT3 position;DirectX::XMFLOAT4 color;};Vertex triangle[] = {DirectX::XMFLOAT3( -0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f ),DirectX::XMFLOAT3( -0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),DirectX::XMFLOAT3(  0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f ),DirectX::XMFLOAT3(  0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),};Directx::Directx() : g_Swapchain(NULL), g_Device(NULL), g_Devicecontext(NULL), g_Rendertarget(NULL){}Directx::~Directx() {}bool Directx::InitializeD3D(HWND hwnd, int width, int height, bool fullscreen) { 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 = hwnd;    sd.SampleDesc.Count = 4;    sd.SampleDesc.Quality = 0;    sd.Windowed = TRUE;D3D_FEATURE_LEVEL featurelevels[] = {D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0,D3D_FEATURE_LEVEL_9_3,D3D_FEATURE_LEVEL_9_2,D3D_FEATURE_LEVEL_9_1,};int numfeaturelevels = 5;if(FAILED(D3D11CreateDeviceAndSwapChain(NULL,  D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featurelevels, numfeaturelevels, D3D11_SDK_VERSION, &sd, &g_Swapchain, &g_Device, NULL, &g_Devicecontext)))return false;ID3D11Texture2D *pBackBuffer;HRESULT hr;if(FAILED(g_Swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer)))return false;hr = g_Device->CreateRenderTargetView(pBackBuffer, NULL, &g_Rendertarget);pBackBuffer->Release();if(FAILED(hr))return false;g_Devicecontext->OMSetRenderTargets(1, &g_Rendertarget, NULL);D3D11_VIEWPORT vp;vp.Height = (FLOAT)height;vp.Width = (FLOAT)width;vp.MaxDepth = 1.0f;vp.MinDepth = 0.0f;vp.TopLeftX = 0;vp.TopLeftY = 0;g_Devicecontext->RSSetViewports(1, &vp);return true; }void Directx::Render() {float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };g_Devicecontext->ClearRenderTargetView(g_Rendertarget, ClearColor);g_Devicecontext->DrawIndexed(6, 0, 0);g_Swapchain->Present(0, 0);}void Directx::DrawTriangle() {DWORD indices[] = {0, 1, 2,0, 2, 3,};D3D11_BUFFER_DESC bd;ZeroMemory(&bd, sizeof(bd));bd.ByteWidth = sizeof(DWORD) * 2 * 3;bd.Usage = D3D11_USAGE_DEFAULT;bd.BindFlags = D3D11_BIND_INDEX_BUFFER;D3D11_SUBRESOURCE_DATA data;ZeroMemory(&data, sizeof(data));data.pSysMem = indices;bd.Usage = D3D11_USAGE_DEFAULT;bd.ByteWidth = sizeof( Vertex ) * 4;bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;bd.CPUAccessFlags = 0;bd.MiscFlags = 0;g_Device->CreateBuffer(&bd, &data, &g_buffer);g_Devicecontext->IASetIndexBuffer(g_buffer, DXGI_FORMAT_R32_UINT, 0);CHAR* pVertexShaderCode =   "void vertex_shader( in float4 posIn : POSITION, in float4 colorIn : COLOR,"   "out float4 posOut : SV_POSITION, out float4 colorOut : COLOR )"   "{ posOut = posIn;"   "  colorOut = colorIn; }";CHAR* pPixelShaderCode =    "float4 pixel_shader( float4 pos : SV_POSITION, float4 color: COLOR) : SV_TARGET"  "{ return color; }";ID3D10Blob* pVertexShaderBytecode = NULL; ID3D10Blob* pCompileErrors = NULL;D3DCompile(pVertexShaderCode, lstrlenA( pVertexShaderCode ) + 1, "vertex shader", NULL, NULL, "vertex_shader", "vs_4_0", NULL, 0, &pVertexShaderBytecode, &pCompileErrors );    if( pCompileErrors != NULL ) {    OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );  pCompileErrors->Release();  }  g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );  ID3D10Blob* pPixelShaderBytecode = NULL; D3DCompile( pPixelShaderCode, lstrlenA( pPixelShaderCode ) + 1, "pixel shader", NULL, NULL, "pixel_shader", "ps_4_0", NULL, 0, &pPixelShaderBytecode, &pCompileErrors );  if( pCompileErrors != NULL ) {   OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );    pCompileErrors->Release();   }    g_Device->CreatePixelShader( pPixelShaderBytecode->GetBufferPointer(), pPixelShaderBytecode->GetBufferSize(), NULL, &g_pPixelShader );   pPixelShaderBytecode->Release(); D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] =    {   { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },   { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }    };    UINT numElements = ARRAYSIZE( inputLayoutDesc );    g_Device->CreateInputLayout( inputLayoutDesc, numElements, pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), &g_pInputLayout );   pVertexShaderBytecode->Release();  g_Devicecontext->IASetInputLayout( g_pInputLayout );UINT stride = sizeof( Vertex ); UINT offset = 0;g_Devicecontext->IASetVertexBuffers(0, 1, &g_buffer, &stride, &offset ); g_Devicecontext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); g_Devicecontext->VSSetShader( g_pVertexShader, NULL, 0 );  g_Devicecontext->PSSetShader( g_pPixelShader, NULL, 0 );}void Directx::ShutdownDirectx() {g_Swapchain->Release();g_Device->Release();g_Devicecontext->Release();g_Rendertarget->Release();g_buffer->Release();g_pPixelShader->Release();g_pInputLayout->Release();g_pVertexShader->Release();}
Your index buffer setup code is wrong. You start to set up the index buffer description, then you seemingly change your mind and make it a vertex buffer instead. Then you try and bind that as an index buffer, which obviously fails. Look at the beginning of the DrawTriangle() function. PS: for debugging I recommend using the debug layer by enabling it in your DirectX control panel, creating a debug device in your code, and then using DebugView or the like to get the debug output. You would have gotten something descriptive like "cannot bind a buffer with D3D11_BIND_VERTEX_BUFFER as an index buffer". Trying to debug D3D errors without the debug layer is tedious at best, voodoo at worst.;Trying to debug D3D errors without the debug layer is tedious at best, voodoo at worst  So true. I wonder how one could sensibly work without (or how a voodoo puppet of my graphics card would look). This should be a pinned thread (though it's mentioned in Evil Steve's thread for D3D9 at least). Sure one can usually find everything in the docs (yes, tedious), but the debug layer not only reports problems, its verbosity sometimes even directs you at a solution, which is really a bliss. More on topic: Also get familiar with PIX or the graphics debugger early on. When everything is setup right and you still not getting the desired output it's time to use one of those to check resource contents, slot assignment, device states and debug shader code...; PS: for debugging I recommend using the debug layer by enabling it in your DirectX control panel, creating a debug device in your code, and then using DebugView or the like to get the debug output. You would have gotten something descriptive like "cannot bind a buffer with D3D11_BIND_VERTEX_BUFFER as an index buffer". Trying to debug D3D errors without the debug layer is tedious at best, voodoo at worst.  i found this link that would help to create a Debug Device http://blogs.msdn.com/b/chuckw/archive/2012/11/30/direct3d-sdk-debug-layer-tricks.aspx;  That's a more advanced setup concerning the info queue to filter the debug layer's messages (to find the needle in the haystack). Using D3D11_CREATE_DEVICE_DEBUG is usually enough.But yes, the possibility to give D3D objects a name helps navigating in PIX:   ;  Found another bug: You have threedimensional positions, but declare DXGI_FORMAT_R32G32_FLOAT in your input layout. It should be DXGI_FORMAT_R32G32B32_FLOAT.   ;The code now looks like this: #include "directx.h"#include <D3Dcompiler.h>struct Vertex {DirectX::XMFLOAT3 position;DirectX::XMFLOAT4 color;};Vertex triangle[] = {DirectX::XMFLOAT3( -0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f ),DirectX::XMFLOAT3( -0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),DirectX::XMFLOAT3(  0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f ),DirectX::XMFLOAT3(  0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),};Directx::Directx() : g_Swapchain(NULL), g_Device(NULL), g_Devicecontext(NULL), g_Rendertarget(NULL){}Directx::~Directx() {}bool Directx::InitializeD3D(HWND hwnd, int width, int height, bool fullscreen) { 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 = hwnd;    sd.SampleDesc.Count = 4;    sd.SampleDesc.Quality = 0;    sd.Windowed = TRUE;D3D_FEATURE_LEVEL featurelevels[] = {D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0,D3D_FEATURE_LEVEL_9_3,D3D_FEATURE_LEVEL_9_2,D3D_FEATURE_LEVEL_9_1,};int numfeaturelevels = 5;if(FAILED(D3D11CreateDeviceAndSwapChain(NULL,  D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featurelevels, numfeaturelevels, D3D11_SDK_VERSION, &sd, &g_Swapchain, &g_Device, NULL, &g_Devicecontext)))return false;ID3D11Texture2D *pBackBuffer;HRESULT hr;if(FAILED(g_Swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer)))return false;hr = g_Device->CreateRenderTargetView(pBackBuffer, NULL, &g_Rendertarget);pBackBuffer->Release();if(FAILED(hr))return false;g_Devicecontext->OMSetRenderTargets(1, &g_Rendertarget, NULL);D3D11_VIEWPORT vp;vp.Height = (FLOAT)height;vp.Width = (FLOAT)width;vp.MaxDepth = 1.0f;vp.MinDepth = 0.0f;vp.TopLeftX = 0;vp.TopLeftY = 0;g_Devicecontext->RSSetViewports(1, &vp);return true; }void Directx::Render() {float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };g_Devicecontext->ClearRenderTargetView(g_Rendertarget, ClearColor);g_Devicecontext->DrawIndexed(6, 0, 0);g_Swapchain->Present(0, 0);}void Directx::DrawTriangle() {DWORD indices[] = {0, 1, 2,0, 2, 3,};D3D11_BUFFER_DESC bd;ZeroMemory(&bd, sizeof(bd));bd.ByteWidth = sizeof(DWORD) * 2 * 3;bd.Usage = D3D11_USAGE_DEFAULT;bd.BindFlags = D3D11_BIND_INDEX_BUFFER;bd.CPUAccessFlags = 0;bd.MiscFlags = 0;D3D11_SUBRESOURCE_DATA data;ZeroMemory(&data, sizeof(data));data.pSysMem = indices;g_Device->CreateBuffer(&bd, &data, &g_buffer);g_Devicecontext->IASetIndexBuffer(g_buffer, DXGI_FORMAT_R32_UINT, 0);CHAR* pVertexShaderCode =   "void vertex_shader( in float4 posIn : POSITION, in float4 colorIn : COLOR,"   "out float4 posOut : SV_POSITION, out float4 colorOut : COLOR )"   "{ posOut = posIn;"   "  colorOut = colorIn; }";CHAR* pPixelShaderCode =    "float4 pixel_shader( float4 pos : SV_POSITION, float4 color: COLOR) : SV_TARGET"  "{ return color; }";ID3D10Blob* pVertexShaderBytecode = NULL; ID3D10Blob* pCompileErrors = NULL;D3DCompile(pVertexShaderCode, lstrlenA( pVertexShaderCode ) + 1, "vertex shader", NULL, NULL, "vertex_shader", "vs_4_0", NULL, 0, &pVertexShaderBytecode, &pCompileErrors );    if( pCompileErrors != NULL ) {    OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );  pCompileErrors->Release();  }  g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );  ID3D10Blob* pPixelShaderBytecode = NULL; D3DCompile( pPixelShaderCode, lstrlenA( pPixelShaderCode ) + 1, "pixel shader", NULL, NULL, "pixel_shader", "ps_4_0", NULL, 0, &pPixelShaderBytecode, &pCompileErrors );  if( pCompileErrors != NULL ) {   OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );    pCompileErrors->Release();   }    g_Device->CreatePixelShader( pPixelShaderBytecode->GetBufferPointer(), pPixelShaderBytecode->GetBufferSize(), NULL, &g_pPixelShader );   pPixelShaderBytecode->Release(); D3D11_INPUT_ELEMENT_DESC inputLayoutDesc[] =    {   { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },   { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }    };    UINT numElements = ARRAYSIZE( inputLayoutDesc );    g_Device->CreateInputLayout( inputLayoutDesc, numElements, pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), &g_pInputLayout );   pVertexShaderBytecode->Release();  g_Devicecontext->IASetInputLayout( g_pInputLayout );UINT stride = sizeof( Vertex ); UINT offset = 0;g_Devicecontext->IASetVertexBuffers(0, 1, &g_buffer, &stride, &offset ); g_Devicecontext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); g_Devicecontext->VSSetShader( g_pVertexShader, NULL, 0 );  g_Devicecontext->PSSetShader( g_pPixelShader, NULL, 0 );}void Directx::ShutdownDirectx() {g_Swapchain->Release();g_Device->Release();g_Devicecontext->Release();g_Rendertarget->Release();g_buffer->Release();g_pPixelShader->Release();g_pInputLayout->Release();g_pVertexShader->Release();}But still doesn't display anything. I'll try the debug layer now.;Sorry for the long reply, I've been busy. run the program in debug and got this: D3D11: ERROR: ID3D11DeviceContext::IASetVertexBuffers: A Buffer trying to be bound to slot 0 did not have the appropriate bind flag set at creation time to allow the Buffer to be bound as a VertexBuffer. [ STATE_SETTING ERROR #238: IASETVERTEXBUFFERS_INVALIDBUFFER ] so it points to this area:D3D11_BUFFER_DESC bd;ZeroMemory(&bd, sizeof(bd));bd.ByteWidth = sizeof(DWORD) * 2 * 3;bd.Usage = D3D11_USAGE_DEFAULT;bd.BindFlags = D3D11_BIND_INDEX_BUFFER;bd.CPUAccessFlags = 0;bd.MiscFlags = 0;D3D11_SUBRESOURCE_DATA data;ZeroMemory(&data, sizeof(data));data.pSysMem = indices;g_Device->CreateBuffer(&bd, &data, &g_buffer);g_Devicecontext->IASetIndexBuffer(g_buffer, DXGI_FORMAT_R32_UINT, 0);;No - look at the error description, it's telling you that you're trying to set as a vertex buffer a buffer that hasn't been set as one. So locate your SetVertexBuffers call:g_Devicecontext->IASetVertexBuffers(0, 1, &g_buffer, &stride, &offset );And sure enough, you're reusing g_buffer, which you have been working with as an index buffer so far. The actual vertex data is in the "triangle" buffer, which you are never actually using. You need to create another D3D buffer with vertex buffer bind flags, put your triangle vertices in it, and then set that as a vertex buffer. Then you will draw 6 indexed vertices, which means the device will read the first 6 elements in your index buffer, fetch the corresponding vertices in the vertex buffer, and then draw these. I highly suggest you comment your code, at least to say what each block of code is supposed to do, so you can keep track of the logical flow of the program more easily. Also maybe name your variables more explicitly: "g_buffer" is not very descriptive, I had to read through the DrawTriangle function at least twice to work out what each variable was supposed to be - "index_buffer" and "vertex_buffer" would be preferable, no?;Thanks for the reply! You gave me some insight as I'm not sure I understood what I was doing. I'm now going to do what you suggested and post back with the results.