Thread: gluBuild2DMipmaps

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    gluBuild2DMipmaps

    I'm using Nehe's code for loading a bitmap in opengl, and it says that you should be able to use any size bitmap when building mipmaps, but that seems to not be the case. My bitmap appears distorted when its width and/or height are not powers of two.

    Thinking Nehe might have been wrong, I looked on MSDN and it says:
    The gluBuild2DMipmaps function obtains the input image and generates all mipmap images (using gluScaleImage) so the input image can be used as a mipmapped texture image. To load each of the images, call glTexImage2D. If the dimensions of the input image are not powers of two, then the image is scaled so that both the width and height are powers of two before the mipmaps are generated.
    I doubt that both Nehe and MSDN could be wrong, but I don't know why it isn't working for me...

    Here's my function:
    Code:
    int LoadGLTexture(char* szTexName, GLuint& tex)								// Load Bitmaps And Convert To Textures
    {
    	int Status=FALSE;							// Status Indicator
    
    	AUX_RGBImageRec *TextureImage;					// Create Storage Space For The Texture
    
    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (TextureImage=LoadBMP(szTexName))
    	{
    		Status=TRUE;							// Set The Status To TRUE
    		glGenTextures(1, &tex);					// Create Three Textures
    
    		glBindTexture(GL_TEXTURE_2D, tex);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
    		if(gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage->sizeX, TextureImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data)) // ( NEW )
    	    Status=FALSE;
      }
    	if (TextureImage)							// If Texture Exists
    	{
    		if (TextureImage->data)					// If Texture Image Exists
    		{
    			free(TextureImage->data);				// Free The Texture Image Memory
    		}
    
    		free(TextureImage);						// Free The Image Structure
    	}
    return Status;								// Return The Status
    }
    I modified it a bit from Nehe's code, and I even added error checking for gluBuild2DMipmaps, so I know that it isn't failing. Maybe I did something stupid here?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Banned
    Join Date
    May 2004
    Posts
    129
    Well for one, are you totally sure it isn't working? Are textures not showing up at all when you attempt this, or does it just appear that the textures are not being swapped? If it appears that the textures are not being swapped then it might be because of your minification/magnification settings you pass in to glubuild2dmipmaps.

    The only real 'discrepancy' I see is that in the first glTexparameteri call for texture magnification you use gl_linear, and for the minification you use gl_linear_mipmap_nearest. I use gl_linear_mipmap_nearest for both. If I don't use that, I use gl_linear_mipmap_linear. Otherwise, the way I load BMPs is exactly the same as you (in this case type is gl_rgb, but it can also be gl_bgr or gl_bgra).

    Here is the code I use for generating video textures.
    Code:
    UINT	GenerateInternalTexture(byte * data, int channels, int width, int height, GLenum type)
    {
    	UINT	texture;
    	glGenTextures(1, &texture);
    	glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    	glBindTexture(GL_TEXTURE_2D, texture);
    
    	gluBuild2DMipmaps(GL_TEXTURE_2D, channels,width,height, type, GL_UNSIGNED_BYTE, data);
    
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); //GL_LINEAR_MIPMAP_NEAREST is faster than GL_LINEAR_MIPMAP_LINEAR
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
    	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    	return texture;
    }
    EDIT:
    I suggest copying my code first, see if that works, so you know it isn't a loading problem. Then, I suggest you try using the same settings I use, but also try setting the minification settings before the magnification settings (the order in which you do it might affect the outcome).

    Good luck
    Last edited by vNvNation; 06-04-2004 at 01:41 PM.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The texture is showing up when I run the program, but it appears in different colors and is sort of slanted so I'm fairly certain it isn't being sized correctly.

    I've also tried most of the different values for the last parameter of the function, to no avail.

    I'll try your code and post back here.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ahh I was missing:
    Code:
     glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    And that seems to make the difference. I don't think that was in Nehe's code, although I could be mistaken.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    just to note, you should try and keep all your textures powers of 2 anyway, becuase video cards are happier with them. OpenGL may even add pixels to get to the next power of 2 dimension.

  6. #6
    Banned
    Join Date
    May 2004
    Posts
    129
    Do you know why that is exactly? The reason I ask is that many jpegs that I use as textures are not in powers of 2.

    And just as a side note, I'm pretty sure that the function you were missing (JaWiB) just specifys how OpenGL stores the textures in memory.
    Last edited by vNvNation; 06-04-2004 at 06:38 PM.

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    The reason why they like textures that are dim'ed to the power of two is because they can be accelerated. Since the computer keeps data in binary, it makes everything easier and faster to manipulate.

    Using jpegs for games is a very bad idea. Despite popular belief, they do not save RAM. ROM yes, RAM no...and RAM is what a game developer should be worried about. It will also take up processor resources if you load and unload the texture frequently.

  8. #8
    Banned
    Join Date
    May 2004
    Posts
    129
    I can see what you mean when talking about how they can be accelerated (I guess it basically just means slightly faster memory access), but I don't know what you mean about the second part. All textures are the same once they are read into memory and turned into a texture, subsequently if you looked at texture data on a video card, you would have very few clues as to whether it 'used to be' a jpeg, bitmap, targa, etc. With that in mind, jpegs take up less space on your drive, and I have not seen any color information loss for basic polygon rasterization using jpegs (Quake3 uses jpegs for many of its textures).

  9. #9
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    You are correct that textures are all the same once they are in memory, assuming you're not holding header data. But there is extra work that needs to be done to put it in there. If you load and unload textures while the game is running (which is a bad idea in itself, but sometimes it's necessary) then you're hogging up processor time loading the jpeg. It's not a matter of color quality, but more a matter practicality.

    Like you said, jpegs have been used, but no real benefit was gained by using them; you won't see them used anymore. If you're worried about HDD space, then you can do other things to fix that beside using jpegs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gluBuild2DMipmaps & access violation
    By glUser3f in forum Game Programming
    Replies: 2
    Last Post: 10-03-2003, 02:31 PM