CG study #1

Basic Idea of these posts is to keep track of my study of Directx 11 from a book 3D Game Programming with DirectX 11 by Frank Luna that I finished reading a while ago. l have also started to build a simple 3d game demo to learn more about game and graphics programming.

Every chapter of the book have exercises at the end of each chapter. I decided to try to finish as much of them as I can same time building my demo project. First I made a plan to finish exercises from 3 or 4 chapters a week but after finishing first 4 I realized its too much if I also want to actually have time to make a game demo. I decided to finish at least one chapter a week and add something to demo every week. Most of the exercises also ask to implement different effects that I can use in my demo.

First three chapters discussed with vectors, matrices, and transformations etc.I used pen and pare to solve the exercises of those chapters.

Chapter 4 was about basic initialization of directx. Exercises asked things like enumerate display adapters and disable alt+enter to switch to full screen, modify the view port etc.

I do not know yet if I should post code solutions for the exercises or only screenshots but here is both for the chapter 4.

	// ch.4 ex. 1 disable fullscreen
	HR(dxgiFactory->MakeWindowAssociation(mhMainWnd, DXGI_MWA_NO_ALT_ENTER));

	// ch.4 ex. 2 number of adapters
	UINT numAdapters = 0;
	while (dxgiFactory->EnumAdapters(numAdapters, &dxgiAdapter) != DXGI_ERROR_NOT_FOUND)
	{
		numAdapters++;
	}

	wchar_t msgbuf[256];
	swprintf(msgbuf, 256, L"***NUM ADAPTERS %d\n", numAdapters);
	OutputDebugString(msgbuf);

	// ch.4 ex. 3 check directx support, note this can not be used for dx11.
	for (UINT i = 0; i < numAdapters; i++)
	{
		// Note  You can use CheckInterfaceSupport only to check whether a Direct3D 10.x interface is supported,
		// and only on Windows Vista SP1 and later versions of the operating system.
		// http://msdn.microsoft.com/en-us/library/windows/desktop/bb174524%28v=vs.85%29.aspx
		HR(dxgiFactory->EnumAdapters(i, &dxgiAdapter));
		if (dxgiAdapter->CheckInterfaceSupport(__uuidof(ID3D10Device), NULL) != DXGI_ERROR_UNSUPPORTED)
		{
			swprintf(msgbuf, 256, L"***D3D11 SUPPORTED FOR ADAPTER %d\n", i);
			OutputDebugString(msgbuf);
		}

	}

	// ch.4 ex. 4 check numper of outputs for default adapter.
	UINT numOutputs = 0;
	HR(dxgiFactory->EnumAdapters(0, &dxgiAdapter));
	IDXGIOutput* dxgiOutput = 0;
	while (dxgiAdapter->EnumOutputs(numOutputs, &dxgiOutput) != DXGI_ERROR_NOT_FOUND)
	{
		numOutputs++;
	}

	swprintf(msgbuf, 256, L"***NUM OUTPUTS FOR DEFAULT ADAPTER = %d\n", numOutputs);
	OutputDebugString(msgbuf);

	// ch.4 ex. 5 list of display modes for outputs
	for (int i = 0; i < numOutputs; i++)
	{
                swprintf(msgbuf, 256, L"***DISPLAY MODES FOR OUTPUT = %d\n", i);
		OutputDebugString(msgbuf);
		HR(dxgiAdapter->EnumOutputs(i, &dxgiOutput));

		// get numModes
		UINT numModes = 0;
		dxgiOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, 0, &numModes, NULL);

		DXGI_MODE_DESC* pDesc = 0;
		pDesc = new DXGI_MODE_DESC[numModes];
		HR(dxgiOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, 0, &numModes, pDesc));

		for (int j = 0; j < numModes; j++)
		{

			UINT w = pDesc->Width;
			UINT h = pDesc->Height;
			DXGI_RATIONAL rr = pDesc->RefreshRate;

			pDesc++;

			swprintf(msgbuf, 256, L"***WIDTH = %d HEIGHT = %d REFRESH = %d\\%d\n", w, h, rr.Numerator, rr.Denominator );
			OutputDebugString(msgbuf);

		}
	}

Modifying the viewport

// ch.4 ex. 6
ZeroMemory(&mScreenViewport, sizeof(D3D11_VIEWPORT));
mScreenViewport.TopLeftX = 100.0f;
mScreenViewport.TopLeftY = 100.0f;
mScreenViewport.Width = static_cast<float>(mClientWidth) - 200.0f;
mScreenViewport.Height = static_cast<float>(mClientHeight)- 200.0f;
mScreenViewport.MinDepth = 0.0f;
mScreenViewport.MaxDepth = 1.0f;

md3dImmediateContext->RSSetViewports(1, &mScreenViewport);

Here is a image for modified view port 🙂

ch4ex6

I will post more of my game demo I started to build after I get something on screen and some basic architecture ready.

Leave a Reply

Your email address will not be published. Required fields are marked *