Direct3D Without All The Garbage
Explains an easy way to implement the bare minimum of 3D engine features.
[size="5"]Initializing
There are two options for initializing Direct3D, the easy way and the hard way. The easy way involves three functions.
D3DXInitialize()
D3DXInitialize() is called before anything else. It has no parameters, so you don't need any explanation.
D3DXCreateContext( DWORD [color="#FF0000"]deviceIndex[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]DWORD [color="#4169E1"]flags[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]HWND [color="#008000"]hwnd[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]DWORD [color="#FF8C00"]width[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]DWORD [color="#FF8C00"]height[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPD3DXCONTEXT* [color="#9932CC"]ppCtx[/color]);
The above function creates a window to the Direct3D world. You can see everything through it, but it shields you from some awful things.
[color="#FF0000"] deviceIndex[/color]
[nbsp][nbsp][nbsp]D3DX_CONTEXT_FULLSCREEN - fullscreen
[nbsp][nbsp]0 - windowed[/bquote]
This function is called to get an interface to the 3d world. Just ask your D3DXCONTEXT, and it will give you a pointer to a IDirect3DDevice7 object. Save this pointer for later use.
This is all for initialization. All of the DirectX functions return a nonzero number if they have an error, and you can get a text description of the error by calling D3DXGetErrorString( int error, int string length, char *pointer to string ).
[size="5"]Textures
Loading textures into memory is rather easy with the Utility Library.
D3DXCreateTextureFromFile( LPDIRECT3DDEVICE7 [color="#FF0000"]pd3dDevice[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPDWORD [color="#808080"]pFlags[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPDWORD [color="#9932CC"]pWidth[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPDWORD [color="#9932CC"]pHeight[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]D3DX_SURFACEFORMAT* [color="#808080"]pPixelFormat[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPDIRECTDRAWPALETTE [color="#808080"]pDDPal[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPDIRECTDRAWSURFACE7* [color="#00BFFF"]ppTexture[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPDWORD [color="#FF8C00"]pNumMipMaps[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPSTR [color="#2E8B57"]pSrcName[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]D3DX_FILTERTYPE [color="#A0522D"]filterType[/color]
[color="#FF0000"]pd3dDevice[/color]
[size="5"]Lighting
Until now, many people did all the lighting they needed themselves. But now there is hardware accelerated lighting. If you let DirectX take care of the lighting, it will take full advantage of accellerations.
There are two functions used for lighting-
D3DDevice::SetLight(int [color="#4169E1"]index[/color], LPD3DLIGHT7 [color="#FF0000"]light[/color]);
This function sets the characteristics of a light.
[color="#4169E1"] index[/color]
After the light properties are set, LightEnable() is called. The index is the index of the light you just set, and the state is set to true or false ( true for on).
[size="5"]Matrices
The camera and world orientation need to be set.
D3Device::SetTransform( D3DTRANSFORMSTATETYPE TransformState, LPD3DMATRIX Matrix)
The TransformState specifies which matrix is being set, use D3DTRANSFORMSTATE_WORLD to set the world matrix, and D3DTRANSFORMSTATE_VIEW to set the camera matrix.
If you don't know how matrices work, I would recommend more reading at the GameDev.net reference section.
You can use your own matrix code, or use the D3DXMatrixStack which is explained in the DirectX help.
[size="5"]Drawing
All drawing uses the DrawPrimitive() function. If you want to draw something with a certain texture on it, first call the D3DDevice::SetTexture() function. Before drawing call D3DDevice::BeginScene() and after drawing call EndScene(), and use DXContext::UpdateFrame() to show your picture.
D3DDevice::SetTexture(int stage, LPDIRECTDRAWSURFACE7 Texture)
The stage should be set to zero unless you plan on doing complex texture blending which you probably shouldn't be doing yet if you are reading this tutorial. The Texture pointer should be the pointer that was retrieved in the texture section of this tutorial.
To clear the screen use the DXContext::Clear() method with D3DCLEAR_TARGET and D3DCLEAR_ZBUFFER as arguments.
DrawPrimitive( D3DPRIMITIVETYPE [color="#FF0000"]PrimitiveType[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]DWORD [color="#2E8B57"]VertexDesc[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]LPVOID [color="#4169E1"]Vertices[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]DWORD [color="#9932CC"]VertexCount[/color],
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]DWORD [color="#808080"]Flags[/color])
[color="#FF0000"]PrimitiveType[/color]
VertexDesc[/color]
Vertices[/color]
VertexCount[/color]
Flags[/color]
[size="5"]Uninitializing
For any textures you made, and the D3DXContext you retrieved, call their Release() function. Then call D3DXUninitialize(), and you're done.
Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Lee Mazurek
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion