Straight to the point, I am using SOIL (http://www.lonesock.net/soil.html) library to upload images into OpenGl. Loading textures works fine but there is this thing i dont fully comprehend.

Lets say I load an image into OpenGL via SOIL and I want to reuse the same image over few other textures so I would not load any more images to save memory. So without SOIL I would do something like this:

glGenTextures(3, &texture[0]);

thus generating 3 textures from 1 image i uploaded into texture[0]. But with SOIL to upload an image I need to use this:

texture[0] = SOIL_load_OGL_texture(
"0.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);

So I have texture[1] and texture[2] and I want to copy over the same texture withouth reloading anything again but dont have a full idea how to do that, in SOIL header file it states something like:

/**
Passed in as reuse_texture_ID, will cause SOIL to
register a new texture ID using glGenTextures().
If the value passed into reuse_texture_ID > 0 then
SOIL will just re-use that texture ID (great for
reloading image assets in-game!)

**/
enum
{
SOIL_CREATE_NEW_ID = 0
};

but like I've said, I cant fully understand how the code would look like I described.

P.S im following this tut http://nehe.gamedev.net/tutorial/texture_filters,lighting&_keyboard_control/15002/
Note that in this tutorial he still uses the old image loading method, im using the one he formed in his previous tutorial.

Recommended Answers

All 4 Replies

You don't need to load a new texture, nor do you need to generate new texture objects (the glGenTextures call). When SOIL loads the image and puts it into an OpenGL texture object texture[0], you then bind it and use it when rendering vertices. If you want to reuse it for other things that you render, simply bind it with those objects too. In other words, you have this type of code:

texture[0] = SOIL_load_OGL_texture( /* .. */ );

// .. other stuff..

glBindTexture(GL_TEXTURE_2D, texture[0]);
// render some stuff ...

// .. some more stuff ..

glBindTexture(GL_TEXTURE_2D, texture[0]); // re-bind the texture again.
// render something else ..

// etc...

So, you should never need to have multiple texture objects that point to the same texture, just re-use the texture ID for other objects if you need to use the same texture for multiple entities that you render.

What i wanted to do was to make 3 same textures and see the difference between GL_LINEAR, GL_NEAREST and so on.
So what i did was (after loading the image into texture[0]):

texture[1] = texture[0];
texture[2] = texture[0];

And now i have a couple of questions:
1. Is it a decently rational way of doing it? depending that i only want to see the diffrence.
2. If i do this first:

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

and then do texture[1] = texture[0], does what i did before go into texture[1] aswell?
If yes, then i guess i could simply overwrite something else on texture[1] but the thing
is that i want to make sure to know all the details if these any more.

Oh and is it normal that i cant see the difference? maybe it just depends on the image, any suggestions what i could use to see the difference decently well?

After you set filters with glTexParameteri for the binded texture object, then that texture object will always be rendered with that filter until you set the filters again to some other value. So, if you want to test the different filters just to see how they do, then you could do:

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glBegin(GL_QUADS);
  // .. render something..
glEnd();

// ..

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glBegin(GL_QUADS);
  // .. render something ..
glEnd();

It is just a matter of binding the texture, and applying a different set of filters for the different objects you render. You might not see this kind of code very often because people tend to apply just one type of filter to one texture, but it is possible to alternate the filtering like in the above.

and then do texture[1] = texture[0], does what i did before go into texture[1] aswell?

Yes. The texture ID is nothing more than an address or pointer to the texture object, so, doing texture[1] = texture[0] doesn't create a new object just like copying a pointer doesn't copy the object that they point to, it just makes them both point to the same object. So, texture[1] and texture[0] will simply represent the exact same texture all the time, whatever you do to one, you do to the other.

You have to understand that the whole point of all this is that the texture itself (the image) is transferred (by SOIL) onto the graphics card (GPU). OpenGL provides you with an ID value for the texture such that you can refer to it to do things with it, that ID is basically like a pointer to the texture, but a pointer that only OpenGL calls can use internally to find your texture and do what you want with it. So, if you ask OpenGL to do something to texture[0] or to texture[1], for OpenGL they both refer to the only one texture object.

Oh and is it normal that i cant see the difference?

Yes, the difference is subtle. It does depend on the texture. Usually, you see it best if the texture has "man-made" things on it, like writings or regular shapes with straight lines and stuff. If you have a very messy and "natural" texture like grass or wood, then the difference will be less noticeable. Also, these are filters that apply to the magnification and miniaturization of the texture. So, if you render the texture such that it appears more or less at its original pixel-size on the screen, then you won't see any difference whatsoever, because the filters don't have any effect. Also, the effects are more noticeable when a gradient in the application of the filter, meaning that part of the texture appears more or less at its original size and another part is largely magnified or minimized. For example, you can apply the texture to a long surface that is tilted such that one edge is close to the screen and the other is very far from the screen, such that each pixel of the texture that is mapped to the near edge occupy more than one pixel on the screen, and each pixel of the texture that is mapped to the far edge occupy much less than one pixel on the screen. In these conditions, you should see the difference between the two filters. I would also recommend that you make your scene movable (with arrow keys or mouse) such that you can actually move around a bit to better see these filtering effects.

Thanks for a quick reply, what i wanted to know is clear now and will do as you suggested to see the difference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.