Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[Fixed]Textures: having multiple containers

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Is there a way I can have multiple texture containers? Like this:
[source lang=cpp]GLuint gui_textures[];
GLuint map_textures[];

glGenTextures(map_n_textures, map_textures);
glGenTextures(gui_n_textures, gui_textures);

Because it doesn't seem to work, any idea's? When I load the textures into map_textures and after that into gui_textures, then when I bind it to a texture in the map_textures array and I get a texture from the gui_textures array OR just white. Thanks, Decrius [Edited by - Decrius on March 14, 2008 4:47:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
texture ids are just unsigned integers. You can store them however you want - opengl knows nothing about them until you pass them as a param.

No offense, but I advise you to take some time to ensure you understand c++, then take some more time to ensure you understand the opengl state machine (at least the texturing part). There are lots of simple mistakes that could lead to the problems you're having, and most of them are caused by not fully understanding that opengl is a state machine.

Don't worry, we've all been there. I can even predict with some degree of certainty that you're learning gl from nehe (the arrays are a giveaway), as I and many others did.

I suspect you're either misunderstanding nehe's loadBMPWhatever function due to lack of experience with c++, or you're misunderstanding opengl's texture state stuff.

Post more code. Ideally just the bits to do with texturing.

Share this post


Link to post
Share on other sites
Sorry, but I'm not a complete newbie as you're implying.

1. I don't use NeHe at all.
2. I KNOW that its just an array, thats why I post it!

Alright, some more code (very simplyfied):
class Gui
{
int gui_n_textures;
int map_n_textures;
GLuint gui_textures[];
GLuint map_textures[];

public:
~Gui();
void load();
void load_map();
void draw();
};

Gui::~Gui()
{
glDeleteTextures(map_n_textures, map_textures);
glDeleteTextures(gui_n_textures, gui_textures);
}

void Gui::load()
{
gui_n_textures = 1;
char *paths[] = {"data/background.png"};

glGenTextures(gui_n_textures, gui_textures);

// Using corona to load the image
corona::Image *image;
for (int i = 0; i < gui_n_textures; i++)
{
image = corona::OpenImage(paths, corona::PF_R8G8B8A8);
if (!image)
{
error("could not load image", true);
}

glBindTexture(GL_TEXTURE_2D, gui_textures);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->getWidth(), image->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->getPixels());
}
delete image;

load_map();
}

void Gui::load_map()
{
map_n_textures = 3;
char *paths[] = {"data/dirt.png", "data/grass.png", "data/me.png"};

glGenTextures(map_n_textures, map_textures);

// Using corona to load the image
corona::Image *image;
for (int i = 0; i < map_n_textures; i++)
{
image = corona::OpenImage(paths, corona::PF_R8G8B8A8);
if (!image)
{
error("could not load image", true);
}

glBindTexture(GL_TEXTURE_2D, map_textures);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->getWidth(), image->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->getPixels());
}
delete image;
}

void Gui::draw()
{
glBindTexture(GL_TEXTURE_2D, gui_textures[0]);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2d(window.x, window.y);
glTexCoord2i(1, 0); glVertex2d(window.x + window.w, window.y);
glTexCoord2f(1, 1); glVertex2d(window.x + window.w, window.y + window.h);
glTexCoord2f(0, 1); glVertex2d(window.x, window.y + window.h);
glEnd();
}


What it displays, is the first texture of map_textures (dirt.png) which is ~10 pixels wide instead of filling the window. It works fine if I do not call load_map() (in width and texture).

Share this post


Link to post
Share on other sites
Try specifying sizes for your arrays, like this:
GLuint gui_textures[5];
GLuint map_textures[5];

AFAIK you can only use implicit array sizing (the empty brackets) if you provide an initialization list when you declare the array. Since they are empty, no space is reserved for them. So there is a chance that whatever address they point to is getting stomped on by some other value in the procedure.

Share this post


Link to post
Share on other sites
The point is, I don't know how many the array will need to have before I read the files to check out which images to load. But, they DO need to be global for the class. Can I somehow do this? Or will I need to use vectors?

Share this post


Link to post
Share on other sites
Yes, I always prefer arrays above vectors, but in this case it'd be better to use vectors :).

Thanks, problem fixed :)

Share this post


Link to post
Share on other sites
You could build a texture manager also using a singleton class.
Tends to be what I prefer for handling globals on final projects.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!