// Pass the world matrix and view/projection matrix to the vertex shader
void VS_XFormFn( int method, CMatrix4x4* worldMatrix, CCamera* camera )
{
	LPD3DXCONSTANTTABLE shaderConsts = renderMethods[method].vertexConsts;

	D3DXMATRIXA16 matViewProj = ToD3DXMATRIX( camera->GetViewProjMatrix() );
	shaderConsts->SetMatrix( g_pd3dDevice, "ViewProjMatrix", &matViewProj );

	D3DXMATRIX* matWorld = ToD3DXMATRIXPtr( worldMatrix );
	shaderConsts->SetMatrix( g_pd3dDevice, "WorldMatrix", matWorld );
}
//-----------------------------------------------------------------------
void CEnvironmentMap::modelShaderApply(OwnMatrix4x4 *pModelMtx /*= NULL*/, bool bRenderEnvMappedMesh)
{
	LPD3DXCONSTANTTABLE constable = mVtxConstTable;

	OwnMatrix4x4 modelMtx, viewProjMtx, modelViewProjMtx;

	if (NULL == pModelMtx)
		modelMtx.setIdentity();
	else
		modelMtx = *pModelMtx;

	viewProjMtx.setMultiply(mCamera->mViewTrans, mCamera->mProjectionTrans);
	modelViewProjMtx.setMultiply(modelMtx, viewProjMtx);

	HRESULT hr;
	D3DXHANDLE handle;

	D3DXHANDLE modelViewProj = mVtxConstTable->GetConstantByName(0, "u_ModelViewProjMatrix");
	hr = mVtxConstTable->SetMatrix(mD3DDevice, modelViewProj, (D3DXMATRIX*)&modelViewProjMtx);

	D3DXHANDLE u_ModelMatrix = constable->GetConstantByName(0, "u_ModelToWorld");
	hr = constable->SetMatrix(mD3DDevice, u_ModelMatrix, (D3DXMATRIX*)&modelMtx);

	LPD3DXCONSTANTTABLE pixConstable = mPxlConstTable;

	float value = bRenderEnvMappedMesh ? 0.5f : 0;
	D3DXHANDLE reflectivity = pixConstable->GetConstantByName(0, "$reflectivity");
	hr = pixConstable->SetFloat(mD3DDevice, reflectivity, value);

}
// draw draws the set of graphics primitives using the object's world
// transformation and reflected colour
//
void Graphic::draw(const iObject* object, int i) {

    // if graphic is not setup, set it up
    if (!isSetup) setup(object);

    if (isSetup) {
		#if   PIPELINE == FIXED_FUNCTION
		#elif PIPELINE == PROGRAMMABLE || PIPELINE == PROGRAMMABLE_EFFECT
		Display* disp = reinterpret_cast<Display*>(display);
		Matrix worldView = object->world() * disp->viewMatrix();
		Matrix worldViewProjection = worldView * disp->projectionMatrix();
		#endif
        #if   PIPELINE == FIXED_FUNCTION
        d3dd->SetTransform(D3DTS_WORLD, (D3DXMATRIX*)(&object->world()));
        d3dd->SetMaterial(&mat);
        if (!strcmp(technique, "opaque") || !strcmp(technique, "skybox"))
            d3dd->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
        else if (!strcmp(technique, "translucent"))
            d3dd->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
        vertexList->draw(i);
        #elif PIPELINE == PROGRAMMABLE
        LPD3DXCONSTANTTABLE vertexTable = (LPD3DXCONSTANTTABLE)uniformVS;
        LPD3DXCONSTANTTABLE fragmentTable = (LPD3DXCONSTANTTABLE)uniformFS;
        vertexTable->SetMatrix(d3dd, "world", (D3DXMATRIX*)&object->world());
        fragmentTable->SetFloatArray(d3dd, "material.ambient", (FLOAT*)&ambient, 4);
        fragmentTable->SetFloatArray(d3dd, "material.diffuse", (FLOAT*)&diffuse, 4);
        fragmentTable->SetFloatArray(d3dd, "material.specular", (FLOAT*)&specular, 4); 
        fragmentTable->SetFloat(d3dd, "material.power", (FLOAT)power);
        if (!strcmp(technique, "opaque") || !strcmp(technique, "skybox"))
            d3dd->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
        else if (!strcmp(technique, "translucent"))
            d3dd->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
        #elif PIPELINE == PROGRAMMABLE_EFFECT
        effect->SetInt(stagesHandle,      nStages);
        effect->SetMatrix(worldHandle,    (D3DXMATRIX*)&object->world());
        effect->SetVector(ambientHandle,  (D3DXVECTOR4*)&ambient);
        effect->SetVector(diffuseHandle,  (D3DXVECTOR4*)&diffuse);
        effect->SetVector(specularHandle, (D3DXVECTOR4*)&specular);
        effect->SetFloat(powerHandle,     power);
		effect->SetFloat(roughnessHandle, object->getRoughness());
		effect->SetFloat(refractionHandle,object->getIndexOfRefraction());
		effect->SetInt(algorithmHandle,   object->getAlgorithm());
		effect->SetMatrix(worldViewHandle, (D3DXMATRIX*)&worldView);
		effect->SetMatrix(clipMatrixHandle, (D3DXMATRIX*)&worldViewProjection);
        effect->CommitChanges();
        Matrix viewProjectionBak;
        effect->GetMatrix("viewProjection", (D3DXMATRIX*)&viewProjectionBak);
        effect->SetTechnique(techniqueHandle);
        unsigned nPasses;
        effect->Begin(&nPasses, 0);
        for (unsigned iPass = 0; iPass < nPasses; iPass++) {
            effect->BeginPass(iPass);
            vertexList->draw(i);
            effect->EndPass();
        }
        effect->End();
        #endif
    }
}
// Pass data to vertex shaders that perform vertex lighting (1 point light)
// Passes full range of data - some shaders don't need all of it. This
// reduces the number of these functions at the expense of redundancy
void VS_VertLit1Fn( int method, CMatrix4x4* worldMatrix, CCamera* camera )
{
	LPD3DXCONSTANTTABLE shaderConsts = renderMethods[method].vertexConsts;

	D3DXMATRIXA16 matViewProj = ToD3DXMATRIX( camera->GetViewProjMatrix() );
	shaderConsts->SetMatrix( g_pd3dDevice, "ViewProjMatrix", &matViewProj );

	D3DXMATRIX* matWorld = ToD3DXMATRIXPtr( worldMatrix );
	shaderConsts->SetMatrix( g_pd3dDevice, "WorldMatrix", matWorld );

	D3DXVECTOR3 cameraPos = ToD3DXVECTOR( camera->Position() );
	shaderConsts->SetFloatArray( g_pd3dDevice, "CameraPosition", (FLOAT*)&cameraPos, 3 ); // If needed

	shaderConsts->SetFloatArray( g_pd3dDevice, "AmbientLight", (FLOAT*)&m_AmbientLight, 3 );
	shaderConsts->SetFloatArray( g_pd3dDevice, "LightPosition", (FLOAT*)&m_Lights[0]->GetPosition(), 3 );
	shaderConsts->SetFloatArray( g_pd3dDevice, "LightColour", (FLOAT*)&m_Lights[0]->GetColour(), 3 );
	shaderConsts->SetFloat( g_pd3dDevice, "LightBrightness", m_Lights[0]->GetBrightness() );

	shaderConsts->SetFloatArray( g_pd3dDevice, "MaterialColour", (FLOAT*)&m_DiffuseColour, 3 ); // If needed
	shaderConsts->SetFloatArray( g_pd3dDevice, "SpecularStrength", (FLOAT*)&m_SpecularColour, 3 ); // If needed
	shaderConsts->SetFloat( g_pd3dDevice, "SpecularPower", m_SpecularPower ); // If needed
}
void Renderer::SetConstantTableMatrix( LPD3DXCONSTANTTABLE pConstantTable, char* pConstantName, D3DXMATRIX* pMatrix )
{
	pConstantTable->SetMatrix( m_pD3DDevice, pConstantName, pMatrix );
}