hackorama
hackorama
D3DView - Qickstart class for using Direct3D with MFC

D3DView is a simple class which you can use with your MFC applications, to create a Direct3D rendering window. D3DView converts the applications View into Direct3D RenderingMode view.

The notes below assumes you are using VisualC++ 5.0 or 6.0 and creating a skeleton application ( MFC appwizard [exe] project type ) using appwizard.

Get the Code

D3DView class - D3dView.cpp + D3dView.h.

Neo.zip is a sample application which uses the D3DView class with a skeleton MFC application.



Building neo

1.If you dont have DirectX libraries get it from DirectX Site

2.Unzip neo.zip.

3.Open neo.dsw in VC++.

4.Make sure you have the D3D libraries selectd for linking.

In project-Settings select Link tab and add "d3drm.lib ddraw.lib dxguid.lib " in Object/Library modules.



CD3dView class

CD3dView class creates a D3D rendering window.

You have to pass the handle to the DC of your MFC application's view to D3dView class to convert your view to a D3D rendering view.

1.Include yourapp.h in D3dView.cpp

2.You should define an object of CD3dView in your applications View class

In youappView.h define a member variable of CD3dView.

........ CD3dView* m_MyD3dView; ........

In the implementation file yourappView.cpp

........ #include "D3dView.h" ........ ........ CYourappView::CYourappView() { // TODO: add construction code here m_MyD3dView = (CD3dView*) new CD3dView(); } ........

[ In the sample code Neo these files will be neo.h, neoView.cpp, neoView.h and the View class will be CNeoView ]

How to use this class, is explained in the following section which describes the 3 public methods of this class



The public methods in Cd3dView

D3dView::SetupDirect3DRetainedMode(HWND hWnd);

CD3dView::LoadXfile(CString filename);

CD3dView::UpdateView();



SetupDirect3DRetainedMode

D3dView::SetupDirect3DRetainedMode(HWND hWnd);

Initialises the window as a Direct3D Rendering Mode window. Call this from the OnCreate() of your applications View class.

[ Go to class wizard and selct CYourappView and select WM_CREATE Message which has the member function OnCreate. ]

int CYourappView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CListView::OnCreate(lpCreateStruct) == -1) return -1; // Initialising the Direct3D Rendering Mode if(! m_MyD3dView->SetupDirect3DRetainedMode(m_hWnd) ){ AfxMessageBox("could not not Setup Direct3DRM"); PostQuitMessage(0); } return 0; }



LoadXfile

CD3dView::LoadXfile(CString filename);

This is a utility function which loads an .x file into the already created D3DRM view. ( just to see something on the view. The x files are available at your DirectX installation path ..\mssdk\samples\Multimedia\D3DRM\media or download teapot.x )

You can load this in any event handler function. Like in a toolbar button event handler in MainFrame class.

[ Go to Resource View of yourapp resources->Toolbar->IDR_MAINFRAME ] Then using Classwizrd select X_BUTTON and select COMMAND message ]

If using the new appwizard "Windows explorer style" ( new for VisualC++ 6.0 )

void CMainFrame::OnXbutton() { // TODO: Add your command handler code here CYourappView* pView = GetRightPane(); CString filename("mslogo.x"); pView->m_MyD3dView->LoadXfile(filename); }

Or if using MFC appwizard "Normal style " ( the default for 5.0 also in 6.0 )

void CMainFrame::OnXbutton() { // TODO: Add your command handler code here CYourappView* pView = (CYourappView*) GetActiveView(); CString filename("mslogo.x"); pView->m_MyD3dView->LoadXfile(filename); }



UpdateView

CD3dView::UpdateView();

This on updates the Direct3D RM view. Call this whenever you need to redraw the D3DRM window. ( You may need to call this whenever the window is resized or moved around etc. or whenever you have rendered something new )


Sunday, 02-Dec-2001 19:54:38 PST kishan at hackorama dot com