• Advertisement
Sign in to follow this  

DX11 Problem with displaying Cube in DX11

This topic is 1514 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

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

Thank you so much!

I was thinking of the old D3DXTransposeMatrix which modified the matrix via a reference.

Share this post


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

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By turanszkij
      Hi, right now building my engine in visual studio involves a shader compiling step to build hlsl 5.0 shaders. I have a separate project which only includes shader sources and the compiler is the visual studio integrated fxc compiler. I like this method because on any PC that has visual studio installed, I can just download the solution from GitHub and everything just builds without additional dependencies and using the latest version of the compiler. I also like it because the shaders are included in the solution explorer and easy to browse, and double-click to open (opening files can be really a pain in the ass in visual studio run in admin mode). Also it's nice that VS displays the build output/errors in the output window.
      But now I have the HLSL 6 compiler and want to build hlsl 6 shaders as well (and as I understand I can also compile vulkan compatible shaders with it later). Any idea how to do this nicely? I want only a single project containing shader sources, like it is now, but build them for different targets. I guess adding different building projects would be the way to go that reference the shader source project? But how would they differentiate from shader type of the sources (eg. pixel shader, compute shader,etc.)? Now the shader building project contains for each shader the shader type, how can other building projects reference that?
      Anyone with some experience in this?
    • By osiris_dev
      Hello!
      Have a problem with reflection shader for D3D11:
      1>engine_render_d3d11_system.obj : error LNK2001: unresolved external symbol IID_ID3D11ShaderReflection
      I tried to add this:
      #include <D3D11Shader.h>
      #include <D3Dcompiler.h>
      #include <D3DCompiler.inl>
      #pragma comment(lib, "D3DCompiler.lib")
      //#pragma comment(lib, "D3DCompiler_47.lib")
      As MSDN tells me but still no fortune. I think lot of people did that already, what I missing?
      I also find this article http://mattfife.com/?p=470
      where recommend to use SDK headers and libs before Wind SDK, but I am not using DirectX SDK for this project at all, should I?
    • By trojanfoe
      Hi there, this is my first post in what looks to be a very interesting forum.
      I am using DirectXTK to put together my 2D game engine but would like to use the GPU depth buffer in order to avoid sorting back-to-front on the CPU and I think I also want to use GPU instancing, so can I do that with SpriteBatch or am I looking at implementing my own sprite rendering?
      Thanks in advance!
    • By Matt_Aufderheide
      I am trying to draw a screen-aligned quad with arbitrary sizes.
       
      currently I just send 4 vertices to the vertex shader like so:
      pDevCon->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
      pDevCon->Draw(4, 0);
       
      then in the vertex shader I am doing this:
      float4 main(uint vI : SV_VERTEXID) : SV_POSITION
      {
       float2 texcoord = float2(vI & 1, vI >> 1);
      return float4((texcoord.x - 0.5f) * 2, -(texcoord.y - 0.5f) * 2, 0, 1);
      }
      that gets me a screen-sized quad...ok .. what's the correct way to get arbitrary sizes?...I have messed around with various numbers, but I think I don't quite get something in these relationships.
      one thing I tried is: 
       
      float4 quad = float4((texcoord.x - (xpos/screensizex)) * (width/screensizex), -(texcoord.y - (ypos/screensizey)) * (height/screensizey), 0, 1);
       
      .. where xpos and ypos is number of pixels from upper right corner..width and height is the desired size of the quad in pixels
      this gets me somewhat close, but not right.. a bit too small..so I'm missing something ..any ideas?
       
      .
  • Advertisement