How to create a texture 2d from a dds file in c++

Hi,

I’m working with DDS files to charge textures, but when I create the texture, the texture show like broken texture.

How is the correct form to create a texture 2d from a dds file?

Thank you.

Hi,
Can you show us the code that you used to load it, and a snapshot of what happens?

Hi, this is the code:
std::unique_ptrm_IgImage =Load(FileName);

EPixelFormat Format;

switch (m_IgImage->GetType())
{
case IgImage::Type::DXT1:
	Format = PF_DXT1;
	break;
case IgImage::Type::DXT3:
	Format = PF_DXT3;
	break;
case IgImage::Type::DXT5:
	Format = PF_DXT5;
	break;
default:
	break;
}
int w = m_IgImage->GetSizeX();
int h = m_IgImage->GetSizeY();
UTexture2D*Texture= UTexture2D::CreateTransient(w, h, Format);
TUniquePtr<FTexturePlatformData>TexturePlatformData = MakeUnique<FTexturePlatformData>();
TexturePlatformData->SizeX = m_IgImage->GetSizeX();
TexturePlatformData->SizeY = m_IgImage->GetSizeY();
TexturePlatformData->PixelFormat = Format;
TexturePlatformData->SetNumSlices(1);
for (int i=0;i<int(m_IgImage->GetNumMipmaps());i++)
{
	FTexture2DMipMap* Level = new FTexture2DMipMap();
	TexturePlatformData->Mips.Add(Level);
	
	Level->SizeX = w;
	Level->SizeY = h;
	Level->BulkData.Lock(LOCK_READ_WRITE);
	
	
	void* MipMapData = Level->BulkData.Realloc(m_IgImage->GetMipmapByteSize(i));
	//FMemory::Memcpy(MipMapData, m_IgImage->GetDataPtr(0), m_IgImage->GetMipmapByteSize(i));
	memcpy_s(MipMapData, m_IgImage->GetMipmapByteSize(i), m_IgImage->GetDataPtr(0), m_IgImage->GetMipmapByteSize(i));
	w >>= 1;
	if (w == 0)
	{
		w = 1;
	}
	h >>= 1;
	if (h == 0)
	{
		h = 1;
	}
	Level->BulkData.Unlock();
}

	
Texture->PlatformData = TexturePlatformData.Release();
Texture->AddToRoot();
Texture->UpdateResource();
return Texture;

When I returned the Texture, this texture is empty of data.

The IgImage, have a vector of unsigned char array, and this is where I saved the data.

I’m not sure what a std::unique_ptrm_IgImage is, google couldn’t find any reference to it and it’s not in the UE source base.

If you set a break point before or after the memcpy - does the MipMapData look valid when viewed from a memory watch? and the destination memory?

The problem is when I returned the Texture2D and Create a Material, this material create with the default texture.

We might need to see that bit of code if that’s where the problem is :slight_smile:

Here’s how I do it: (If you’re wanting it to run in a packaged project this won’t work - only editor)

	UMaterialInstanceConstant* mi=(UMaterialInstanceConstant*)miFactory->FactoryCreateNew(UMaterialInstanceConstant::StaticClass(),Package,*MaterialBaseName,RF_Standalone|RF_Public,NULL,GWarn);
	
	if(texD) mi->SetTextureParameterValueEditorOnly(FMaterialParameterInfo("Diffuse"),texD);
	if(texN) mi->SetTextureParameterValueEditorOnly(FMaterialParameterInfo("Normal"),texN);
	if(texO) mi->SetTextureParameterValueEditorOnly(FMaterialParameterInfo("Opacity"),texO);
	if(texMSR) mi->SetTextureParameterValueEditorOnly(FMaterialParameterInfo("MSR"),texMSR);

Im also trying to import a dds file, Did you every figure this out?