9 Replies - 379 Views - Last Post: 29 January 2018 - 01:19 PM Rate Topic: -----

#1 ZOulhadj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-September 17

DirectX 11 rendering rectangle instead of triangle C++

Posted 28 January 2018 - 02:47 PM

I have a question and I was wondering if you guys would be able to help. So at the moment, I am trying to render a basic triangle with shaders with DirectX11. I have set up the device, device context, swap chain and render target. I have given DirectX the correct vertices however for some reason it shows up as a pink rectangle to the right of the window. Here is the code for the rendering file. I have done my research about the graphics pipeline, looked at tutorials, looked at websites and more but I am just stuck at this point. So, if you guys could point out the issues then that would be much appreciated.

using namespace Utils;

namespace API { namespace Direct3D {

	// TODO: Understand more how DirectX works and it's renderer.
	// TODO: Clean up the code and if needed create separate classes
	

	CHAR *strVS =
		"void VS( in float4 posIn : POSITION,\n"
		"         out float4 posOut : SV_Position )\n"
		"{\n"
		"    // Output the vertex position, unchanged\n"
		"    posOut = posIn;\n"
		"}\n";

	CHAR *strPS =
		"void PS( out float4 colorOut : SV_Target )\n"
		"{\n"
		"    // Make each pixel yellow, with alpha = 1\n"
		"    colorOut = float4( 0.8f, 0.4f, 0.6f, 1.0f );\n"
		"}\n";
	
	struct Vertex
	{
		float x, y, z;
	};

	ID3D11Device *device;
	ID3D11DeviceContext *deviceContext;
	IDXGISwapChain *swapChain;
	ID3D11RenderTargetView *renderTargetView;
	ID3D11Buffer *vertexBuffer;
	ID3D11VertexShader *vertexShader;
	ID3D11PixelShader *pixelShader;
	ID3D11InputLayout *inputLayout;

	DXRenderer::DXRenderer()
	{}

	DXRenderer::~DXRenderer()
	{}

	bool DXRenderer::Init(Window *window, HWND hWnd)
	{
		// Create device
		HRESULT hResult;
		ZeroMemory(&hResult, sizeof(HRESULT));

		DXGI_SWAP_CHAIN_DESC swapChainDesc;
		ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

		swapChainDesc.BufferCount = 1;
		swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
		swapChainDesc.OutputWindow = hWnd;
		swapChainDesc.SampleDesc.Count = 4;
		swapChainDesc.Windowed = TRUE;

		// Create swap chain, device and device context
		hResult = D3D11CreateDeviceAndSwapChain(
			NULL,
			D3D_DRIVER_TYPE_HARDWARE,
			NULL,
			NULL,
			NULL,
			NULL,
			D3D11_SDK_VERSION,
			&swapChainDesc,
			&swapChain,
			&device,
			NULL,
			&deviceContext
		);

		if (!&hResult)
		{
			Log::DebugMessage(1, "Failed to create D3D11Device and SwapChain");

			return false;
		}

		// Create render target
		ID3D11Texture2D *backBuffer;
		ZeroMemory(&backBuffer, sizeof(ID3D11Texture2D));

		swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
		device->CreateRenderTargetView(backBuffer, NULL, &renderTargetView);
		backBuffer->Release();

		ID3D10Blob *blobVS;
		ZeroMemory(&blobVS, sizeof(ID3D10Blob));

		ID3D10Blob *blobError;
		ZeroMemory(&blobError, sizeof(ID3D10Blob));

		D3DCompile(strVS, lstrlen(strVS) + 1, "VS", NULL, NULL, "VS", "vs_4_0", D3D10_SHADER_DEBUG, 0, &blobVS, &blobError);
		device->CreateVertexShader(blobVS->GetBufferPointer(), blobVS->GetBufferSize(), NULL, &vertexShader);

		ID3D10Blob *blobPS;
		ZeroMemory(&blobPS, sizeof(ID3D10Blob));

		D3DCompile(strPS, lstrlen(strPS) + 1, "PS", NULL, NULL, "PS", "ps_4_0", D3D10_SHADER_DEBUG, 0, &blobPS, &blobError);
		device->CreatePixelShader(blobPS->GetBufferPointer(), blobPS->GetBufferSize(), NULL, &pixelShader);

		blobPS->Release();

		D3D11_INPUT_ELEMENT_DESC elements[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		UINT numElements = _countof(elements);

		device->CreateInputLayout(elements, numElements, blobVS->GetBufferPointer(), blobVS->GetBufferSize(), &inputLayout);

		blobVS->Release();

		Vertex vertices[] =
		{
			/*
			* TODO(Critical): Need to fix the vertex points. Instead of a triangle rendering
			* onto the screen a rectangle is appearing.
			*
			* NOTE: This has to be done / fixed before we can continue
			*/
			0.0f, 0.5f, 0.0f,
			0.5f, -0.5f, 0.0f,
			-0.5f, -0.5f, 0.0f,
		};

		D3D11_BUFFER_DESC bd;
		ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC));

		bd.Usage = D3D11_USAGE_DYNAMIC;
		bd.ByteWidth = sizeof(vertices);
		bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
		bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
		bd.MiscFlags = 0;
		bd.StructureByteStride = 0;

