Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to render a cube in c++ using DirectX 11. I managed to render it, however I am not able to transform the model to the world position with perspective projection. I followed the MSDN tutorial for this
Here's the code :
C++
{
	DirectX::XMMATRIX g_World;
	DirectX::XMMATRIX g_View;
	DirectX::XMMATRIX g_Projection;

	//D3D11_BUFFER_DESC bd;
		ZeroMemory(&bd, sizeof(bd));
		bd.Usage = D3D11_USAGE_DEFAULT;
		bd.ByteWidth = sizeof(ConstantBuffer);
		bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
		bd.CPUAccessFlags = 0;
		Device->CreateBuffer(&bd, NULL, &pConstantBuffer);

	// Initialize the world matrix
	g_World = DirectX::XMMatrixIdentity();

	// Initialize the view matrix
	DirectX::XMVECTOR Eye = DirectX::XMVectorSet(0.0f, 1.0f, -5.0f, 0.0f);
	DirectX::XMVECTOR At = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
	DirectX::XMVECTOR Up = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
	g_View = DirectX::XMMatrixLookAtLH(Eye, At, Up);

	// Initialize the projection matrix
	g_Projection = DirectX::XMMatrixPerspectiveFovLH(1.570796327f, winWidth / (FLOAT)winHeight, 0.01f, 100.0f);

	//Update variables
	ConstantBuffer cb;
		cb.mWorld = XMMatrixTranspose(g_World);
		cb.mView = XMMatrixTranspose(g_View);
		cb.mProjection = XMMatrixTranspose(g_Projection);
		DeviceContext->UpdateSubresource(pConstantBuffer, 0, NULL, &cb, 0, 0);
}



and here's the HLSL shader code :

C++
cbuffer ConstantBuffer : register(b0)
{
	matrix World;
	matrix View;
	matrix Projection;
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// VERTEX SHADER
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

float4 VShader(float4 position : POSITION) : SV_POSITION
{
	position = mul(position, World);
	position = mul(position, View);
	position = mul(position, Projection);
	return position;
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//PIXEL SHADER
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
float4 PShader(float4 position : SV_POSITION) : SV_TARGET
{
	return float4(1.f, 0.f, 0.f, 1.f);
}


What I have tried:

all the matrices that are fed to the shader are calculated correctly, as it seems, but I'm not quite sure about the shader.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900