void
Render()
{
if(!g_pd3dDevice)return;
//<-------------------Start of our pixel shader
code--------------------------->
const char
SimpleShader[] =
{
"ps.1.0 // Pixel Shader Version 1.0 \n"
"tex t0 // Declare that where using texture
t0\n"
"mov
r0, t0 // Output texture to the screen\n"
};
DWORD dwShaderID;
ID3DXBuffer* pVS;
HRESULT e =D3DXAssembleShader( SimpleShader, sizeof(SimpleShader)-1,
0, NULL, &pVS, NULL );
g_pd3dDevice->CreatePixelShader( (DWORD*)pVS->GetBufferPointer(), &dwShaderID
);
// g_pd3dDevice->SetPixelShader( dwShaderID );
// Don't forget to put these lines in your
code...I've added them to the bottom
// of the function!...always release resources
after creating them.
// g_pd3dDevice->DeletePixelShader(dwShaderID);
// pVS->Release();
//<-------------------End of our Pixel Shader
code----------------------------->
struct stVert
{
FLOAT x, y, z; // D3DFVF_XYZ
FLOAT tu, tv; // D3DFVF_TEX1
};
UINT myFVFFormat = D3DFVF_XYZ | D3DFVF_TEX1 ;
stVert myBox[]=
{
{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f },
{ 0.5f, 0.5f, 0.0f, 1.0f, 1.0f },
{-0.5f, 0.5f, 0.0f, 0.0f, 1.0f },
{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f },
{ 0.5f, 0.5f, 0.0f, 1.0f, 1.0f },
{ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f }
};
IDirect3DVertexBuffer8 * pVertexBuffer;
g_pd3dDevice->CreateVertexBuffer( 6*sizeof(stVert),
0,
myFVFFormat, D3DPOOL_MANAGED, &pVertexBuffer
);
unsigned char
*p;
pVertexBuffer->Lock(0,0, &p, 0);
memcpy(p, myBox, 6*sizeof(stVert) );
pVertexBuffer->Unlock();
//Turn off lighting becuase we are specifying that
our vertices have colour
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);
IDirect3DTexture8* pTexture;
D3DXCreateTextureFromFile(g_pd3dDevice, "image.bmp", &pTexture);
// Clear the backbuffer and the zbuffer
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0x00,0x00,0xff),
1.0f, 0 );
g_pd3dDevice->SetStreamSource(0, pVertexBuffer,
sizeof(stVert));
//<-------------------Using our Pixel Shader Here
---------------------------->
g_pd3dDevice->SetPixelShader( dwShaderID );
g_pd3dDevice->SetVertexShader(myFVFFormat);
g_pd3dDevice->SetTexture(0,pTexture);
// Draw our triangle.
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// End the scene
g_pd3dDevice->EndScene();
// Present the backbuffer contents to the display
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
g_pd3dDevice->DeletePixelShader(dwShaderID);
pVS->Release();
pTexture->Release();
pVertexBuffer->Release();
} |