#### Archived

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

# Only textures created as mipmaps are shown??

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

## Recommended Posts

Im having a very weird problem: All textures that are created with glTexImage2D are not shown at all(just the empty quad im trying to texture). *But* if i create the texture with gluBuild2DMipmaps it works just fine! This is the code that loads the texture: (legend: textures is a hash_map with keys being filenames and the value being a Texture object, which contains the texture id and its width and height.
	SDL_Surface* temp_surface = NULL;
Texture new_texture;
textures[filename] = new_texture;
Texture* tex = &textures[filename];
if(NULL == temp_surface) exit(-5);
glGenTextures(1, &(tex->id));
glBindTexture(GL_TEXTURE_2D,tex->id);
tex->height = temp_surface->h;
tex->width = temp_surface->w;

/*glTexImage2D(GL_TEXTURE_2D,0,3,
temp_surface->w, temp_surface->h,0,GL_BGR_EXT,
GL_UNSIGNED_BYTE, temp_surface->pixels);*/
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,temp_surface->w, temp_surface->h,GL_BGR_EXT, GL_UNSIGNED_BYTE, temp_surface->pixels);
if(NULL != temp_surface)
{
SDL_FreeSurface(temp_surface);
}
if(true != glIsTexture(textures[filename].id))exit(-10);

If i use the commented glTexImage instead of the gluBuild2Dmipmaps the texture just isnt shown! The code that displays the texture:
	glMatrixMode(GL_MODELVIEW);
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glTranslatef(0,0,-50.0f);
glScalef(0.2f,0.2f,0.2f);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_ALPHA_TEST);

Texture* tex = &textures[filename];
//glBindTexture(GL_TEXTURE_2D,tex->id);

glTexCoord2f(0.0f, 0.0f); glVertex3f( x, y - tex->height,  0.0f);	// Bottom Left
glTexCoord2f(1.0f, 0.0f); glVertex3f( x + tex->width, y - tex->height,  0.0f);	// Bottom Right
glTexCoord2f(1.0f, 1.0f); glVertex3f( x + tex->width,  y,  0.0f);	// Top Right
glTexCoord2f(0.0f, 1.0f); glVertex3f( x,  y,  0.0f);  // Top left

glEnd();

A few other states are set earlier(the usual ones found in nehe''s tutorials).[and yes i know there are smarter ways to handle state changes etc, and im going to change it as soon as i understand whats wrong with my texturing ] I really dont need mipmaps, since im trying to create a 2d game, so everything will be scaled so its drawn 1:1 on the screen. Any help appreciated, and go right ahead and call me an idiot if you want

##### Share on other sites
I''ve experienced the same, where quads with scaled-down textures would generate white (untextured) quads; generating mipmaps fixed the problem. I''m not really worried about sapping users'' VRAM, as my game is not incredibly complex (nor is the required amount of texture memory high), so I simply ignore the issue.

(Note that this is not a good practise, but I''m more concerned with finishing my game than optimizing it right now).

MatrixCubed
http://MatrixCubed.cjb.net

##### Share on other sites
Perhaps i should try adjusting the camera/projection so that the textures are drawn so that one pixel in the bitmap is one pixel on screen? That would mean that the mipmaps wouldnt be needed.

If it makes a difference im setting my view with glOrtho because i dont need perspective, i only need depth to decide if 2d objects are behind or in front of 2d objects.

##### Share on other sites
To show the texture not only with mipmaps you must provide on of the following image sizes:32/32,64/64,128/128,256/256,512/512 and etc

##### Share on other sites
The image im currently using is a 128x128 24-bit BMP image.
And if the problem was related to the dimensions of the image, wouldnt the problem show up both with the mipmap and the texture build methods?

##### Share on other sites
Hmm, i managed to fix it by scaling it so that 1 unit = 1 pixel.

##### Share on other sites
when you use gluBuild2DMipmaps, it scales the image into some power of 2 ( eg. 64,128,256 ) no matter what the original dimension of the image was. But when you use gluTexImage2D, you have to do the scaling by yourself. btw, if your texture dimension is not a power of 2, opengl disables texturing without any prior notice ..... Are you sure your texture dimension is some power of 2 ( like , 64,128,256...)

