////////////////////////////////////////////////////////////////////////////////////
//
\\
// This little function is called over and over again, and does the
rendering of \\
// our triangles...Its an all in one Pixel Shader function...the code has
been \\
// kept together for tutorial/explanation purposes...so you can see it in
action \\
// See how simple it really
is.... \\
//
\\
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void
Render()
{
if(!g_pd3dDevice)return;
// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
NEW NEW NEW NEW NEW NEW NEW
// CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED
CHANGED CHANGED CHANGED
const char
SimpleShader[] =
{
"vs.1.1 // Shader version 1.1 \n"
// Rotates our square
"m4x4 oPos, v0, c0 // oPos-Output position, v0-Input
position, c0-Constant register\n"
// Caluculates our new normals, and
applys them to our colour
"dp3 r0, v3, c4 // r0-Temp register, v3-Input
normal, c4-Constant\n"
"mul oD0, r0.x, v5 // oDn-Output colour, v5-Input
colour value\n"
};
// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
NEW NEW NEW NEW NEW NEW NEW
// CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED
CHANGED CHANGED CHANGED
DWORD dwDecl[] =
{
D3DVSD_STREAM(0),
D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
// D3DVSDE_POSITION=0 e.g. v0
D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3),
// D3DVSDE_NORMAL =3 e.g v3
D3DVSD_REG(D3DVSDE_DIFFUSE, D3DVSDT_D3DCOLOR),
// D3DVSDE_DIFFUSE =5 e.g. v5
D3DVSD_END()
};
DWORD dwShaderID;
ID3DXBuffer* pVS;
D3DXAssembleShader( SimpleShader, sizeof(SimpleShader)-1,
0, NULL, &pVS, NULL );
g_pd3dDevice->CreateVertexShader(dwDecl, (DWORD*)pVS->GetBufferPointer(),
&dwShaderID, 0);
//g_pd3dDevice->SetVertexShader( dwShaderID );
//g_pd3dDevice->DeleteVertexShader(dwDecl);
//pVS->Release();
// Setup the world, view, and projection matrices
WorldMatrices();
D3DXMATRIX mat;
D3DXMatrixMultiply(&mat, &matWorld, &matView);
D3DXMatrixMultiply(&mat, &mat, &matProj);
D3DXMatrixTranspose(&mat, &mat);
// +---
The data we are passing
// \|/
// |
g_pd3dDevice->SetVertexShaderConstant(0, &mat, 4);
// | |
// /|\ /|\
// | +-- Number of 4 byte
chunks!
// +-- Constant
Register c[0]
D3DXMATRIX m;
D3DXMatrixInverse(&m, NULL, &matWorld);
D3DXVECTOR3 vLight = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVec3TransformNormal(&vLight, &vLight, &m);
D3DXVec3Normalize(&vLight, &vLight);
vLight= -vLight;
g_pd3dDevice->SetVertexShaderConstant(4, &vLight, 1);
// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
NEW NEW NEW NEW NEW NEW NEW
// CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED
CHANGED CHANGED CHANGED
struct stVert
{
float x, y, z;
// D3DFVF_XYZ
float nx,ny,nz;
// D3DFVF_NORMAL
DWORD colour; // D3DFVF_DIFFUSE
};
UINT myFVFFormat = D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_NORMAL ;
// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
NEW NEW NEW NEW NEW NEW NEW
// CHANGED CHANGED CHANGED CHANGED CHANGED CHANGED
CHANGED CHANGED CHANGED
stVert myBox[]=
{
// xyz position normal
colour
// _________________ _______________
________
// / \ / \
/ \
{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff00ff00},
{ 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff00ff00},
{-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff00ff00},
{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff0ff000},
{ 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff0ff000},
{ 0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0xff0ff000}
};
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);
// Clear the backbuffer and the zbuffer
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(0x00,0x00,0xff),
1.0f, 0 );
// Draw our triangle.
g_pd3dDevice->SetStreamSource(0, pVertexBuffer,
sizeof(stVert));
g_pd3dDevice->SetVertexShader( dwShaderID );
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->DeleteVertexShader(dwShaderID);
pVS->Release();
pVertexBuffer->Release();
} |