Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Textures withing texture?

This topic is 3815 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

Hello people! :) Im making a tile-based game. Each tile is an image loaded by sdl and converted to opengl texture. The redrawing part of the loop looks like this: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, textureid); for(int x=0;x<22;x++){ for(int y=0;y<17;y++) { glBegin(GL_QUADS); // bottom left glTexCoord2f(0, 0); glVertex2f(32*x+xpos, 32*y+ypos); // bottom right glTexCoord2f(1, 0); glVertex2f(32*x+32+xpos, 32*y+ypos); // top right glTexCoord2f(1, 1); glVertex2f(32*x+32+xpos, 32*y+32+ypos); // top left glTexCoord2f(0, 1); glVertex2f(32*x+xpos, 32*y+32+ypos); glEnd(); } } The code is pretty straight forward so I didnt comment a lot. As you can see, the texture is redrawn 22*17 times. Is there a way to combine those 22*17 textures into one for performance benefits? Thank you.

Share this post

Link to post
Share on other sites
I'm assuming you meant that you have many textures and you put
glBindTexture(GL_TEXTURE_2D, textureid);
in your for loop.

Yes, you can make 1 larger texture by putting all the smaller ones together and adjust the texture coordinates accordingly.
Of course, if you are using mipmapping, care must be taken so that the mipmaps are properly generated.

Share this post

Link to post
Share on other sites
Currently, you render ONE texture 17 x 22 times. Is this what you actually wanted?

Your problem is essentially a geometry and texture issue. The geometry can be better optimized by creating the grid once by(a) a display list, or (b) using a 3D modeling tool and importing the resulting grid back into your application. (c) Transforming the data into a vertex array format.

Since by the description of you problem the code/application is small scale - I would recommend the display list approach.

The code below is an example of creating a new display list. For this approach to work you still require your tile-grid drawing routine. Perhaps calling it drawTileGrid()

uint aDisplayListID = glGenLists (1);
glNewList (glDisplayListID, Gl.GL_COMPILE);
... place a call to your standard grid drawing code here
glEndList ();

Then in your render loop you simply perform this call instead:

glCallList (aDisplayListID);

What has this done? The original drawTileGrid() code was now been captured and stored within the Video cards memory. Thus making subsequent calls to draw it much much faster.

If you need to switch between different textures this is okay too. Simply perform the glBindTexture() command in the inner body of your loop.
Of course you need some way of controlling WHICH texture is appropriate but that's your application specific code bit.

Share this post

Link to post
Share on other sites
Yes you can have "textures within textures" :) You simply change the texture coordinates to the section of the texture you want to use.

Ignoring my crappy coder art, this image shows a 512x512 texture with four "sub textures in it": Here

As you can see to access each "sub texture" you just change the tex coords. This means you can have the same texture bound for many different tiles if you wanted to. I use one texture for my world tiles and another for my unit tiles, so I can draw the whole world and all its units with just two texture binds.

Share this post

Link to post
Share on other sites
Thank you very much ZeroSum, BionicBytes and V-man. All of your answers were very helpfull. I optimized the drawing routine by calling the list code and saved all my tile graphics in one bmp file.

Share this post

Link to post
Share on other sites
Im having a problem with the display list. When a new tile is reached while walking, I want the display list to clear and redraw different tiles in it(x-1 when moved left etc). Here is what I have so far:

void reLoadTiles(){
glBindTexture(GL_TEXTURE_2D, textureYou); // textureYou is the bmp file that stores all kinds of 32x32 pix tiles

glNewList (DisplayListID, GL_COMPILE);

for(int x=0;x<22;x++){
for(int y=0;y<17;y++) {
posX = tileMap[currentTileX+x][currentTileY+y].tilePosX;
//the tilePosX stores the fraction that represents the tile that should be drawn (for example when there are 5x5 tiles in a bmp file, tilePosX would be position of the tile + 1/5...)

posY = tileMap[currentTileX+x][currentTileY+y].tilePosY;

// bottom left
glVertex2f(32*x, 32*y);
// bottom right
glVertex2f(32*x+32, 32*y);
// top right
glTexCoord2f(posX+(float)1/mapWidth, posY+(float)1/mapHeight);
glVertex2f(32*x+32, 32*y+32);
// top left
glTexCoord2f(posX, posY+(float)1/mapHeight);
glVertex2f(32*x, 32*y+32);
glEndList ();


Share this post

Link to post
Share on other sites
So what you have is an atlas based texture. I.e a texture that has NxM textures on it. Presumably matching this is a piece of geometry arranged as a grid. The texture coordinates are arranged in such as way as to mimic the NxM tile based textures. Is this correct?

What's not so clear is the 'when a new tile is reached'. Sounds like your wanting to dynamically generate a new set of extra tiles.
Perhaps there is an alternative method - why not simply reuse the exiting grid. You could simply displace the grid 'n' units along the XZ axis using a translate () command before you issue the glCallList () command. To update the tile - if you have access to the original data then use this to update a chunk of the texture atlas using glTexCopy2D().

This approach might be better than regenerating the entire geometry grid every time you want to give the illusion of moving/shifting a tile to the left/right etc...

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!