Advertisement Jump to content
Sign in to follow this  

OpenGL question on using parts of single texture with the multiple instances of same geometry

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

What is the best way to use parts of a single texture with the multiple instances of same geometry?


I am using WebGL (opengl es).


I have this texture here...





This is the vertex array buffer (simple quad) that I want to re-use...

//position, point size, colors, st interleaved
var vertData = new Float32Array([
        -0.25, 0.25, 		4.0,		1.0, 0.0, 0.0,		0.0, 0.5,
	-0.25, -0.25,		4.0,		0.0, 1.0, 0.0,		0.0, 0.0,
	0.25, 0.25,		4.0,		0.0, 0.0, 1.0,		0.5, 0.5,
	0.25, -0.25,		4.0,		1.0, 1.0, 0.0,		0.5, 0.0,

Those (s,t) coordinates show the bottom left part of the image on rendering in WebGL in this case. What I want to do is use this as sort of a 'texture atlas' (I think that's the buzz word) so I can use a single texture for an 'n' (say >100) number of quads without having to repeat all of the vertex data each time I want to use a different part of the single texture. So what's the best way to do that? I think of some solutions but I'm kind of a novice so I don't know what is best to use. I could...


a) Change the shaders to pass in what part of the texture I want to use

b) Repeat the vertex buffer data four times (in this case) for each piece of the texture I want to use with different st coordinates.

c) Get rid of the 'texture atlas' and just use four (in this case) different textures.


How do most people solve this problem? I know it's sort of trivial with such simple geometry but it seems like it could get out of hand fast...

Share this post

Link to post
Share on other sites

Solution c) is probably the worst: It not only requires to call the drawing method once for each quad, but it also requires to switch the texture for each call.


Solution a) means presumably to use a uniform vector for passing in a texture offset?! If so, you still have to invoke a draw method once for each quad this time with altering uniforms.


Solution b) may mean that you have a VBO ready to use on the GPU, and call a drawing with the sub-mesh to use. Again, you would call a drawing method once for each quad.


OTOH b) may mean that you build the VBO as needed and send it to the GPU (a.k.a. geometry batching). Assuming that the vertex structure as shown is needed, then a VBO consumes 100 * 4 * 8 * 4 = 12.800 bytes. (For comparison: A texture will consume significantly more; e.g. a RGB texture at 512*512 texels consumes 786.432 bytes if being uncompressed.) There is a break-through point where the overhead of calling the drawing method often enough is less effective than sending the small VBO to the GPU. I don't know where it is. But in general I think that batching the geometry and sending it as dynamic draw VBO is the way to go. Think of a way to make the VBO data smaller (is "point size" really needed?).


BTW: The particular sub-images should not touch each other or else interpolation may cause bleeding in color from neighbors.

Share this post

Link to post
Share on other sites

For your solution (a), and since you're using equal-sized subimages in your atlas, you could just add s-offset and t-offset as an additional 2-float vertex attrib per-instance.


With full OpenGL, and where texture arrays are available, using a texture array instead of an atlas would be the preferred approach here: it would avoid the problem of adjacent images bleeding into each other, and you would also be able to mipmap them properly.  In that case, a single float indicating which array slice to use would be your additional per-instance attrib.

Share this post

Link to post
Share on other sites

I strongly support mhagain and its proposal of using texture arrays.

There's really no other way around it. Tiling will screw you up sooner or later. Just say NO.

Share this post

Link to post
Share on other sites

A texture unit is the place where a texture can be bound to. The equivalent in the shader script is a sampler to access the bound texture. A GPU nowadays provides several units to work with; the exact amount can be requested during runtime.


An array texture (considering that the basis is 2D here) means that a texture has a 3rd dimension to sample, i.e. it is build of layered 2D texel images. As an array the 3rd dimension is not interpolated. How many levels are available at what maximal 2D size can be requested at runtime, too.


When being bound, an array texture needs a single texture unit and a single sampler. The 3rd texture co-ordinate is used to define which layer you want to access. With the image shown in your OP in mind, the array texture could be build of 4 layers each one with one quadrant of the original image.


Notice that this method is fine if all of your sub-images are nearly equal in size. As soon as a few sub-images are very big compared to the majority of the remaining images, using an array texture in this way is wasteful w.r.t. memory (mhagain has mentioned this already: "… using equal-sized subimages ...").


The reasoning for using an array texture here is that it supports mip-mapping. When using mip-mapping with a texture atlas instead, the sub-images must be in regions limited by co-ordinates at a power of two. This may play a role if the textures are used for billboards, particles, decals, and so on. OTOH, if they are used for sprites or UI glyphs, mip-mapping normally plays no role, and using an atlas is still a option. If both ways will work equally good for you, you should prefer array textures.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!