##### Share on other sites
Ooops, it appears that you might be correct, i totally forgot i had resized the image i was using. Just tried again with another image that isnt sized like that and the texture disappeared.
*But* i also use the TexFont code i found on the net to draw fonts, and its texture build method(txfEstablishTexture) gives you the choice between making mipmaps or not. Before my little scaling stunt, the font textures was only rendered when i choose mipmapping and empty quads otherwise. This is quite confusing

PS:
does this limit textures to only X*X or will X*2X also be acceptable?

##### Share on other sites
Erm, just a thought, but you are uncommenting the glBindTexture function right? (you probably are, but I thought I''d check anyway)

Also, in all my code, I set the filters before calling glTexImage2D, maybe that''s it?

##### Share on other sites
quote:

Erm, just a thought, but you are uncommenting the glBindTexture function right?

Actually im not, i commented it because the loadTexture binds the texture for me, so there is no need to do it again(good call though, its usually such silly things that make the most horrible bugs
As i tried explaining above, my own code seems to be working fine now, but im still confused about what was wrong with the font renderer before i did the scaling and that too worked.

The TexFont library(though i edited it somewhat) uses this method to make the texture:
(USE_DISPLAYLIST *is* defined and useLuminanceAlpha = 1)

  GLuint txfEstablishTexture( TexFont * txf, GLuint texobj,GLboolean setupMipmaps){	if (txf->texobj == 0) 	{		if (texobj == 0) 		{			#if !defined(USE_DISPLAY_LISTS)			glGenTextures(1, &txf->texobj);			#else	   		txf->texobj = glGenLists(1);			#endif		} 		else 		{			txf->texobj = texobj;		}	}	#if !defined(USE_DISPLAY_LISTS)	glBindTexture(GL_TEXTURE_2D, txf->texobj);	#else	glNewList(txf->texobj, GL_COMPILE);	#endif  if(1 == useLuminanceAlpha)   {    if (setupMipmaps) 	{      gluBuild2DMipmaps(GL_TEXTURE_2D, GL_LUMINANCE_ALPHA,        txf->tex_width, txf->tex_height,        GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, txf->teximage);    } 	else 	{      glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA,        txf->tex_width, txf->tex_height, 0,        GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, txf->teximage);    }  }   else   {    /* Use GL_INTENSITY4 as internal texture format since we want to use as       little texture memory as possible. */    if (setupMipmaps) 	{			gluBuild2DMipmaps(GL_TEXTURE_2D, GL_INTENSITY4,        txf->tex_width, txf->tex_height,        GL_LUMINANCE, GL_UNSIGNED_BYTE, txf->teximage);    } 	else 	{		glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY4,        txf->tex_width, txf->tex_height, 0,        GL_LUMINANCE, GL_UNSIGNED_BYTE, txf->teximage);    }}	#if defined(USE_DISPLAY_LISTS)	glEndList();	glCallList(txf->texobj);	#endif	return txf->texobj;}

And then before drawing text i call its bind method:

  void txfBindFontTexture(TexFont * txf){	#if !defined(USE_DISPLAY_LISTS)	glBindTexture(GL_TEXTURE_2D, txf->texobj);	#else	glCallList(txf->texobj);	#endif}

So right now, scaled to 1:1 both TexFont and my own drawing methods texture correctly, but before setting this scale the TexFont AND my methods only textured if i used mipmaps.
*But* just now i tried changing the scale so that the images got smaller, but it still worked, so something else must have been wrong(i did clean up my state management since then, perhaps i removed the bug without noticing)....
I feel kind of silly now, it would seem that the problem has gone away Im still having some texture problems with the above texfont stuff though(loading 2 different fonts makes only the last one display correctly), but i wont bother anyone with that yet, until i tried to solve it myself.

So anyway, thanks for all the help.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632649
• Total Posts
3007644
• ### Who's Online (See full list)

There are no registered users currently online

×