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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include "Direct3D.h"
DefineSingleton(Direct3D);
Direct3D::Direct3D(void)
{
m_Rotate = 0.0f;
}
Direct3D::~Direct3D(void)
{
m_vbuffer->Release(); // close and release the vertex buffer
m_D3Ddev->Release(); // close and release the 3D device
m_D3D->Release(); // close and release Direct3D
}
int
Direct3D::CreateDevice(HWND hWindow,int ScreenWidth, int ScreenHeight,bool Windowed)
{
HRESULT hRes; //Variable for error checking
m_screenWidth = ScreenWidth;
m_screenHeight = ScreenHeight;
m_Windowed = Windowed;
m_D3D = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
if(Windowed)
{
d3dpp.Windowed = TRUE; // program windowed
}
else
{
d3dpp.Windowed = FALSE; // program fullscreen
}
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWindow; // set the window to be used by Direct3D
//This sets the bit channels to 8 per channel
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = ScreenWidth; // set the width of the buffer
d3dpp.BackBufferHeight = ScreenHeight; // set the height of the buffer
// create a device class using this information and information from the d3dpp stuct
hRes = m_D3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&m_D3Ddev);
if (hRes != S_OK)
{
return 0;
}
//Set the light
m_D3Ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // turn off the 3D lighting
return 1;
}
void
Direct3D::RestoreSurfaces()
{
}
void
Direct3D::ClearSurface(HWND hWindow)
{
}
void
Direct3D::PresentBackBuffer(HWND hWindow)
{
}
void
Direct3D::RenderFrame()
{
//As an example clear window to blue
m_D3Ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
m_D3Ddev->BeginScene(); // begins the 3D scene
//Any 3D rendering onto the back buffer to be done here
// select which vertex format we are using
m_D3Ddev->SetFVF(CUSTOMFVF);
/************************************************************************************************************************/
//SET UP PIPELINE
/*************************************************************************************************************************/
//WORLD TRANSFORMATION
/*************************************************************************************************************************/
D3DXMATRIX matrixRotateY; // a matrix to store the rotation information on X axis
m_Rotate += 0.5f;
// build a matrix to rotate the model by Rotate
D3DXMatrixRotationX(&matrixRotateY, m_Rotate);
// tell Direct3D about our matrix
m_D3Ddev->SetTransform(D3DTS_WORLD, &matrixRotateY);
/*****************************************************************************************************************************/
//VIEW TRANSFORMATION
/******************************************************************************************************************************/
D3DXMATRIX matrixView; // the view transform matrix
D3DXMatrixLookAtLH(&matrixView,
&D3DXVECTOR3 (100.0f, 100.0f, 100.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
m_D3Ddev->SetTransform(D3DTS_VIEW, &matrixView); // set the view transform to matrixView
/***********************************************************************************************************************/
//PROJECTION TRANSFORMATION
/***********************************************************************************************************************/
D3DXMATRIX matrixProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matrixProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)m_screenWidth / (FLOAT)m_screenHeight, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
m_D3Ddev->SetTransform(D3DTS_PROJECTION, &matrixProjection); // set the projection transform
/***************************************************************************************************************************/
// select the vertex buffer to display
m_D3Ddev->SetStreamSource(0, m_vbuffer, 0, sizeof(CustomVertex));
// copy the vertex buffer to the back buffer
m_D3Ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
m_D3Ddev->EndScene(); // ends the 3D scene
m_D3Ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame
}
void
Direct3D::InitialiseGraphics(Triangle* tri)
{
// create the vertex and store the pointer into v_buffer
m_D3Ddev->CreateVertexBuffer(tri->NumVertices*sizeof(CustomVertex),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&m_vbuffer,
NULL);
VOID* pVoid; // the void pointer
m_vbuffer->Lock(0, 0, (void**)&pVoid, 0); // lock the vertex buffer
memcpy(pVoid, tri->m_Vertex, sizeof(tri->m_Vertex)); // copy the vertices to the locked buffer
m_vbuffer->Unlock(); // unlock the vertex buffer
}
LPDIRECT3DDEVICE9
Direct3D::GetDirect3DObject()
{
return m_D3Ddev;
}
|