• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Wedvich

Getting started with Direct3D

1 post in this topic

Hi guys! I've been playing around with making games for a little while now, using SDL at first and then SFML. It's been nice to have the basics done for me, but now I'd like to take another step and learn how to do the graphics from the ground up. So I'm trying to learn the ropes of Direct3D, and have been reading a couple of tutorials on D3D 11 to get started. I'm just trying to get the absolute basics going by drawing a white triangle on a dark background... but I can't get it to work.

I'm using the Visual Studio 2012 RC and the Windows 8 SDK, so there's some C++11 syntax in here, plus there's no D3DX. I've managed to set up the window and the initialization of Direct3D seems to go just fine. It runs the rendering part of the main loop, as it clears the screen in the color I specify. However, my fancy white triangle just won't show up. I've gone through my code several times, and with my lack of experience I can't tell what is wrong with it. I've cut out the window initialization/d3d shutdown and other irrelevant parts, but I'm afraid to cut off too much in case some of it is relevant to my problem, so... big-wall-of-code-warning.

I think I have all the required steps in place; I create the device, device context, swap chain and render target. Then I create a vertex shader+input layout and a pixel shader. After that I create a vertex buffer, add the vertices for my triangle and set the primitive topology to TRIANGLELIST, and that's it for the initialization. In my main loop, I point D3D to my vertex/pixel shaders, and tell it to draw the 3 vertices i inserted into the buffer. But only the dark blue background from the call to ClearRenderTargetView shows up; no triangle. Have I missed any steps, or done anything wrong on the way?

Here is the code I have (I'll shamefully admit to have slacked a bit with the code conventions on this one, so no [b]p[/b] before pointer variables etc.):

Main loop:
[source lang="cpp"] while ( msg.message != WM_QUIT )
{
if ( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
g_d3dContext->ClearRenderTargetView( g_renderTarget, clearColor );
g_d3dContext->VSSetShader( g_vertexShader, nullptr, 0 );
g_d3dContext->PSSetShader( g_pixelShader, nullptr, 0 );
g_d3dContext->Draw( 3, 0 );
g_swapChain->Present( 0, 0 );
}
}[/source]

D3D init code:
[source lang="cpp"]HRESULT hr = S_OK;

RECT rc;
GetClientRect( g_hWnd, &rc );
float width = static_cast< float >( rc.right - rc.left );
float height = static_cast< float >( rc.bottom - rc.top );

uint createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

const uint numDriverTypes = 3;
D3D_DRIVER_TYPE driverTypes[ numDriverTypes ] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE
};

const uint numFeatureLevels = 3;
D3D_FEATURE_LEVEL featureLevels[ numFeatureLevels ] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};

DXGI_SWAP_CHAIN_DESC sd = { 0 };
sd.BufferCount = 1;
sd.BufferDesc.Width = static_cast< uint >( width );
sd.BufferDesc.Height = static_cast< uint >( height );
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = true;

for( uint driverTypeIndex : driverTypes )
{
g_driverType = driverTypes[ driverTypeIndex ];
hr = D3D11CreateDeviceAndSwapChain( nullptr, g_driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &sd, &g_swapChain, &g_d3d, &g_featureLevel, &g_d3dContext );
if( SUCCEEDED( hr ))
break;
}
if( FAILED( hr ))
return hr;

// Create a render target view
ID3D11Texture2D* backBuffer = nullptr;
hr = g_swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &backBuffer ));
if( FAILED( hr ))
return hr;

hr = g_d3d->CreateRenderTargetView( backBuffer, nullptr, &g_renderTarget );
backBuffer->Release();
if( FAILED( hr ))
return hr;

g_d3dContext->OMSetRenderTargets( 1, &g_renderTarget, nullptr );

// Setup the viewport
D3D11_VIEWPORT vp = { 0 };
vp.Width = width;
vp.Height = height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
g_d3dContext->RSSetViewports( 1, &vp );

// Create vertex shader and input layout
ID3DBlob* vsBlob = nullptr;
ID3DBlob* errorBlob = nullptr;
hr = D3DCompileFromFile( L"VertexShader.hlsl", nullptr, nullptr, "main", "vs_4_0", 0, 0, &vsBlob, &errorBlob );
if ( FAILED( hr ))
return hr;

hr = g_d3d->CreateVertexShader( vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), nullptr, &g_vertexShader );
if ( FAILED( hr ))
{
vsBlob->Release();
return hr;
}

D3D11_INPUT_ELEMENT_DESC ied = { 0 };
ied.AlignedByteOffset = 0;
ied.Format = DXGI_FORMAT_R32G32B32_FLOAT;
ied.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
ied.InputSlot = 0;
ied.InstanceDataStepRate = 0;
ied.SemanticIndex = 0;
ied.SemanticName = "POSITION";

hr = g_d3d->CreateInputLayout( &ied, 1, vsBlob->GetBufferPointer(), vsBlob->GetBufferSize(), &g_inputLayout );
vsBlob->Release();
if ( FAILED ( hr ))
return hr;

g_d3dContext->IASetInputLayout( g_inputLayout );

// Create pixel shader
ID3DBlob* psBlob = nullptr;
errorBlob = nullptr;
hr = D3DCompileFromFile( L"PixelShader.hlsl", nullptr, nullptr, "main", "ps_4_0", 0, 0, &psBlob, &errorBlob );
if ( FAILED( hr ))
return hr;

hr = g_d3d->CreatePixelShader( psBlob->GetBufferPointer(), psBlob->GetBufferSize(), nullptr, &g_pixelShader );
psBlob->Release();
if ( FAILED( hr ))
return hr;

// Put some vertices up in this bitch
Vector3f vertices[] =
{
Vector3f( 0.5f, -0.5f, 0.5f ),
Vector3f( 0.5f, -0.5f, 0.5f ),
Vector3f( -0.5f, -0.5f, 0.5f )
};

D3D11_BUFFER_DESC bd = { 0 };
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.ByteWidth = sizeof( Vector3f ) * 3;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
bd.StructureByteStride = 0;
bd.Usage = D3D11_USAGE_DEFAULT;

D3D11_SUBRESOURCE_DATA initData = { 0 };
initData.pSysMem = vertices;
initData.SysMemPitch = 0;
initData.SysMemSlicePitch = 0;

hr = g_d3d->CreateBuffer( &bd, &initData, &g_vertexBuffer );
if ( FAILED( hr ))
return hr;

uint stride = sizeof( Vector3f );
uint offset = 0;

g_d3dContext->IASetVertexBuffers( 0, 1, &g_vertexBuffer, &stride, &offset );
g_d3dContext->IASetPrimitiveTopology( D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

return S_OK;[/source]

My vertex struct:
[source lang="cpp"]struct Vector3f
{
Vector3f( float ix, float iy, float iz )
: x( ix ), y( iy ), z( iz ) {}

float x;
float y;
float z;
};[/source]

My vertex shader:
[source lang="cpp"]float4 main( float4 pos : POSITION ) : SV_POSITION
{
return pos;
}[/source]

My pixel shader:
[source lang="cpp"]float4 main() : SV_TARGET
{
return float4( 1.0f, 1.0f, 1.0f, 1.0f );
}[/source]
0

Share this post


Link to post
Share on other sites
Eventually I figured it out; I was inserting bad vertex data. Changed to this:
[source lang="cpp"] Vector3f vertices[] =
{
Vector3f( -0.5f, 0.5f, 0.5f ),
Vector3f( 0.5f, 0.5f, 0.5f ),
Vector3f( -0.5f, -0.5f, 0.5f )
};[/source]
And everything is fine.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0