Jump to content
  • Advertisement
Sign in to follow this  
Cranberry

DX11 Problem with displaying Cube in DX11

This topic is 1815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Guys,

I'm developing a game using DirectX 11, so I started of by creating the basic structure and tried to render a simple cube.

The problem is that the cube is somehow stretched vertically over the window and the colors (red for top / blue for bottom) are mixed:

f8a6869a5c.jpg

 

I checked everything via the Graphics Debugger and all the values seem to be okay, but the cube already looks strange in the vertex shader:

f4d2aa0605.jpg

 

I thought there might be a problem with the projection matrix but it looks okay to me:

m_ProjectionMatrix = XMMatrixPerspectiveFovLH(0.25f*3.14159265359, (float)m_pWindow->GetHeight() / (float)m_pWindow->GetWidth(), 1.0f, 1000.0f);

Has anyone had the same problem or does anybody know how I could solve this problem?

Thanks in advance,

Cranberry

Edited by Cranberry

Share this post


Link to post
Share on other sites
Advertisement
D3D11_VIEWPORT viewPort;
viewPort.TopLeftX = 0.0f;
viewPort.TopLeftY = 0.0f;
viewPort.Width = static_cast<float>(pWindow->GetWidth());
viewPort.Height = static_cast<float>(pWindow->GetHeight());
viewPort.MinDepth = 0.0f;
viewPort.MaxDepth = 1.0f;

m_pDeviceContext->RSSetViewports(1, &viewPort);

Looks fine to me.

Share this post


Link to post
Share on other sites

That looks good. What does your vertex shader look like? Are you transposing your matrices when filling your constant buffer?

Share this post


Link to post
Share on other sites

Vertex Shader: 

cbuffer cbPerObject
{
	matrix worldMatrix;
	matrix viewMatrix;
	matrix projectionMatrix;
};

struct VertexIn
{
	float3 Pos : POSITION;
	float4 Color : COLOR;
};

struct VertexOut
{
	float4 PosH : SV_POSITION;
	float4 Color : COLOR;
};

VertexOut VS(VertexIn vin)
{
	VertexOut vout;
	vout.PosH = mul(float4(vin.Pos, 1.0f), worldMatrix);
	vout.PosH = mul(vout.PosH, viewMatrix);
	vout.PosH = mul(vout.PosH, projectionMatrix);
	vout.Color = vin.Color;
	return vout;
}

float4 PS(VertexOut pin) : SV_TARGET
{
	return pin.Color;
}

Yeah I transpose all three of them.

 

Here's my Render function, maybe you can spot a mistake:

void Game::Render()
{
	m_pRenderSystem->PreRender();

	//Set ColorShader
	m_pColorShader->SetInputLayoutAndTopology(m_pRenderSystem->GetDeviceContext());

	//Set geometry buffers
	UINT stride = sizeof(Vertex);
	UINT offset = 0;

	m_pRenderSystem->GetDeviceContext()->IASetVertexBuffers(0, 1, &m_pCubeVertexBuffer, &stride, &offset);
	m_pRenderSystem->GetDeviceContext()->IASetIndexBuffer(m_pCubeIndexBuffer, DXGI_FORMAT_R32_UINT, 0);


	//Set constant buffer
	D3D11_BUFFER_DESC cbPerObjectDesc;
	cbPerObjectDesc.Usage = D3D11_USAGE_DYNAMIC;
	cbPerObjectDesc.ByteWidth = sizeof(cbPerObject);
	cbPerObjectDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cbPerObjectDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	cbPerObjectDesc.MiscFlags = 0;
	cbPerObjectDesc.StructureByteStride = 0;

	m_pRenderSystem->GetDevice()->CreateBuffer(&cbPerObjectDesc, 0, &m_cbPerObject);

	D3D11_MAPPED_SUBRESOURCE mappedResource;
	cbPerObject* dataPtr;
	unsigned int bufferNumber;

	m_pRenderSystem->GetDeviceContext()->Map(m_cbPerObject, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);

	dataPtr = (cbPerObject*) mappedResource.pData;

	XMMatrixTranspose(m_WorldMatrix);
	XMMatrixTranspose(m_ViewMatrix);
	XMMatrixTranspose(m_ProjectionMatrix);

	dataPtr->worldMatrix = m_WorldMatrix;
	dataPtr->viewMatrix = m_ViewMatrix;
	dataPtr->projectionMatrix = m_ProjectionMatrix;

	m_pRenderSystem->GetDeviceContext()->Unmap(m_cbPerObject, 0);

	// Set the position of the constant buffer in the vertex shader.
	bufferNumber = 0;

	// Finanly set the constant buffer in the vertex shader with the updated values.
	m_pRenderSystem->GetDeviceContext()->VSSetConstantBuffers(bufferNumber, 1, &m_cbPerObject);

	m_pColorShader->SetAsShader(m_pRenderSystem->GetDeviceContext());

	//Draw cube
	m_pRenderSystem->GetDeviceContext()->DrawIndexed(36, 0, 0);

	m_pRenderSystem->PostRender();
}

Share this post


Link to post
Share on other sites

vout.PosH = mul(float4(vin.Pos, 1.0f), worldMatrix);
vout.PosH = mul(vout.PosH, viewMatrix);
vout.PosH = mul(vout.PosH, projectionMatrix);

 

and

 

XMMatrixTranspose(m_WorldMatrix);

 

Should give you the deriered multiplication order. (im not in touch with XMMath)

Try to add #pragma pack_matrix(row_major) in your shader codes

Share this post


Link to post
Share on other sites

XMMatrixTranspose will return the transpose of the matrix, it won't modify the matrix that's passed in as its argument. So you either need to assign the result to a temporary variable, or assign the result directly to your mapped constant buffer data.

 

As the above poster suggested, you can tell the shader compiler to expect row-major matrices which will allow you to skip the annoying transpose step in your C++ code. To do this you can use the pragma that he mentioned, or you can add "row_major" to the declaration of the matrix in your HLSL constant buffer definition. There are also flags that you can pass to fxc.exe or D3DCompile that will achieve the same thing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!