1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
// MYDIRECT3D.cpp ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.
#include "MYDIRECT3D.h"
MYDIRECT3D::MYDIRECT3D()
// The constructor sets the Direct3D object & device to NULL.
{
pd3d=NULL; // Direct3D object.
pd3dDevice=NULL; // Direct3D device.
}
MYDIRECT3D::~MYDIRECT3D()
// The destructor releases the Direct3D device & object.
{
// If NULL, do not release.
if(pd3dDevice)pd3dDevice->Release(); // Direct3D device.
if(pd3d)pd3d->Release(); // Direct3D object.
// Other stuff to release?
}
HRESULT MYDIRECT3D::CreateStage(HWND hWnd)
// Inititalizes Direct3D & calls d3dOnCreate(). Call only ONCE!
{
// Create Direct3D object.
pd3d=Direct3DCreate9(D3D_SDK_VERSION);
// Gather window data.
RECT Rect; GetClientRect(hWnd,&Rect);
WindowData.hInstance=GetModuleHandle(NULL);
WindowData.WindowHandle=hWnd;
WindowData.ClientWidth=Rect.right;
WindowData.ClientHeight=Rect.bottom;
// Presentation parameters.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed=TRUE;
d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow=WindowData.WindowHandle;
d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
d3dpp.BackBufferCount=1;
d3dpp.BackBufferWidth=WindowData.ClientWidth;
d3dpp.BackBufferHeight=WindowData.ClientHeight;
// Create the Direct3D device.
pd3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
WindowData.WindowHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice);
d3dOnCreate(); // Initialize other Direct3D/Application data.
return S_OK; // all OK.
}
HRESULT MYDIRECT3D::Render()
// Begins a rendering scene & then calls d3dOnRender(). When d3dOnRender()
// returns, the render scene ends & the back buffer is presented to the screen.
{
// Clear the Direct3D back buffer.
pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(80,80,80),1.0f,0);
pd3dDevice->BeginScene(); // Begin rendering.
d3dOnRender(); // Handles the drawing work.
pd3dDevice->EndScene(); // End rendering.
// Present the back buffer to display.
pd3dDevice->Present(NULL,NULL,NULL,NULL);
return S_OK; // all OK.
}
HRESULT MYDIRECT3D::InitViewport()
// Initializes the viewport & field of view matrices. This is critical if you
// are using untransformed vertices, otherwise, the scene comes up blank!
{
// Setup viewport.
D3DXVECTOR3 Eye(0,0,-10);
D3DXVECTOR3 Target(0,0,0);
D3DXVECTOR3 Up(0,1,0);
D3DXMATRIXA16 Viewport;
D3DXMatrixLookAtLH(&Viewport,&Eye,&Target,&Up);
pd3dDevice->SetTransform(D3DTS_VIEW,&Viewport);
// The viewport works the following way. The Eye is the position to look
// from. The Eye will always look at the Target. Up is the orientation of the
// Eye(i.e 0,-1,0) would flip the scene upside down, normally it is (0,1,0).
// Setup field of view.
D3DXMATRIX FieldOfView;
D3DXMatrixPerspectiveFovLH
(&FieldOfView, // Where to store data.
D3DX_PI*0.25f, // Field of view, in the y direction, in radians.
WindowData.ClientWidth/ // Client width.
WindowData.ClientHeight, // Client Height.
1,50); // near & far viewing ranges.
pd3dDevice->SetTransform(D3DTS_PROJECTION,&FieldOfView);
// WARNING!! Failure to setup the camera viewport & field of view will
// result in a blank scene. It is a MUST to setup these matrices or
// you will not see anything, save yourself tremendous frustration!
return S_OK; // all OK.
}
|