#include "StdAfx.h"
#include "Laser.h"

const Vertex cubeVertexData[] = 
{
	{D3DXVECTOR3(0,0,0),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),0,0},
	{D3DXVECTOR3(1,0,0),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),1,0},
	{D3DXVECTOR3(1,1,0),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),1,1},
	{D3DXVECTOR3(0,1,0),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),0,1},
	{D3DXVECTOR3(0,0,1),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),1,1},
	{D3DXVECTOR3(1,0,1),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),0,1},
	{D3DXVECTOR3(1,1,1),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),0,0},
	{D3DXVECTOR3(0,1,1),D3DXVECTOR3(0,0,0),D3DCOLOR_XRGB(255,255,0),1,0},
};

const WORD cubeIndexData[] = 
{
	0,1,2, 0,2,3,
	4,5,6, 4,6,7,
	
	0,1,5, 0,5,4,
	3,2,6, 3,6,7,

	0,4,7, 0,7,3,
	1,5,6, 1,6,2
};


CLaser::CLaser(CDirect3D *d3d):
  C3DObject(d3d->g_pd3dDevice)
{
 this->d3d = d3d;

 vertex_count = 8;
 faces_count = 12;
 
 device->CreateVertexBuffer(
 	vertex_count * sizeof(Vertex),
	D3DUSAGE_WRITEONLY,
	Vertex::FVF,
	D3DPOOL_MANAGED,
	&VB,
	0);
 device->CreateIndexBuffer(
	3*faces_count * sizeof(WORD),
	D3DUSAGE_WRITEONLY,
	D3DFMT_INDEX16,
	D3DPOOL_MANAGED,
	&IB,
	0);
 
 Vertex* vertices;
 VB->Lock(0, 0, (void**)&vertices, 0);
  memcpy(vertices, cubeVertexData, sizeof(cubeVertexData));
 VB->Unlock();

 WORD* indices = 0;
 IB->Lock(0, 0, (void**)&indices, 0);
  memcpy(indices, cubeIndexData, sizeof(cubeIndexData));
 IB->Unlock();
 
 Scale(0.1,0.1,1); 
 Translate (-ObjectCenter().x,-ObjectCenter().y,0);
}

 void CLaser::Render ()
 {
	if( d3d->g_pd3dDevice)
	{
	  d3d->g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
	  d3d->g_pd3dDevice->SetTexture(0, NULL);
   
	  d3d->g_pd3dDevice->SetStreamSource(0, VB, 0, sizeof(Vertex));
	  d3d->g_pd3dDevice->SetIndices(IB);
	  d3d->g_pd3dDevice->SetFVF(Vertex::FVF);
	  d3d->g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0, 0, vertex_count, 0, faces_count);

	}
 }

CLaser::~CLaser(void)
{
}
