Skip to main content
GameDev.net gamedev.net
🔒 Locked

Trying to set a constant buffer, ortho projection (Solved)

Started by JWColeman Aug 27, 2018 at 5:13 PM 0 replies 1.1k views
Original Post
JWColeman
JWColeman

Hi all, I ended up figuring out how to accomplish this!


So, I had to set a constant buffer, I kept everything very simple.. This is my C++ representation of my Shader cbuffer


	struct ConstantBuffer
	{
		D3DXMATRIX projection;
	};

And here is my pointer to the constant buffer, as well as the matrix I will create using directx:


ID3D11Buffer *pCBuffer;				   // the constant buffer
D3DXMATRIX orthoMatrix;


Here is the shader cbuffer:


cbuffer ConstantBuffer : register(b0)
{
	matrix projection;
}


Now, I had to map this stuff, this tutorial here was extremely helpful: https://docs.microsoft.com/en-us/windows/desktop/direct3d11/overviews-direct3d-11-resources-buffers-constant-how-to

I was able to translate their example into my own code:


	ConstantBuffer cbuffer;
	D3DXMatrixOrthoLH(&orthoMatrix, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 1);
	cbuffer.projection = orthoMatrix;

	// Fill in a buffer description.
	D3D11_BUFFER_DESC cbDesc;
	cbDesc.ByteWidth = sizeof(ConstantBuffer);
	cbDesc.Usage = D3D11_USAGE_DYNAMIC;
	cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	cbDesc.MiscFlags = 0;
	cbDesc.StructureByteStride = 0;


	// Fill in the subresource data.
	D3D11_SUBRESOURCE_DATA InitData;
	InitData.pSysMem = &cbuffer
	InitData.SysMemPitch = 0;
	InitData.SysMemSlicePitch = 0;

	dev->CreateBuffer(&cbDesc, &InitData, &pCBuffer);
	devcon->VSSetConstantBuffers(0, 1, &pCBuffer);

Then, my updated shader code to apply the transformation:


VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
	VOut output;

	output.position = mul(position, projection);
	output.color = color;

	return output;
}


Now, instead of using relative coordinates for my triangle vertices, I use some pixel coordinates:


	// create a triangle using the VERTEX struct
	Vertex OurVertices[] =
	{
		{ D3DXVECTOR2(0, 100), D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
		{ D3DXVECTOR2(100, -100), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) },
		{ D3DXVECTOR2(-100, -100), D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) }
	};

image.thumb.png.622af00bf5835cfd61bca25ab2893acb.png


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.