///////////////////////////////////////////////////////////////////// // // D3dView.cpp: implementation of the CD3dView class. // // kishan@hackorama.com http://www.hackorama.com // // Mon Nov 1 19:17:26 PST 1999 // ////////////////////////////////////////////////////////////////////// //standard headers #include "stdafx.h" #include "D3dView.h" //INCLUDE YOUR APPLICATION HEADER FILE HERE //FOR EXAMPLE IF YOUR APPLICATIO WAS CALLED NEO #include "neo.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CD3dView::CD3dView() { } CD3dView::~CD3dView() { } BOOL CD3dView::SetupDirect3DRetainedMode(HWND hWnd) { //Fire up D3D if(Direct3DRMCreate(&pMyD3DRetaindMode)!=D3DRM_OK){ TRACE("MyD3DView::FAILED TO CREATE DIRECT3D RM \n"); return FALSE; } //Create D3D Scene if(pMyD3DRetaindMode->CreateFrame(NULL,&pMyScene)!=D3DRM_OK){ TRACE("MyD3DView::FAILED TO CREATE SCENE FRAME\n"); return FALSE; } //Create a root frame under Scene ( this is the global coordinate system ) if(pMyD3DRetaindMode->CreateFrame(pMyScene,&pMyRootFrame)!=D3DRM_OK){ TRACE("MyD3DView::FAILED TO CREATE ROOT FRAME\n"); return FALSE; } //Create a camera under scene if(pMyD3DRetaindMode->CreateFrame(pMyScene,&pMyCamera)!=D3DRM_OK){ TRACE("MyD3DView::FAILED TO CREATE CAMERA FRAME\n"); return FALSE; } //Position the camera in the scene ( initially at 20 units away from origin ) if( pMyCamera->AddTranslation(D3DRMCOMBINE_REPLACE, 0.0, 0.0, -20.0 ) ) return FALSE; //Create the clipper if(DirectDrawCreateClipper(0,&pMyClipper,NULL)!=DD_OK){ TRACE("MyD3DView::FAILED TO CREATE DIRECTDRAW CLIPPER \n"); return FALSE; } //Associate clipper with the window if(pMyClipper->SetHWnd(0,hWnd)!=DD_OK){ TRACE("MyD3DView::FAILED TO ASSOCIATE THE WINDOW WITH CLIPPER\n"); return FALSE; } //Create Device and ViewPort if(!CreateDeviceAndViewPort()){ TRACE("MyD3DView::FAILED AT 'CreateDeviceAndViewPort()' \n"); return FALSE; } else{ if(!SetRenderingOptions()){ TRACE("MyD3DView::FAILED AT 'SetRenderingOptions()' \n"); return FALSE; } else{ TRACE("MyD3DView::SUCCESSFULLY CONFIGURED\n"); } } return TRUE; } BOOL CD3dView::SetRenderingOptions(void) { LPDIRECT3DRMLIGHT pAmbient = NULL; // Ambient light LPDIRECT3DRMLIGHT pDirectional = NULL; // Directional light // Create lights if(FAILED(pMyD3DRetaindMode->CreateLightRGB(D3DRMLIGHT_AMBIENT, 0.1f, 0.1f, 0.1f, &pAmbient))){ TRACE("MyD3DView::FAILED TO CREATE LIGHT \n"); return FALSE; } if(FAILED(pMyD3DRetaindMode->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL, 0.1f, 0.1f, 0.1f, &pDirectional))){ TRACE("MyD3DView::FAILED TO CREATE LIGHT \n"); return FALSE; } // Add lights to main scene if(FAILED(pMyScene->AddLight(pDirectional))) return FALSE; if(FAILED(pMyScene->AddLight(pAmbient))) return FALSE; // Set shading mode to gouraud, turn on the lights, solid object pMyDevice->SetQuality(D3DRMLIGHT_ON | D3DRMFILL_SOLID |D3DRMSHADE_GOURAUD); return TRUE; } BOOL CD3dView::CreateDeviceAndViewPort() { //The size of the viewport ( using arbitrary value 800,600 ) int right = 800; int bottom = 600; //Creating device from clipper if(pMyD3DRetaindMode->CreateDeviceFromClipper(pMyClipper,NULL, right,bottom,&pMyDevice)!=D3DRM_OK){ TRACE("MyD3DView::FAILED TO CREATE DEVICE \n"); return FALSE; } //Create a ViewPort if(pMyD3DRetaindMode->CreateViewport(pMyDevice,pMyCamera, 0,0, pMyDevice->GetWidth(), pMyDevice->GetHeight(), &pMyViewport) != D3DRM_OK){ TRACE("MyD3DView::FAILED TO CREATE VIEWPORT \n"); return FALSE; } // Set the Background to an arbitrarily large value pMyViewport->SetBack(500.0f); return TRUE; } void CD3dView::UpdateView() { pMyDevice->SetQuality(D3DRMRENDER_GOURAUD); //pMyDevice->SetQuality(D3DRMRENDER_WIREFRAME); pMyViewport->Clear(); // Clear the viewport pMyViewport->Render(pMyScene); // Render the scene pMyDevice->Update(); // Update Scene } void CD3dView::LoadXfile(CString filename) { //UpdateView(); LPDIRECT3DRMMESHBUILDER pMeshB; //I. Create a mesh builder on D3DRM ptr HRESULT hr = pMyD3DRetaindMode->CreateMeshBuilder( &pMeshB ); //II load an x file to the created mesh builder hr = pMeshB->Load((void*)(LPCTSTR)filename, NULL, D3DRMLOAD_FROMFILE, NULL, NULL); if(!hr){ //III create a local frame on Scene frame LPDIRECT3DRMFRAME pFrame; pMyD3DRetaindMode->CreateFrame(pMyRootFrame,&pFrame); //IV Now add the created mesh to the local frame. pFrame->AddVisual(pMeshB); } else{ TRACE("CMyD3DView::FAILED TO LOAD X FILE: %s\n",filename); } UpdateView(); TRACE("CMyD3DView::DID YOU SEE ANYTHING YET ?\n"); }