if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice))) {
return E_FAIL;
}
//Turn on back face culling. This is becuase we want to hide the back of our polygons
g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
//Turn off lighting becuase we are specifying that our vertices have colour
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
return S_OK;
}
HRESULT InitialiseVertexBuffer() {
VOID* pVertices;
//Store each point of the cube together with it's colour
//Make sure that the points of a polygon are specified in a clockwise direction,
//this is because anti-clockwise faces will be culled
//We will use a three triangle strips to render these polygons (Top, Sides, Bottom).
CUSTOMVERTEX cvVertices[] = {
//Top Face
{-5.0f, 5.0f, –5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 0 – Blue
{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 – Red
{5.0f, 5.0f, –5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 2 – Red
{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 3 – Green
//Face 1
{-5.0f, –5.0f, –5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 4 – Red
{-5.0f, 5.0f, –5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 5 – Blue
{5.0f, –5.0f, –5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 6 – Green
{5.0f, 5.0f, –5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 7 – Red
//Face 2
{5.0f, –5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 8 – Blue
{5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 9 – Green
//Face 3
{-5.0f, –5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 10 – Green
{-5.0f, 5.0f, 5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 11 – Red
//Face 4
{-5.0f, –5.0f, –5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 12 – Red
{-5.0f, 5.0f, –5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 13 – Blue
//Bottom Face
{5.0f, –5.0f, –5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 14 – Green
{5.0f, –5.0f, 5.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 15 – Blue
{-5.0f, –5.0f, –5.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 16 – Red
{-5.0f, –5.0f, 5.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 17 – Green
};
//Create the vertex buffer from our device.
if (FAILED(g_pD3DDevice->CreateVertexBuffer(18 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVertexBuffer))) {
return E_FAIL;
}
//Get a pointer to the vertex buffer vertices and lock the vertex buffer
if (FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0))) {
return E_FAIL;
}
//Copy our stored vertices values into the vertex buffer
memcpy(pVertices, cvVertices, sizeof(cvVertices));
//Unlock the vertex buffer
g_pVertexBuffer->Unlock();
return S_OK;
}
void SetupRotation() {
//Here we will rotate our world around the x, y and z axis.
D3DXMATRIX matWorld, matWorldX, matWorldY, matWorldZ;
//Create the transformation matrices
D3DXMatrixRotationX(&matWorldX, timeGetTime()/400.0f);
D3DXMatrixRotationY(&matWorldY, timeGetTime()/400.0f);
D3DXMatrixRotationZ(&matWorldZ, timeGetTime()/400.0f);
//Combine the transformations by multiplying them together
D3DXMatrixMultiply(&matWorld, &matWorldX, &matWorldY);
D3DXMatrixMultiply(&matWorld, &matWorld, &matWorldZ);
//Apply the transformation
g_pD3DDevice->SetTransform(D3DTS_WORLD, &matWorld);
}
void SetupCamera() {
//Here we will setup the camera.
//The camera has three settings: "Camera Position", "Look at Position" and "Up Direction"
//We have set the following:
//Camera Position: (0, 0, –30)
//Look at Position: (0, 0, 0)
//Up direction: Y-Axis.
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f,-30.0f), //Camera Position
&D3DXVECTOR3(0.0f, 0.0f, 0.0f), //Look At Position
&D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //Up Direction
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matView);
}
void SetupPerspective() {
//Here we specify the field of view, aspect ration and near and far clipping planes.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 500.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
}
void Render() {
if (g_pD3DDevice == NULL) {
return;
}
//Clear the backbuffer to black
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
//Begin the scene
g_pD3DDevice->BeginScene();
//Setup the rotation, camera, and perspective matrices
SetupRotation();
SetupCamera();
SetupPerspective();
//Rendering our objects
g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); //Top
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 8); //Sides
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 2); //Bottom
//End the scene
g_pD3DDevice->EndScene();
//Filp the back and front buffers so that whatever has been rendered on the back buffer
//will now be visible on screen (front buffer).
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
Читать дальше