Introduction
Direct3D is a group of Microsoft Api that allows the developer to control a 3D environment. The 3D environment processes vertexes through the Geometry pipeline. Step 1 transforms the vertexes into the World coordinates, step 2 applies camera matrices to the vertex, step 3 applies projection matrices that transform the vertex for a 3D coordinate to a 2D coordinate, and the 2D coordinate is normalized to window coordinates (this is mapping the viewport to the window).
In this tutorial we will use one of three objects: 1. A triangle with normals 2. A tiger loaded from a direct.x file 3. A cylinder calculated from an equation.
All the 3D code is encapsulated in the D3DEngine class. All music segment is defined in the DXMusic class. The DirectInput will be managed by CDX. For more information on CDX click here.
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
Step 2: Create a Method called ProcessGeometry. Add void ProcessGeometry to the direct3dview.h file. Insert source into direct3dview.cpp
Step 3: Use the Class Wizard to build a message map method for OnInitialUpdate for the following reasons
- To Instantiate the DirectInput class. The DirectInput class captures all keyboard, mouse, and joystick events.
- To create the DirectGraphics Devices. The DirectGraphic Devices allow the application to manage system and video card resources through apis.
- To Create the DirectMusic class. The DirectMusic class initializes and loads music segments to be played.
- To Create a Timer event running at maximum speed.
- 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
- The include directivs for the directinput, direct3d, and directmusic
- 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
- Arrow key events control camera movement
- 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);
}
Defining the Direct3D class
The constructor initializes the direct3D interfaces to NULL. It creates a Matrix Stack variable. Matrix Stacks allow the developer to create hierarchial transformation trees
by using push and pop methods. For the current matrix the developer call apply local transformations which rotate, scale, or translate from the objects local coordinate axis or the developer can elect to select tranformations relative from the parent matrix. All matrix multiplication is accumulative, so parent tranformations apply to the children.
- The BuildSceneGraph,TraverseSceneGraph, and CleanupSceneGraph are used to build a hierarchial tree, traverse it and run mapped functions (DrawObject1,DrawObject2,DrawObject3), and cleanup the scenegraph when the base class is destroyed.
- The InitializeEngine creates the Direct3d object, gets the current desktop display mode, create the direct3d device which allows control of video card resources.
- The InitializeGeometry, InitializeGeometry2, and LoadTigerModel create data structures that contain vertex information. The vertex information is streamed to one of two vertex buffers used to by DrawObject1, DrawObject2, or DrawObject3.
- The SetEnvironment method trues on Z buffering, enables Back Face Removal, and calls InitializeGeometry, InitializeGeometry2, and LoadTigerModel.
- The Render method clears both the backbuffer and zbuffer; begins the scene; sets up the world, view, and project matrixes, and traverses the scenegraph; and ends the scene.
- Introduction to Direct 3D (0)2007/04/29
- Direct X (Direct Draw3) (0)2007/04/05
- Direct X (Direct Draw2) (0)2007/04/05
- Direct X (Direct Draw) (0)2007/04/05
- 얼굴인식의 구현과 유사도 판단-5 (0)2005/09/14

수안이의 컴퓨터 연구실



Leave your greetings.