Code:
//FIREFLY: includes
#include "StdAfx.h"
#include "MyApp.h"
#include "d3d9.h"
#include "mmsystem.h"
#include <math.h>
//FIREFLY: definitions
const DWORD MY_VERTEX_TYPE = ( D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1 );
struct MyVertexType
{
float x;
float y;
float z;
D3DXVECTOR3 normal;
DWORD color;
};
MyApp::MyApp(void)
{
}
MyApp::~MyApp(void)
{
}
MyApp::MyApp(HWND hWnd)
{
this->hWnd = hWnd;
g_pD3D = NULL;
pDevice = NULL;
pMesh = NULL;
verBuff = NULL;
inBuff = NULL;
}
//FIREFLY: initilisation, create device
HRESULT MyApp::Init()
{
if( NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))){
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &pDevice ) ) ){
return E_FAIL;
}
D3DXVECTOR3 norm(0.5f,0.5f,0.5f); //define normal as middle of screen
//CLIENT STUFF HERE?
MyVertexType vertArray[] =
{
{0,0,0,norm ,D3DCOLOR_XRGB(0,255,255)}, //for rectangle
{0,240,0,norm ,D3DCOLOR_XRGB(0,255,255)},
{320,0,0,norm ,D3DCOLOR_XRGB(0,255,255)},
{320,240,0,norm ,D3DCOLOR_XRGB(0,255,255)}, //end of rectangle
{0,1,0,norm ,D3DCOLOR_XRGB(255,255,255)},
{1,-1,0,norm ,D3DCOLOR_XRGB(255,255,255)},
{-1,-1,0,norm ,D3DCOLOR_XRGB(255,255,255)},
};
unsigned int indexArray[] =
{ 0,1,1,3,3,2,2,0 };
if(FAILED(pDevice->CreateVertexBuffer(3*sizeof(MyVertexType),0,MY_VERTEX_TYPE,D3DPOOL_SYSTEMMEM,&verBuff,NULL)))
{
return E_FAIL;
}
if( FAILED(pDevice->CreateIndexBuffer(8*sizeof(MyVertexType),0,D3DFMT_INDEX32,D3DPOOL_SYSTEMMEM,&inBuff,NULL)))
{
return E_FAIL;
}
void *lockPoint;
if( FAILED( verBuff->Lock( 0,sizeof(vertArray),(void**)&lockPoint,0 ) ) )
{
return E_FAIL;
}
memcpy( lockPoint,vertArray, sizeof(vertArray));
verBuff->Unlock();
if( FAILED( inBuff->Lock( 0,sizeof(indexArray),(void**)&lockPoint,0 ) ) )
{
return E_FAIL;
}
memcpy( lockPoint,indexArray, sizeof(indexArray));
inBuff->Unlock();
return D3D_OK;
}
void MyApp::Render()
{
D3DCOLOR background = 0x000000; //set background colour
if (pDevice)
{
pDevice->Clear(0,0,(D3DCLEAR_TARGET| D3DCLEAR_ZBUFFER),background,1.0f,0);
pDevice->BeginScene();
pDevice->SetStreamSource(0,verBuff,0,sizeof(MyVertexType));
pDevice->SetIndices( inBuff );
pDevice->SetFVF(MY_VERTEX_TYPE);
//moves camera around
Transforms();
//what factors to render with
RenderStates();
//draw a basic triangle
//pDevice->DrawPrimitive(D3DPT_LINELIST,0,4);
pDevice->DrawIndexedPrimitive(D3DPT_LINELIST,0,0,4,0,4); //this draws the rectangle
//pDevice->DrawPrimitive(D3DPT_POINTLIST,4,13); //or alternatively, this draws the pointlist
//draw a basic poly
//pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0,0,4,0,2);
//draw a mesh
//pMesh->DrawSubset(0);
pDevice->EndScene();
pDevice->Present(0,0,0,0);
}
}
void MyApp::RenderStates()
{
pDevice->SetRenderState(D3DRS_CULLMODE,false );
pDevice->SetRenderState(D3DRS_LIGHTING,false );
pDevice->SetRenderState( D3DRS_AMBIENT, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f) );
pDevice->SetRenderState(D3DRS_SHADEMODE,3 );
pDevice->SetRenderState(D3DRS_SPECULARENABLE,false);
pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
}
void MyApp::Lighting()
{
}
void MyApp::Materials()
{
}
void MyApp::Transforms()
{
D3DXMATRIX pMatrix;
D3DXMATRIX RotMatrix;
D3DXVECTOR3 lookat(0,120,0); //where the camera starts
D3DXVECTOR3 up (0,1,0);
D3DXVECTOR3 eye(0, 0, -500); //how far away the camera is
//D3DXMatrixRotationX( &RotMatrix,D3DXToRadian(90) );
D3DXMatrixRotationY( &pMatrix,timeGetTime()/1000.0F);
//pMatrix = RotMatrix * pMatrix;
pDevice->SetTransform( D3DTS_WORLD, &pMatrix );
D3DXMatrixLookAtLH( &pMatrix,&eye,&lookat,&up);
pDevice->SetTransform( D3DTS_VIEW , &pMatrix );
D3DXMatrixPerspectiveFovLH( &pMatrix,D3DXToRadian(90),640/480,1.0f,1000.0f);
pDevice->SetTransform( D3DTS_PROJECTION , &pMatrix );
}
void MyApp::Shutdown()
{
g_pD3D->Release();
pDevice->Release();
//pMesh->Release();
verBuff->Release();
//inBuff->Release();
}
Here is an example of what it draws at present: