JMU
Direct3D Basics
with Examples in C/C++


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


The Direct3D Architecture
Direct3D Concepts
Direct3D Concepts (cont.)
Coordinates
Vertices
Vertices (cont.)

An Example

directx9examples/basics/cube/cube.cpp (Fragment: vertices)
        // Specify the struct for a vertex
struct CUSTOMVERTEX
{
    float x, y, z;
        DWORD color;
};

// Specify the flexible vertex format (FVF) for the struct above
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)


// Color Definitions
DWORD colorBlue   = 0xFF0000FF;
DWORD colorGold   = 0xFFC2A14D; // JMU Gold
DWORD colorGray   = 0xFFAAAAAA;
DWORD colorGreen  = 0xFF00FF00;
DWORD colorPurple = 0xFF450084; // JMU Purple
DWORD colorRed    = 0xFFFF0000;
DWORD colorYellow = 0xFFFFFF00;

// Create the vertex elements
CUSTOMVERTEX vertices[] =
{
        {-1.0f, 1.0f,-1.0f, colorBlue},
        { 1.0f, 1.0f,-1.0f, colorBlue},
        {-1.0f,-1.0f,-1.0f, colorBlue},
        { 1.0f,-1.0f,-1.0f, colorBlue},
        
        {-1.0f, 1.0f, 1.0f, colorGold},
        {-1.0f,-1.0f, 1.0f, colorGold},
        { 1.0f, 1.0f, 1.0f, colorGold},
        { 1.0f,-1.0f, 1.0f, colorGold},
        
        {-1.0f, 1.0f, 1.0f, colorGray},
        { 1.0f, 1.0f, 1.0f, colorGray},
        {-1.0f, 1.0f,-1.0f, colorGray},
        { 1.0f, 1.0f,-1.0f, colorGray},
        
        {-1.0f,-1.0f, 1.0f, colorGreen},
        {-1.0f,-1.0f,-1.0f, colorGreen},
        { 1.0f,-1.0f, 1.0f, colorGreen},
        { 1.0f,-1.0f,-1.0f, colorGreen},

        { 1.0f, 1.0f,-1.0f, colorPurple},
        { 1.0f, 1.0f, 1.0f, colorPurple},
        { 1.0f,-1.0f,-1.0f, colorPurple},
        { 1.0f,-1.0f, 1.0f, colorPurple},
        
        {-1.0f, 1.0f,-1.0f, colorRed},
        {-1.0f,-1.0f,-1.0f, colorRed},
        {-1.0f, 1.0f, 1.0f, colorRed},
        {-1.0f,-1.0f, 1.0f, colorRed}
};

        
Vertices (cont.)
Vertices (cont.)

Creating a Vertex Buffer

directx9examples/basics/cube/cube.cpp (Fragment: createBuffer)
                // Create the vertex buffer
        d3dDevice->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX),0,  D3DFVF_CUSTOMVERTEX,
                                  D3DPOOL_DEFAULT, &vertexBuffer, NULL );

        // Declare the source data for the buffer
        VOID* data = NULL;

        // Lock the vertex buffer
    vertexBuffer->Lock( 0, sizeof(vertices), (void**)&data, 0 );

        // Copy the vertices into the buffer
    memcpy(data, vertices, sizeof(vertices));

        // Unlock the vertex buffer
    vertexBuffer->Unlock();
        
Points, Lines and Polygons
Points, Lines and Polygons
Points, Lines and Polygons (cont.)
The Rendering Process
Rendering (cont.)
Rendering (cont.)

An Example

directx9examples/basics/cube/cube.cpp (Fragment: render)
        /**
 * The function that is called when the scene needs to be rendered
 */
void CALLBACK paint(IDirect3DDevice9* d3dDevice, 
                                        double time, float elapsedTime, 
                                        void* userContext )
{
    HRESULT hr;

    // Clear the render target and the zbuffer 
    V(d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
                           D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) );


    // Render the scene
    if( SUCCEEDED(d3dDevice->BeginScene()))
    {
                // Transform the model
                D3DXMATRIX rotate, translate, world;

                D3DXMatrixTranslation(&translate, 0.0f, 0.0f, 5.0f );
                D3DXMatrixRotationY(&rotate, rotateY);

                D3DXMatrixMultiply(&world, &rotate, &translate);
                d3dDevice->SetTransform(D3DTS_WORLD, &world );


                // Starting to define the scene
                d3dDevice->BeginScene();

                // Bind the vertex buffer to the device
                d3dDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(CUSTOMVERTEX));

                // Setup the vertex shader
                d3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);

                // Draw the various shapes (two triangles for each face)
                d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,  0, 2);
                d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,  4, 2);
                d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,  8, 2);
                d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);
                d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);
                d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);

        V(d3dDevice->EndScene());
    }
}
        
Viewing
  1. World Matrix:
    • Transformations to perform in model space
  2. View Matrix:
    • The "camera" for the scene
  3. Projection Matrix
    • How geometry is transformed from 3D view space to 2D viewport space
Viewing (cont.)
Viewing (cont.)
The Projection Transform
Viewing (cont.)

An Example

directx9examples/basics/cube/cube.cpp (Fragment: projection)
                // Use a perspective projection
    D3DXMATRIX projection;
    D3DXMatrixPerspectiveFovLH( &projection, D3DXToRadian( 45.0f ), 
                                640.0f / 480.0f, 0.1f, 100.0f );
    d3dDevice->SetTransform(D3DTS_PROJECTION, &projection);
        
Transformations

Using a Mouse Callback

directx9examples/basics/cube/cube.cpp (Fragment: mousecallback)
        /**
 * The function that is called when the mouse is
 * moved or clicked
 */
void CALLBACK mouseEvent(bool leftDown, bool rightDown, bool middleDown,
                                                 bool side1Down, bool side2Down,
                                                 int wheelDelta, int x, int y, void* userContext)
{
        if (rightDown)
        {
                rotateY -= (float)D3DX_PI/10.0f;
        }
        else if (leftDown)
        {
                rotateY += (float)D3DX_PI/10.0f;
        }
}