Simple draw command not working.

Started by
3 comments, last by SteveHatcher 10 years, 3 months ago

Hi Guys,

I am working on a very primitive 'engine' that should just display a quad. The quad is its own class, and right now the program is working but just displaying the back buffers single color. I will post what I think is the relevant code and any help in identifying the problem would be greatly appreciated.

The quad 'object' is initialized in my engine::initialize routene


quad = new quad();
quad->initialize(graphics);

The quads initialization routine is:


bool quad::initialize(Graphics *g)
{
	graphics = g;                

	Vertex vtx[] =
		{
			Vertex(-0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
			Vertex(0.2f, 0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
			Vertex(-0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
			Vertex(0.2f, -0.2f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		};

	graphics->createVertexBuffer(ARRAYSIZE(vtx), vtx, vertexBuffer);
	graphics->createShaders();

	return true;
}

The graphics createVertexBuffer member function is:


HRESULT Graphics::createVertexBuffer(unsigned int numVertices, Vertex *vertexData, ID3D11Buffer* vertexBuffer)
{

	HRESULT result = E_FAIL;

	D3D11_BUFFER_DESC VertexBufferDesc;
	ZeroMemory(&VertexBufferDesc, sizeof(VertexBufferDesc));

	VertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	VertexBufferDesc.ByteWidth = sizeof(Vertex)*numVertices;
	VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	VertexBufferDesc.CPUAccessFlags = 0;
	VertexBufferDesc.MiscFlags = 0;

	D3D11_SUBRESOURCE_DATA VertexBufferData;

	ZeroMemory(&VertexBufferData, sizeof(VertexBufferData));
	VertexBufferData.pSysMem = vertexData;
	result = d3d11Device->CreateBuffer(&VertexBufferDesc, &VertexBufferData, &vertexBuffer);

	return result;
}

The graphics createShaders() function is:


HRESULT Graphics::createShaders()
{
	D3DX11CompileFromFile("Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
	D3DX11CompileFromFile("Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);

	d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
	d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

	d3d11DevCon->VSSetShader(VS, 0, 0);
	d3d11DevCon->PSSetShader(PS, 0, 0);

	return result;
}

The main loop contains the quad->draw command which is:


const void quad::draw()
{
	graphics->drawQuad(vertexBuffer); 
}

and finally drawQuad has a lot of stuff dumped in it which probably should be in other DirectX initialization areas but I have been trying everything to get it working.


bool Graphics::drawQuad(ID3D11Buffer *vertexBuffer)
{
	float bgColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; //Clear our backbuffer
	d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);

	d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	D3D11_BUFFER_DESC cbbd;
	ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));

	cbbd.Usage = D3D11_USAGE_DEFAULT;
	cbbd.ByteWidth = sizeof(cbPerObject);
	cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cbbd.CPUAccessFlags = 0;
	cbbd.MiscFlags = 0;
	d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);

	d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	UINT stride = sizeof(Vertex);
	UINT offset = 0;

	d3d11DevCon->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);

	D3D11_VIEWPORT viewport;
	ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = GAME_WIDTH;
	viewport.Height = GAME_HEIGHT;
	viewport.MinDepth = 0.0f;
	viewport.MaxDepth = 1.0f;

	d3d11Device->CreateInputLayout(layout, numElements, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &vertLayout);
	d3d11DevCon->IASetInputLayout(vertLayout);

	d3d11DevCon->RSSetViewports(1, &viewport);

	XMMATRIX WVP = XMMatrixIdentity();
	cbPerObj.WVP = XMMatrixTranspose(WVP);
	d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
	d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);

	d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
	d3d11DevCon->Draw(4, 0);

	return true;
}

and finally in the loop graphics->showBackbuffer(); is called which is simply


HRESULT Graphics::showBackbuffer()
{
	SwapChain->Present(0, 0);
	return result;
}

My base graphics.h class is set out like:


struct Vertex	//Overloaded Vertex Structure
{
	Vertex(){}
	Vertex(float x, float y, float z, float cr, float cg, float cb, float ca) : pos(x, y, z), color(cr, cg, cb, ca){}

	XMFLOAT3 pos;
	XMFLOAT4 color;
};

struct cbPerObject
{
	XMMATRIX  WVP;
};

const 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 },
};

const UINT numElements = ARRAYSIZE(layout);

class Graphics
{
private:
	//DirectX pointers
	IDXGISwapChain* SwapChain;
	ID3D11Device* d3d11Device = NULL;
	ID3D11DeviceContext* d3d11DevCon;
	ID3D11RenderTargetView* renderTargetView;
	ID3D11Texture2D* depthStencilBuffer;
	ID3D11DepthStencilView* depthStencilView;
	ID3D11Buffer* vertexBuffer;
	ID3D11Buffer* cbPerObjectBuffer;
	ID3D11VertexShader* VS;
	ID3D11PixelShader* PS;
	ID3D10Blob* VS_Buffer;
	ID3D10Blob* PS_Buffer;
	ID3D11InputLayout* vertLayout;
	ID3D11Buffer* vertexBufferInternal;

I have set the WVP to an identity matrix just because I don't want to worry about a camera class just yet. So I am setting as much to a simple value that should work as I can.

There is much more but hopefully the error lies somewhere in what I have posted. Any help is greatly appreciated.

Thanks

Advertisement
  1. I cannot see ID3D11DeviceContext::OMSetRenderTargets() call
  2. Vertex & Pixel shaders might contain problem as well, would be useful to see them
  3. Graphics::createVertexBuffer creates buffer but doesn't save it to variable like you imagine; it needs pointer reference or double pointer

Hi,

Thanks for the reply.

1. This is taken care of in the graphics::initialize section:


	ID3D11Texture2D* BackBuffer;
	SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);

	d3d11Device->CreateRenderTargetView(BackBuffer, NULL, &renderTargetView);
	BackBuffer->Release();

	d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, NULL);

2. I am quite confident the Vertex and Pixel shaders are okay because this program I am making is adapted from a no classes version that has mostly the same code with the same shaders and works.

3. I have read some things like this but don't fully understand it quite yet. I found a sample from microsoft, is this what mine should be like instead?


createVertexBuffer(unsigned int numVertices, BasicVertex *vertexData, ID3D11Buffer **vertexBuffer);

Thanks for your help.


is this what mine should be like instead?

That should do it. Does it work yet?

If not try to enable (if not yet enabled) debug layer. Debugging will cause D3D to throw messages into Visual Studio if any problems arise.

Ahh yes, thank you very much. That worked perfectly.

Could you please give me a very dumbed down explanation as to why I need to pass a pointer to a pointer? When I did not use objects, I simply needed to save it as type ID3D11Buffer*.

Thanks

This topic is closed to new replies.

Advertisement