void TerrainController::init(ID3D11Device* device)
{
	width = Config::getValue<int>(ConfigKeys::terrainWidth);
	length = Config::getValue<int>(ConfigKeys::terrainLength);

	position = Vector3(width / -2.0f, 0, length / -2.0f);

	bumpiness = Config::getValue<float>(ConfigKeys::terrainBumpiness);
	quality = Config::getValue<float>(ConfigKeys::terrainQuality);

	D3DX11_IMAGE_LOAD_INFO loadInfo;
	ZeroMemory( &loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO) );
	loadInfo.Usage = D3D11_USAGE_STAGING;
	loadInfo.Format = DXGI_FORMAT_FROM_FILE;
	loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_READ;

	ID3D11Texture2D *heightmapTexture, *normalmapTexture;

	D3DX11CreateTextureFromFile( device, L"Content/Textures/Terrain/Heightmap.png", &loadInfo, NULL, (ID3D11Resource**)(&heightmapTexture), NULL );
	D3DX11CreateTextureFromFile( device, L"Content/Textures/Terrain/Normalmap.png", &loadInfo, NULL, (ID3D11Resource**)(&normalmapTexture), NULL );

	heightmapTexture->GetDesc(&heightmapDesc);
	normalmapTexture->GetDesc(&normalmapDesc);

	std::vector<HeightmapFormat> hData = std::vector<HeightmapFormat>(heightmapDesc.Width * heightmapDesc.Height);
	std::vector<NormalmapFormat> nData = std::vector<NormalmapFormat>(normalmapDesc.Width * normalmapDesc.Height);

	ID3D11DeviceContext* deviceContext;
	device->GetImmediateContext(&deviceContext);

	D3D11_MAPPED_SUBRESOURCE heightmapData;
	deviceContext->Map(heightmapTexture, 0, D3D11_MAP_READ, 0, &heightmapData);
	memcpy((void*)&hData[0], heightmapData.pData, heightmapDesc.Width * heightmapDesc.Height * sizeof(HeightmapFormat));
	deviceContext->Unmap(heightmapTexture, 0);

	D3D11_MAPPED_SUBRESOURCE normalmapData;
	deviceContext->Map(normalmapTexture, 0, D3D11_MAP_READ, 0, &normalmapData);
	memcpy((void*)&nData[0], normalmapData.pData, normalmapDesc.Width * normalmapDesc.Height * sizeof(NormalmapFormat));
	deviceContext->Unmap(normalmapTexture, 0);

	heightData = HeightData(heightmapDesc.Width * heightmapDesc.Height);
	normalData = NormalData(normalmapDesc.Width * normalmapDesc.Height);

	std::transform(hData.begin(), hData.end(), heightData.begin(), &TerrainController::convertHeight);
	std::transform(nData.begin(), nData.end(), normalData.begin(), &TerrainController::convertNormal);
	
	heightmapTexture->Release();
	normalmapTexture->Release();
	deviceContext->Release();
}
Example #2
0
HRESULT load_texture(ID3D11Device * device, const wchar_t * filename, ID3D11ShaderResourceView ** out_srv)

