示例#1
1
void FBXObject::SetupDrawConstantBuffer()
{
	static std::vector<XMFLOAT4X4> mBoneTransforms(128);
	for(std::size_t i = 0; i < this->AnimController.Mesh.second->mNumBones; ++i)
    {
		XMMATRIX invBind = XMLoadFloat4x4(&this->AnimController.Mesh.second->mSkeleton[i].invBindPose);
		XMMATRIX currPose = XMLoadFloat4x4(&this->AnimController.CurrentGlobalPose[i]);
		XMMATRIX total = invBind * currPose;
		XMMATRIX invTotal = XMMatrixTranspose( total);
		XMFLOAT4X4 inv;
		XMStoreFloat4x4(&inv, invTotal);
	
		mBoneTransforms[i] = inv;
	}

	cBuffer::cbChangeEveryFrame cb;
	XMFLOAT4X4 world = this->object.CalculateMatrix();
	cb.mWorld = XMLoadFloat4x4(&world);
	cb.colour.diffuse = this->object.Colour.Diffuse;
	cb.colour.ambient = this->object.Colour.Ambient;
	cb.colour.spec = this->object.Colour.Spec;

	ID3D11DeviceContext* pImmediateContext = DX11App::getInstance()->direct3d.pImmediateContext;

	pImmediateContext->UpdateSubresource( this->pCBChangesEveryFrame.second, 0, NULL, &cb, 0, 0 );    	
    pImmediateContext->VSSetConstantBuffers( 2, 1, &(this->pCBChangesEveryFrame.second) );
	pImmediateContext->PSSetConstantBuffers( 2, 1, &(this->pCBChangesEveryFrame.second) );

	pImmediateContext->UpdateSubresource( this->pAnimBonesBuffer.second, 0, NULL, &(mBoneTransforms.front()), 0, 0 );
	pImmediateContext->VSSetConstantBuffers( 3, 1, &(this->pAnimBonesBuffer.second) );
}
void VerticalBlurShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, const XMMATRIX &worldMatrix, const XMMATRIX &viewMatrix, const XMMATRIX &projectionMatrix, ID3D11ShaderResourceView* texture, float height)
{
	HRESULT result;
	D3D11_MAPPED_SUBRESOURCE mappedResource;
	MatrixBufferType* dataPtr;
	ScreenSizeBufferType* widthPtr;
	unsigned int bufferNumber;
	XMMATRIX tworld, tview, tproj;


	// Transpose the matrices to prepare them for the shader.
	tworld = XMMatrixTranspose(worldMatrix);
	tview = XMMatrixTranspose(viewMatrix);
	tproj = XMMatrixTranspose(projectionMatrix);

	// Lock the constant buffer so it can be written to.
	result = deviceContext->Map(m_matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);

	// Get a pointer to the data in the constant buffer.
	dataPtr = (MatrixBufferType*)mappedResource.pData;

	// Copy the matrices into the constant buffer.
	dataPtr->world = tworld;// worldMatrix;
	dataPtr->view = tview;
	dataPtr->projection = tproj;

	// Unlock the constant buffer.
	deviceContext->Unmap(m_matrixBuffer, 0);

	// Set the position of the constant buffer in the vertex shader.
	bufferNumber = 0;

	// Now set the constant buffer in the vertex shader with the updated values.
	deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);

	//Additional
	// Send light data to pixel shader
	deviceContext->Map(m_ScreenSizeBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	widthPtr = (ScreenSizeBufferType*)mappedResource.pData;
	widthPtr->screenHeight = height;
	widthPtr->padding = XMFLOAT3(1.0f, 1.f, 1.f);
	deviceContext->Unmap(m_ScreenSizeBuffer, 0);
	bufferNumber = 1;
	deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_ScreenSizeBuffer);

	// Set shader texture resource in the pixel shader.
	deviceContext->PSSetShaderResources(0, 1, &texture);
}
    void DXCamera2D::VSetOrthoRHOffCenter(OrthoRect rect, float zNear, float zFar)
    {
        m_rect = rect;

        XMMATRIX P = XMMatrixOrthographicOffCenterRH(rect.left, rect.right, rect.bottom, rect.top, zNear, zFar);
        XMStoreFloat4x4(&m_projection, XMMatrixTranspose(P));
    }
