0
\$\begingroup\$

I'm writing a game engine which is going very fine. However, I'm now posed with handling textures.

My Engine is in 2D, for simplicity reasons mostly to get a good idea of how to work with OpenGL. I do this in C++, and I have a hierarchical set-up when it comes to classes. There's a base class and some other classes derive from that.

So now I'm onto the part where I want to load up some textures and apply them to polygons. The way my engine works in drawing things is like this:

  1. Load up a tile map file and parse it. The tile map contains all the information like events, lights, passability. All very interesting, not. Except the part where it loads a tileset, as defined by the map.
  2. The parser realises the map uses a tileset, which is stored in resources/tilesets/dungeon_interior_cave1.xml, which contains all the information on what textures are used.
  3. It loads the textures as said by the tileset-XML-file, it gives them an ID and then the SceneManager-class knows how to lay out the tiles and what texture to slap onto each of them. Once this is done, it tosses the instance of the SceneManager to the OpenGL renderer.

This might not be the best explanation of how my engine works, but simply put:

  1. SceneManager loads the file resources/maps/dungeon3Interor.msf (msf being a custom map format (MSF stands for M age S cene F ile ). The MSF file refers to a tilset.
  2. SceneManager loads the tile set at resources/tilesets/dungeon_interior_cave1.mts.
  3. SceneManager loads up all the textures from resources/textures/< path_to_textures >
  4. SceneManager tosses itself to the OpenGL render, and OpenGL renders the whole thing.

I think that's the best I can do at explaining. Know that the SceneManager itself contains no OpenGL code, just paths and information that OpenGL needs to know to render the scene.

What I'm interested in, what is the best practice to handle this, and how to handle OpenGL texture storage. I understand OpenGL keeps the textures in it's texture memory, and that I have to use glGenTextures to allocate a texture and that I then swap betwe--.. I actually have no clue.

What's a good way to keep track of all the textures you loaded into the memory and switch between them in C++, preferably using a class or a set of classes to accomplish this.

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Well, in general working with textures consists of a few steps:

glGenTextures(1, &texID);             // Get memory for texture
glBindTexture(GL_TEXTURE_2D, texID);  // Tell OpenGL you are using this texture
// Set its properties:
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Set its data
glTexImage2D(GL_TEXTURE_2D, 0, 4, sizeX, sizeY, 0, GL_BGRA, GL_UNSIGNED_BYTE, img);

texID for example can be a member variable of the class, so each object of the class holds one texture. (Side note: In copy constructor, you actually have to generate another texture, use glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, other_img); to get the image and copy it to the other texture).

Then on use, you have to have GL_TEXTURE_2D enabled (by glEnable(GL_TEXTURE_2D); and the texture bound by:

glBindTexture(GL_TEXTURE_2D, texID);

and when you want each vertex of your polygon bound to any point, you call:

glTexCoord2f(tex_x, tex_y);

Note: These stuff are legacy OpenGL though (for example OpenGL v1.2). If you are using new OpenGLs, such as OpenGL 4, I don't really know.

Anyway, if you want to see a texture handled by a class, you can check out the texture part of my raster graphics library: header and source

\$\endgroup\$
1
  • \$\begingroup\$ ... it was too obvious. Thank you, I guess I simply wanted a confirmation. :) Also, thank you for the examples, those are a nice reference. \$\endgroup\$
    – user1019020
    Oct 28, 2011 at 20:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .