Archived

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

TGA Extraction

This topic is 6069 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 trying to extract cells out of an image. I''ve done this before in Direct Draw and just cant get it to work in OGL. I''m using Nehe''s Targa loader. For some reason its not working
  
int Scan_Targa(TextureImage *picture, Tile *tile, int x, int y, int width, int height)
{
	GLubyte	 *buffer = NULL;
	GLubyte	 *dest = NULL;
	int bytes_per_line;
	GLuint type = GL_RGBA;
	int bpp = picture->bpp/8;

	dest = (UCHAR *)malloc(width*height*bpp);
	buffer = (UCHAR *)malloc((picture->width*picture->height*bpp));

	x = x * width + 2;
	y = y * height + 2;
	buffer = (UCHAR *)(picture->buffer + y * picture->width + x * bpp);

	bytes_per_line = picture->width * bpp;
	for (int index = 0; index < 64; index++)
	{
		memcpy((UCHAR * )dest, (UCHAR * )picture->buffer, bytes_per_line ); 

		dest	+= width;
		picture->buffer  += picture->width;
	}
	
	glGenTextures(1, &tile->id);
	glBindTexture(GL_TEXTURE_2D, tile->id);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);	

	if(picture->bpp == 24)
		type = GL_RGB;

	gluBuild2DMipmaps(GL_TEXTURE_2D, type, width, height, type, GL_UNSIGNED_BYTE, dest);

	dest = NULL;
	buffer = NULL;
	return 1;
}
  
Yes I know very sloppy but what can you expect when someones been messing with it for awhile trying to get it to work

Share this post


Link to post
Share on other sites
At first I don''t know why you allocate memory pointed to by buffer and then three lines later fill this pointer with another address. You lose the pointer to allocated block of memory man! As I see these block of memory is unused at all so delete this malloc line. Now to address the correct byte offset of your image you have to use "buffer = (UCHAR *)(picture->buffer + y * bytes_per_line + x * bpp)" instead of "buffer = (UCHAR *)(picture->buffer + y * picture->width + x * bpp)". If I understand correctly your tiles are 64x64. So your for cycle may looks like this:
for (int index = 0; index < 64; index++)
{
memcpy((UCHAR * )dest, (UCHAR * )picture->buffer, bytes_per_line );
dest += bytes_per_line;
picture->buffer += picture->width * bpp;
}

And at the end when you create a texture and build mipmaps you can free the memory previously allocated. So except of setting dest to NULL you have to call free(dest);

Share this post


Link to post
Share on other sites
The reason I was doing that was because it was crashing without allocating them I know I shouldn''t have to and I thought it was weird that I had to go figure and yeah I know its sloppy but like I said I was getting desperate thanks

Share this post


Link to post
Share on other sites
Still doesnt work heres a cleaned up version
  

int Scan_Targa(TextureImage *picture, Tile *tile, int x, int y, int width, int height)
{
UCHAR *buffer;
int bytes_per_line;
GLuint type = GL_RGBA;
int bpp = picture->bpp/8;


x = x * width + 2;
y = y * height + 2;

buffer = (UCHAR *)(picture->buffer + y * picture->width + x * bpp);
bytes_per_line = width * bpp;
tile->buffer = (GLubyte *)malloc(width*height*bpp);
for (int index = 0; index < 64; index++)
{
memcpy((UCHAR * )tile->buffer, (UCHAR * )buffer, bytes_per_line );
tile->buffer += bytes_per_line;
picture->buffer += picture->width * bpp;
}

glGenTextures(1, &tile->id);
glBindTexture(GL_TEXTURE_2D, tile->id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

if(picture->bpp == 24)
type = GL_RGB;

gluBuild2DMipmaps(GL_TEXTURE_2D, type, width, height, type, GL_UNSIGNED_BYTE, tile->buffer);
return 1;
}

If I put malloc(height * bytes * bytes_per_line) it doesnt crash but my texture isnt there when I bind it and if I do it like I have it now it crashes glu becuase of the build2dmipmaps so I have no clue whats up

Share this post


Link to post
Share on other sites