Click here to Skip to main content
14,028,787 members
Rate this:
 
Please Sign up or sign in to vote.
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 :
{
	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 :

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190419.4 | Last Updated 23 Mar 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100