Skip to main content
gamedev.net
🔒 Locked

glut: Textures

Started by revanthedarth Jan 6, 2007 at 7:08 AM 1 replies 4.5k views
Original Post
revanthedarth
revanthedarth
I'm having a texture problem. I checked out some tutorials about glut (such as http://www.morrowland.com/ ) and learnt how to use textures. In its tutorials, the author uses this function to import JPG files: ( jpeg.lib and jpeglib.h is included, you can download this file, which includes jpeg.lib and jpeglib.h : http://www.morrowland.com/apron/tutorials/gl/gl_texture.zip ) LoadJPG function:

tImage *LoadJPG(const char *strFileName){
	struct jpeg_decompress_struct cinfo;
	tImage *pImageData = NULL;
	FILE *pFile;	
	if((pFile = fopen(strFileName, "rb")) == NULL) 
		return NULL;	
	jpeg_error_mgr jerr;	
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);
	jpeg_stdio_src(&cinfo, pFile);
	pImageData = (tImage*)malloc(sizeof(tImage));
	jpeg_read_header(&cinfo, TRUE);
	jpeg_start_decompress(&cinfo);
	pImageData->channels = cinfo.num_components;
	pImageData->sizeX    = cinfo.image_width;
	pImageData->sizeY    = cinfo.image_height;
	int rowSpan = cinfo.image_width * cinfo.num_components;
	pImageData->data = ((unsigned char*)malloc(sizeof(unsigned char)*rowSpan*pImageData->sizeY));
	unsigned char** rowPtr = new unsigned char*[pImageData->sizeY];
	for (int i = 0; i < pImageData->sizeY; i++)
		rowPtr = &(pImageData->data[i * rowSpan]);
	int rowsRead = cinfo.output_height-1;
	while (cinfo.output_scanline < cinfo.output_height) 
	{ rowsRead -= jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead); }
delete [] rowPtr;
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);
	fclose(pFile);
	return pImageData;
}
and the function i use to load textures:

bool Load_Texture(UINT &texture, LPSTR strFileName)
{	if(!strFileName)  return false;
	tImage *pImage = NULL;
	pImage = LoadJPG(strFileName);
	if(pImage == NULL)	return false;
	glGenTextures(1, &texture);
	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
	glBindTexture(GL_TEXTURE_2D, texture);
	int textureType = GL_RGB;
	if(pImage->channels == 4)
		textureType = GL_RGBA;
	gluBuild2DMipmaps(GL_TEXTURE_2D, pImage->channels, pImage->sizeX, 
		pImage->sizeY, textureType, GL_UNSIGNED_BYTE, pImage->data);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	if (pImage){
		if (pImage->data)	free(pImage->data);		
		free(pImage);
	}
	return true;
}
With the texture file included, it works fine (resolution of that file is 300x300) But when i try to use a texture file, it works like that: http://img225.imageshack.us/img225/9279/textureps2.jpg (the cube behind is the texture file included, the texture on the front is my texture (downloaded from NeHe, about blending). You see that my texture (128x128) has vertical lines on it. I made its resolution 300x300 and the problem wasn't solved. How can i solve this problem?
zedzeek
zedzeek
* jpgs aint good for 3d graphics
* youre specifying mipmaps but using GL_LINEAR, u need to use eg GL_LINEAR_MIPMAP_LINEAR
* try to use a power of 2 sized texture eg 256x256 instead of 300x300, gluBuild2DMipmaps resizes that image to 256x256 with a crap filter (better to do it yourself with a paint program)
* perhaps the problem is with the pixelunpack being set to 4 as default try 1
Krohm
Krohm
Quote:
Original post by zedzeek
* jpgs aint good for 3d graphics

Just plain out false. Lots of games are using them.

As for the problem, did you try unpacking 1 line per loop? It seems it could be a problem with block decoding. Try to init the image data to a known pattern and check if the line is overwritten.
Also, try to get a dump of how much lines per loop are you decoding.

I have an idea about it but I'm not sure.

Last note: for easier reading, I suggest to change the pointer initialization to
for (int i = 0; i < pImageData->sizeY; i++)    rowPtr = pImageData->data + i * rowSpan; // theres a plus sign here                                                 // but it does not display

It's a matter of taste.



Previously "Krohm"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.