Пример #1
0
void WavesApp::BuildWavesGeometryBuffers()
{
	// Create the vertex buffer.  Note that we allocate space only, as
	// we will be updating the data every time step of the simulation.

	D3D11_BUFFER_DESC vbd;
	vbd.Usage = D3D11_USAGE_DYNAMIC;
	vbd.ByteWidth = sizeof(Vertex) * m_waves.GetVertexCount();
	vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	vbd.MiscFlags = 0;
	HR(m_pD3dDevice->CreateBuffer(&vbd,0,&m_pWavesVB));

	// Create the index buffer.  The index buffer is fixed, so we only 
	// need to create and set once.
	std::vector<UINT> indices(3 * m_waves.GetTriangleCount()); // 3 indices per face

	// Iterate over each quad.
	UINT m = m_waves.GetRowCount();
	UINT n = m_waves.GetColumnCount();
	int k = 0;
	for (UINT i = 0; i < m - 1; ++i)
	{
		for (DWORD j = 0; j < n - 1; ++j)
		{
			indices[k] = i*n + j;
			indices[k + 1] = i*n + j + 1;
			indices[k + 2] = (i + 1)*n + j;

			indices[k + 3] = (i + 1)*n + j;
			indices[k + 4] = i*n + j + 1;
			indices[k + 5] = (i + 1)*n + j + 1;

			k += 6; // next quad
		}
	}

	{
		D3D11_BUFFER_DESC ibd;
		ibd.Usage = D3D11_USAGE_IMMUTABLE;
		ibd.ByteWidth = sizeof(UINT) * indices.size();
		ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
		ibd.CPUAccessFlags = 0;
		ibd.MiscFlags = 0;
		D3D11_SUBRESOURCE_DATA iinitData;
		iinitData.pSysMem = &indices[0];
		HR(m_pD3dDevice->CreateBuffer(&ibd, &iinitData, &m_pWavesIB));
	}
}
Пример #2
0
void LightDemo::BuildWavesBuffers()
{
    // Create the vertex buffer.  Note that we allocate space only, as
	// we will be updating the data every time step of the simulation.

    D3D11_BUFFER_DESC vbd;
    vbd.Usage = D3D11_USAGE_DYNAMIC; //Dynamic instead of Immutable
    vbd.ByteWidth = sizeof(Vertex) * m_waves.VertexCount();
    vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; //Write access since it's dynamic
    vbd.MiscFlags = 0;
    //No data is passed because we will set it at each frame
    HR(m_dxDevice->CreateBuffer(&vbd, 0, m_wavesVB.GetAddressOf()));

	// Create the index buffer.  The index buffer is fixed, so we only 
	// need to create and set once.
	std::vector<uint32> indices(3*m_waves.TriangleCount()); // 3 indices per face

	// Iterate over each quad.
	uint32 m = m_waves.RowCount();
	uint32 n = m_waves.ColumnCount();
	int k = 0;
	for(uint32 i = 0; i < m-1; ++i)
	{
		for(uint32 j = 0; j < n-1; ++j)
		{
			indices[k]   = i*n+j;
			indices[k+1] = i*n+j+1;
			indices[k+2] = (i+1)*n+j;

			indices[k+3] = (i+1)*n+j;
			indices[k+4] = i*n+j+1;
			indices[k+5] = (i+1)*n+j+1;

			k += 6; // next quad
		}
	}

	D3D11_BUFFER_DESC ibd;
    ibd.Usage = D3D11_USAGE_IMMUTABLE;
	ibd.ByteWidth = sizeof(uint32) * indices.size();
    ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
    D3D11_SUBRESOURCE_DATA iinitData;
    iinitData.pSysMem = &indices[0];
    HR(m_dxDevice->CreateBuffer(&ibd, &iinitData, m_wavesIB.GetAddressOf()));
}
Пример #3
0
void WavesApp::UpdateScene(float dt)
{
	// Convert Spherical to Cartesian coordinates.
	float x = m_fRadius * sinf(m_fPhi) * cosf(m_fTheta);
	float z = m_fRadius * sinf(m_fPhi) * sinf(m_fTheta);
	float y = m_fRadius*cosf(m_fPhi);

	XMVECTOR pos = XMVectorSet(x, y, z, 1.0f);
	XMVECTOR target = XMVectorZero();
	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

	XMMATRIX V = XMMatrixLookAtLH(pos, target, up);
	XMStoreFloat4x4(&m_viewMatrix, V);


	//
	// Every quarter second, generate a random wave.
	//
	static float s_baseT = 0.0f;
	if ((mTimer.GetTotalTime() - s_baseT)>=0.25f)
	{
		s_baseT += 0.25f;
		UINT i = 5 + rand() % 190;
		UINT j = 5 + rand() % 190;

		float r = MathHelper::RandF(1.0f, 2.0f);

		m_waves.Disturb(i, j, r);//jingz 假设这个是生成浪花,数据变化时更新了什么内容呢?
	}

	m_waves.Update(dt);

	//
	// Update the wave vertex buffer with the new solution.
	//
	D3D11_MAPPED_SUBRESOURCE mappedData;
	HR(m_pD3dImmediateContext->Map(m_pWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));

	Vertex * vertices = reinterpret_cast<Vertex *>(mappedData.pData);

	for (UINT i = 0; i < m_waves.GetVertexCount();++i)
	{
		vertices[i].Pos = m_waves[i];
		vertices[i].Color = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
	}

	m_pD3dImmediateContext->Unmap(m_pWavesVB,0);
}
Пример #4
0
bool BlendApp::Init()
{
	if(!D3DApp::Init())
		return false;

	mWaves.Init(160, 160, 1.0f, 0.03f, 5.0f, 0.3f);

	// Must init Effects first since InputLayouts depend on shader signatures.
	BuildShaders();
	BuildInputLayout();
	RenderStates::InitAll(md3dDevice);

	HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice, 
		L"Textures/grass.dds", 0, 0, &mGrassMapSRV, 0 ));

	HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice, 
		L"Textures/water2.dds", 0, 0, &mWavesMapSRV, 0 ));

	HR(D3DX11CreateShaderResourceViewFromFile(md3dDevice, 
		L"Textures/WireFence.dds", 0, 0, &mBoxMapSRV, 0 ));

	BuildLandGeometryBuffers();
	BuildWaveGeometryBuffers();
	BuildCrateGeometryBuffers();
	BuildConstBuffer();

	return true;
}
Пример #5
0
bool WavesApp::Init()
{
	if (!D3DApp::Init())
	{
		return false;
	}

	m_waves.Init(200, 200, 0.8f, 0.03f, 3.25f, 0.4f);


	BuildLandGeometryBuffers();
	BuildWavesGeometryBuffers();
	BuildFX();
	BuildVertexLayout();

	D3D11_RASTERIZER_DESC wireframeDesc;
	ZeroMemory(&wireframeDesc, sizeof(D3D11_RASTERIZER_DESC));
	wireframeDesc.FillMode = D3D11_FILL_WIREFRAME;
	wireframeDesc.CullMode = D3D11_CULL_BACK;
	wireframeDesc.FrontCounterClockwise = false;
	wireframeDesc.DepthClipEnable = true;

	HR(m_pD3dDevice->CreateRasterizerState(&wireframeDesc, &m_pWireframeRS));

	return true;
}
Пример #6
0
bool LightDemo::Init()
{
    m_waves.Init(160, 160, 1.0f, 0.03f, 3.25f, 0.4f);

    if(!DemoApp::Init())
		return false;

	return true;
}
Пример #7
0
bool TreeBillboardApp::Init()
{
    m_waves.Init(160, 160, 1.0f, 0.03f, 5.0f, 0.3f);

    if(!DemoApp::Init())
		return false;

	return true;
}
Пример #8
0
Waves* Waves::create(float duration, const Size& gridSize, unsigned int waves, float amplitude, bool horizontal, bool vertical)
{
    Waves *action = new Waves();

    if (action)
    {
        if (action->initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical))
        {
            action->autorelease();
        }
        else
        {
            CC_SAFE_RELEASE_NULL(action);
        }
    }

    return action;
}
Пример #9
0
bool LightingApp::Init()
{
	if(!D3DApp::Init())
		return false;

	mWaves.Init(160, 160, 1.0f, 0.03f, 3.25f, 0.4f);

	BuildLandGeometryBuffers();
	BuildWaveGeometryBuffers();
	BuildFX();
	BuildVertexLayout();
	mObjectConstantBuffer.Initialize(md3dDevice);
	mFrameConstantBuffer.Initialize(md3dDevice);
	return true;
}
Пример #10
0
Waves loadWave(const std::string& waveFile)
{
	Waves waves;

//	std::ifstream ifs(waveFile);
//	picojson::value v;
//	ifs >> v;

	std::string fileStr = readFile(waveFile);
	picojson::value v;
	std::string err;
	picojson::parse(v, fileStr.begin(), fileStr.end(), &err);

	picojson::object& obj = v.get<picojson::object>();
	auto wavesArray = obj["waves"].get<picojson::array>();

	for (picojson::array::const_iterator i = wavesArray.begin(); i != wavesArray.end(); ++i)
	{

		Wave w;
		auto waveObj = (*i).get<picojson::object>();
		auto creepsArray = waveObj["creeps"].get<picojson::array>();

		for (picojson::array::const_iterator creeps = creepsArray.begin(); creeps != creepsArray.end(); ++creeps)
		{
			auto creepObj = (*creeps).get<picojson::object>();

			w.push_back(
					WaveCreep( creepObj["name"].get<std::string>(), creepObj["quantity"].get<double>(), creepObj["delay"].get<double>() )
					);
		}
		waves.push_back(w);
	}

	return waves;
}
Пример #11
0
void LightDemo::UpdateScene(float dt)
{
    DemoApp::UpdateScene(dt);

	// Every quarter second, generate a random wave.
	static float t_base = 0.0f;
	if( (m_timer.TotalTime() - t_base) >= 0.25f )
	{
		t_base += 0.25f;
 
		uint32 i = 5 + rand() % (m_waves.RowCount()-10);
		uint32 j = 5 + rand() % (m_waves.ColumnCount()-10);

		float r = MathHelper::RandF(1.0f, 2.0f);

		m_waves.Disturb(i, j, r);
	}

	m_waves.Update(dt);

	// Update the wave vertex buffer with the new solution.
	
	D3D11_MAPPED_SUBRESOURCE mappedData;
	HR(m_dxImmediateContext->Map(m_wavesVB.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));

	Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData);
	for(UINT i = 0; i < m_waves.VertexCount(); ++i)
	{
		v[i].pos = m_waves[i];
		v[i].normal = m_waves.Normal(i);
	}

	m_dxImmediateContext->Unmap(m_wavesVB.Get(), 0);

	// Animate the lights.

	// Circle light over the land surface.
	m_pointLight.position.x = 70.0f*cosf( 0.2f*m_timer.TotalTime() );
	m_pointLight.position.z = 70.0f*sinf( 0.2f*m_timer.TotalTime() );
	m_pointLight.position.y = MathHelper::Max(GetHeight(m_pointLight.position.x, 
		m_pointLight.position.z), -3.0f) + 10.0f;


	// The spotlight takes on the camera position and is aimed in the
	// same direction the camera is looking.  In this way, it looks
	// like we are holding a flashlight.
    m_spotLight.position = m_camPosition;

    XMVECTOR pos = XMLoadFloat3(&m_camPosition);
    XMVECTOR target = XMLoadFloat3(&m_camTarget);
	XMStoreFloat3(&m_spotLight.direction, XMVector3Normalize(target - pos));
}
Пример #12
0
void LightDemo::DrawScene()
{
    m_dxImmediateContext->ClearRenderTargetView(m_renderTargetView.Get(), reinterpret_cast<const float*>(&oc::Colors::LightSteelBlue));
    m_dxImmediateContext->ClearDepthStencilView(m_depthStencilView.Get(), D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	m_dxImmediateContext->IASetInputLayout(m_inputLayout.Get());
    m_dxImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	uint32 stride = sizeof(Vertex);
    uint32 offset = 0;

	// Set constants
	XMMATRIX view  = XMLoadFloat4x4(&m_view);
	XMMATRIX proj  = XMLoadFloat4x4(&m_proj);
	XMMATRIX viewProj  = view*proj;

    // Set per frame constants.
	m_fxDirLight->SetRawValue(&m_dirLight, 0, sizeof(m_dirLight));
	m_fxPointLight->SetRawValue(&m_pointLight, 0, sizeof(m_pointLight));
	m_fxSpotLight->SetRawValue(&m_spotLight, 0, sizeof(m_spotLight));
	m_fxEyePosW->SetRawValue(&m_camPosition, 0, sizeof(m_camPosition));
 
    D3DX11_TECHNIQUE_DESC techDesc;
    m_tech->GetDesc(&techDesc);
    for(uint32 p = 0; p < techDesc.Passes; ++p)
    {
        //Draw the land
        m_dxImmediateContext->IASetVertexBuffers(0, 1, m_landVB.GetAddressOf(), &stride, &offset);
        m_dxImmediateContext->IASetIndexBuffer(m_landIB.Get(), DXGI_FORMAT_R32_UINT, 0);

        //Set per object constants
        XMMATRIX world = XMLoadFloat4x4(&m_landWorld);
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
        XMMATRIX worldViewProj = world*viewProj;

		m_fxWorld->SetMatrix(reinterpret_cast<float*>(&world));
		m_fxWorldInvTranspose->SetMatrix(reinterpret_cast<float*>(&worldInvTranspose));
		m_fxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));
        m_fxMaterial->SetRawValue(&m_landMat, 0, sizeof(m_landMat));

        m_tech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get());
        m_dxImmediateContext->DrawIndexed(m_landIndexCount, 0, 0);

        //Draw the wave
        m_dxImmediateContext->IASetVertexBuffers(0, 1, m_wavesVB.GetAddressOf(), &stride, &offset);
        m_dxImmediateContext->IASetIndexBuffer(m_wavesIB.Get(), DXGI_FORMAT_R32_UINT, 0);

        world = XMLoadFloat4x4(&m_wavesWorld);
        worldInvTranspose = MathHelper::InverseTranspose(world);
        worldViewProj = world*viewProj;

		m_fxWorld->SetMatrix(reinterpret_cast<float*>(&world));
		m_fxWorldInvTranspose->SetMatrix(reinterpret_cast<float*>(&worldInvTranspose));
		m_fxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));
        m_fxMaterial->SetRawValue(&m_wavesMat, 0, sizeof(m_wavesMat));

        m_tech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get());
        m_dxImmediateContext->DrawIndexed(3*m_waves.TriangleCount(), 0, 0);
    }

	HR(m_swapChain->Present(0, 0));
}
Пример #13
0
void WavesApp::DrawScene()
{
	assert(m_pD3dImmediateContext);
	assert(m_pSwapChain);

	m_pD3dImmediateContext->ClearRenderTargetView(m_pRenderTargetView, reinterpret_cast<const float *>(&Colors::LightSteelBlue));
	m_pD3dImmediateContext->ClearDepthStencilView(m_pDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	//设置顶点输入数据流格式
	m_pD3dImmediateContext->IASetInputLayout(m_pInputLayout);

	//设置顶点拼装方式
	m_pD3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	UINT stride = sizeof(Vertex);
	UINT offset = 0;
	

	//Update Constants before drawcall
	
	XMMATRIX view = XMLoadFloat4x4(&m_viewMatrix);
	XMMATRIX proj = XMLoadFloat4x4(&m_projMaxtrix);
	

	

	//Draw all the passes
	D3DX11_TECHNIQUE_DESC techDesc;
	m_pShaderTech->GetDesc(&techDesc);
	for (UINT i = 0; i < techDesc.Passes;++i)
	{
		{
			//
			//Draw the land.
			//

			m_pD3dImmediateContext->IASetVertexBuffers(0, 1, &m_pLandVB, &stride, &offset);
			m_pD3dImmediateContext->IASetIndexBuffer(m_pLandIB, DXGI_FORMAT_R32_UINT, 0);
			XMMATRIX world = XMLoadFloat4x4(&m_GridWorldMatrix);
			XMMATRIX worldViewProj = world*view*proj;
			m_pShaderMVP->SetMatrix(reinterpret_cast<float *>(&worldViewProj));
			m_pShaderTech->GetPassByIndex(i)->Apply(0, m_pD3dImmediateContext);//首参数无意义,已被放弃
			m_pD3dImmediateContext->DrawIndexed(m_uLandGridIndexCount, 0, 0);

		}
		

		
		{
			//
			//Draw the Waves.
			//
			m_pD3dImmediateContext->RSSetState(m_pWireframeRS);

			m_pD3dImmediateContext->IASetVertexBuffers(0, 1, &m_pWavesVB, &stride, &offset);
			m_pD3dImmediateContext->IASetIndexBuffer(m_pWavesIB, DXGI_FORMAT_R32_UINT, 0);
			
			XMMATRIX world = XMLoadFloat4x4(&m_WavesWorldMatrix);
			XMMATRIX worldViewProj = world*view*proj;
			m_pShaderMVP->SetMatrix(reinterpret_cast<float *>(&worldViewProj));
			m_pShaderTech->GetPassByIndex(i)->Apply(0, m_pD3dImmediateContext);//首参数无意义,已被放弃
			m_pD3dImmediateContext->DrawIndexed(3 * m_waves.GetTriangleCount(), 0, 0);

			m_pD3dImmediateContext->RSSetState(nullptr);
		}
		
	}


	HR(m_pSwapChain->Present(0,0));
}
Пример #14
0
void LightingApp::DrawScene()
{
	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::LightSteelBlue));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);

	md3dImmediateContext->IASetInputLayout(mInputLayout);
	md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	md3dImmediateContext->PSSetShader(mPixelShader, NULL, 0);
	md3dImmediateContext->VSSetShader(mVertexShader, NULL, 0);

	UINT stride = sizeof(Vertex);
	UINT offset = 0;

	XMMATRIX view = XMLoadFloat4x4(&mView);
	XMMATRIX proj = XMLoadFloat4x4(&mProj);
	XMMATRIX viewProj = view*proj;

	// Set per frame constants.
	mFrameConstantBuffer.Data.mDirLight = mDirLight;
	mFrameConstantBuffer.Data.mPointLight = mPointLight;
	mFrameConstantBuffer.Data.mSpotLight = mSpotLight;
	mFrameConstantBuffer.Data.mEyePosW = mEyePosW;

	//
	// Draw the hills.
	//
	md3dImmediateContext->IASetVertexBuffers(0, 1, &mLandVB, &stride, &offset);
	md3dImmediateContext->IASetIndexBuffer(mLandIB, DXGI_FORMAT_R32_UINT, 0);

	// Set per object constants.
	XMMATRIX world = XMLoadFloat4x4(&mLandWorld);
	XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
	XMMATRIX worldViewProj = XMMatrixTranspose(world*view*proj);

	mObjectConstantBuffer.Data.mWorld = mLandWorld;
	XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldInvTranspose, worldInvTranspose);
	XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldViewProj, worldViewProj);
	mObjectConstantBuffer.Data.mMaterial = mLandMat;

	mObjectConstantBuffer.ApplyChanges(md3dImmediateContext);
	mFrameConstantBuffer.ApplyChanges(md3dImmediateContext);

	ID3D11Buffer* buffer[2] = { mObjectConstantBuffer.Buffer(), mFrameConstantBuffer.Buffer() };
	md3dImmediateContext->VSSetConstantBuffers(0, 1, &buffer[0]);
	md3dImmediateContext->PSSetConstantBuffers(0, 2, buffer);


	md3dImmediateContext->DrawIndexed(mLandIndexCount, 0, 0);

	//
	// Draw the waves.
	//
	md3dImmediateContext->IASetVertexBuffers(0, 1, &mWavesVB, &stride, &offset);
	md3dImmediateContext->IASetIndexBuffer(mWavesIB, DXGI_FORMAT_R32_UINT, 0);

	// Set per object constants.
	world = XMLoadFloat4x4(&mWavesWorld);
	worldInvTranspose = MathHelper::InverseTranspose(world);
	worldViewProj = XMMatrixTranspose(world*view*proj);

	mObjectConstantBuffer.Data.mWorld = mWavesWorld;
	XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldInvTranspose, worldInvTranspose);
	XMStoreFloat4x4(&mObjectConstantBuffer.Data.mWorldViewProj, worldViewProj);
	mObjectConstantBuffer.Data.mMaterial = mWavesMat;

	mObjectConstantBuffer.ApplyChanges(md3dImmediateContext);
	mFrameConstantBuffer.ApplyChanges(md3dImmediateContext);

	md3dImmediateContext->VSSetConstantBuffers(0, 1, &buffer[0]);
	md3dImmediateContext->PSSetConstantBuffers(0, 2, buffer);

	md3dImmediateContext->DrawIndexed(3 * mWaves.TriangleCount(), 0, 0);


	HR(mSwapChain->Present(0, 0));
}
Пример #15
0
void TreeBillboardApp::UpdateScene(float dt)
{
    DemoApp::UpdateScene(dt);

	// Every quarter second, generate a random wave.
	static float t_base = 0.0f;
	if( (m_timer.TotalTime() - t_base) >= 0.1f )
	{
		t_base += 0.1f;
 
		uint32 i = 5 + rand() % (m_waves.RowCount()-10);
		uint32 j = 5 + rand() % (m_waves.ColumnCount()-10);

		float r = MathHelper::RandF(0.5f, 1.0f);

		m_waves.Disturb(i, j, r);
	}

	m_waves.Update(dt);

	// Update the wave vertex buffer with the new solution.
	
	D3D11_MAPPED_SUBRESOURCE mappedData;
	HR(m_dxImmediateContext->Map(m_wavesVB.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));

    Vertex::Basic32* v = reinterpret_cast<Vertex::Basic32*>(mappedData.pData);
	for(UINT i = 0; i < m_waves.VertexCount(); ++i)
	{
		v[i].Pos = m_waves[i];
		v[i].Normal = m_waves.Normal(i);

        // Derive tex-coords in [0,1] from position.
		v[i].Tex.x  = 0.5f + m_waves[i].x / m_waves.Width();
		v[i].Tex.y  = 0.5f - m_waves[i].z / m_waves.Depth();
	}

	m_dxImmediateContext->Unmap(m_wavesVB.Get(), 0);

	// Animate water texture coordinates.

	// Tile water texture.
	XMMATRIX wavesScale = XMMatrixScaling(5.0f, 5.0f, 0.0f);

	// Translate texture over time (this animate the texture)
	m_waterTexOffset.y += 0.05f*dt;
	m_waterTexOffset.x += 0.1f*dt;	
	XMMATRIX wavesOffset = XMMatrixTranslation(m_waterTexOffset.x, m_waterTexOffset.y, 0.0f);

	// Combine scale and translation.
	XMStoreFloat4x4(&m_waterTexTransform, wavesScale*wavesOffset);

	// Switch the render mode based in key input.
	if( GetAsyncKeyState('1') & 0x8000 )
        m_renderOptions = RenderOptions::Lighting; 

	if( GetAsyncKeyState('2') & 0x8000 )
		m_renderOptions = RenderOptions::Textures; 

	if( GetAsyncKeyState('3') & 0x8000 )
		m_renderOptions = RenderOptions::TexturesAndFog; 

    if( GetAsyncKeyState('R') & 0x8000 )
		m_alphaToCoverageOn = true;

	if( GetAsyncKeyState('T') & 0x8000 )
		m_alphaToCoverageOn = false;
}
Пример #16
0
void BlendApp::DrawScene()
{
	md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::Silver));
	md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	md3dImmediateContext->IASetInputLayout(mInputLayout);
    md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
 
	float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};

	UINT stride = sizeof(Vertex);
    UINT offset = 0;
 
	XMMATRIX view  = XMLoadFloat4x4(&mView);
	XMMATRIX proj  = XMLoadFloat4x4(&mProj);
	XMMATRIX viewProj = view*proj;

	mConstStruct.gEyePosW = mEyePosW;

	md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVB, &stride, &offset);
	md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);

	// Set per object constants.
	XMMATRIX world = XMLoadFloat4x4(&mBoxWorld);
	XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
	XMMATRIX worldViewProj = world*view*proj;

	mConstStruct.gWorld = XMMatrixTranspose(world);
	mConstStruct.gWorldInvTranspose = XMMatrixTranspose(worldInvTranspose);
	mConstStruct.gWorldViewProj = XMMatrixTranspose(worldViewProj);
	mConstStruct.gTexTransform = XMMatrixTranspose(XMMatrixIdentity());

	md3dImmediateContext->UpdateSubresource(mConstBuffer, 0, NULL, &mConstStruct, 0, 0);

	md3dImmediateContext->VSSetShader(mVertexShader, NULL, 0);
	md3dImmediateContext->VSSetConstantBuffers(0, 1, &mConstBuffer);
	md3dImmediateContext->PSSetShader(mPixelShader, NULL, 0);
	md3dImmediateContext->PSSetConstantBuffers(0, 1, &mConstBuffer);
	
	md3dImmediateContext->RSSetState(RenderStates::NoCullRS);
	
	md3dImmediateContext->PSSetShaderResources(0, 1, &mBoxMapSRV);
	md3dImmediateContext->DrawIndexed(36, 0, 0);

	// Restore default render state.
	md3dImmediateContext->RSSetState(0);
		

	//
	// Draw the hills.
	//
	md3dImmediateContext->IASetVertexBuffers(0, 1, &mLandVB, &stride, &offset);
	md3dImmediateContext->IASetIndexBuffer(mLandIB, DXGI_FORMAT_R32_UINT, 0);

	// Set per object constants.
	world = XMLoadFloat4x4(&mLandWorld);
	worldInvTranspose = MathHelper::InverseTranspose(world);
	worldViewProj = world*view*proj;

	mConstStruct.gWorld = XMMatrixTranspose(world);
	mConstStruct.gWorldInvTranspose = XMMatrixTranspose(worldInvTranspose);
	mConstStruct.gWorldViewProj = XMMatrixTranspose(worldViewProj);
	mConstStruct.gTexTransform = XMMatrixTranspose(XMLoadFloat4x4(&mGrassTexTransform));
	mConstStruct.gMaterial = mLandMat;

	md3dImmediateContext->PSSetShaderResources(0, 1, &mGrassMapSRV);
	md3dImmediateContext->UpdateSubresource(mConstBuffer, 0, NULL, &mConstStruct, 0, 0);
	md3dImmediateContext->DrawIndexed(mLandIndexCount, 0, 0);

	//
	// Draw the waves.
	//
	md3dImmediateContext->IASetVertexBuffers(0, 1, &mWavesVB, &stride, &offset);
	md3dImmediateContext->IASetIndexBuffer(mWavesIB, DXGI_FORMAT_R32_UINT, 0);

	// Set per object constants.
	world = XMLoadFloat4x4(&mWavesWorld);
	worldInvTranspose = MathHelper::InverseTranspose(world);
	worldViewProj = world*view*proj;

	mConstStruct.gWorld = XMMatrixTranspose(world);
	mConstStruct.gWorldInvTranspose = XMMatrixTranspose(worldInvTranspose);
	mConstStruct.gWorldViewProj = XMMatrixTranspose(worldViewProj);
	mConstStruct.gTexTransform = XMMatrixTranspose(XMLoadFloat4x4(&mWaterTexTransform));
	mConstStruct.gMaterial = mWavesMat;

	md3dImmediateContext->PSSetShaderResources(0, 1, &mWavesMapSRV);
	md3dImmediateContext->UpdateSubresource(mConstBuffer, 0, NULL, &mConstStruct, 0, 0);

	md3dImmediateContext->OMSetBlendState(RenderStates::TransparentBS, blendFactor, 0xffffffff);
	md3dImmediateContext->DrawIndexed(3 * mWaves.TriangleCount(), 0, 0);

	// Restore default blend state
	md3dImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);
		
	HR(mSwapChain->Present(0, 0));
}
Пример #17
0
void BlendApp::UpdateScene(float dt)
{
	// Convert Spherical to Cartesian coordinates.
	float x = mRadius*sinf(mPhi)*cosf(mTheta);
	float z = mRadius*sinf(mPhi)*sinf(mTheta);
	float y = mRadius*cosf(mPhi);

	mEyePosW = XMFLOAT3(x, y, z);

	// Build the view matrix.
	XMVECTOR pos    = XMVectorSet(x, y, z, 1.0f);
	XMVECTOR target = XMVectorZero();
	XMVECTOR up     = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

	XMMATRIX V = XMMatrixLookAtLH(pos, target, up);
	XMStoreFloat4x4(&mView, V);

	//
	// Every quarter second, generate a random wave.
	//
	static float t_base = 0.0f;
	if( (mTimer.TotalTime() - t_base) >= 0.1f )
	{
		t_base += 0.1f;
 
		DWORD i = 5 + rand() % (mWaves.RowCount()-10);
		DWORD j = 5 + rand() % (mWaves.ColumnCount()-10);

		float r = MathHelper::RandF(0.5f, 1.0f);

		mWaves.Disturb(i, j, r);
	}

	mWaves.Update(dt);

	//
	// Update the wave vertex buffer with the new solution.
	//
	


	D3D11_MAPPED_SUBRESOURCE mappedData;
	HR(md3dImmediateContext->Map(mWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));

	Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData);
	for(UINT i = 0; i < mWaves.VertexCount(); ++i)
	{
		v[i].Pos    = mWaves[i];
		v[i].Normal = mWaves.Normal(i);

		// Derive tex-coords in [0,1] from position.
		v[i].Tex.x  = 0.5f + mWaves[i].x / mWaves.Width();
		v[i].Tex.y  = 0.5f - mWaves[i].z / mWaves.Depth();
	}

	md3dImmediateContext->Unmap(mWavesVB, 0);

	//
	// Animate water texture coordinates.
	//


	// Tile water texture.
	XMMATRIX wavesScale = XMMatrixScaling(5.0f, 5.0f, 0.0f);

	// Translate texture over time.
	mWaterTexOffset.y += 0.05f*dt;
	mWaterTexOffset.x += 0.1f*dt;	
	XMMATRIX wavesOffset = XMMatrixTranslation(mWaterTexOffset.x, mWaterTexOffset.y, 0.0f);

	// Combine scale and translation.
	XMStoreFloat4x4(&mWaterTexTransform, wavesScale*wavesOffset);

	//
	// Switch the render mode based in key input.
	//
	if( GetAsyncKeyState('1') & 0x8000 )
		mRenderOptions = RenderOptions::Lighting; 

	if( GetAsyncKeyState('2') & 0x8000 )
		mRenderOptions = RenderOptions::Textures; 

	if( GetAsyncKeyState('3') & 0x8000 )
		mRenderOptions = RenderOptions::TexturesAndFog; 
}
Пример #18
0
void TreeBillboardApp::DrawScene()
{
    m_dxImmediateContext->ClearRenderTargetView(m_renderTargetView.Get(), reinterpret_cast<const float*>(&oc::Colors::Silver));
    m_dxImmediateContext->ClearDepthStencilView(m_depthStencilView.Get(), D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

    float blendFactor[] = {0.0f, 0.0f, 0.0f, 0.0f};

	// Set constants
	XMMATRIX view  = XMLoadFloat4x4(&m_view);
	XMMATRIX proj  = XMLoadFloat4x4(&m_proj);
	XMMATRIX viewProj  = view*proj;

    //Draw tree sprites (change states, input layout and primitive topology)
    DrawTreeSprites(viewProj);

    m_dxImmediateContext->IASetInputLayout(InputLayouts::Basic32.Get());
    m_dxImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	uint32 stride = sizeof(Vertex::Basic32);
    uint32 offset = 0;

    // Set per frame constants.
    Effects::BasicFX->SetDirLights(m_dirLight);
    Effects::BasicFX->SetEyePosW(m_camPosition);
   	Effects::BasicFX->SetFogColor(oc::Colors::Silver);
	Effects::BasicFX->SetFogStart(25.0f);
	Effects::BasicFX->SetFogRange(200.0f);

   	ID3DX11EffectTechnique* boxTech = nullptr;
    ID3DX11EffectTechnique* landAndWavesTech = nullptr;

    switch(m_renderOptions)
	{
	case RenderOptions::Lighting:
		boxTech = Effects::BasicFX->Light3Tech;
		landAndWavesTech = Effects::BasicFX->Light3Tech;
		break;
	case RenderOptions::Textures:
		boxTech = Effects::BasicFX->Light3TexAlphaClipTech;
		landAndWavesTech = Effects::BasicFX->Light3TexTech;
		break;
	case RenderOptions::TexturesAndFog:
		boxTech = Effects::BasicFX->Light3TexAlphaClipFogTech;
		landAndWavesTech = Effects::BasicFX->Light3TexFogTech;
		break;
	}
 
    D3DX11_TECHNIQUE_DESC techDesc;
    boxTech->GetDesc(&techDesc);
    for(uint32 p = 0; p < techDesc.Passes; ++p)
    {
        //Draw the box with alpha clipping
        m_dxImmediateContext->IASetVertexBuffers(0, 1, m_boxVB.GetAddressOf(), &stride, &offset);
        m_dxImmediateContext->IASetIndexBuffer(m_boxIB.Get(), DXGI_FORMAT_R32_UINT, 0);

        // Set per object constants.
		XMMATRIX world = XMLoadFloat4x4(&m_boxWorld);
		XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
		XMMATRIX worldViewProj = world*view*proj;
		
		Effects::BasicFX->SetWorld(world);
		Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::BasicFX->SetWorldViewProj(worldViewProj);
		Effects::BasicFX->SetTexTransform(XMMatrixIdentity());
		Effects::BasicFX->SetMaterial(m_boxMat);
		Effects::BasicFX->SetDiffuseMap(m_boxMapSRV.Get());

        //Disable backface culling to render the crate properly (else the back of the crate won't be rendered)
        m_dxImmediateContext->RSSetState(RenderStates::NoCullRS.Get());
        boxTech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get());
		m_dxImmediateContext->DrawIndexed(36, 0, 0);

		// Restore default render state.
		m_dxImmediateContext->RSSetState(0);
    }

    //Draw the hills and water with texture and fog (no alpha clipping needed)
    landAndWavesTech->GetDesc(&techDesc);
    for(uint32 p = 0; p < techDesc.Passes; ++p)
    {
        //Draw the land
        m_dxImmediateContext->IASetVertexBuffers(0, 1, m_landVB.GetAddressOf(), &stride, &offset);
        m_dxImmediateContext->IASetIndexBuffer(m_landIB.Get(), DXGI_FORMAT_R32_UINT, 0);

        //Set per object constants
        XMMATRIX world = XMLoadFloat4x4(&m_landWorld);
        XMMATRIX worldInvTranspose = MathHelper::InverseTranspose(world);
        XMMATRIX worldViewProj = world*viewProj;

        Effects::BasicFX->SetWorld(world);
		Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::BasicFX->SetWorldViewProj(worldViewProj);
		Effects::BasicFX->SetTexTransform(XMLoadFloat4x4(&m_grassTexTransform));
        Effects::BasicFX->SetMaterial(m_landMat);
        Effects::BasicFX->SetDiffuseMap(m_grassMapSRV.Get());

        landAndWavesTech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get());
        m_dxImmediateContext->DrawIndexed(m_landIndexCount, 0, 0);

        //Draw the wave
        m_dxImmediateContext->IASetVertexBuffers(0, 1, m_wavesVB.GetAddressOf(), &stride, &offset);
        m_dxImmediateContext->IASetIndexBuffer(m_wavesIB.Get(), DXGI_FORMAT_R32_UINT, 0);

        world = XMLoadFloat4x4(&m_wavesWorld);
        worldInvTranspose = MathHelper::InverseTranspose(world);
        worldViewProj = world*viewProj;

        Effects::BasicFX->SetWorld(world);
		Effects::BasicFX->SetWorldInvTranspose(worldInvTranspose);
		Effects::BasicFX->SetWorldViewProj(worldViewProj);
        Effects::BasicFX->SetTexTransform(XMLoadFloat4x4(&m_waterTexTransform));
        Effects::BasicFX->SetMaterial(m_wavesMat);
        Effects::BasicFX->SetDiffuseMap(m_wavesMapSRV.Get());

        m_dxImmediateContext->OMSetBlendState(RenderStates::TransparentBS.Get(), blendFactor, 0xffffffff);
        landAndWavesTech->GetPassByIndex(p)->Apply(0, m_dxImmediateContext.Get());
        m_dxImmediateContext->DrawIndexed(3*m_waves.TriangleCount(), 0, 0);

		// Restore default blend state
		m_dxImmediateContext->OMSetBlendState(0, blendFactor, 0xffffffff);
    }

	HR(m_swapChain->Present(0, 0));
}
Пример #19
0
void LightingApp::UpdateScene(float dt)
{
	// Convert Spherical to Cartesian coordinates.
	float x = mRadius*sinf(mPhi)*cosf(mTheta);
	float z = mRadius*sinf(mPhi)*sinf(mTheta);
	float y = mRadius*cosf(mPhi);

	mEyePosW = XMFLOAT3(x, y, z);

	// Build the view matrix.
	XMVECTOR pos    = XMVectorSet(x, y, z, 1.0f);
	XMVECTOR target = XMVectorZero();
	XMVECTOR up     = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

	XMMATRIX V = XMMatrixLookAtLH(pos, target, up);
	XMStoreFloat4x4(&mView, V);

	//
	// Every quarter second, generate a random wave.
	//
	static float t_base = 0.0f;
	if( (mTimer.TotalTime() - t_base) >= 0.25f )
	{
		t_base += 0.25f;
 
		DWORD i = 5 + rand() % (mWaves.RowCount()-10);
		DWORD j = 5 + rand() % (mWaves.ColumnCount()-10);

		float r = MathHelper::RandF(1.0f, 2.0f);

		mWaves.Disturb(i, j, r);
	}

	mWaves.Update(dt);

	//
	// Update the wave vertex buffer with the new solution.
	//
	
	D3D11_MAPPED_SUBRESOURCE mappedData;
	HR(md3dImmediateContext->Map(mWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));

	Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData);
	for(UINT i = 0; i < mWaves.VertexCount(); ++i)
	{
		v[i].Pos    = mWaves[i];
		v[i].Normal = mWaves.Normal(i);
	}

	md3dImmediateContext->Unmap(mWavesVB, 0);

	//
	// Animate the lights.
	//

	// Circle light over the land surface.
	mPointLight.Position.x = 70.0f*cosf( 0.2f*mTimer.TotalTime() );
	mPointLight.Position.z = 70.0f*sinf( 0.2f*mTimer.TotalTime() );
	mPointLight.Position.y = MathHelper::Max(GetHillHeight(mPointLight.Position.x, 
		mPointLight.Position.z), -3.0f) + 10.0f;


	// The spotlight takes on the camera position and is aimed in the
	// same direction the camera is looking.  In this way, it looks
	// like we are holding a flashlight.
	mSpotLight.Position = mEyePosW;
	XMStoreFloat3(&mSpotLight.Direction, XMVector3Normalize(target - pos));
}