示例#4
0
void c_app_scene::on_frame_render(ID3D11Device *d3d11_device, ID3D11DeviceContext *d3d11_device_context, double time, float elapsed_time)
{
    XMMATRIX mat_world;
    XMMATRIX mat_view;
    XMMATRIX mat_proj;
    XMMATRIX mat_inv_proj;
    XMMATRIX mat_wvp;
    
    mat_world = convert_d3dxmat_to_xnamat(*m_camera->GetWorldMatrix()); 
    mat_proj = convert_d3dxmat_to_xnamat(*m_camera->GetProjMatrix());
    mat_view = convert_d3dxmat_to_xnamat(*m_camera->GetViewMatrix());
    mat_wvp = mat_world * mat_view * mat_proj;
    XMVECTOR det_vec; 
    mat_inv_proj = XMMatrixInverse(&det_vec, mat_proj); 
    
    //////////////////////////////////////////////////////////////////////////
    // Set input layout
    d3d11_device_context->IASetInputLayout(m_mesh_input_layout);

    //////////////////////////////////////////////////////////////////////////
    // Update constant buffer
    cb0 cb;
    cb.mat_wvp = XMMatrixTranspose(mat_wvp);  
    d3d11_device_context->UpdateSubresource(m_cb0, 0, NULL, &cb, 0, 0);
    m_cb0_var->SetConstantBuffer(m_cb0);
    
    //////////////////////////////////////////////////////////////////////////
    // Render mesh
    m_render_scene_tech->GetPassByIndex(0)->Apply(0, d3d11_device_context); 
    m_city_mesh->Render(d3d11_device_context, 0); 
    m_column_mesh->Render(d3d11_device_context, 0); 
    
}
示例#5
0
void Transform::UpdateCBuffer()
{
	//updatesubresource med den nya transformData
	XMMATRIX tempWorld = XMMatrixIdentity();

	XMMATRIX tempScale = XMMatrixIdentity();
	XMMATRIX tempRotation = XMMatrixIdentity();
	XMMATRIX tempPosition = XMMatrixIdentity();

	tempScale = XMMatrixScaling(transformData.scale.x, transformData.scale.y, transformData.scale.z);
	//XMMatrixRotationQuaternion använd en quaternion istället! cool stuff, sen bör det funka
	//transformData.pos.z = transformData.pos.z * -1.0f;
	//transformData.rot.x = transformData.rot.x * -1.0f;
	//transformData.rot.y = transformData.rot.y * -1.0f;
	XMVECTOR rotationQuat = XMVectorSet(transformData.rot.x, transformData.rot.y, transformData.rot.z, transformData.rot.w);
	tempRotation = XMMatrixRotationQuaternion(rotationQuat);
	tempPosition = XMMatrixTranslation(transformData.pos.x, transformData.pos.y, transformData.pos.z);

	tempWorld = tempRotation * tempScale * tempPosition;
	if (parent != nullptr)
	{
		parent->UpdateCBuffer();
		XMMATRIX parentWorld = XMLoadFloat4x4(&parent->transformCBufferData.world);
		tempWorld = tempWorld /** parentWorld*/;
	}

	XMStoreFloat4x4(&transformCBufferData.world, XMMatrixTranspose(tempWorld));
	//transformdata ligger på plats 0, material på 1, osv
	gDeviceContext->UpdateSubresource(transformCBuffer, 0, NULL, &transformCBufferData.world, 0, 0); //skapa en separat struct för transformdata som ska in i shadern, world osv
	gDeviceContext->VSSetConstantBuffers(0, 1, &transformCBuffer);
}
示例#6
0
void SpriteGame::render()
{
	float backgroundColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
	deviceContext->ClearRenderTargetView(backBuffer, backgroundColor);

	unsigned int stride = sizeof(Vertex);
	unsigned int offset = 0;
	deviceContext->IASetInputLayout(inputLayout);
	deviceContext->IASetVertexBuffers(0, 1, &(vertexBuffer.p), &stride, &offset);
	deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

	deviceContext->VSSetShader(vertexShader, nullptr, 0);
	deviceContext->PSSetShader(pixelShader, nullptr, 0);

	deviceContext->PSSetShaderResources(0, 1, &(texture.p));
	deviceContext->PSSetSamplers(0, 1, &(textureSampler.p));

	for(int i=0; i<sizeof(sprites)/sizeof(Sprite); i++)
	{
		XMMATRIX mvpMatrix = sprites[i].getWorldMatrix() * vpMatrix;
		mvpMatrix = XMMatrixTranspose(mvpMatrix);
		
		deviceContext->UpdateSubresource(mvpMatrixBuffer, 0, nullptr, &mvpMatrix, 0, 0);
		deviceContext->VSSetConstantBuffers(0, 1, &(mvpMatrixBuffer.p));
		
		deviceContext->Draw(4, 0);
	}

	swapChain->Present(0, 0);
}
示例#7
0
void CascadeShadow::Update(){
	
	CascadeUpdate();

	{
		mCBChangesLightCamera.mParam.mLViewProjection[0] = XMMatrixTranspose(m_ShadowMatrix[0]);
		mCBChangesLightCamera.mParam.mLViewProjection[1] = XMMatrixTranspose(m_ShadowMatrix[1]);
		mCBChangesLightCamera.mParam.mLViewProjection[2] = XMMatrixTranspose(m_ShadowMatrix[2]);
		mCBChangesLightCamera.mParam.mLViewProjection[3] = XMMatrixTranspose(m_ShadowMatrix[3]);
		mCBChangesLightCamera.mParam.mSplitPosition = XMFLOAT4(m_SplitPos[0], m_SplitPos[1], m_SplitPos[2], m_SplitPos[3]);

		mCBChangesLightCamera.UpdateSubresource();
		mCBChangesLightCamera.VSSetConstantBuffers();
		mCBChangesLightCamera.PSSetConstantBuffers();
	}
}
void CubeMesh::Draw(RenderContext& context)
{
#if defined(DEBUG_OBB)
    context.SetRasterizerState(m_rasterType);
    context.ApplyShader(m_useShaderType);

    context.GetDeviceContext().IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    context.GetDeviceContext().IASetVertexBuffers(0, 1, &m_pVB, &m_Stride, &m_Offset);
    context.GetDeviceContext().IASetIndexBuffer(m_pIB, DXGI_FORMAT_R16_UINT, m_Offset);

    // Set constants
    cbWorld wbuffer = { XMMatrixTranspose(m_World) };
    cbInvTransposeWorld invTransWorld = { MatrixInverseTranspose(m_World) };
    cbMatTexPerObject matTexPerObject = { *m_texture->GetTextureCoordinateMatrix(), m_material };

    if (m_useShaderType == ShaderType_LightTexture)
    {
        LightTextureShader* lightTexShader = (LightTextureShader*)context.GetShader(ShaderType_LightTexture);
        lightTexShader->setCBWorld(context.GetDeviceContext(), &wbuffer);
        lightTexShader->setCBInvTransposeMatrix(context.GetDeviceContext(), &invTransWorld);
        lightTexShader->setCBMatTexPerObject(context.GetDeviceContext(), &matTexPerObject);
        lightTexShader->setTexture2D(context.GetDeviceContext(), m_texture->GetTextureResource());
    }
    else
    {
        //Solid Shader
        SolidColorShader* solidColorShader = (SolidColorShader*)context.GetShader(ShaderType_SolidColor);
        solidColorShader->setCBWorld(context.GetDeviceContext(), wbuffer);
    }

    context.DrawIndexedInstanced(36);
#endif
}
示例#9
0
void DemoApp::RenderMiniWindow()
{
	//Set Buffers, Layout, Topology and Render States
	UINT stride = sizeof(Vertex::VertexPNT);
	UINT offset = 0;
	md3dImmediateContext->IASetVertexBuffers(0, 1, &m_pScreenQuadVB, &stride, &offset);
	md3dImmediateContext->IASetIndexBuffer(m_pScreenQuadIB, DXGI_FORMAT_R32_UINT, 0);
	md3dImmediateContext->IASetInputLayout(InputLayouts::VertexPNT);
	md3dImmediateContext->RSSetState(RenderStates::NoCullRS);

	//Resize mini window and translate to upper left corner
	XMMATRIX scale = XMMatrixScaling(1.0f / AspectRatio(), 1.0f, 1.0f);
	XMMATRIX world = XMMATRIX(
		0.25f, 0.0f, 0.0f, 0.0f,
		0.0f, 0.25f, 0.0f, 0.0f,
		0.0f, 0.0f, 1.0f, 0.0f,
		0.25 / AspectRatio() - 1, 0.75f, 0.0f, 1.0f);

	CBPerFrameScreenQuad cbScreenQuadPerFrame;
	cbScreenQuadPerFrame.wvp = XMMatrixTranspose(scale * world);
	md3dImmediateContext->UpdateSubresource(m_pCBPerFrameScreenQuad, 0, NULL, &cbScreenQuadPerFrame, 0, 0);

	md3dImmediateContext->VSSetShader(m_pDebugTextureVS, NULL, 0);
	md3dImmediateContext->VSSetConstantBuffers(0, 1, &m_pCBPerFrameScreenQuad);
	md3dImmediateContext->PSSetShader(m_pDebugTexturePS, NULL, 0);
	md3dImmediateContext->PSSetShaderResources(0, 1, &m_pDepthSRV);
	md3dImmediateContext->PSSetSamplers(0, 1, &m_pSampleLinear);

	md3dImmediateContext->DrawIndexed(6, 0, 0);
}
示例#10
0
void TrackingCamera::Update(float deltaTime, float totalTime)
{
	XMVECTOR vPosition = XMLoadFloat3(&position);
	XMVECTOR vForward = XMLoadFloat3(&forward);
	XMVECTOR vTarget = XMLoadFloat3(&(target->GetTranslation()));
	XMVECTOR vUp = XMLoadFloat3(&up);

	//get vector to object from camera
	XMVECTOR vToObject = vTarget - vPosition;

	//get vector projection and rejection of toObject onto forward
	XMVECTOR vProjection = (XMVector3Dot(vToObject, vForward) / XMVector3Dot(vForward, vForward)) * vForward;
	XMVECTOR vRejection = vToObject - vProjection;

	//multiply rejection by trackStrength
	vRejection = vRejection * trackStrength;

	//add projection and multiplied rejection
	XMVECTOR vDirection = vProjection + vRejection;

	//use new vector as look to
	XMMATRIX mViewMat = XMMatrixLookToLH(vPosition, vDirection, vUp);

	XMStoreFloat4x4(&viewMat, XMMatrixTranspose(mViewMat));
}
void Simulation::Draw()
{
	m_Camera.UpdateViewMatrix();

	XMStoreFloat4x4(&(matrixBufferData.view), XMMatrixTranspose(m_Camera.View()));
	
	const float clearColor[4] = { 0.0f, 0.0, 0.0f, 1.0f };

	devCon->ClearRenderTargetView(renderTargetView, clearColor);
	devCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	float blendFactors[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
	devCon->OMSetBlendState(blendState, blendFactors, 0xFFFFFF);

	devCon->IASetInputLayout(inputLayout);
	devCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	devCon->VSSetConstantBuffers(0, 1, &matrixBuffer);
	devCon->OMSetDepthStencilState(depthStencilState, 0);

	for (GameObject* obj : objects)
	{
		devCon->RSSetState(current);
		XMStoreFloat4x4(&(matrixBufferData.world), XMLoadFloat4x4(&(obj->GetWorldMatrix())));
		devCon->UpdateSubresource(matrixBuffer, 0, NULL, &matrixBufferData, 0, 0);
		obj->Draw(devCon);
	}
	
	swapChain->Present(0, 0);
}
void Simulation::OnResize()
{
	Game::OnResize();

	m_Camera.SetLens(0.25f * 3.1415926535f, AspectRatio(), 0.1f, 200.0f);
	XMStoreFloat4x4(&(matrixBufferData.projection), XMMatrixTranspose(m_Camera.Proj()));
}
示例#13
0
	void ProxyModel::Draw(const GameTime& gameTime)
	{
		ID3D11DeviceContext* direct3DDeviceContext = mGame->GetD3DDeviceContext();			
		direct3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
		direct3DDeviceContext->IASetInputLayout(mInputLayout.Get());

		UINT stride = sizeof(VertexPositionColorAlpha);
		UINT offset = 0;
		direct3DDeviceContext->IASetVertexBuffers(0, 1, mVertexBuffer.GetAddressOf(), &stride, &offset);
		direct3DDeviceContext->IASetIndexBuffer(mIndexBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);

		direct3DDeviceContext->VSSetShader(mVertexShader.Get(), nullptr, 0);
		direct3DDeviceContext->PSSetShader(mPixelShader.Get(), nullptr, 0);

		XMMATRIX worldMatrix = XMLoadFloat4x4(&mWorldMatrix);
		XMMATRIX wvp = worldMatrix * mCamera->ViewProjectionMatrix();
		XMStoreFloat4x4(&mVertexCBufferPerObjectData.WorldViewProjection, XMMatrixTranspose(wvp));

		direct3DDeviceContext->UpdateSubresource(mVertexCBufferPerObject.Get(), 0, nullptr, &mVertexCBufferPerObjectData, 0, 0);
		direct3DDeviceContext->VSSetConstantBuffers(0, 1, mVertexCBufferPerObject.GetAddressOf());

		if (mDisplayWireframe)
		{
			mGame->GetD3DDeviceContext()->RSSetState(RasterizerStates::Wireframe.Get());
			direct3DDeviceContext->DrawIndexed(mIndexCount, 0, 0);
			mGame->GetD3DDeviceContext()->RSSetState(nullptr);
		}
		else
		{
			direct3DDeviceContext->DrawIndexed(mIndexCount, 0, 0);
		}
	}
示例#14
0
		XMMATRIX InverseTranspose(CXMMATRIX M)
		{
			XMMATRIX A = M;
			A.r[3] = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
			XMVECTOR det = XMMatrixDeterminant(A);
			return XMMatrixTranspose(XMMatrixInverse(&det, A));
		}
示例#15
0
	void Camera::ApplyTransform(ConstantBufferInstance* buffer, const Engine::Transform& transform) const
	{
		XMMATRIX mvp;
		if (!transform.GetIs2D())
		{
			mvp = XMMatrixTranspose(transform.GetMatrix() * _vp);
		}
		else
		{
			mvp = XMMatrixTranspose(transform.GetMatrix());
		}

		XMFLOAT4X4 mvpT;
		XMStoreFloat4x4(&mvpT, mvp);
		buffer->SetData("mvp", mvpT);
	}
示例#16
0
void Down(__int64 FPS, XMMATRIX* ViewMatrix, XMVECTOR* CurrentCamPos) {
	*ViewMatrix = XMMatrixTranspose(*ViewMatrix);
	XMMATRIX ViewInvers = XMMatrixInverse(NULL, *ViewMatrix);
	XMFLOAT4X4 View4x4;
	XMStoreFloat4x4(&View4x4, ViewInvers);
	XMVECTOR CamPos = XMVectorSet(View4x4._41, View4x4._42, View4x4._43, 0);
	XMVECTOR CamLook = XMVectorSet(0, View4x4._32, 0, 0);
	CamLook = XMVector3Normalize(CamLook);
	XMVECTOR newCamPos = (CamPos - (XMVectorSet(0, 1, 0, 0) * (movementSpeed / FPS)));
	XMVECTOR newCamLook = (XMVectorSet(View4x4._31, View4x4._32, View4x4._33, 0)) + newCamPos;

	*CurrentCamPos = newCamPos;

	*ViewMatrix = XMMatrixLookAtLH(newCamPos, newCamLook, { 0, 1, 0 });
	*ViewMatrix = XMMatrixTranspose(*ViewMatrix);
}
示例#17
0
bool FontShaderHandler::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix,
	XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* fontTexture, XMFLOAT3 color)
{
	HRESULT hresult;
	D3D11_MAPPED_SUBRESOURCE mappedResource;
	MatrixBufferSimple* dataPtr;
	unsigned int bufferNumber;

	//Transpose each matrix to prepare for shaders (requirement in directx 11)
	worldMatrix = XMMatrixTranspose(worldMatrix);
	viewMatrix = XMMatrixTranspose(viewMatrix);
	projectionMatrix = XMMatrixTranspose(projectionMatrix);

	//Map the constant buffer so we can write to it (denies GPU access)
	hresult = deviceContext->Map(this->matrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if (FAILED(hresult)) {
		return false;
	}

	//Get pointer to the data
	dataPtr = (MatrixBufferSimple*)mappedResource.pData;

	//Copy the matrices to the constant buffer
	dataPtr->world = worldMatrix;
	dataPtr->view = viewMatrix;
	dataPtr->projection = projectionMatrix;
	dataPtr->color = color;

	//Unmap the constant buffer to give the GPU access agin
	deviceContext->Unmap(this->matrixBuffer, 0);

	//Set constant buffer position in vertex shader
	bufferNumber = 0;

	//Set the constant buffer in vertex and pixel shader with updated values
	deviceContext->VSSetConstantBuffers(bufferNumber, 1, &this->matrixBuffer);
	deviceContext->PSSetConstantBuffers(bufferNumber, 1, &this->matrixBuffer);

	if (fontTexture) {
		//Set shader color texture resource for pixel shader
		deviceContext->PSSetShaderResources(0, 1, &fontTexture);
	}

	deviceContext->OMSetBlendState(this->transparencyBlendState, NULL, 0xffffffff);

	return true;
}
示例#18
0
bool ModelsDemo::DrawGameObject( GameObject * game_object )
{
	//////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////

	// THESE OPERATIONS ARE NOT ENCAPSULATED YET !!!

	// activate the vertex layout on the graphics card
	d3dContext_->IASetInputLayout( inputLayout_ );

	// activate the vertex shader on the graphics card
	d3dContext_->VSSetShader( textureMapVS_, 0, 0 );
	
	// activate the pixel shader on the graphics card
	d3dContext_->PSSetShader( textureMapPS_, 0, 0 );

	// activate the texture sampler on the graphics card
	d3dContext_->PSSetSamplers( 0, 1, &colorMapSampler_ );

	//////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////

	// get the display object from the game object
	DisplayObject * display_object = game_object->GetDisplayObject();

	// activate the GameObject's vertex buffer on the graphics card
	display_object->GetModel()->Render( d3dContext_ );

	// activate the GameObject's texture buffer on the graphics card
	display_object->GetTexture()->Render( d3dContext_ );

	// create matrices to create a single world matrix for the GameObject's transform
	XMMATRIX scale_mat = XMMatrixScaling(game_object->getSX(), game_object->getSY(), game_object->getSZ());
	XMMATRIX rotation_mat = XMMatrixRotationRollPitchYaw(game_object->getRX(), game_object->getRY(), game_object->getRZ());
	XMMATRIX position_mat = XMMatrixTranslation(game_object->getX(), game_object->getY(), game_object->getZ());
	
	// 1) scale 
	// 2) rotate 
	// 3) position
	XMMATRIX world_mat = XMMatrixTranspose( scale_mat * rotation_mat * position_mat );

	//////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////

	// THESE OPERATIONS ARE NOT ENCAPSULATED YET !!!

	// update the world matrix on the graphics card
	d3dContext_->UpdateSubresource( worldCB_, 0, 0, &world_mat, 0, 0 );

	// set the world matrix on the vertex shader
	d3dContext_->VSSetConstantBuffers( 0, 1, &worldCB_ );

	//////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////

	d3dContext_->Draw( display_object->GetModel()->GetVertexCount(), 0 );

	return true;
}
示例#19
0
// рисовать модель
bool AnimModel::Draw(bool toShadowMap) {

	// установить константы
	myXMFLOAT3 positionJP = position - Mediator::camera->GetPosition();
	XMMATRIX W_JP = XMMatrixTranslation(positionJP.x, positionJP.y, positionJP.z);
	XMMATRIX R = XMLoadFloat4x4(&GetRotationMatrix());
	if (recreateFinalMatrices) {
		if (restCurClip) CreateFinalMatrices(curClip, curFrame[curClip], finalMatrices);
		else CreateFinalMatrices(curFrame, blendFactors, finalMatrices);
		recreateFinalMatrices = false;
	}
	UINT vertexStride = sizeof(VertPos3Nor2Tex2W1Bone2);
	UINT vertexOffset = 0;

	// установить параметры
	Mediator::pDevContext->IASetVertexBuffers(0, 1, &pVertexBuffer, &vertexStride, &vertexOffset);
	Mediator::pDevContext->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
	Mediator::pDevContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	Mediator::pDevContext->IASetInputLayout(Mediator::pInputLayoutPos3Nor3Tex2W1Bone2);
	if (toShadowMap) {
		Mediator::pDevContext->VSSetShader(Mediator::pVSShadow, NULL, 0);
		Mediator::pDevContext->PSSetShader(Mediator::pPSEmpty, NULL, 0);
	}
	else {
		Mediator::pDevContext->PSSetShaderResources(TEXTURE_BUFFER, 1, &pSRtexture);
		Mediator::pDevContext->VSSetShader(Mediator::pVSPosNorTexWBone, NULL, 0);
		Mediator::pDevContext->PSSetShader(Mediator::pPSPosNorTex, NULL, 0);
	}

	// обновить константный буфер шейдера
	D3D11_MAPPED_SUBRESOURCE mappedData;
	HR(Mediator::pDevContext->Map(Mediator::cbufAnimModel, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));
	CbufAnimModel* cbuf = reinterpret_cast<CbufAnimModel*>(mappedData.pData);
	XMStoreFloat4x4(&cbuf->gWorld, XMMatrixTranspose(R * W_JP));
	for (UINT i(0); i < bonesAmount; i++) {
		XMStoreFloat4x4(&cbuf->gBones[i], XMMatrixTranspose(XMLoadFloat4x4(&finalMatrices[i])));
	}
	Mediator::pDevContext->Unmap(Mediator::cbufAnimModel, 0);
	Mediator::pDevContext->VSSetConstantBuffers(SR_ANIMMODEL, 1, &Mediator::cbufAnimModel);

	// рисование
	Mediator::pDevContext->DrawIndexed(indicesAmount, 0, 0);

	return true;

}
示例#20
0
bool TerrainShaderClass::SetShadowConstantBufferParamters(ID3D11DeviceContext* pDeviceContext, ObjectClass* pObject, CameraClass* pCamera, PointLightClass* pPointLight)
{
	HRESULT result;
	D3D11_MAPPED_SUBRESOURCE mappedResource;
	ShadowBuffer* dataPtr;
	unsigned int bufferNumber;

	// Lock the constant buffer so it can be written to.
	result = pDeviceContext->Map(mShadowBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if (FAILED(result))
	{
		return false;
	}

	// Get a pointer to the data in the constant buffer.
	dataPtr = (ShadowBuffer*)mappedResource.pData;
	
	XMMATRIX world = XMLoadFloat4x4(&pObject->GetWorldMatrix());

	XMMATRIX view = XMLoadFloat4x4(&pCamera->GetViewMatrix());
	XMMATRIX proj = XMLoadFloat4x4(&pCamera->GetProjMatrix());

	XMMATRIX lview = XMLoadFloat4x4(&pPointLight->GetViewMatrix());
	XMMATRIX lproj = XMLoadFloat4x4(&pPointLight->GetProjMatrix());

	XMMATRIX lWVP = XMMatrixTranspose(world*lview*lproj);
	XMMATRIX WVP = XMMatrixTranspose(world*view*proj);


	XMStoreFloat4x4(&dataPtr->worldViewProj, WVP);
	XMStoreFloat4x4(&dataPtr->world, world);
	XMStoreFloat4x4(&dataPtr->LightWorldViewProj, lWVP);
	XMFLOAT3 cP = pCamera->GetPosition();
	dataPtr->camPos = XMFLOAT4(cP.x, cP.y, cP.z, 0);

	// Unlock the constant buffer.
	pDeviceContext->Unmap(mShadowBuffer, 0);

	// Set the position of the constant buffer in the vertex shader.
	bufferNumber = 0;

	// Set the constant buffer in the shader with the updated values.
	pDeviceContext->GSSetConstantBuffers(bufferNumber, 1, &mShadowBuffer);

	return true;
}
示例#21
0
文件: Model.cpp 项目: HxTenshi/TGS25
void Model::Update(){

	mCBuffer.mParam.mBeforeWorld = mCBuffer.mParam.mWorld;
	mCBuffer.mParam.mWorld = XMMatrixTranspose(mWorld);

	mCBuffer.UpdateSubresource();

}
示例#22
0
	void ParticleRenderer::ExtractScreenSpaceUnitVectors(const XMFLOAT4X4& projWorldMatrix) {
		// inverse, so screen->world?
		XMFLOAT4X4 invProj;
		XMStoreFloat4x4(&invProj, XMMatrixTranspose(XMLoadFloat4x4(&projWorldMatrix)));
		XMStoreFloat3(&screenSpaceUnitX, XMVector3Normalize(XMLoadFloat3((XMFLOAT3*)&invProj._11)));
		XMStoreFloat3(&screenSpaceUnitY, XMVector3Normalize(XMLoadFloat3((XMFLOAT3*)&invProj._21)));
		XMStoreFloat3(&screenSpaceUnitZ, XMVector3Normalize(XMLoadFloat3((XMFLOAT3*)&invProj._31)));
	}
	void TransparencyDemo::Draw(const GameTime& gameTime)
	{
		ID3D11DeviceContext* direct3DDeviceContext = mGame->GetD3DDeviceContext();
		direct3DDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
		direct3DDeviceContext->IASetInputLayout(mInputLayout.Get());

		UINT stride = static_cast<UINT>(sizeof(VertexPositionTextureNormal));
		UINT offset = 0;
		direct3DDeviceContext->IASetVertexBuffers(0, 1, mVertexBuffer.GetAddressOf(), &stride, &offset);

		direct3DDeviceContext->VSSetShader(mVertexShader.Get(), nullptr, 0);
		direct3DDeviceContext->PSSetShader(mPixelShader.Get(), nullptr, 0);

		XMMATRIX worldMatrix = XMLoadFloat4x4(&mWorldMatrix);
		XMMATRIX wvp = worldMatrix * mCamera->ViewProjectionMatrix();
		XMStoreFloat4x4(&mVertexCBufferPerObjectData.WorldViewProjection, XMMatrixTranspose(wvp));
		XMStoreFloat4x4(&mVertexCBufferPerObjectData.World, XMMatrixTranspose(worldMatrix));

		const XMFLOAT3& cameraPosition = mCamera->Position();
		mVertexCBufferPerFrameData.CameraPosition = XMFLOAT4(cameraPosition.x, cameraPosition.y, cameraPosition.z, 1.0f);
		mPixelCBufferPerFrameData.CameraPosition = mVertexCBufferPerFrameData.CameraPosition;

		direct3DDeviceContext->UpdateSubresource(mVSCBufferPerFrame.Get(), 0, nullptr, &mVertexCBufferPerFrameData, 0, 0);
		direct3DDeviceContext->UpdateSubresource(mVSCBufferPerObject.Get(), 0, nullptr, &mVertexCBufferPerObjectData, 0, 0);
		direct3DDeviceContext->UpdateSubresource(mPSCBufferPerFrame.Get(), 0, nullptr, &mPixelCBufferPerFrameData, 0, 0);
		direct3DDeviceContext->UpdateSubresource(mPSCBufferPerObject.Get(), 0, nullptr, &mPixelCBufferPerObjectData, 0, 0);

		static ID3D11Buffer* VSConstantBuffers[] = { mVSCBufferPerFrame.Get(), mVSCBufferPerObject.Get() };
		direct3DDeviceContext->VSSetConstantBuffers(0, ARRAYSIZE(VSConstantBuffers), VSConstantBuffers);

		static ID3D11Buffer* PSConstantBuffers[] = { mPSCBufferPerFrame.Get(), mPSCBufferPerObject.Get() };
		direct3DDeviceContext->PSSetConstantBuffers(0, ARRAYSIZE(PSConstantBuffers), PSConstantBuffers);

		static ID3D11ShaderResourceView* PSShaderResources[] = { mColorTexture.Get(), mTransparencyMap.Get() };
		direct3DDeviceContext->PSSetShaderResources(0, ARRAYSIZE(PSShaderResources), PSShaderResources);
		direct3DDeviceContext->PSSetSamplers(0, 1, mTrilinearSampler.GetAddressOf());

		mRenderStateHelper.SaveAll();

		direct3DDeviceContext->OMSetBlendState(BlendStates::AlphaBlending.Get(), 0, 0xFFFFFFFF);
		direct3DDeviceContext->Draw(mVertexCount, 0);
		mProxyModel->Draw(gameTime);

		mRenderStateHelper.RestoreBlendState();
		mRenderStateHelper.RestoreAll();
	}
示例#24
0
void BezierSceneObject::renderTheScene(GraphicsContext & p_in)
{
	auto * invoker = m_quadInvoker;
	auto * Model = m_model;

	updateIncrement += p_in.deltaTime * 0.01f *  m_model->TweakVariables["timeMod"];

	ID3D11DeviceContext * tempDevice = p_in.GetD3DContext()->GetDeviceContext();
	
	XMMATRIX tempMat;
	Model->ModelMatrix(tempMat);
	Model->UpdateBuffers(p_in.c_d3d->GetDeviceContext(), updateBezierPoints(updateIncrement, m_model->TweakVariables["AmpIncrement"]));
	
	XMMATRIX orbitMatrix = XMMatrixTranslation(10.0f , 0.0f, 0.0f) * XMMatrixRotationY(updateIncrement * 0.1f);

	// Release the vertex array as it is no longer needed.
	delete [] vertices;
	vertices = 0;

	QuadInvoker::MatrixBufferType *tempMatBuff = invoker->MapCBuffer<QuadInvoker::MatrixBufferType>(tempDevice , "matrixBuffer");
	tempMatBuff->worldMatrix = XMMatrixTranspose(tempMat* orbitMatrix);
	tempMatBuff->viewMatrix = XMMatrixTranspose( *p_in.c_view);
	tempMatBuff->projectionMatrix = XMMatrixTranspose(*p_in.c_projection);
	invoker->UnMapCBuffer(tempDevice , "matrixBuffer");
	tempDevice->VSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["matrixBuffer"]);
	tempDevice->DSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["matrixBuffer"]);

	QuadInvoker::TessellationBufferType *tempTessBuff = invoker->MapCBuffer<QuadInvoker::TessellationBufferType>(tempDevice , "tessBuffer");
	tempTessBuff->cameraPosition = p_in.c_cam->GetPos();
	tempTessBuff->misc = XMFLOAT3();
	tempTessBuff->tessellationAmount = m_model->TweakVariables["TessFactor"];
	tempTessBuff->LODModifier = 0;
	invoker->UnMapCBuffer(tempDevice , "tessBuffer");
	tempDevice->HSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["tessBuffer"]);

	QuadInvoker::LightBufferType *tempLightBuff = invoker->MapCBuffer<QuadInvoker::LightBufferType>(tempDevice, "LightBuffer");
	tempLightBuff->ambientColor = p_in.c_lightInfo->GetAmbientColor();
	tempLightBuff->diffuseColor = p_in.c_lightInfo->GetDiffuseColor();
	tempLightBuff->lightDirection = p_in.c_lightInfo->Getdirection();
	tempLightBuff->specularPower = p_in.c_lightInfo->GetSpecularPower();
	invoker->UnMapCBuffer(tempDevice, "LightBuffer");
	tempDevice->PSSetConstantBuffers(0, 1, &invoker->GetBufferMap()["LightBuffer"]);

	Model->RenderAllBuffers<ModelLightingClass::LightingVertexType> (tempDevice, D3D11_PRIMITIVE_TOPOLOGY_16_CONTROL_POINT_PATCHLIST);
	invoker->RenderShader(tempDevice ,  Model->GetIndexCount());
}
示例#25
0
void SpriteRenderer::Render(ID3D11ShaderResourceView* texture,
                            const XMMATRIX& transform,
                            const XMFLOAT4& color,
                            const XMFLOAT4* drawRect)
{
    _ASSERT(context);
    _ASSERT(initialized);

    D3DPERF_BeginEvent(0xFFFFFFFF, L"SpriteRenderer Render");

    // Set the vertex shader
    context->VSSetShader(vertexShader, NULL, 0);

    // Set the input layout
    context->IASetInputLayout(inputLayout);

    // Set the vertex buffer
    UINT stride = sizeof(SpriteVertex);
    UINT offset = 0;
    ID3D11Buffer* vb = vertexBuffer.GetInterfacePtr();
    context->IASetVertexBuffers(0, 1, &vb, &stride, &offset);

    // Set per-batch constants
    D3D11_TEXTURE2D_DESC desc = SetPerBatchData(texture);

    // Set per-instance data
    SpriteDrawData perInstance;
    perInstance.Transform = XMMatrixTranspose(transform);
    perInstance.Color = color;

    // Draw rect
    if (drawRect == NULL)
        perInstance.DrawRect = XMFLOAT4(0, 0, static_cast<float>(desc.Width), static_cast<float>(desc.Height));
    else
    {
        _ASSERT(drawRect->x >= 0 && drawRect->x < desc.Width);
        _ASSERT(drawRect->y >= 0 && drawRect->y < desc.Height);
        _ASSERT(drawRect->z > 0 && drawRect->x + drawRect->z < desc.Width);
        _ASSERT(drawRect->w > 0 && drawRect->y + drawRect->w < desc.Height);
        perInstance.DrawRect = *drawRect;
    }

    // Copy in the buffer data
    D3D11_MAPPED_SUBRESOURCE mapped;
    DXCall(context->Map(vsPerInstanceCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped));
    CopyMemory(mapped.pData, &perInstance, sizeof(SpriteDrawData));
    context->Unmap(vsPerInstanceCB, 0);

    ID3D11Buffer* buffers [2] = { vsPerBatchCB, vsPerInstanceCB };
    context->VSSetConstantBuffers(0, 2, buffers);

    // Set the texture
    context->PSSetShaderResources(0, 1, &texture);

    context->DrawIndexed(6, 0, 0);

    D3DPERF_EndEvent();
}
示例#26
0
bool DX11ViewportRenderer::drawBounds( const MMatrix &matrix, const MBoundingBox &box, float color[3] )
{
	// Transform from object to world space
	//
	XMMATRIX mat = XMMATRIX
		(
		(float)matrix.matrix[0][0], (float)matrix.matrix[0][1], (float)matrix.matrix[0][2], (float)matrix.matrix[0][3],
		(float)matrix.matrix[1][0], (float)matrix.matrix[1][1], (float)matrix.matrix[1][2], (float)matrix.matrix[1][3],
		(float)matrix.matrix[2][0], (float)matrix.matrix[2][1], (float)matrix.matrix[2][2], (float)matrix.matrix[2][3],
		(float)matrix.matrix[3][0], (float)matrix.matrix[3][1], (float)matrix.matrix[3][2], (float)matrix.matrix[3][3]
		);

	// Adjust the unit cube to the bounds
	//
	MPoint	minPt = box.min();
	MPoint	maxPt = box.max();
	float minVal[3] = { (float)minPt.x, (float)minPt.y, (float)minPt.z };
	float maxVal[3] = { (float)maxPt.x, (float)maxPt.y, (float)maxPt.z };
	XMMATRIX bounds( 0.5f*(maxVal[0]-minVal[0]), 0.0f,						 0.0f,							0.0f,
					 0.0f,						 0.5f*(maxVal[1]-minVal[1]), 0.0f,							0.0f,
					 0.0f,						 0.0f,						 0.5f*(maxVal[2]-minVal[2]),	0.0f,
					 0.5f*(maxVal[0]+minVal[0]), 0.5f*(maxVal[1]+minVal[1]), 0.5f*(maxVal[2]+minVal[2]),	1.0f );

    // Set vertex buffer
    UINT stride = sizeof( BoundsVertex );
    UINT offset = 0;
    m_pD3DDeviceCtx->IASetVertexBuffers( 0, 1, &m_pBoundsVertexBuffer, &stride, &offset );

	// Set index buffer
    m_pD3DDeviceCtx->IASetIndexBuffer( m_pBoundsIndexBuffer, DXGI_FORMAT_R16_UINT, 0 );

	// Set constant buffer
    BoundsConstants cb;
	cb.fWVP = XMMatrixTranspose( bounds * mat * m_currentViewMatrix * m_currentProjectionMatrix );
	cb.fDiffuseMaterial = XMFLOAT3( color[0], color[1], color[2] );
	m_pD3DDeviceCtx->UpdateSubresource( m_pBoundsConstantBuffer, 0, NULL, &cb, 0, 0 );

    // Set primitive topology
    m_pD3DDeviceCtx->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_LINELIST );

	// get shader
	SurfaceEffectItemList::const_iterator it = m_resourceManager.getSurfaceEffectItemList().find( "Maya_unlit" );
	if ( it == m_resourceManager.getSurfaceEffectItemList().end() )
		return false;
	const SurfaceEffectItem* sei = it->second;

	// bind shaders
    m_pD3DDeviceCtx->VSSetShader( sei->fVertexShader, NULL, 0 );
	m_pD3DDeviceCtx->VSSetConstantBuffers( 0, 1, &m_pBoundsConstantBuffer );
    m_pD3DDeviceCtx->IASetInputLayout( sei->fInputLayout );
    m_pD3DDeviceCtx->PSSetShader( sei->fPixelShader, NULL, 0 );
	m_pD3DDeviceCtx->PSSetConstantBuffers( 0, 1, &m_pBoundsConstantBuffer );

	// draw
    m_pD3DDeviceCtx->DrawIndexed( 24, 0, 0 );

	return true;
}
示例#27
0
void UpdateConstantBuffer(){
	
	//Update variables
	ConstantBuffer cb;
	cb.rotationMatrix = XMMatrixTranspose( gRotationMatrix );
	cb.viewMatrix = XMMatrixTranspose( gViewMatrix );
	cb.projectionMatrix = XMMatrixTranspose( gProjectionMatrix );
	cb.translationMatrix = XMMatrixTranspose( gTranslationMatrix );

	gDeviceContext->UpdateSubresource( 
			gConstantBuffer,
			0,
			NULL,
			&cb,
			0,
			0
		);
}
示例#28
0
XMMATRIX D3DRenderer::ConvertToXMMatrix(EnMatrix4x4 m)
{
	//We have to transpose as it appears that the XMMATRIX is expecting information in a per row basis, however we use per column.
	XMMATRIX newMatrix(	m.c[0].x, m.c[1].x, m.c[2].x, m.c[3].x, 
						m.c[0].y, m.c[1].y, m.c[2].y, m.c[3].y, 
						m.c[0].z, m.c[1].z, m.c[2].z, m.c[3].z, 
						m.c[0].w, m.c[1].w, m.c[2].w, m.c[3].w);
	return XMMatrixTranspose(newMatrix);
}
示例#29
0
void Rotate() {
    angle += 0.001f;
    DirectX::XMFLOAT3 pos( 1, 1, 1 );
    DirectX::XMVECTOR normal = DirectX::XMLoadFloat3( &pos );
    objProjection = DirectX::XMMatrixRotationAxis( normal, angle );
    ConstantBuffer cb;
    cb.m_modelMatrix = XMMatrixTranspose( objProjection );
    d3d11DeviceContext->UpdateSubresource( constantBuffer, 0, nullptr, &cb, 0, 0 );
}
示例#30
0
void SceneInt::InitialiseBuffers()
{
    XMVECTOR eye = XMVectorSet(-10.0f, 0.7f, -8.0f, 0.0f);
    XMVECTOR at = XMVectorSet(0.0f, 8.0f, 0.0f, 0.0f);
    XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

	auto matrix = XMMatrixTranspose(XMMatrixLookAtRH(eye, at, up));
	XMStoreFloat4x4(&_constantBufferData.view, matrix);
}