need help from texture mapping experts

Started by
5 comments, last by chare 20 years, 3 months ago
As suggested I'ved added MAG_FILTER at the top which didn't help. The texture is indeed 64x64; There is this ugly nasty code I have to work with that already has texture mapping working with mipmaps (so I have no idea what the state variables are) but in the code below I am trying to use a nonmipmap texture in place of glDrawPixels without success. The pixel pointer is good as a call to glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels) in the old drawimage works. No gl errors occur. Instead of getting texture mapping I get a rectangle of the color of the last glColor3f which isn't suppose to happen because of glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) right? So I figure that for some reason texture mapping is being turned off because of some important state variables are wrong. So if you really know opengl well, maybe you can tell me exactly which state variables could possibly cause texture mapping to be turned off here.

void drawDamnTestImage(int width, int height, void *pixels)
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
	static int init = 0;
	static unsigned int i = 0;
	glPushMatrix();
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, 1.0, 1.5, 100);
	if (init == 0) {
		init = 1;
		glGenTextures(1, &i);
		glBindTexture(GL_TEXTURE_2D, i);
		glTexImage2D(GL_TEXTURE_2D, 0, 3,
			width, height, 0, GL_RGB,
			GL_UNSIGNED_BYTE, pixels);
	}
	glBindTexture(GL_TEXTURE_2D, i);
	glMatrixMode(GL_MODELVIEW);
	glTranslatef(0,0,-10);
	glEnable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  0);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  0);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  0);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  0);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
	if (glGetError() != GL_NO_ERROR)
		exit(0);
[edited by - chare on January 12, 2004 3:33:37 AM] [edited by - chare on January 12, 2004 3:35:36 AM] [edited by - chare on January 12, 2004 1:36:24 PM]
Advertisement
The fact that you get a quad coloured with the last parameters sent to glColor3f indicates that the texture isn't being displayed. I notice you specified a minification filter (GL_TEXTURE_MIN_FILTER), but not a magnification filter (GL_TEXTURE_MAG_FILTER). It's essential that you specify both, or texturing won't work. The first time I did texturing, I missed out the magnification filter due to a typo, and the texture didn't show up until I fixed it. The rest of your code seems to be OK, but you need to set the colour to glColor3f(1.0f, 1.0f, 1.0f); before you draw your quad for the texture to show with the correct colour...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  

Windows 95 - 32 bit extensions and a graphical shell for a 16 bit patch
to an 8 bit operating system originally coded for a 4 bit microprocessor,
written by a 2 bit company that can't stand 1 bit of competition.

[edited by - iNsAn1tY on January 12, 2004 6:43:05 AM]
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
quote:Original post by iNsAn1tY
I notice you specified a minification filter (GL_TEXTURE_MIN_FILTER), but not a magnification filter (GL_TEXTURE_MAG_FILTER). It''s essential that you specify both, or texturing won''t work. The first time I did texturing, I missed out the magnification filter due to a typo, and the texture didn''t show up until I fixed it.

GL_LINEAR is the default magnification filter, so you don''t have to set it explicitly. I bet that there was something else wrong too, and you ''accidently'' fixed both things at the same time, or something like that.

quote:Original post by chare
There is this ugly nasty code I have to work with that already has texture mapping working with mipmaps (so I have no idea what the state variables are) but in the code below I am trying to use a nonmipmap texture in place of glDrawPixels without success. The pixel pointer is good as a call to glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels) in the old drawimage works. No gl errors occur. Instead of getting texture mapping I get a rectangle of the color of the last glColor3f which isn''t suppose to happen because of glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) right?

An invalid texture disables the texture unit, which is likely happening, since you enable texturing, but only the primary color is there. Most common problem is non-power of two dimensions. And you say that it works with mipmaps. Do you mean you use gluBuild2DMipmaps? That function will rescale the image to proper size before uploading it to OpenGL.
Out of pure curiosity, does altering the program so that you bind the texture after enabling texturing have a different effect?

i normally enable texturing first. I wouldn''t think it should make any difference.

Try making sure that the image dimensions are correct, opengl will not create textures if they are not powers of 2 and less than its maximum texture size.


Twitter: [twitter]CaffinePwrdAl[/twitter]

Website: (Closed for maintainance and god knows what else)

Are you sure you are setting the filters for the correct texture object? You are trying to change them at the top there, but you haven''t bound the texture yet. So you may be changing the filter for another texture object. You have to bind the texture before setting its filter properties.

The reason that I think that''s the problem is that, if you have mipmapping elsewhere in the app, and the texture filter for this texture happens to be left as a mipmap filter when you didn''t load in mipmaps, OpenGL disables texturing without telling you. In case that didn''t make sense, check out the filtering section of the Red book. It has a caution about this. If you just load a simple texture (no mipmaps built) and try to turn on a mipmap filter like GL_LINEAR_MIPMAP_LINEAR, OpenGL will disable texturing.
You need to bind the texture object BEFORE you change any texturing parameters.
Thanks that did the trick.

This topic is closed to new replies.

Advertisement