Simple Texture Mapping

Hi all,

I am new in texture mapping. Here is the code I have written:

glEnable( GL_TEXTURE_2D );
glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

glTexImage2D(GL_TEXTURE_2D,0,4, VWIDTH, VHEIGHT,0,GL_RGBA, GL_UNSIGNED_BYTE,dataRGBA);

with VWIDTH, VHEIGHT and dataRGBA filled.
It doesn’t show anything.
Waht 's wrong I have done?

Didn’t forget to use glTexCoord2f(v, w); for each vertex?
I allso don’t see glGenTextures(…); anywhere and no glBindTexture(…); too.
Maybe you should look at nehe’s tutorials which explain this pretty good: http://nehe.gamedev.net

John

There is no need to use texture objects for such simple test. This example uses the immediate texture.
You have specified the minification filter to be mipmapped, but only downloaded the level 0 texture.
For the driver that means the texture is not valid and therefore does not draw something.
Try the same program with GL_LINEAR or GL_NEAREST and you’ll see.

How about the size of the image?
Is it restricted to power of 2?

Also, I just want to display an image in a 2D space, so I want to show every pixel clearly. What flag I need to set for mipmap?

Thanks

Yes , texture images have power of two sizes, but don’t need to be square.
You have to query for the maximum texture size with glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxSize). The value gives you the biggest texture (square, mipmapped, RGBA, biggest color resolution) which is downloadable with the running OpenGL implementation.
If you want to download non-power-of-two images, you’ll have to blow them up to the next power of two value.
Mipmap textures are simply generated by downloading all levels of details with glTexImage2d (e.g. for a 4x4 texture you need 4x4, 2x2, 1x1 which are levels 0, 1, and 2 respectively.) Then the minification filtering of your code should work.
For crisp display where no filtering occurs and the texture is displayed in its real size you wouldn’t need mipmaps. GL_NEAREST filtering and the correct size of a textured quad on the screen will do.

you don’t HAVE to blow a non-power-of-2 image to the nearest power of 2. Rescaling an image requries resampling, and the results might not be exactly what you want.
But! You say! Textures in OpenGL must have a dimension of power of 2. What you can do is just not use the padding and recalculate texture coordinates to only index the “used” part of the texture. For example:

<—aw—><-pw->
±-------±----+
| | |
±-------+ |
| |
±-------------+

the texture coordinate for the bottom right corner is not 1,1, but aw/pw,ah/ph. Yes, you waste texture space, but you don’t have to resample the image.

cheers,
John