		D3D11_SUBRESOURCE_DATA srd;
		srd.pSysMem = vertices;
		device->CreateBuffer(&bd, &srd, &vertexBuffer);

		deviceContext->IASetInputLayout(inputLayout);

		UINT stride = sizeof(Vertex);
		UINT offset = 0;

		deviceContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
		deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
		deviceContext->VSSetShader(vertexShader, NULL, 0);
		deviceContext->PSSetShader(pixelShader, NULL, 0);


		// Create a viewport
		D3D11_VIEWPORT viewport;
		ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

		viewport.TopLeftX = 0;
		viewport.TopLeftY = 0;
		viewport.Width = (FLOAT)window->GetWidth();
		viewport.Height = (FLOAT)window->GetHeight();

		deviceContext->RSSetViewports(1, &viewport);

		// OM Stage
		deviceContext->OMSetRenderTargets(1, &renderTargetView, NULL);

		return true;
	}

	void DXRenderer::Render()
	{
		float clearColor[4] = { 0.0f, 0.125f, 0.3f, 1.0f };
		// clear the back buffer to a color
		deviceContext->ClearRenderTargetView(renderTargetView, clearColor);
		// Render
		deviceContext->Draw(3, 0);
		// switch the back buffer and the front buffer
		swapChain->Present(1, 0);
	}

	void DXRenderer::Cleanup()
	{
		// Release all COM objects
		swapChain->Release();
		device->Release();
		deviceContext->Release();
		renderTargetView->Release();

		vertexBuffer->Release();
		vertexShader->Release();
		pixelShader->Release();
		inputLayout->Release();
	}

}}



Also here is the output as I forgot to add it to my original post: https://imgur.com/a/Q2TYi

Is This A Good Question/Topic? 0
  • +

Replies To: DirectX 11 rendering rectangle instead of triangle C++

#2 Radius Nightly  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 07-May 15

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 28 January 2018 - 07:40 PM

Do you know anything about 3D models?

Edit: I see you fix a shape. You defined pink, and thats a texture, nothing wrong with that. If shaders doesnt appear, there is something wrong with them, maybe bad order or so. Try using one by one in combination, thats the easiest way to start.

This post has been edited by Radius Nightly: 28 January 2018 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 28 January 2018 - 10:10 PM

directx rendering draws from left to right so you have to start at the left hand side of the triangle.

like so

-0.5,-0.5,0.0
0.0,0.5,0.0
0.5,-0.5,0.0

Regards
Was This Post Helpful? 1
  • +
  • -

#4 ZOulhadj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-September 17

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 06:17 AM

Well, I have just used the vertices to have given me. So,
-0.5f,-0.5f,0.0f,
0.0f,0.5f,0.0f,
0.5f,-0.5f,0.0f


However, now I just get another weird shape. Here is what I get: https://imgur.com/a/pR6bV
At this point, I'm starting to think it's something to do with which way it renders. You did say though that DirectX
renders from left to right so I'm not too sure.

If you are wondering yes I have tried almost all possible variations of the vertices with no luck.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6016
  • View blog
  • Posts: 20,693
  • Joined: 05-May 12

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 07:25 AM

In older versions of DirectX, you were force to use clockwise winding direction for vertices. In current versions, you can choose the winding direction.
Primitive Topologies


Instead of rolling your own code right off the bat, I suggest following along with the DirectX walkthrough/tutorial before doing other work.
Getting Started with Direct3D

Disclaimer: I last did the DirectX 9 version of "Getting Started with Direct3D", I don't know what changes have gone into the DirectX 11 or 12 versions of the walkthrough.
Was This Post Helpful? 1
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 10:42 AM

Hmm,
having looked at this closer you mix a few technologies which is confusing.
You have a 2drendertarget
you have d3d10blobs
d3d11 viewports, rendering etc.

This makes it quite difficult to diagnose but i feel its to do with elements[] which has information for one vertex but of course your vertex buffer has 3 vertices.

try changing elements[] to
{ L"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,
D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ L"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12,
D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ L"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20,
D3D11_INPUT_PER_VERTEX_DATA, 0 },
Was This Post Helpful? 2
  • +
  • -

#7 ZOulhadj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-September 17

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 11:17 AM

After changing my elements array I am pleased to say it's working. So thank you. I will take a closed look and try to understand what exactly is going on behind the scenes. Here is the output if you're interested: https://imgur.com/a/JROhT
So thanks to Snoopy11, Skydiver, and Radius Nightly for all helping me out.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6016
  • View blog
  • Posts: 20,693
  • Joined: 05-May 12

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 11:27 AM

There is more you need to review. For example, The comment in the shader says yellow but that RGB value does not look right.
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1437
  • View blog
  • Posts: 4,626
  • Joined: 20-March 10

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 11:49 AM

Aah i always get there in the end,

just getting slower the older I get ha! 😃
Was This Post Helpful? 0
  • +
  • -

#10 ZOulhadj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 21-September 17

Re: DirectX 11 rendering rectangle instead of triangle C++

Posted 29 January 2018 - 01:19 PM

Yes Skydiver, I will look at that now and see what the issue is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1