Sign in to follow this  
Mumsfilibaba

Quad won't render

Recommended Posts

Hello, I've started to build my own engine in Direct3D 11. After reading several tutorials (and of course also write a fair bit of code) I felt ready to start developing, and I started to create a plane. So the plan was to get a single colored quad/plane to render before I started with textures, but the thing is, nothing shows up. Only a window with the color I used to clear the rendertarget with. 

 

I have tried different things but none of them works, so now when I can't come up with more solutions I write here to see if someone can help. I have done I new main.cpp where I copied all the code from the classes and methods and skipped all the error-handling code, just to make it more readble oterwise it's the same.

 

Would be great if someone could help me to get on the right track again. 

 

//Alex

#include <Engine.h>

Window* mainWindow = nullptr;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* immediateContext = nullptr;
IDXGISwapChain* swapChain = nullptr;
ID3D11RenderTargetView* renderTarget = nullptr;
ID3D11VertexShader* vertexShader = nullptr;
ID3D11PixelShader* pixelShader = nullptr;
ID3D11InputLayout* inputLayout = nullptr;
ID3D11Buffer* vBuffer = nullptr;
ID3D11Buffer* iBuffer = nullptr;

DXGI_SWAP_CHAIN_DESC swapChainDescription;

void init(HINSTANCE hInstance)
{
	mainWindow = new Window(hInstance);
	mainWindow->initialize();

	D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_DEBUG, nullptr, 0, D3D11_SDK_VERSION, &device, nullptr, &immediateContext);

	IDXGIDevice* dxgiDevice = nullptr;
	IDXGIAdapter* dxgiAdapter = nullptr;
	IDXGIFactory* dxgiFactory = nullptr;

	device->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);
	dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter);
	dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory);

	swapChainDescription.BufferCount = 1;
	swapChainDescription.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDescription.BufferDesc.Height = mainWindow->getWindowClientHeight();
	swapChainDescription.BufferDesc.Width = mainWindow->getWindowClientWidth();
	swapChainDescription.BufferDesc.RefreshRate.Numerator = 0;
	swapChainDescription.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDescription.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
	swapChainDescription.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	swapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDescription.OutputWindow = mainWindow->getWindowHandle();
	swapChainDescription.Flags = 0;
	swapChainDescription.SampleDesc.Count = 1;
	swapChainDescription.SampleDesc.Quality = 0;
	swapChainDescription.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
	swapChainDescription.Windowed = true;

	dxgiFactory->CreateSwapChain(dxgiDevice, &swapChainDescription, &swapChain);

	dxgiDevice->Release();
	dxgiDevice = nullptr;
	dxgiAdapter->Release();
	dxgiAdapter = nullptr;
	dxgiFactory->Release();
	dxgiFactory = nullptr;

	ID3D11Texture2D* backBuffer = nullptr;

	swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);

	device->CreateRenderTargetView(backBuffer, nullptr, &renderTarget);

	backBuffer->Release();
	backBuffer = nullptr;

	D3D11_VIEWPORT viewport;
	viewport.Height = swapChainDescription.BufferDesc.Height;
	viewport.Width = swapChainDescription.BufferDesc.Width;
	viewport.TopLeftX = 0.0f;
	viewport.TopLeftY = 0.0f;
	viewport.MaxDepth = 1.0f;
	viewport.MinDepth = 0.0f;

	immediateContext->OMSetRenderTargets(1, &renderTarget, nullptr);
	immediateContext->RSSetViewports(1, &viewport);

	ID3DBlob *VS, *PS;
    
	D3DCompileFromFile(L"shader.vs", nullptr, nullptr, "VShader", "vs_5_0", 0, 0, &VS, nullptr);
	D3DCompileFromFile(L"shader.ps", nullptr, nullptr, "PShader", "ps_5_0", 0, 0, &PS, nullptr);

	device->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &vertexShader);
	device->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pixelShader);

	// create the input layout object
	D3D11_INPUT_ELEMENT_DESC ied[] =
	{
		{ "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 },
	};

	device->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &inputLayout);
	immediateContext->IASetInputLayout(inputLayout);

	VS->Release();
	VS = nullptr;
	PS->Release();
	PS = nullptr;

	VERTEX vertices[4];
	UINT indices[6];

	vertices[0].color = Color::red.getXMFLOAT4();
	vertices[0].position = DirectX::XMFLOAT3(-0.5f, 0.5f, 0.0f);
	vertices[1].color = Color::red.getXMFLOAT4();
	vertices[1].position = DirectX::XMFLOAT3(0.5f, 0.5f, 0.0f);
	vertices[2].color = Color::red.getXMFLOAT4();
	vertices[2].position = DirectX::XMFLOAT3(-0.5f, -0.5f, 0.0f);
	vertices[3].color = Color::red.getXMFLOAT4();
	vertices[3].position = DirectX::XMFLOAT3(0.5f, -0.5f, 0.0f);

	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;
	indices[3] = 2;
	indices[4] = 1;
	indices[5] = 3;

	D3D11_BUFFER_DESC bd;
	RtlSecureZeroMemory(&bd, sizeof(bd));

	D3D11_SUBRESOURCE_DATA ms;

	bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	bd.ByteWidth = sizeof(VERTEX) * 4;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	bd.StructureByteStride = sizeof(VERTEX);
	bd.Usage = D3D11_USAGE_DEFAULT;

	ms.pSysMem = vertices;

	device->CreateBuffer(&bd, NULL, &vBuffer);

	bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
	bd.ByteWidth = sizeof(UINT) * 6;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	bd.StructureByteStride = 0;
	bd.Usage = D3D11_USAGE_DEFAULT;

	ms.pSysMem = indices;

	device->CreateBuffer(&bd, &ms, &iBuffer);
}

