Пример #1
0
void Model::draw(D3DXMATRIX toWorld, DefaultShader* pShader)
{	
	// Transform AABB into the world space.
	m_MeshBones[0].toParentXForm = toWorld;	//Co can thiet lap lai matrix trong ham ve hay ko - hay dung trong ham update
	//m_MeshBones[0].toWorldXForm = getScaleMatrix()* getRotateMatrix();
	m_MeshBones[0].pos= D3DXVECTOR3(toWorld._41, toWorld._42, toWorld._43);

	buildBoneWorldTransforms();

	gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
	for(int i = 0; i < m_NumBones; ++i)
	{
		D3DXMATRIX world = m_MeshBones[i].toWorldXForm;		
		UINT numMtrl = m_MeshBones[i].getNumMaterial();
		UINT numTex = m_MeshBones[i].getNumTexture();

		D3DXMATRIX worldInvTrans;
		pShader->setWVP(&(world*gCamera->viewProj()));
		pShader->setWorld(&world, &worldInvTrans);

		for(UINT j = 0; j < numMtrl; ++j)
		{
			D3DMATERIAL9* pMat = m_MeshBones[i].getMaterial(j);
			Mtrl mat(pMat->Ambient, pMat->Diffuse, pMat->Specular, pMat->Power);
			pShader->setMaterial(&mat);
			// If there is a texture, then use.
			IDirect3DTexture9* pTex = m_MeshBones[i].getTexture(j);
			if( pTex != 0)
			{
				pShader->setTexture(pTex);
			}
			HR(pShader->getFX()->CommitChanges());
			HR(m_MeshBones[i].getMesh()->DrawSubset(j));
		}
	}
	gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
}
Пример #2
0
void RobotArmDemo::drawScene()
{
	// Clear the backbuffer and depth buffer.
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

	HR(gd3dDevice->BeginScene());

	HR(mFX->SetValue(mhLight, &mLight, sizeof(DirLight)));
	
	HR(mFX->SetTechnique(mhTech));
	UINT numPasses = 0;
	HR(mFX->Begin(&numPasses, 0));
	HR(mFX->BeginPass(0));

	// Build the world transforms for each bone, then render them.
	buildBoneWorldTransforms();
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T, -NUM_BONES, 0.0f, 0.0f);
	for(int i = 0; i < NUM_BONES; ++i)
	{
		// Append the transformation with a slight translation to better
		// center the skeleton at the center of the scene.
		mWorld = mBones[i].toWorldXForm * T;
		HR(mFX->SetMatrix(mhWVP, &(mWorld*mView*mProj)));
		D3DXMATRIX worldInvTrans;
		D3DXMatrixInverse(&worldInvTrans, 0, &mWorld);
		D3DXMatrixTranspose(&worldInvTrans, &worldInvTrans);
		HR(mFX->SetMatrix(mhWorldInvTrans, &worldInvTrans));
		HR(mFX->SetMatrix(mhWorld, &mWorld));
		for(int j = 0; j < mMtrl.size(); ++j)
		{
			HR(mFX->SetValue(mhMtrl, &mMtrl[j], sizeof(Mtrl)));
		
			// If there is a texture, then use.
			if(mTex[j] != 0)
			{
				HR(mFX->SetTexture(mhTex, mTex[j]));
			}

			// But if not, then set a pure white texture.  When the texture color
			// is multiplied by the color from lighting, it is like multiplying by
			// 1 and won't change the color from lighting.
			else
			{
				HR(mFX->SetTexture(mhTex, mWhiteTex));
			}
		
			HR(mFX->CommitChanges());
			HR(mBoneMesh->DrawSubset(j));
		}
	}

	HR(mFX->EndPass());
	HR(mFX->End());
	
	mGfxStats->display();

	HR(gd3dDevice->EndScene());

	// Present the backbuffer.
	HR(gd3dDevice->Present(0, 0, 0, 0));
}