bate's blog

調べたこと実装したことなどを取りとめもなく書きます。

ビルボード的なやつ

ここでカンニングしつつ。
http://www.c3.club.kyutech.ac.jp/gamewiki/index.php?%A5%D3%A5%EB%A5%DC%A1%BC%A5%C9#w8301370

頂点バッファ作成時の頂点座標決める時に板がXY平面と平行になるようにした。
[ワールド変換行列] = [ワールドスケール行列] * [平行移動成分を無視したビュー行列の逆行列] * [ワールド回転行列] * [ワールド平行移動行列]

void cGround::render(ID3D11DeviceContext* deviceContext, cCamera* camera)
{
	UINT offset = 0;
	sConstantBuffer cb;
	deviceContext->IASetInputLayout( m_pVertexLayout );
	deviceContext->IASetVertexBuffers( 0, 1, &m_pVertexBuffer, &m_iStride, &offset );
	deviceContext->IASetIndexBuffer( m_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0 );
#if 1
	// ビルボード用
	XMMATRIX view = camera->getViewMatrix();
	view._41 = 0.f; view._42 = 0.f;
	view._43 = 0.f; view._44 = 1.f;
	XMVECTOR Determinant;
	XMMATRIX invView = XMMatrixInverse( &Determinant, view );
#else
	XMMATRIX invView = XMMatrixIdentity();
#endif

	// ワールド
	XMMATRIX mat = XMMatrixTranslation( 0.f, 0.f, 2.f );
	XMMATRIX rot = XMMatrixIdentity();
	XMMATRIX smat = XMMatrixScaling( 2.f, 2.f, 2.f );
	mat = smat * invView * rot * mat;

	cb.mWorld = XMMatrixTranspose( mat );
	cb.mView = XMMatrixTranspose( camera->getViewMatrix() );
	cb.mProjection = XMMatrixTranspose( camera->getProjectionMatrix() );
	cb.mInvView = XMMatrixTranspose( invView );
	deviceContext->UpdateSubresource( m_pConstantBuffer, 0, NULL, &cb, 0, 0 );

	deviceContext->VSSetShader( m_pVertexShader, NULL, 0 );
	deviceContext->VSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
	deviceContext->PSSetShader( m_pPixelShader, NULL, 0 );
	deviceContext->PSSetConstantBuffers( 0, 1, &m_pConstantBuffer );
	deviceContext->PSSetShaderResources( 0, 1, &m_pTextureRV );
    deviceContext->PSSetSamplers( 0, 1, &m_pSamplerLinear );
	deviceContext->DrawIndexed( 6, 0, 0 );
}