Jump to content
  • Advertisement
Sign in to follow this  
sirlemonhead

Writing TGA data for D3DX texture function

This topic is 3800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the middle of writing code to create a TGA file in memory for passing into the D3DXCreateTextureFromFileInMemory() function for creating a texture. i'm doing it as so:
LPDIRECT3DTEXTURE9 CreateD3DTexture(AVPTexture *tex, unsigned char *buf) 
{
	LPDIRECT3DTEXTURE9 destTexture = NULL;

	/* create and fill tga header */
	TGA_HEADER *TgaHeader = new TGA_HEADER;
	TgaHeader->idlength = 0;
	TgaHeader->x_origin = tex->width;
	TgaHeader->y_origin = tex->height;
	TgaHeader->colourmapdepth = 0;
	TgaHeader->colourmaplength = 0;
	TgaHeader->colourmaporigin = 0;
	TgaHeader->colourmaptype = 0;
	TgaHeader->datatypecode = 2;			// RGB
	TgaHeader->bitsperpixel = 32;
	TgaHeader->imagedescriptor = 0x20;		// set origin to top left
	TgaHeader->height = tex->height;
	TgaHeader->width = tex->width;

	/* size of raw image data */
	int imageSize = tex->height * tex->width * 4;

	/* create new buffer for header and image data */
	byte *buffer = new byte[sizeof(TGA_HEADER) + imageSize];

	/* copy header and image data to buffer */
	memcpy(buffer, TgaHeader, sizeof(TGA_HEADER));
	memcpy(buffer + sizeof(TGA_HEADER), buf, (tex->height * tex->width * 4));
	/* create direct3d texture */
	if(FAILED(D3DXCreateTextureFromFileInMemory(d3d.lpD3DDevice,
		buffer,
		sizeof(TGA_HEADER) + imageSize,
		&destTexture)))
	{
		OutputDebugString("\n no, din't work");	
}

	delete TgaHeader;
	delete[] buffer;
	return destTexture;

This works a charm (images pixels are always 4 bytes big), only my image data is RGBA format which D3D thinks it should rearrange *from* BGRA which TGA uses. Since the RGB data gets reversed it actually ends up as BGR in the d3d texture, which is obviously wrong. So I want to convert the image data from RGBA to BGRA, but i'm having trouble. I figure i'll start off with getting the loop for pixel swapping to just directly copy the image data as is. so I change my code to this:
	/* size of raw image data */
	int imageSize = tex->height * tex->width * 4;

	/* create new buffer for header and image data */
	byte *buffer = new byte[sizeof(TGA_HEADER) + imageSize];

	/* copy header and image data to buffer */
	memcpy(buffer, TgaHeader, sizeof(TGA_HEADER));
//	memcpy(buffer + sizeof(TGA_HEADER), buf, (tex->height * tex->width * 4));
	buffer += sizeof(TGA_HEADER);

	for (int i = 0; i < imageSize; i+=4)
	{
		buffer = buf;
	}

	/* create direct3d texture */
	if(FAILED(D3DXCreateTextureFromFileInMemory(d3d.lpD3DDevice,
		buffer,
		sizeof(TGA_HEADER) + imageSize,
		&destTexture)))
	{
		OutputDebugString("\n no, din't work");
	}

	delete TgaHeader;
	delete[] buffer;
	return destTexture;

which causes the D3DX function to fail, leaving me with white coloured walls. I'm guess this line is the culprit? buffer += sizeof(TGA_HEADER); Is this the correct way to increment the buffer pointer so I don't overwrite the previously added TGA Header? should I use a different data type for buffer other than byte? Is the problem on some other line completely? Would I be better off using a BMP file format?

Share this post


Link to post
Share on other sites
Advertisement
You changed the location buffer was pointing at so it no longer points at your tga header. All D3DXCreateTextureFromFileInMemory is getting is a pointer to your image data and not the TGA file data.

Try something more along the lines of:

/* size of raw image data */
int imageSize = tex->height * tex->width * 4;

/* create new buffer for header and image data */
byte *buffer = new byte[sizeof(TGA_HEADER) + imageSize];

/* copy header and image data to buffer */
memcpy(buffer, TgaHeader, sizeof(TGA_HEADER));

byte * imagedata = buffer + sizeof(TGA_HEADER);
for (int i = 0; i < imageSize; i+=4)
{
imagedata = buf;
}

/* create direct3d texture */
if(FAILED(D3DXCreateTextureFromFileInMemory(d3d.lpD3DDevice,
buffer,
sizeof(TGA_HEADER) + imageSize,
&destTexture)))
{
OutputDebugString("\n no, din't work");
}

delete TgaHeader;
delete[] buffer;
return destTexture;


Share this post


Link to post
Share on other sites
That got it, thanks :)

Which line was the problem?

buffer += sizeof(TGA_HEADER);

or first run of the loop?

for (int i = 0; i < imageSize; i+=4)
{
buffer = buf;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!