Code:
#include "TexturedPlane.h"
struct PlaneVertex
{
PlaneVertex(float x,float y,float z,float u,float v):x(x),y(y),z(z),u(u),v(v) {};
float x,y,z,u,v;
static const DWORD FVF;
};
const DWORD FVF=D3DFVF_XYZ | D3DFVF_TEX1;
TexturedPlane::TexturedPlane()
{
_Device=0;
_Texture=0;
_height=0.0f;
_scale=0.0f;
_VBuf=0;
_IBuf=0;
}
void TexturedPlane::Init(IDirect3DDevice9 *device,std::string File,float height,float scale)
{
_Device=device;
_height=height;
_scale=scale;
TextureFile=File;
D3DXCreateTextureFromFile(Device,File.c_str(),&Texture);
_Device->CreateVertexBuffer(4*sizeof(SkyPlaneVertex),
D3DUSAGE_WRITEONLY,
PlaneVertex::FVF,
D3DPOOL_MANAGED,
&_VBuf,
0);
PlaneVertex* v=0;
_VBuf->Lock(0,0,(void **)&v,0);
v[0]=SkyPlaneVertex(-1.0f*scale,-1.0f*height,-1.0f*scale,0.0f,1.0f);
v[1]=SkyPlaneVertex(-1.0f*scale,-1.0f*height, 1.0f*scale,0.0f,0.0f);
v[2]=SkyPlaneVertex( 1.0f*scale,-1.0f*height, 1.0f*scale,1.0f,0.0f);
v[3]=SkyPlaneVertex( 1.0f*scale,-1.0f*height,-1.0f*scale,1.0f,1.0f);
_VBuf->Unlock;
_Device->CreateIndexBuffer(6*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&_IBuf,
0);
WORD *indices=0;
_IBuf->Lock(0,0,(void **)&indices,0);
//Counter clockwise winding order - can see when below
//0,1,2 3,5,4 -> clockwise winding order - can see when above
indices[0]=0;indices[1]=2;indices[2]=1;
indices[3]=0;indices[4]=3;indices[5]=2;
_IBuf->Unlock();
}
void TexturedPlane::Render(D3DXVECTOR3 &cameraVec)
{
D3DXMATRIX W;
D3DXMatrixTranslation(&W,
cameraVec.x,
cameraVec.y,
cameraVec.z);
_Device->SetTransform(D3DTS_WORLD,&W);
_Device->SetStreamSource(0,VB,0,sizeof(SkyPlaneVertex));
_Device->SetFVF(SkyPlaneVertex::FVF);
_Device->SetIndices(IB);
_Device->SetTexture(0,_Texture);
_Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0,0,4,0,2);
}