void render()
{
	immediateContext->ClearRenderTargetView(renderTarget, Color::cornFlowerBlue.get());
	
	UINT stride = sizeof(VERTEX);
	UINT offset = 0;

	immediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	immediateContext->IASetVertexBuffers(0, 1, &vBuffer, &stride, &offset);
	immediateContext->IASetIndexBuffer(iBuffer, DXGI_FORMAT_R32_UINT, 0);
	immediateContext->VSSetShader(vertexShader, 0, 0);
	immediateContext->PSSetShader(pixelShader, 0, 0);

	immediateContext->DrawIndexed(6, 0, 0);

	swapChain->Present(1, 0);
}

void release()
{
	if (mainWindow)
	{
		delete mainWindow;
		mainWindow = nullptr;
	}

	if (device)
	{
		device->Release();
		device = nullptr;
	}

	if (immediateContext)
	{
		immediateContext->Release();
		immediateContext = nullptr;
	}

	if (swapChain)
	{
		swapChain->Release();
		swapChain = nullptr;
	}

	if (vBuffer)
	{
		vBuffer->Release();
		vBuffer = nullptr;
	}

	if (iBuffer)
	{
		iBuffer->Release();
		iBuffer = nullptr;
	}

	if (inputLayout)
	{
		inputLayout->Release();
		inputLayout = nullptr;
	}

	if (vertexShader)
	{
		vertexShader->Release();
		vertexShader = nullptr;
	}

	if (pixelShader)
	{
		pixelShader->Release();
		pixelShader = nullptr;
	}

	if (renderTarget)
	{
		renderTarget->Release();
		renderTarget = nullptr;
	}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE pHInstance, LPSTR cmdLine, int cmdShow)
{
	init(hInstance);

	mainWindow->showWindow(cmdShow);

	MSG message;
	RtlSecureZeroMemory(&message, sizeof(MSG));

	while (message.message != WM_QUIT)
	{
		while (PeekMessageW(&message, 0, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&message);

			DispatchMessageW(&message);
		}

		render();
	}

	release();
	return message.wParam;
}

Share this post


Link to post
Share on other sites

You're not checking if any your Direct3D calls succeeded. They return HRESULTs for a reason. If something's not working, checking the return values is the first thing I'd do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this