Introduction to Direct 3D

Project Generation

Project Generation

  • Press File
  • Press New
  • Select "Project Workspace"
  • Select MFC Application
  • Call the project "Direct3D"
  • Step 1 select "Multiple Document"
  • Press Next
  • Step 2 Press Next
  • Step 3 Press Next
  • Step 4 Press Next
  • Step 5 Press Next
  • Step 6 Press Finish and Ok

Implementing the DirectSound,Direct3D,DirectInput classes

Step 1: Add the following header and pragma defines to direct3dview.cpp

#include <dinput.h>
#include <basetsd.h>

#pragma comment ( lib, "dxguid.lib" ) /* guiid definitions */
//#if DIRECTINPUT_VERSION == 0x0800
//#pragma comment ( lib, "dinput8.lib" )
//#else
#pragma comment ( lib, "dinput.lib" )
#pragma comment ( lib, "dsound.lib" )
#pragma comment ( lib, "winmm.lib" )
#pragma comment ( lib, "strmbase.lib")
//Direct3D libraries.
#pragma comment ( lib, "d3d8.lib")
#pragma comment ( lib, "d3dx8.lib")
#pragma comment ( lib, "dxerr8.lib" )


Step 2: Create a Method called ProcessGeometry. Add void ProcessGeometry to the direct3dview.h file. Insert source into direct3dview.cpp

void CDirect3dView::ProcessGeometry()
{
     mD3DEngine.Render();
}


Step 3: Use the Class Wizard to build a message map method for OnInitialUpdate for the following reasons

  1. To Instantiate the DirectInput class. The DirectInput class captures all keyboard, mouse, and joystick events.
  2. To create the DirectGraphics Devices. The DirectGraphic Devices allow the application to manage system and video card resources through apis.
  3. To Create the DirectMusic class. The DirectMusic class initializes and loads music segments to be played.
  4. To Create a Timer event running at maximum speed.
  5. Insert the following source into direct3dview.cpp

void CDirect3dView::OnInitialUpdate()
{
   CView::OnInitialUpdate();    #ifdef _WIN64
       HINSTANCE hInst = (HINSTANCE) GetWindowLongPtr( AfxGetMainWnd()->m_hWnd, GWLP_HINSTANCE );
   #else
       HINSTANCE hInst = (HINSTANCE) GetWindowLong( AfxGetMainWnd()->m_hWnd, GWL_HINSTANCE );
   #endif

   mDIInput.Create(hInst,AfxGetMainWnd()->m_hWnd);

   mD3DEngine.InitializeEngine(AfxGetMainWnd()->m_hWnd);
   mD3DEngine.EnvironmentSetup();

   mDXMusic.Create(AfxGetMainWnd()->m_hWnd);
   SetTimer( 0, 0, NULL );
}

Add the following source into direct3dview.h

  1. The include directivs for the directinput, direct3d, and directmusic
  2. Class variables for directinput, direct3d, and directmusic

#include "cdxinput.h"
#include "dsnEngine.h"
#include "music.h"

   Public
   CDXInput mDIInput;
   //3D Engine
   D3DEngine mD3DEngine;
   //Music Manager
   DXMusic mDXMusic;



Step 4 Add Source code to the WM_TIMER method OnTimer

  1. Arrow key events control camera movement
  2. Keys 1 and 2 play music segments

void CDirect3dView::OnTimer(UINT nIDEvent)
{
   // TODO: Add your message handler code here and/or call default
   long MouseX, MouseY;
   HRESULT hr;

   mDIInput.Update();

   mDIInput.GetMouseDeltas(&MouseX, &MouseY);


   if(mDIInput.GetKeyState(CDXKEY_UPARROW))
   {
       mD3DEngine.AdvanceCamera();
   }

   if(mDIInput.GetKeyState(CDXKEY_DOWNARROW))
   {
       mD3DEngine.RetreatCamera();
   }

   if(mDIInput.GetKeyState(CDXKEY_LEFTARROW))
   {
       mD3DEngine.CameraLeft();
   }

   if(mDIInput.GetKeyState(CDXKEY_RIGHTARROW))
   {
       mD3DEngine.CameraRight();
   }

   if(mDIInput.GetKeyState(CDXKEY_LEFTCTRL) || mDIInput.GetKeyState(CDXKEY_RIGHTCTRL))
   {
   }
   if(mDIInput.GetKeyState(CDXKEY_1))
   {
       hr = mDXMusic.g_pMusicSegments[0]->Play( DMUS_SEGF_DEFAULT, mDXMusic.g_p3DAudiopath );
   }
   if (mDIInput.GetKeyState(CDXKEY_2))
   {
       hr = mDXMusic.g_pMusicSegments[1]->Play( DMUS_SEGF_DEFAULT | DMUS_SEGF_SECONDARY,
                                                         mDXMusic.g_p3DAudiopath );
   }

   ProcessGeometry();
   
   CView::OnTimer(nIDEvent);
}

You might also like...

Comments

About the author

David Nishimoto United States

NishiSoft provides Part I of the Information Technology Project collaboration. Sign up and list your IT project tasks, assign task too friends, and get percent complete task.

Part will ...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr