Archived

This topic is now archived and is closed to further replies.

outRider

Texture corruption, improper blending

Recommended Posts

I have two problems when creating texture interfaces myself, as opposed to using D3DXCreateTextureFromFile(): I have two textures, one stretched across a heightmap, another tiled across it. The textures are modulated together. First, everything appears far too dark after the modulation. Second, the texture edges display some wierd corruption. Even when not multitexturing the corruption appears as red around the terrain color texture. D3DX...() seems to do everything correctly (as evidenced by the first 5 images below), so I''m not sure where I''m going wrong. Here is a link to some images, scroll to the bottom to see the problem. The color texture is 24bit, the detail texture is 8bit grayscale. The color texture is copied onto a 4 byte aligned surface before copying it to the locked texture. The detail texture is just copied straight, but I create it as format L8, since that''s what seems reasonable to me.
  
	if (Tga.Load("D:\\terrain.tga") != CTGA::Successful)
	{
		char	dbgString[255];
		sprintf(dbgString, "File %s - Line %d - Could not load D:\\terrain.tga.", __FILE__, __LINE__ - 3);
		OutputDebugString(dbgString);
		DebugBreak();
	}

	IDirect3DTexture8		*pTerrainTexture = NULL;

	switch (Tga.getBitsPerPixel())
	{
	case 8:
		{
			pD3DDevice->CreateTexture(Tga.getWidth(), Tga.getHeight(), 0, D3DUSAGE_BORDERSOURCE_TEXTURE, D3DFMT_L8, NULL, &pTerrainTexture);
			break;
		}
	case 24:
		{
			pD3DDevice->CreateTexture(Tga.getWidth(), Tga.getHeight(), 0, D3DUSAGE_BORDERSOURCE_TEXTURE, D3DFMT_X8R8G8B8, NULL, &pTerrainTexture);
			break;
		}
	case 32:
		{
			pD3DDevice->CreateTexture(Tga.getWidth(), Tga.getHeight(), 0, D3DUSAGE_BORDERSOURCE_TEXTURE, D3DFMT_A8R8G8B8, NULL, &pTerrainTexture);
			break;
		}
	default:
		{
			char	dbgString[255];
			sprintf(dbgString, "File %s - Line %d - Texture bit depth is unsupported.", __FILE__, __LINE__ - 3);
			OutputDebugString(dbgString);
			DebugBreak();
		}
	}

	D3DLOCKED_RECT		Rect;

	memset(&Rect, 0, sizeof(D3DLOCKED_RECT));

	pTerrainTexture->LockRect(0, &Rect, NULL, 0);

	// If the texture image isn''t 1, 2, or 4 byte aligned we must transfer it to a buffer that is before we can proceed


	if (Tga.getBitsPerPixel() == 24)
	{
		unsigned char		*pTextureData = new unsigned char[Tga.getWidth() * 4 * Tga.getHeight() * 4];
		const unsigned char	*pTgaData = Tga.getImageData();

		for (unsigned int y = 0; y < Tga.getHeight(); ++y)
		{
			for (unsigned int x = 0; x < Tga.getWidth(); ++x)
			{
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4)]		= pTgaData[(y * (Tga.getWidth() * (Tga.getBitsPerPixel() >> 3))) + (x * (Tga.getBitsPerPixel() >> 3))];
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4) + 1]	= pTgaData[(y * (Tga.getWidth() * (Tga.getBitsPerPixel() >> 3))) + (x * (Tga.getBitsPerPixel() >> 3)) + 1];
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4) + 2]	= pTgaData[(y * (Tga.getWidth() * (Tga.getBitsPerPixel() >> 3))) + (x * (Tga.getBitsPerPixel() >> 3)) + 2];
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4) + 3]	= 0;
			}
		}

		// Transfer from pTextureData to Rect.pBits goes here


		delete [] pTextureData;
	}
	else
		// Transfer from Tga to Rect.pbits goes here


	pTerrainTexture->UnlockRect(0);

	Tga.Unload();

	if (Tga.Load("D:\\detail.tga") != CTGA::Successful)
	{
		char	dbgString[255];
		sprintf(dbgString, "File %s - Line %d - Could not load D:\\terrain.tga.", __FILE__, __LINE__ - 3);
		OutputDebugString(dbgString);
		DebugBreak();
	}

	IDirect3DTexture8		*pDetailTexture = NULL;

	switch (Tga.getBitsPerPixel())
	{
	case 8:
		{
			pD3DDevice->CreateTexture(Tga.getWidth(), Tga.getHeight(), 0, D3DUSAGE_BORDERSOURCE_TEXTURE, D3DFMT_L8, NULL, &pDetailTexture);
			break;
		}
	case 24:
		{
			pD3DDevice->CreateTexture(Tga.getWidth(), Tga.getHeight(), 0, D3DUSAGE_BORDERSOURCE_TEXTURE, D3DFMT_X8R8G8B8, NULL, &pDetailTexture);
			break;
		}
	case 32:
		{
			pD3DDevice->CreateTexture(Tga.getWidth(), Tga.getHeight(), 0, D3DUSAGE_BORDERSOURCE_TEXTURE, D3DFMT_A8R8G8B8, NULL, &pDetailTexture);
			break;
		}
	default:
		{
			char	dbgString[255];
			sprintf(dbgString, "File %s - Line %d - Texture bit depth is unsupported.", __FILE__, __LINE__ - 3);
			OutputDebugString(dbgString);
			DebugBreak();
		}
	}

	memset(&Rect, 0, sizeof(D3DLOCKED_RECT));

	pDetailTexture->LockRect(0, &Rect, NULL, 0);

	// If the texture image isn''t 1, 2, or 4 byte aligned we must transfer it to a buffer that is before we can proceed


	if (Tga.getBitsPerPixel() == 24)
	{
		unsigned char		*pTextureData = new unsigned char[Tga.getWidth() * 4 * Tga.getHeight() * 4];
		const unsigned char	*pTgaData = Tga.getImageData();

		for (unsigned int y = 0; y < Tga.getHeight(); ++y)
		{
			for (unsigned int x = 0; x < Tga.getWidth(); ++x)
			{
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4)]		= pTgaData[(y * (Tga.getWidth() * (Tga.getBitsPerPixel() >> 3))) + (x * (Tga.getBitsPerPixel() >> 3))];
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4) + 1]	= pTgaData[(y * (Tga.getWidth() * (Tga.getBitsPerPixel() >> 3))) + (x * (Tga.getBitsPerPixel() >> 3)) + 1];
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4) + 2]	= pTgaData[(y * (Tga.getWidth() * (Tga.getBitsPerPixel() >> 3))) + (x * (Tga.getBitsPerPixel() >> 3)) + 2];
				pTextureData[(y * (Tga.getWidth() * 4)) + (x * 4) + 3]	= 0;
			}
		}

		// transfer from pTextureData to Rect.pbits goes here


		delete [] pTextureData;
	}
	else
		// // transfer from Tga to Rect.pbits goes here


	pDetailTexture->UnlockRect(0);

	Tga.Unload();
  
Any help would be appreciated. For the darkness issue something tells me I shouldn''t be creating the detail texture as L8. The corruption I have no idea about. ------------ - outRider -

Share this post


Link to post
Share on other sites