Direct3D Without All The Garbage

Published June 08, 2000 by Lee Mazurek, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement
This tutorial will show the easiest way to implement the bare minimum of 3D engine features. It is a good way to get started with Direct3D, and you can add complexity to the things you learn here as you get more familiar with DirectX. The tutorial has sections on initializing, textures, lighting, matrices, drawing, and uninitializing Direct3D. It will show the most necessary features of the functions it explains, and will leave out the unneeded complexities.


[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]
[bquote]This specifies hardware and software devices, you could enumerate them all. But I recommend you specify D3DX_DEFAULT, and let it decide for you.[/bquote]
[color="#4169E1"] flags[/color]
[bquote]This is to select fullscreen mode, or windowed mode.
[nbsp][nbsp][nbsp]D3DX_CONTEXT_FULLSCREEN - fullscreen
[nbsp][nbsp]0 - windowed[/bquote]
[color="#008000"]hwnd[/color]
[bquote]This is the only confusing part. When your application first starts, it creates a window. When it creates a window, it returns the handle of the window. This is what Direct3D wants. You don't need to specify an hwnd if it is windowed mode.[/bquote]
[color="#FF8C00"]width, height[/color]
[bquote]These should be obvious. These are the width and height of the window in pixels. In windowed mode these can be anything, but in fullscreen they have to match with certain allowable resolutions. You could enumerate the resolutions, or just try standard ones like 640x480 or 800x600[/bquote]
[color="#9932CC"] ppCtx [/color]
[bquote]This is a pointer to a LPD3DXCONTEXT variable. You give it the pointer and it fills it with a valid D3DXCONTEXT. This is what you want from the function and you should save this variable somewhere. It is your gateway to the screen.[/bquote]
ID3DXContext::GetD3DDevice()

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]
[bquote]This is the pointer to the to the Direct3dDevice that we got in the initialization.[/bquote]
[color="#808080"] pFlags[/color]
[bquote]Don't bother with this.[/bquote]
[color="#9932CC"] pWidth, pHeight[/color]
[bquote]Width and heigh in pixels that you want, if you specify D3DX_DEFAULT the width and height will be taken from the file.[/bquote]
[color="#808080"] pPixelFormat[/color]
[bquote]Pass zero as a parameter and DirectX will automatically decide which pixel format to use.[/bquote]
[color="#808080"] pDDPal[/color]
[bquote]Pass zero as a parameter and DirectX will automatically decide which pallete to use[/bquote]
[color="#4169E1"] ppTexture[/color]
[bquote]This is a pointer to a LPDIRECTDRAWSURFACE7 variable. The function fills this with a pointer to the texture.[/bquote]
[color="#FF8C00"] pNumMipsMaps[/color]
[bquote]This is the number of mipmaps it ends up creating, mipmaps are used for accellerating drawing by using different size images.[/bquote]
[color="#2E8B57"] pSrcName[/color]
[bquote]This is the name of the file you want to load. It can either be a BMP, DIB, or DDS file.[/bquote]
[color="#A0522D"] filterType[/color]
[bquote]This is related to mipmaps, I would just use D3DX_FT_DEFAULT for default.[/bquote]
This is the only function you need to load a texture. Just remember to save the pointer to the texture that it gives you.


[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]
[bquote]This is the zero base index of the light you want to set. If there is already a light at that index, its properties will be changed.[/bquote]
[color="#FF0000"] light[/color]
[bquote]This is a pointer to a D3DLIGHT7 structure. The important members of the D3DLIGHT7 structure are. D3DLIGHTTYPE dltType
[bquote]This can be a point light, spotlight, or directional light. (D3DLIGHT_POINT, D3DLIGHT_SPOT, D3DLIGHT_DIRECTIONAL)[/bquote]
D3DCOLORVALUE dcvDiffuse, dcvSpecular, dcvAmbient
[bquote]These are the diffuse (general), specular (shiny), and ambient (all around) components of the light. The D3DCOLORVALUE structure has r, g, and b (red, green, blue) components. They are floats from zero to one, with one being full light.[/bquote]
D3DVECTOR dvPosition
[bquote]This is the center of the light, its source. The D3DVECTOR structure has an x, y, and z component.[/bquote]
D3DVECTOR dvDirection
[bquote]The light's direction is a vector, with an x, y and z component.[/bquote]
float dvRange
[bquote]This is the distance before the light no longer affects and object.[/bquote]
float dvFalloff
[bquote]This is how much dimmer the light gets with an increasing angle from its direction.[/bquote]
float dvTheta
[bquote]This is the angle (in radians) to the edge of the light cone.[/bquote][/bquote]
D3DDevice::LightEnable(int index, BOOL state);

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]

[bquote]This tells DirectX what to draw. The three most common types are point list, line list, and triangle list. ( D3DPT_POINTLIST, 3DPT_LINELIST, D3DPT_TRIANGLELIST )[/bquote]
[color="#2E8B57"]
VertexDesc[/color]


[bquote]This explains what type of data you are passing to the functions. The type you will probably use is D3DFVF_VERTEX. This type has an x, y, and z, a normal, and texture coordinates. Texture coordinates are float values from zero to one, (0,0) is the upper left corner of the texture.[/bquote]
[color="#4169E1"]
Vertices[/color]


[bquote]This is a pointer to the list of vertices being drawn. To draw one triangle with vertices v1, v2, v3. The list would have the point v1 followed by v2 followed by v3.[/bquote]
[color="#9932CC"]
VertexCount[/color]


[bquote]This is the number of vertices in the above list.[/bquote]
[color="#808080"]
Flags[/color]


[bquote]Threre aren't any flags that are absolutely necessary to explain.[/bquote]

[size="5"]Uninitializing

For any textures you made, and the D3DXContext you retrieved, call their Release() function. Then call D3DXUninitialize(), and you're done.
Cancel Save
0 Likes 0 Comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!

Explains an easy way to implement the bare minimum of 3D engine features.

Advertisement
Advertisement
Advertisement