How do I manage textures in memory in OpenGL?

Jesse Brands
  • How do I manage textures in memory in OpenGL? Jesse Brands

    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.

  • 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:
    // 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

c++ opengl textures
Related questions and answers
  • with different types of objects in the same scene. I was also thinking about making a MeshNode class and then make a Mesh object that contains them, but then I have some conflict on where to store some data...well... I'm building the animation system of my game engine (the skeletal and skinned animation stuff), and I came to a point where I added so much functionality in the frame and node structures... know if my speculations are ok, as I don't have much experience with 3d animations yet. I want to make a well decision as any option I choose would require a lot of work to get it to render and I

  • my classes and still have them do what I want. Here's a few examples of a dependency chain: I have a status effect class. The class has a number of methods (Apply/Unapply, Tick, etc.) to apply... related to the graphics library I'm using, but is more of a conceptual thing. In C#, I coupled graphics in with alot of my classes which i know is a terrible idea. Wanting to do it decoupled... question about it is, is this a worthwhile approach at all? If it's a bad design I want to know now before I invest too much more time making all the screens I'm going to need. How do you build up

  • a decision which Components to register (and how to organize them). For example, GameSubsystemRender can register Renderable Components. pro. Components know nothing about how they are used. Low... Components. 3: Component registers itself in GameSubsystem(s). We know at compile-time that there is a GameSubsystemRenderer, so let's ComponentImageRender will call something like...I'm creating a component-based game object system. Some tips: GameObject is simply a list of Components. There are GameSubsystems. For example, rendering, physics etc. Each GameSubsystem contains

  • vector container. I have chosen this way because I parse an object map file. I have object type IDs (integer values) which I need to translate them into real objects. Because I have over 100...Dear all, this is going to be tough: I have created a game object factory that generates objects of my wish. However, I get memory leaks which I can not fix. Memory leaks are generated by return new Object(); in the bottom part of the code sample. static BaseObject * CreateObjectFunc() { return new Object(); } How and where to delete the pointers? I wrote bool ReleaseClassType(). Despite

  • from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've... and traveling throug it. This is due to some odd reason for the "numb_collisions", which is used to count how many collisions have occured since initial collision (if it's 0 it means it IS the initial...((char*)new_map[r].c_str());} // make sure this works later // Insert Time if(command== ALL || command== TIME){ enum{ time_loc_y= 22, time_loc_x= 38

  • Say I develop a game for mobile platform running OpenGL ES 2.0. I have done 2D part, and now I wish to import some 3D objects. The imported 3D objects must contain the following: Vertices positions Normals UVs Texturing information Animation information Collision mesh Maybe some other things... I am aware, that I could and (maybe should) create my own file format that brings these data from... the above mentioned features plus things I haven't considered yet Programming language is: C++. Currently, I'm developing under Windows 7, Visual Studio 2010, and OpenGL ES 2.0 emulator. Later

  • I am quite new to OpenGL, I have managed after long trial and error to integrate Nehe's Cel-Shading rendering with my Model loaders, and have them drawn using the Toon shade and outline...? Something unnecessary that maybe you guys could spot? Both MD2 and 3DS loader have an InitToon() function called upon creation to load the shader initToon(){ int i... (1, &shaderTexture[0]); // Get A Free Texture ID ( NEW ) glBindTexture (GL_TEXTURE_1D, shaderTexture[0]); // Bind This Texture. From Now On It Will Be 1D

  • to work correctly. Player movement ends up being very glitchy and repositions the player when I don't want it to. Part of the reason is probably because I feel like this is something so simple but I'm... loading, etcetera. I am also using OpenGL via the FreeGLUT library in conjunction with SDL to display graphics. My method of collision detection is AABB (Axis-Aligned Bounding Box), which is really all I need to start with. What I need is an easy way to both detect which side the collision occurred on and handle the collisions properly. So, basically, if the player collides with the top

  • After spending time today to jot down some notes regarding the implementation of walls into my tile-based game, I've suddenly realized it's not going to be as simple as I imagined before. While the current stage of my work is not even close to actually making the wall-related code, I've come up with three different ways to do it. Right now I'm unsure which one of my ideas will work best... the players double-wall the whole map, I'm going to beat them to it. Every wall has two halves that are attached to the edge of the tile from inside. So, to make a single "Wall unit" I'll have to create

Data information