예제 #1
0
	void Texture::LoadFromDisk(const std::wstring& a_Path)
	{
		size_t lastDot = a_Path.rfind(L".");
		if (lastDot == std::string::npos) 
		{
			throw std::runtime_error("Panther Texture ERROR: File path is missing '.tga' extension.");
		}

		std::wstring extension = a_Path.substr(lastDot);
		if (extension == L".tga")
			LoadTarga(a_Path);
	}
예제 #2
0
bool TextureClass::Initialize(OpenGLClass* OpenGL, char* filename, unsigned int textureUnit, bool wrap)
{
	bool result;


	// Load the targa file.
	result = LoadTarga(OpenGL, filename, textureUnit, wrap);
	if(!result)
	{
		return false;
	}

	return true;
}
예제 #3
0
void FTexture::Load(unsigned char* _resourceData)
{
	bool result;

	// Load the targa image
	result = LoadTarga(_resourceData, 0); // _textureUnit ?
	if (!result)
	{
		// ASSERT
		return;
	}

	return;
}
예제 #4
0
bool TextureClass::Initialize(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* filename)
{
	bool result;
	int height, width;
	D3D11_TEXTURE2D_DESC textureDesc;
	HRESULT hResult;
	unsigned int rowPitch;
	D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;


	// Load the targa image data into memory.
	result = LoadTarga(filename, height, width);
	if (!result)
	{
		return false;
	}

	// Setup the description of the texture.
	textureDesc.Height = height;
	textureDesc.Width = width;
	textureDesc.MipLevels = 0;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_DEFAULT;
	textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	textureDesc.CPUAccessFlags = 0;
	textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

	// Create the empty texture.
	hResult = device->CreateTexture2D(&textureDesc, NULL, &m_texture);
	if (FAILED(hResult))
	{
		return false;
	}

	// Set the row pitch of the targa image data.
	rowPitch = (width * 4) * sizeof(unsigned char);

	// Copy the targa image data into the texture.
	deviceContext->UpdateSubresource(m_texture, 0, NULL, m_targaData, rowPitch, 0);

	// Setup the shader resource view description.
	srvDesc.Format = textureDesc.Format;
	srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	srvDesc.Texture2D.MostDetailedMip = 0;
	srvDesc.Texture2D.MipLevels = -1;

	// Create the shader resource view for the texture.
	hResult = device->CreateShaderResourceView(m_texture, &srvDesc, &m_textureView);
	if (FAILED(hResult))
	{
		return false;
	}

	// Generate mipmaps for this texture.
	deviceContext->GenerateMips(m_textureView);

	// Release the targa image data now that the image data has been loaded into the texture.
	delete[] m_targaData;
	m_targaData = 0;

	return true;
}
bool TextureObject::Initialize(ID3D11Device * device, ID3D11DeviceContext * deviceContext, char * fileName)
{
	bool result = false;
	int width = 0, height = 0;
	HRESULT hResult;
	unsigned int rowPitch = 0;
	D3D11_TEXTURE2D_DESC textureDesc;
	D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;

	//Load the image!!!!
	result = LoadTarga(fileName, height, width);
	if (!result)
	{
		return false;
	}

	textureDesc.Height = height;
	textureDesc.Width = width;
	textureDesc.MipLevels = 0;
	textureDesc.ArraySize = 1;
	textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	textureDesc.SampleDesc.Count = 1;
	textureDesc.SampleDesc.Quality = 0;
	textureDesc.Usage = D3D11_USAGE_DEFAULT;
	textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
	textureDesc.CPUAccessFlags = 0;
	textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

	//Create the empty picture and update it with the data. We could have initiated it with the data but this works.
	hResult = device->CreateTexture2D(&textureDesc, NULL, &m_texture);
	if (FAILED(hResult))
	{
		return false;
	}

	//Set the row pitch of the targa image data.
	rowPitch = (width * 4) * sizeof(unsigned char);
	//Copy the targa image data into the texture.
	deviceContext->UpdateSubresource(this->m_texture, 0, NULL, m_targaData, rowPitch, 0);

	//Setup the shader resource view description
	srvDesc.Format = textureDesc.Format;
	srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
	srvDesc.Texture2D.MostDetailedMip = 0;
	srvDesc.Texture2D.MipLevels = -1;

	//Create the shader resource biew for the texture
	hResult = device->CreateShaderResourceView(this->m_texture, &srvDesc, &this->m_textureView);
	if (FAILED(hResult))
	{
		return false;
	}

	//Generate mipmaps for this texture. For higher quality we may want to load our own MipMap levels manually.
	deviceContext->GenerateMips(m_textureView);
	
	//Release the targa image data now that the image data has been loaded into the texture
	delete[] this->m_targaData;
	this->m_targaData = NULL;

	return true;
}
예제 #6
0
bool BitmapFontClass::InitializeTextureView(ID3D11Device* device, ID3D11DeviceContext* deviceContext, char* textureFilename)
{
	HRESULT result;

	// Texture View
	D3D11_TEXTURE2D_DESC texture2DDesc;
	D3D11_SHADER_RESOURCE_VIEW_DESC shaderDesc;

	int fileNameLength = strlen(textureFilename);
	string fileExtenstion;
	string strFileName(textureFilename);

	for (int i = fileNameLength - 3; i < fileNameLength; i++)
	{
		fileExtenstion += strFileName.at(i);
	}

	if (fileExtenstion != "tga" && fileExtenstion != "dds")
	{
		HRESULT hResult;
		textureFilename = "Resource Files/font_bitmap.png";
		std::basic_ifstream<unsigned char> file(textureFilename, std::ios::binary);

		if (file.is_open())
		{
			file.seekg(0, ios::end);
			int length = (int)file.tellg();
			file.seekg(0, ios::beg);

			unsigned char* buffer = new unsigned char[length];
			file.read(&buffer[0], length);
			file.close();

			hResult = CreateWICTextureFromMemory(device, deviceContext, &buffer[0], (size_t)length, NULL, &m_textureView, NULL);
			if (FAILED(hResult))
			{
				MessageBox(0, L"Failed to create texture from memory!", L"Compile Error", MB_OK);
				return false;
			}
		}
		else
		{
			MessageBox(0, L"Failed to open texture file!", L"Compile Error", MB_OK);
			return false;
		}
	}
	else if (fileExtenstion == "dds")
	{
		HRESULT hResult;
		WCHAR* tempFilePath;
		int nCharsVertex = MultiByteToWideChar(CP_ACP, 0, textureFilename, -1, NULL, 0);
		tempFilePath = new WCHAR[nCharsVertex];
		MultiByteToWideChar(CP_ACP, 0, textureFilename, -1, tempFilePath, nCharsVertex);

		hResult = CreateWICTextureFromFile(device, tempFilePath, NULL, &m_textureView, NULL);
		if (FAILED(hResult))
		{
			MessageBox(0, L"Failed to load the texture image!", L"Compile Error", MB_OK);
			delete tempFilePath;
			return false;
		}
		delete tempFilePath;
	}
	else
	{
		int height, width;
		unsigned int rowPitch;

		// Load the targa image data into memory.
		result = LoadTarga(textureFilename, height, width);
		if (FAILED(result))
		{
			if (m_targaData)
			{
				delete[] m_targaData;
				m_targaData = NULL;
			}
			MessageBox(0, L"Failed to load Targa image data into memory!", L"Compile Error", MB_OK);
			return false;
		}

		// Setup the description of the texture.
		texture2DDesc.Height = height;
		texture2DDesc.Width = width;
		texture2DDesc.MipLevels = 0;
		texture2DDesc.ArraySize = 1;
		texture2DDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		texture2DDesc.SampleDesc.Count = 1;
		texture2DDesc.SampleDesc.Quality = 0;
		texture2DDesc.Usage = D3D11_USAGE_DEFAULT;
		texture2DDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
		texture2DDesc.CPUAccessFlags = 0;
		texture2DDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;

		// Create the empty texture.
		result = device->CreateTexture2D(&texture2DDesc, NULL, &m_texture);
		if (FAILED(result))
		{
			delete[] m_targaData;
			m_targaData = NULL;
			MessageBox(0, L"Failed to create empty Targa texture!", L"Compile Error", MB_OK);
			return false;
		}

		// Set the row pitch of the targa image data.
		rowPitch = (width * 4) * sizeof(unsigned char);

		// Copy the targa image data into the texture.
		deviceContext->UpdateSubresource(m_texture, 0, NULL, m_targaData, rowPitch, 0);

		// Setup the shader resource view description.
		shaderDesc.Format = texture2DDesc.Format;
		shaderDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
		shaderDesc.Texture2D.MostDetailedMip = 0;
		shaderDesc.Texture2D.MipLevels = -1;

		// Create the shader resource view for the texture.
		result = device->CreateShaderResourceView(m_texture, &shaderDesc, &m_textureView);
		if (FAILED(result))
		{
			delete[] m_targaData;
			m_targaData = NULL;
			MessageBox(0, L"Failed to create shader resource view for Targa texture", L"Compile Error", MB_OK);
			return false;
		}
	}
	deviceContext->GenerateMips(m_textureView);

	if (m_targaData)
	{
		delete[] m_targaData;
		m_targaData = NULL;
	}

	// Sample State
	D3D11_SAMPLER_DESC sampleStateDesc;
	ZeroMemory(&sampleStateDesc, sizeof(sampleStateDesc));
	sampleStateDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sampleStateDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sampleStateDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sampleStateDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
	sampleStateDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	sampleStateDesc.MaxLOD = D3D11_FLOAT32_MAX;

	result = device->CreateSamplerState(&sampleStateDesc, &m_samplerState);
	if (FAILED(result))
	{
		MessageBox(0, L"Failed to create sampler state!", L"Compile Error", MB_OK);
		return false;
	}

	return true;
}