C++ Projects

DirectX Input and 3D Lighting

Table of Contents

  • Input Functions
  • Light Functions
  • This project demonstrates the use of DirectX input library. It allows for user input to move and rotate a 3D object. Pressing the 1,2,3 keys allow for different lighting to be applied to the 3D object in realtime. The three different lights are: directional light, spot light, and point light.

    Download Full Source

    Input Functions

    View Full Code :

    // ====================================================================
    //       DIRECTINPUT FUNCTIONS
    // ====================================================================
     
    // ----------------------[Initialize]----------------------------
    void DInput_Init(HINSTANCE hInstance, HWND hWnd)
    {
     // create the DirectInput interface
        DirectInput8Create(hInstance,    // the handle to the application
                           DIRECTINPUT_VERSION,    // the compatible version
                           IID_IDirectInput8,    // the DirectInput interface version
                           (void**)&din,    // the pointer to the interface
                           NULL);
     
        // create the keyboard device
        din->CreateDevice(GUID_SysKeyboard,    // the default keyboard ID being used
                          &dinKeyboard,    // the pointer to the device interface
                          NULL);
         // set the data format to keyboard format
        dinKeyboard->SetDataFormat(&c_dfDIKeyboard);
     
        // set the control you will have over the keyboard
        dinKeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
     
        return;
    }
     
    // ----------------------[Detect Keys]----------------------------
    void DInput_DetectKeys(void)
    {
        static BYTE keystate[256];    // create a static storage for the key-states
     
        dinKeyboard->Acquire();    // get access to the keyboard device
         // fill keystate with values
        dinKeyboard->GetDeviceState(sizeof(keystate), (LPVOID)&keystate);
     
     if (keystate[DIK_ESCAPE] & 0x80)
      exitprog = true; // exit program
     
     if ((keystate[DIK_A] & 0x80) || (keystate[DIK_LEFT] & 0x80))
      yAxisMovement += 0.1f;  // move left
     if (keystate[DIK_D] & 0x80 || (keystate[DIK_RIGHT] & 0x80))
      yAxisMovement -= 0.1f;  // move right
     if (keystate[DIK_W] & 0x80 || (keystate[DIK_UP] & 0x80))
      xAxisMovement += 0.1f;  // move up
     if (keystate[DIK_S] & 0x80 || (keystate[DIK_DOWN] & 0x80))
      xAxisMovement -= 0.1f;  // move down
     
     if (keystate[DIK_1] & 0x80)
     {
      lightDirectional = true; // directional light enable
      lightPoint = false;
      lightSpot = false;
     }
     else if (keystate[DIK_2] & 0x80)
     {
      lightPoint = true;   // point light enable
      lightDirectional = false;
      lightSpot = false;
     }
     else if (keystate[DIK_3] & 0x80)
     {
      lightSpot = true;   // spot light enable
      lightDirectional = false;
      lightPoint = false;
     }
     
        return;
    }
     
    // ----------------------[Clean Up]----------------------------
    void DInput_Close(void)
    {
     dinKeyboard->Unacquire();  // gives up access to the keyboard
     din->Release();  // closes DirectInput before exiting
    }
    // ====================================================================
    // ====================================================================

    Light Functions

    View Full Code :

    // ====================================================================
    //       LIGHT FUNCTIONS
    // ====================================================================
     
    // ----------------------[Initialize]----------------------------
    void Lights_Init(void)
    {
        D3DLIGHT9 light;    // create the light struct
     D3DXVECTOR3 vecDirection;
     D3DXVECTOR3 vecPosition;
     ZeroMemory(&light, sizeof(light));    // clear out the struct for use
     light.Range = 100.0f;
     
     if (lightDirectional)
     {
      lightPoint = false;
      lightSpot = false;
      light.Type = D3DLIGHT_DIRECTIONAL;    // sets the light type
      light.Diffuse.r = 0.5f;    // red
      light.Diffuse.g = 0.5f;    // green
      light.Diffuse.b = 0.9f;    // blue
      light.Diffuse.a = 1.0f;    // full alpha
     
      vecDirection = D3DXVECTOR3(-2.0f, -2.0f, -1.0f);    // the direction of the light
      // applying the light direction
      D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &vecDirection);
     }
     else if (lightPoint)
     {
      lightDirectional = false;
      lightSpot = false;
      light.Type = D3DLIGHT_POINT;    // sets the light type
      light.Diffuse.r = 0.9f;    // red
      light.Diffuse.g = 0.5f;    // green
      light.Diffuse.b = 0.5f;    // blue
      light.Diffuse.a = 1.0f;    // full alpha
     
      vecPosition = D3DXVECTOR3(0.0f, -10.0f, 0.0f);    // the position of the light
      light.Position = vecPosition; // position of light
      light.Attenuation0 = 0.1f;  //brightness
     }
     else if (lightSpot)
     {
      lightPoint = false;
      lightDirectional = false;
      light.Type = D3DLIGHT_SPOT;    // sets the light type
      light.Diffuse.r = 0.5f;    // red
      light.Diffuse.g = 0.9f;    // green
      light.Diffuse.b = 0.5f;    // blue
      light.Diffuse.a = 1.0f;    // full alpha
     
      vecPosition = D3DXVECTOR3(10.0f, 20.0f, 0.0f);    // the position of the light
      vecDirection = D3DXVECTOR3(-2.0f, -2.0f, 0.0f);    // the direction of the light
      light.Position = vecPosition;
      light.Direction = vecDirection;
      light.Theta = 0.5f;   // inner cone
      light.Phi = 1.0f;   // outer cone
      light.Falloff = 1.0f;  // light range falloff
      light.Attenuation0 = 1.0f; // light strength
     }
     
        d3ddev->SetLight(0, &light);    // send the light struct properties to light #0
        d3ddev->LightEnable(0, TRUE);    // turn on light #0
     d3ddev->SetRenderState(D3DRS_LIGHTING, true); // turns off 3D lighting
     // ambient light
     d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));
     
        return;
    }
    // ====================================================================
    // ====================================================================