Archived

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

zunino

NeHe's lesson 6 and multiple textures

Recommended Posts

Hello. I sucessfully completed NeHe''s lesson 6, which introduces texture mapping. I used DevIL (http://openil.sourceforge.net/) to handle the image loading and everything worked fine. After that, however, I decided to try Jeff''s suggestion at the end of the lesson, which consists in attempting to apply different textures for each face of the cube. I first created 6 bmp files, named 1.bmp,...,6.bmp. Then, I modified the code to load the images and assign them to OpenGL textures. The problem I am having is that only the last texture is being successfully created. The texture at textures[5] is fine, but all the others seem to not have been properly created. Here is a snippet of relevant code: namespace { const int numberOfTextures = 6; GLuint textures[numberOfTextures]; } void prepareTextures() { glGenTextures(numberOfTextures, &textures[0]); ilInit(); ILuint images[numberOfTextures]; ilGenImages(numberOfTextures, &images[0]); stringstream imageFileName; for (int i = 0; i < numberOfTextures; ++i) { ilBindImage(images); imageFileName.str(""); imageFileName << "resources/" << i + 1 << ".bmp"; ilLoadImage(const_cast(imageFileName.str().c_str())); ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE); glBindTexture(GL_TEXTURE_2D, textures[i]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, GL_RGB, GL_UNSIGNED_BYTE, ilGetData()); } ilShutDown(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } And this is how I alternate between the textures in my render function: // Front Face glBindTexture(GL_TEXTURE_2D, textures[0]); glBegin(GL_QUADS); // glTexCoord2f and glVertex3f calls glEnd(); // Back Face glBindTexture(GL_TEXTURE_2D, textures[1]); glBegin(GL_QUADS); // glTexCoord2f and glVertex3f calls glEnd(); ... // Left Face glBindTexture(GL_TEXTURE_2D, textures[5]); glBegin(GL_QUADS); // glTexCoord2f and glVertex3f calls glEnd(); As I said earlier, only the texture at position textures[5] comes out ok, so only the left face gets its texture. What am I doing wrong? Thank you, Ney André de Mello Zunino

Share this post


Link to post
Share on other sites
1.Are width and height of all textures power of two?
2.I think, it is better to do:

GLuint err;

glBindTexture(...);
glTexImage2D(...);

err = glGetError();
if (err != GL_NO_ERROR)
{
//Handle errors...
}

Share this post


Link to post
Share on other sites
quote:
Original post by PM
1.Are width and height of all textures power of two?
2.I think, it is better to do:
[error checking suggestion]

To answer your question, yes, all the textures are 256x256 24-bit bmp images.

Anyway, I will definitely add your error checking suggestion and see what I get. If you have any other suggestions, I would be glad to hear them.

Thank you,

Ney André de Mello Zunino

Share this post


Link to post
Share on other sites
Well, I added GL error checking all over the code and always got GL_NO_ERROR. I also tried using glIsTexture() on my textures array to see if I had valid textures there and it always gave me positive answers. This must mean the problem is related to DevIL.

Thank you,

Ney André de Mello Zunino

Share this post


Link to post
Share on other sites
I don''t know... but:
1. I think, you should call
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
after EVERY call to glTexImage2D, after all calls (this needn9t to help - but try it
2. Where are you enabling and disabling GL_TEXTURE_2D and GL_COLOR_MATERIAL (GL_COLOR_MATERIAL is optional)?
GL_TEXTURE_2D should be always enabled, when calling glTexImage2D, glTexParamteri, glBindTexture, ...

I hope, it will help...

Share this post


Link to post
Share on other sites
When it don't help... you was probably right:
"This must mean the problem is related to DevIL."

It helped - so it's OK!

I wanted to change the icon from to , but there is a bug in the editation - it stays!

[edited by - PM on July 19, 2002 12:43:42 PM]

Share this post


Link to post
Share on other sites
You know what? It did it! I must confess when I first read your suggestion I didn''t think it would help. As a matter of fact I am still curious to know why it made a difference. I thought you would only need to make those settings once. Anyway, since I am an OpenGL newbie, I shouldn''t be making so many assumptions. But I will definitely try to find out why that was the case.

Thank you again!

Share this post


Link to post
Share on other sites
quote:
Original post by zunino
You know what? It did it! I must confess when I first read your suggestion I didn't think it would help. As a matter of fact I am still curious to know why it made a difference. I thought you would only need to make those settings once. Anyway, since I am an OpenGL newbie, I shouldn't be making so many assumptions. But I will definitely try to find out why that was the case.

Thank you again!


Hi! I'm a sort of a newbie to
but the thing here is ( I think that you have to apply the texture settings to EVERY texture, thats becouse you don't might want the same texture setting for "world" textures and for example your in game text.

and if you wright your own texture loading class that you will call from another class, you don't load all the game textures at once, you load one each call to the class, right?

feel free to correct me everyone
but thats how i think about it anyway.

[edited by - CondiS on July 18, 2002 10:46:02 AM]

Share this post


Link to post
Share on other sites
zunino: That is absolutely normal in respect to OpenGL specifications.

In the initial state, the TEXTURE_MAG_FILTER is LINEAR and the TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR.
Those parameters are defined in each texture object. That's why you have to call glTexParameter for every texture, because glTexParameter only affects the last bound texture.
(as a side note, remember that some other texture functions such as glTexEnv are depedant to the texture unit, not the texture object ; that is you don't need to call glTexEnv for every texture object).

The problem is that the default GL_NEAREST_MIPMAP_LINEAR value implictly needs mipmaps. Since you're not feeding all mipmap levels, then OpenGL assumes that the mipmap is incomplete, and then OpenGL doesn't display the texture you wanted to load.
When you call glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR), you explicitly tell OpenGL that there are no mipmap, so it is enough to feed "only" the first texture level.

[edited by - vincoof on July 18, 2002 10:53:48 AM]

Share this post


Link to post
Share on other sites