Texture buffer from tiles

Started by
8 comments, last by Programmer16 15 years, 9 months ago
I am trying to make a tilegenerated texture in opengl. I need to generate one big texture based on a bunch of smaller textures(tiles). When I have my texture I want to render it to a big quad, essentially saving thousands of polygons in game. This is really simple, I just need to know how to generate a texture from several images instead of just loading one image into the texture. I hope you understand my problem and have some kind of solution.
Advertisement
To me it sounds like you're looking for Wang tiles.

http://en.wikipedia.org/wiki/Wang_tile

You might also want to google: texture splatting
Quote:Original post by hejfelix
This is really simple, I just need to know how to generate a texture from several images instead of just loading one image into the texture.
You can allocate the texture using glTexImage2D(), with the correct size, depth and passing NULL for the data pointer. Then you can load each tile into the texture with glTexSubImage2D().

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Using gltexsubimage2d, I get to give x,y offset and width,height. But this makes for one image. I need to be able to take a specified region of one image, and place it in a specified region on another image.

With gltexsubimage2d, how will I determine where in my new texture it will be placed?

I really need an example of this. It's not easy working with when I havent made much texture stuff already.

[Edited by - hejfelix on July 2, 2008 3:15:19 AM]
glTexSubImage2D takes the destination parameters as input. The portion of your source image you're interested in has to be extracted manually.

Another solution could be to render the tiles to the destination texture once and than use this.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Quote:Original post by Lord_Evil
Another solution could be to render the tiles to the destination texture once and than use this.


Isn't this what he's trying to do? Show an example please if you know how to do that. I'm helping hejfelix code this but I'm not very experienced with OpenGL.

Basically we've got a palette consisting of our tiles with width and height of 32.

Image Hosted by ImageShack.us

and then we want our code to draw these according to a matrix that dictates the position of each tile. Performance wise drawing each tile seperately takes to big a toll on our machines with the big maps we have in mind and thats why we want to use one humongus image which we create at startup instead from the tiles. We just don't know how to generate this one image in opengl and save it.

Heres an example of a finished generated image

And if you have any examples it would be very appreciated.



*bump* sorry...
From what I can see, you want to create a HUGE texture for the entire map. BE CAREFUL, this could exceed OpenGL's maximum texture sizes and be very inefficient either.

Edit: The linked image has a size of 8000x800, which is clearly beyond current maximum texture sizes (which is around 4096x4096 IIRC).

You should consider drawing the tiles indivually yet as a batch. You could keep a 2D array of tiles that have their vertex coordinates set to the correct positions so that you don't need to transform them each frame. Then each frame you create a "temporary" array that only contains the tiles in the visible area. You only have to recreate that array if the new tiles are visible.

When rendering you just feed that array to OpenGL using 1 texure and 1 draw call.

Short ASCII example:

Your map could be like this (visible tiles being X, invisible ones O):
OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXOOOOOOOOOOOOOOOOOOOOOOXXXXXOOOOOOOOOOOOOOOOOOOOOOXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

You construct the temporary array (by copying the vertices) so that it looks like this:

XXXXXXXXXXXXXXX // 1D array, rows are just layed out one after the other

Now bind the texture, bind the array and draw the array.

--------------------------------------

In case you don't want to use the approach suggested above, here's a short explanation of my first suggestion:

I don't have any code for FBO yet since my current machine doesn't support that. I could post the PBO code but that would be much, too.

So I'll give you a short pseudocode explanation:

1.) bind your FBO/PBO to make it the current render target
2.) set up the projection matrix accordingly (I suggest an ortho matrix with width and height equal to the destination image dimensions)
3.) render the tiles positioning them at the corresponding coordinates
4.) unbind the FBO/PBO

5.) each frame render a quad using that texture.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
I wouldn't mind any example using the PBO, I think thats what we need to do.

My google skills arent great, but I will see if I can find something myself too.

Thanks for your help, it looks doable :)
I have no experience with OpenGL as of yet, so I can't actually help with a coding example, but I can tell you that what you want is some sort of render target system/render to texture system.

load your mapCreate a render-target/texture to render to (determining the size depending on the map's size and your tile's size.)iterate through and render each tile to it's appropriate place on the render-target.display the render target.


This is the exact same way I do it in my game. Whenever I need to, I load a new map and render it to using my render to texture system. Then I render that texture to the screen.

Search on google for the term '"opengl" + "render to texture"' (including the double quotes.)

The first link I checked seems decent IMHO: nVidia - OGL Render To Texture

HTH!

~P16

This topic is closed to new replies.

Advertisement