TGA Extraction

Started by
3 comments, last by Derilect101 22 years, 11 months ago
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
Advertisement
Please someone help or just give some general information
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);

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
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

This topic is closed to new replies.

Advertisement