{

	ID3D11Resource* new_texture;

	D3DX11_IMAGE_LOAD_INFO image_info;

	image_info.Format = DXGI_FORMAT_R8G8B8A8_TYPELESS;

	HRESULT hr = D3DX11CreateTextureFromFile(device, filename, &image_info, NULL, &new_texture, NULL);



	D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc;

	ZeroMemory(&srv_desc, sizeof(srv_desc));

	srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;

	srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;

	srv_desc.Texture2D.MipLevels = 1;

	srv_desc.Texture2D.MostDetailedMip = 0;

	hr = device->CreateShaderResourceView(new_texture, &srv_desc, out_srv);



    return hr;

}
Example #3
0
static HRESULT MyCreateTextureFromFile(ID3D11Device *dev, LPCWSTR filename, D3DX11_IMAGE_LOAD_INFO *nfo,
    ID3DX11ThreadPump *pump, ID3D11Resource **ppTexture, HRESULT *pHResult)
{
    HRESULT hr;
    if (!pump && !pHResult && SUCCEEDED(hr = LoadDDSFast(dev, filename, nfo, ppTexture)))
        return hr;

    return D3DX11CreateTextureFromFile(dev, filename, nfo, pump, ppTexture, pHResult);
}
Example #4
0
void LoadSkybox(ID3D11Device* d3dDevice, LPCWSTR fileName)
{
    ID3D11Resource* resource = 0;
    HRESULT hr;
    hr = D3DX11CreateTextureFromFile(d3dDevice, fileName, 0, 0, &resource, 0);
    assert(SUCCEEDED(hr));

    d3dDevice->CreateShaderResourceView(resource, 0, &gSkyboxSRV);
    resource->Release();
}
HRESULT LoadSkyboxFromFile(LPCSTR file, ID3D11Device* pd3dDevice)
{

	HRESULT hr;
		
	D3DX11_IMAGE_INFO SrcInfo;
	hr = D3DX11GetImageInfoFromFile(file, NULL, &SrcInfo, NULL);

	D3DX11_IMAGE_LOAD_INFO texLoadInfo;
	texLoadInfo.Width          = SrcInfo.Width;
	texLoadInfo.Height         = SrcInfo.Height;
	texLoadInfo.Depth          = SrcInfo.Depth;
	texLoadInfo.FirstMipLevel  = 0;
	texLoadInfo.MipLevels      = SrcInfo.MipLevels;
	texLoadInfo.Usage          = D3D11_USAGE_DEFAULT;
	texLoadInfo.BindFlags      = D3D11_BIND_SHADER_RESOURCE;
	texLoadInfo.CpuAccessFlags = 0;
	texLoadInfo.MiscFlags      = SrcInfo.MiscFlags;
	texLoadInfo.Format         = SrcInfo.Format;
	texLoadInfo.Filter         = D3DX11_FILTER_TRIANGLE;
	texLoadInfo.MipFilter      = D3DX11_FILTER_TRIANGLE;
	texLoadInfo.pSrcInfo       = &SrcInfo;
	ID3D11Resource *pRes = NULL;

	D3DX11CreateTextureFromFile(pd3dDevice, file, &texLoadInfo, NULL, &pRes, NULL);
	if (pRes)
	{
		ID3D11Texture2D* texture;

		pRes->QueryInterface(__uuidof(ID3D11Texture2D), (LPVOID*)&texture);
		D3D11_SHADER_RESOURCE_VIEW_DESC SRVDesc;
		ZeroMemory(&SRVDesc, sizeof(SRVDesc));
		SRVDesc.Format = texLoadInfo.Format;
		SRVDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
		SRVDesc.Texture2D.MostDetailedMip = 0;
		SRVDesc.Texture2D.MipLevels = texLoadInfo.MipLevels;

		ID3D11ShaderResourceView* textureRview;
		pd3dDevice->CreateShaderResourceView(texture, &SRVDesc, &textureRview);

		g_Skybox.OnD3D11CreateDevice(pd3dDevice, 1, texture, textureRview);

		// Sky box class holds references.
		//SAFE_RELEASE(texture);
		//SAFE_RELEASE(textureRview);
	}

	SAFE_RELEASE(pRes);
	return S_OK;
}
Example #6
0
ComputeTexture* ComputeWrap::CreateTexture(TCHAR* textureFilename, char* debugName)
{
	ComputeTexture* texture = new ComputeTexture();
	texture->_D3DContext = mD3DDeviceContext;

	if(SUCCEEDED(D3DX11CreateTextureFromFile(mD3DDevice, textureFilename, NULL, NULL, (ID3D11Resource**)&texture->_Resource, NULL)))
	{
		texture->_ResourceView = CreateTextureSRV(texture->_Resource);
		
		if(debugName)
		{
			if(texture->_Resource)				SetDebugName(texture->_Resource, debugName);
			if(texture->_Staging)				SetDebugName(texture->_Staging, debugName);
			if(texture->_ResourceView)			SetDebugName(texture->_ResourceView, debugName);
			if(texture->_UnorderedAccessView)	SetDebugName(texture->_UnorderedAccessView, debugName);
		}
	}
	return texture;
}
void DxTexture::InitialiseCubeMap(ID3D11Device* device)
{
    const std::string filePath = m_texture.Path() + ".dds";
    if (!boost::filesystem::exists(filePath))
    {
        Logger::LogError("DirectX: " + filePath + " doesn't exist");
    }

    D3DX11_IMAGE_LOAD_INFO loadInfo;
    loadInfo.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;

    ID3D11Texture2D* texture = nullptr;
    SetDebugName(texture, filePath + "_texture");

    if (FAILED(D3DX11CreateTextureFromFile(device, filePath.c_str(),
                                           &loadInfo, 0, (ID3D11Resource**)&texture, 0)))
    {
        Logger::LogError("DirectX: Failed to create texture " + filePath);
    }

    D3D11_TEXTURE2D_DESC textureDesc;
    texture->GetDesc(&textureDesc);

    D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
    viewDesc.Format = textureDesc.Format;
    viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
    viewDesc.TextureCube.MipLevels = textureDesc.MipLevels;
    viewDesc.TextureCube.MostDetailedMip = 0;

    if (FAILED(device->CreateShaderResourceView(texture, &viewDesc, &m_view)))
    {
        Logger::LogError("DirectX: Failed to resource view " + filePath);
    }

    texture->Release();
}
Example #8
0
bool PerlinFire::LoadTexture2D(ID3D11Device * pd3dDevice, LPCWSTR fileName, ID3D11Texture2D ** tex, ID3D11ShaderResourceView ** texRV)
{
	HRESULT hr;
	ID3D11Resource * pRes = NULL;

	D3DX11CreateTextureFromFile( pd3dDevice, fileName, NULL, NULL, &pRes, &hr );

	pRes->QueryInterface( __uuidof(ID3D11Texture2D), (LPVOID*) tex);

	D3D11_TEXTURE2D_DESC desc;
	(*tex)->GetDesc(& desc);

	D3D11_SHADER_RESOURCE_VIEW_DESC descSRV;
	ZeroMemory( &descSRV, sizeof(descSRV) );
	descSRV.Format = desc.Format;
	descSRV.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	descSRV.Texture2D.MipLevels = desc.MipLevels;

	pd3dDevice->CreateShaderResourceView( *tex, &descSRV, texRV );

	pRes->Release();

	return true;
}
ID3D11ShaderResourceView* d3dHelper::CreateTexture2DArraySRV(
		ID3D11Device* device, ID3D11DeviceContext* context,
		std::vector<std::wstring>& filenames,
		DXGI_FORMAT format,
		UINT filter, 
		UINT mipFilter)
{
	//
	// Load the texture elements individually from file.  These textures
	// won't be used by the GPU (0 bind flags), they are just used to 
	// load the image data from file.  We use the STAGING usage so the
	// CPU can read the resource.
	//

	UINT size = filenames.size();

	std::vector<ID3D11Texture2D*> srcTex(size);
	for(UINT i = 0; i < size; ++i)
	{
		D3DX11_IMAGE_LOAD_INFO loadInfo;

        loadInfo.Width  = D3DX11_FROM_FILE;
        loadInfo.Height = D3DX11_FROM_FILE;
        loadInfo.Depth  = D3DX11_FROM_FILE;
        loadInfo.FirstMipLevel = 0;
        loadInfo.MipLevels = D3DX11_FROM_FILE;
        loadInfo.Usage = D3D11_USAGE_STAGING;
        loadInfo.BindFlags = 0;
        loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
        loadInfo.MiscFlags = 0;
        loadInfo.Format = format;
        loadInfo.Filter = filter;
        loadInfo.MipFilter = mipFilter;
		loadInfo.pSrcInfo  = 0;
		/*
		this is causing a linker error. God knows why... undo uncommet duncan help todo fix error
		*/
        HR(D3DX11CreateTextureFromFile(device, filenames[i].c_str(), 
			&loadInfo, 0, (ID3D11Resource**)&srcTex[i], 0));
		
	}

	//
	// Create the texture array.  Each element in the texture 
	// array has the same format/dimensions.
	//

	D3D11_TEXTURE2D_DESC texElementDesc;
	srcTex[0]->GetDesc(&texElementDesc);

	D3D11_TEXTURE2D_DESC texArrayDesc;
	texArrayDesc.Width              = texElementDesc.Width;
	texArrayDesc.Height             = texElementDesc.Height;
	texArrayDesc.MipLevels          = texElementDesc.MipLevels;
	texArrayDesc.ArraySize          = size;
	texArrayDesc.Format             = texElementDesc.Format;
	texArrayDesc.SampleDesc.Count   = 1;
	texArrayDesc.SampleDesc.Quality = 0;
	texArrayDesc.Usage              = D3D11_USAGE_DEFAULT;
	texArrayDesc.BindFlags          = D3D11_BIND_SHADER_RESOURCE;
	texArrayDesc.CPUAccessFlags     = 0;
	texArrayDesc.MiscFlags          = 0;

	ID3D11Texture2D* texArray = 0;
	HR(device->CreateTexture2D( &texArrayDesc, 0, &texArray));

	//
	// Copy individual texture elements into texture array.
	//

	// for each texture element...
	for(UINT texElement = 0; texElement < size; ++texElement)
	{
		// for each mipmap level...
		for(UINT mipLevel = 0; mipLevel < texElementDesc.MipLevels; ++mipLevel)
		{
			D3D11_MAPPED_SUBRESOURCE mappedTex2D;
			HR(context->Map(srcTex[texElement], mipLevel, D3D11_MAP_READ, 0, &mappedTex2D));

			context->UpdateSubresource(texArray, 
				D3D11CalcSubresource(mipLevel, texElement, texElementDesc.MipLevels),
				0, mappedTex2D.pData, mappedTex2D.RowPitch, mappedTex2D.DepthPitch);

			context->Unmap(srcTex[texElement], mipLevel);
		}
	}	

	//
	// Create a resource view to the texture array.
	//
	
	D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
	viewDesc.Format = texArrayDesc.Format;
	viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
	viewDesc.Texture2DArray.MostDetailedMip = 0;
	viewDesc.Texture2DArray.MipLevels = texArrayDesc.MipLevels;
	viewDesc.Texture2DArray.FirstArraySlice = 0;
	viewDesc.Texture2DArray.ArraySize = size;

	ID3D11ShaderResourceView* texArraySRV = 0;
	HR(device->CreateShaderResourceView(texArray, &viewDesc, &texArraySRV));

	//
	// Cleanup--we only need the resource view.
	//

	ReleaseCOM(texArray);

	for(UINT i = 0; i < size; ++i)
		ReleaseCOM(srcTex[i]);

	return texArraySRV;
}
Example #10
0
void Texture2D::CreateFromFile(const char * filepath)
{
	ID3D11Resource* pRes = static_cast<ID3D11Resource*>(m_pTexture);
	D3DX11CreateTextureFromFile(DX11API::D3D11Device(), StringHelper::s2ws(filepath).c_str(), nullptr, nullptr, &pRes, nullptr);
	m_pTexture = static_cast<ID3D11Texture2D*>(pRes);
}
Example #11
0
HRESULT ParticleSystem::CreateTexArray(const vector<string>& fileNames)
{
	HRESULT hr = S_OK;

	// Load the texture elements individually from file.  These textures
	// won't be used by the GPU (0 bind flags), they are just used to 
	// load the image data from file.  We use the STAGING usage so the
	// CPU can read the resource.
	this->mNrOfTextures = fileNames.size();

	vector<ID3D11Texture2D*> srcTex(this->mNrOfTextures, 0);
	for(UINT i = 0; i < this->mNrOfTextures; i++) 
	{
		D3DX11_IMAGE_LOAD_INFO loadInfo;

        loadInfo.Width  = D3DX11_FROM_FILE;
        loadInfo.Height = D3DX11_FROM_FILE;
        loadInfo.Depth  = D3DX11_FROM_FILE;
        loadInfo.FirstMipLevel = 0;
        loadInfo.MipLevels = D3DX11_FROM_FILE;
        loadInfo.Usage = D3D11_USAGE_STAGING;
        loadInfo.BindFlags = 0;
        loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
        loadInfo.MiscFlags = 0;
        loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
        loadInfo.Filter = D3DX11_FILTER_NONE;
        loadInfo.MipFilter = D3DX11_FILTER_NONE;
		loadInfo.pSrcInfo  = 0;

		hr = D3DX11CreateTextureFromFile(this->gDevice, fileNames[i].c_str(), &loadInfo, 0, (ID3D11Resource**)&srcTex[i], 0);
		if(FAILED(hr))
		{
			MessageBox(0, "Couldn't load particle texture(s)", "Create texture from file error", MB_ICONERROR); 
			return hr;
		}
	}

	// Create the texture array.  Each element in the texture 
	// array has the same format/dimensions.
	D3D11_TEXTURE2D_DESC texElementDesc;
	srcTex[0]->GetDesc(&texElementDesc);

	D3D11_TEXTURE2D_DESC texArrayDesc;
	texArrayDesc.Width              = texElementDesc.Width;
	texArrayDesc.Height             = texElementDesc.Height;
	texArrayDesc.MipLevels          = texElementDesc.MipLevels;
	texArrayDesc.ArraySize          = this->mNrOfTextures; //nr of texture elements to store
	texArrayDesc.Format             = DXGI_FORMAT_R8G8B8A8_UNORM;
	texArrayDesc.SampleDesc.Count   = 1;
	texArrayDesc.SampleDesc.Quality = 0;
	texArrayDesc.Usage              = D3D11_USAGE_DEFAULT;
	texArrayDesc.BindFlags          = D3D11_BIND_SHADER_RESOURCE;
	texArrayDesc.CPUAccessFlags     = 0;
	texArrayDesc.MiscFlags          = 0;

	ID3D11Texture2D* texArray = 0;
	hr = this->gDevice->CreateTexture2D( &texArrayDesc, 0, &texArray);
	if(FAILED(hr))
	{
		MessageBox(0, "Couldn't create particle texture(s)", "Create texture2d error", MB_ICONERROR);
		return hr;
	}

	
	// Copy individual texture elements into texture array.
	// for each texture element...
	for(UINT i = 0; i < this->mNrOfTextures; i++)
	{
		// for each mipmap level...
		for(UINT j = 0; j < texElementDesc.MipLevels; ++j)
		{
			D3D11_MAPPED_SUBRESOURCE mappedSubres;
			UINT subResource = D3D11CalcSubresource(j, i, texElementDesc.MipLevels); //**samma för texarray?**
			//map
			this->gDeviceContext->Map(srcTex[i], subResource, D3D11_MAP_READ_WRITE, 0, &mappedSubres); //**write onödig?**
			//update
			this->gDeviceContext->UpdateSubresource(texArray, subResource, 0, mappedSubres.pData, mappedSubres.RowPitch, 0);
			//unmap
			this->gDeviceContext->Unmap(srcTex[i], subResource);

			/*D3D10:
			D3D10_MAPPED_TEXTURE2D mappedTex2D;
			srcTex[i]->Map(j, D3D10_MAP_READ, 0, &mappedTex2D);
			
            this->gDevice->UpdateSubresource(texArray, 
				D3D10CalcSubresource(j, i, texElementDesc.MipLevels),
                0, mappedTex2D.pData, mappedTex2D.RowPitch, 0);

            srcTex[i]->Unmap(j);*/
		}
	}

	// Create a resource view to the texture array.
	D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
	viewDesc.Format = texArrayDesc.Format;
	viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
	viewDesc.Texture2DArray.MostDetailedMip = 0;
	viewDesc.Texture2DArray.MipLevels = texArrayDesc.MipLevels;
	viewDesc.Texture2DArray.FirstArraySlice = 0;
	viewDesc.Texture2DArray.ArraySize = this->mNrOfTextures;

	hr = this->gDevice->CreateShaderResourceView(texArray, &viewDesc, &this->mTexArraySRV);

	// Cleanup--we only need the resource view.
	if ( texArray ) texArray->Release(), texArray=0;

	for(UINT i = 0; i < this->mNrOfTextures; i++)
	{
		if(srcTex[i])
		{
			srcTex[i]->Release();
		}
	}

	return hr;
}
void Texture::CreateTexture2DArray(
	ID3D11Device* d3dDevice,
	ID3D11DeviceContext* d3dDeviceContext,
	std::vector<std::wstring>& filenames,
	DXGI_FORMAT format,
	UINT filter,
	UINT mipFilter)
{
	if (nullptr != SRV)
	{
		MessageBox(0, L"[Texture::CreateTexture2DArraySRV] This View have been created!", 0, 0);
		assert(false);
	}
	//
	// 从文件加载单独的纹理元素。这些纹理不能被GPU使用(0 bind flags),
	// 只是用来从文件中加载图像数据。我们使用STAGING让CPU可以访问资源。
	//

	UINT size = filenames.size();

	std::vector<ID3D11Texture2D*> srcTex(size);
	for (UINT i = 0; i < size; ++i)
	{
		D3DX11_IMAGE_LOAD_INFO loadInfo;

		loadInfo.Width = D3DX11_FROM_FILE;
		loadInfo.Height = D3DX11_FROM_FILE;
		loadInfo.Depth = D3DX11_FROM_FILE;
		loadInfo.FirstMipLevel = 0;
		loadInfo.MipLevels = D3DX11_FROM_FILE;
		loadInfo.Usage = D3D11_USAGE_STAGING;
		loadInfo.BindFlags = 0;
		loadInfo.CpuAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
		loadInfo.MiscFlags = 0;
		loadInfo.Format = format;
		loadInfo.Filter = filter;
		loadInfo.MipFilter = mipFilter;
		loadInfo.pSrcInfo = 0;

		HR(D3DX11CreateTextureFromFile(d3dDevice, filenames[i].c_str(),
			&loadInfo, 0, (ID3D11Resource**)&srcTex[i], 0));
	}

	//
	// 创建纹理数组。每个纹理元素都具有相同的格式/大小。
	//

	D3D11_TEXTURE2D_DESC texElementDesc;
	srcTex[0]->GetDesc(&texElementDesc);

	D3D11_TEXTURE2D_DESC texArrayDesc;
	texArrayDesc.Width = texElementDesc.Width;
	texArrayDesc.Height = texElementDesc.Height;
	texArrayDesc.MipLevels = texElementDesc.MipLevels;
	texArrayDesc.ArraySize = size;
	texArrayDesc.Format = texElementDesc.Format;
	texArrayDesc.SampleDesc.Count = 1;
	texArrayDesc.SampleDesc.Quality = 0;
	texArrayDesc.Usage = D3D11_USAGE_DEFAULT;
	texArrayDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	texArrayDesc.CPUAccessFlags = 0;
	texArrayDesc.MiscFlags = 0;

	ID3D11Texture2D* texArray = 0;
	HR(d3dDevice->CreateTexture2D(&texArrayDesc, 0, &texArray));

	//
	// 将单独的纹理元素复制到纹理数组中。
	//

	// 处理每个纹理元素...
	for (UINT texElement = 0; texElement < size; ++texElement)
	{
		// 处理每个渐进纹理层...
		for (UINT mipLevel = 0; mipLevel < texElementDesc.MipLevels; ++mipLevel)
		{
			D3D11_MAPPED_SUBRESOURCE mappedTex2D;
			HR(d3dDeviceContext->Map(srcTex[texElement], mipLevel, D3D11_MAP_READ, 0, &mappedTex2D));

			d3dDeviceContext->UpdateSubresource(texArray,
				D3D11CalcSubresource(mipLevel, texElement, texElementDesc.MipLevels),
				0, mappedTex2D.pData, mappedTex2D.RowPitch, mappedTex2D.DepthPitch);

			d3dDeviceContext->Unmap(srcTex[texElement], mipLevel);
		}
	}

	//
	// 创建纹理数组的资源视图
	//

	D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
	viewDesc.Format = texArrayDesc.Format;
	viewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
	viewDesc.Texture2DArray.MostDetailedMip = 0;
	viewDesc.Texture2DArray.MipLevels = texArrayDesc.MipLevels;
	viewDesc.Texture2DArray.FirstArraySlice = 0;
	viewDesc.Texture2DArray.ArraySize = size;

	HR(d3dDevice->CreateShaderResourceView(texArray, &viewDesc, &SRV));

	//
	// 清除--我们要的只是资源视图
	//

	SafeRelease(texArray);

	for (UINT i = 0; i < size; ++i)
		SafeRelease(srcTex[i]);
}
Example #13
0
void DxManager::CreateSkyBox(string texture)
{
	if(this->skybox)
		delete this->skybox;

	SkyBox* sb = new SkyBox(this->camera->getPosition(), 10, 10);
	MeshStrip* strip = sb->GetStrips()->get(0);

	// Create the desc for the buffer
	BUFFER_INIT_DESC BufferDesc;
	BufferDesc.ElementSize = sizeof(Vertex);
	BufferDesc.InitData = strip->getVerts();
	BufferDesc.NumElements = strip->getNrOfVerts();
	BufferDesc.Type = VERTEX_BUFFER;
	BufferDesc.Usage = BUFFER_DEFAULT;

	// Create the buffer
	Buffer* VertexBuffer = new Buffer();
	if(FAILED(VertexBuffer->Init(this->Dx_Device, this->Dx_DeviceContext, BufferDesc)))
		MaloW::Debug("Failed to init skybox");



	BUFFER_INIT_DESC indiceBufferDesc;
	indiceBufferDesc.ElementSize = sizeof(int);
	indiceBufferDesc.InitData = strip->getIndicies();
	indiceBufferDesc.NumElements = strip->getNrOfIndicies();
	indiceBufferDesc.Type = INDEX_BUFFER;
	indiceBufferDesc.Usage = BUFFER_DEFAULT;

	Buffer* IndexBuffer = new Buffer();

	if(FAILED(IndexBuffer->Init(this->Dx_Device, this->Dx_DeviceContext, indiceBufferDesc)))
		MaloW::Debug("Failed to init skybox");

	D3DX11_IMAGE_LOAD_INFO loadSMInfo;
	loadSMInfo.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;

	ID3D11Texture2D* SMTexture = 0;
	D3DX11CreateTextureFromFile(this->Dx_Device, texture.c_str(), 
		&loadSMInfo, 0, (ID3D11Resource**)&SMTexture, 0);


	D3D11_TEXTURE2D_DESC SMTextureDesc;
	SMTexture->GetDesc(&SMTextureDesc);

	D3D11_SHADER_RESOURCE_VIEW_DESC SMViewDesc;
	SMViewDesc.Format = SMTextureDesc.Format;
	SMViewDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURECUBE;
	SMViewDesc.TextureCube.MipLevels = SMTextureDesc.MipLevels;
	SMViewDesc.TextureCube.MostDetailedMip = 0;
	ID3D11ShaderResourceView* text;
	this->Dx_Device->CreateShaderResourceView(SMTexture, &SMViewDesc, &text);

	SMTexture->Release();

	Object3D* ro = new Object3D(VertexBuffer, IndexBuffer, text, sb->GetTopology());
	strip->SetRenderObject(ro);

	this->skybox = sb;
}
Example #14
0
bool Game::InitPipeline()
{
	InitFloor();
	InitCube();

	//Loading a mesh from the models folder
	if (!LoadObjModel(L"Media/Models/Dinosaur/Raptor.obj", &meshVertBuff, &meshIndexBuff, meshSubsetIndexStart, meshSubsetTexture, material, meshSubsets, true, false))
		return false;

	CreateSphere(10, 10);

	//Compile Shaders from shader file
	hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS_Buffer, 0, 0);
	hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS_Buffer, 0, 0);
	hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "SKYMAP_VS", "vs_5_0", 0, 0, 0, &SKYMAP_VS_Buffer, 0, 0);
	hr = D3DX11CompileFromFile(L"Effects.fx", 0, 0, "SKYMAP_PS", "ps_5_0", 0, 0, 0, &SKYMAP_PS_Buffer, 0, 0);

	//Create the Shader Objects
	hr = device->CreateVertexShader(VS_Buffer->GetBufferPointer(), //pointer to start of shaders buffer
		VS_Buffer->GetBufferSize(), //size of the buffer
		NULL, //pointer to class linkage interface
		&VS); //returned shader

	hr = device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

	hr = device->CreateVertexShader(SKYMAP_VS_Buffer->GetBufferPointer(), SKYMAP_VS_Buffer->GetBufferSize(), NULL, &SKYMAP_VS);
	hr = device->CreatePixelShader(SKYMAP_PS_Buffer->GetBufferPointer(), SKYMAP_PS_Buffer->GetBufferSize(), NULL, &SKYMAP_PS);

	D3D11_INPUT_ELEMENT_DESC layout[] =
	{
		//position is 3 floats, each float use 4 bytes leaving texcoord at an offset of 12
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	};

	//amount of elements in the input layout
	UINT numElements = ARRAYSIZE(layout);

	//Create the Input Layout
	device->CreateInputLayout(layout, //array of elements within vertex layout
		numElements, //number of elements in vertex layout
		VS_Buffer->GetBufferPointer(), //pointer to start of shader
		VS_Buffer->GetBufferSize(), //size of vertex shader
		&vertLayout); //pointer to input layout

	//Set the vertLayout to be the active input layout
	deviceContext->IASetInputLayout(vertLayout);

	//Set Primitive Topology, what kind of primitive is being set (disconnected triangles)
	deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	//Create the Viewport
	D3D11_VIEWPORT viewport;
	ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = WIDTH;
	viewport.Height = HEIGHT;
	viewport.MinDepth = 0.0f;
	viewport.MaxDepth = 1.0f;

	//Set the Viewport
	deviceContext->RSSetViewports(1, &viewport);

	//buffer to send to constant buffer in effect file
	D3D11_BUFFER_DESC cbbd;
	ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));

	cbbd.Usage = D3D11_USAGE_DEFAULT;
	cbbd.ByteWidth = sizeof(cbPerObject);
	cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cbbd.CPUAccessFlags = 0;
	cbbd.MiscFlags = 0;

	//creating constant buffer
	hr = device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);
	
	//defining camera position, target and up direction
	camPosition = XMVectorSet(0.0f, 5.0f,-8.0f,0.0f);
	camTarget = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
	camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

	//initializing camera position, target and up direction
	camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);

	//projecting the camera, parameters: FOV along Y axis, Aspect Ratio, Near clip planes, Far clip planes
	camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14f, (float)WIDTH / HEIGHT, 1.0f, 1000.0f);

	//loading a texture from file, shader resource view (&CubesTexture) holds texture
	hr = D3DX11CreateShaderResourceViewFromFile(device, L"Media/Textures/Wood.jpg",
		NULL, NULL, &CubesTexture, NULL);

	//loading floor texture
	hr = D3DX11CreateShaderResourceViewFromFile(device, L"Media/Textures/JungleFloor.jpg",
		NULL, NULL, &groundTexture, NULL);

	//loading the models texture
	hr = D3DX11CreateShaderResourceViewFromFile(device, L"Media/Models/Dinosaur/Texture/raptor.jpg",
		NULL, NULL, &meshTexture, NULL);

	D3DX11_IMAGE_LOAD_INFO loadSMInfo;
	loadSMInfo.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;

	//Load the texture
	ID3D11Texture2D* SMTexture = 0;
	hr = D3DX11CreateTextureFromFile(device, L"Media/Skyboxes/skymap.dds",
		&loadSMInfo, 0, (ID3D11Resource**)&SMTexture, 0);

	//Create the textures description
	D3D11_TEXTURE2D_DESC SMTextureDesc;
	SMTexture->GetDesc(&SMTextureDesc);

	//Tell D3D We have a cube texture, which is an array of 2D textures
	D3D11_SHADER_RESOURCE_VIEW_DESC SMViewDesc;
	SMViewDesc.Format = SMTextureDesc.Format;
	SMViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
	SMViewDesc.TextureCube.MipLevels = SMTextureDesc.MipLevels;
	SMViewDesc.TextureCube.MostDetailedMip = 0;

	//Create the Resource view
	hr = device->CreateShaderResourceView(SMTexture, &SMViewDesc, &smrv);
	
	ZeroMemory(&sampDesc, sizeof(sampDesc)); //clear memory
	sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; //filtering method in use
	sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; //what to do if the u value is >1 or <0
	sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; //what to do if the v value is >1 or <0
	sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; //what to do if the w value is >1 or <0
	sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; //compares sampled mipmap data with another mipmaps sampled data for this texture
	sampDesc.MinLOD = 0; //min detail of mipmap, 0 is max
	sampDesc.MaxLOD = D3D11_FLOAT32_MAX; //max detail of mipmap, 0 is max

	hr = device->CreateSamplerState(&sampDesc, &CubesTexSamplerState);
	hr = device->CreateSamplerState(&sampDesc, &groundSamplerState);
	hr = device->CreateSamplerState(&sampDesc, &meshSamplerState);

	D3D11_RASTERIZER_DESC cmdesc;

	ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));
	cmdesc.FillMode = D3D11_FILL_SOLID;
	cmdesc.CullMode = D3D11_CULL_BACK;
	cmdesc.FrontCounterClockwise = true;
	hr = device->CreateRasterizerState(&cmdesc, &CCWcullMode);

	cmdesc.FrontCounterClockwise = false;

	hr = device->CreateRasterizerState(&cmdesc, &CWcullMode);

	cmdesc.CullMode = D3D11_CULL_NONE;
	hr = device->CreateRasterizerState(&cmdesc, &RSCullNone);

	D3D11_DEPTH_STENCIL_DESC dssDesc;
	ZeroMemory(&dssDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
	dssDesc.DepthEnable = true;
	dssDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
	dssDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;

	device->CreateDepthStencilState(&dssDesc, &DSLessEqual);

	return true;
}