How to Sub Image or Sub Texture?

I have been looking through the documentation and the wiki and haven’t been able to find anything that discusses what I am trying to achieve. Maybe I missed it.

Say, for example, I have an Image, say 500 by 500 and I wish the Geometry to be mapped to a region “sub image” of say 100 by 100. During rendering this sub image changes in size and location giving an altering texture effect. Ideally, only one image should be loaded into the card and sub images of that are used as textures as there will be countless sub images with differing parameters.

The function below is pretty much the equivalent as it only requires one set of data…

BufferedImage.getSubimage(int x, int y, int w, int h);

As a workaround I could alter the texture mappings on the fly but that doesn’t seem like an ideal approach.

I will very much appreciate if someone could offer a suggestion on how to achieve my goal.

It sounds like you are describing a texture atlas… and using texture coordinates is exactly how you’d do it. No additional object creation necessary in that case.

OpenGL has no concept of a ‘sub texture’ as you’ve described because it already has texture coordinates to get only part of an image.

The best approach is to dive into shader programming and use some uniform input to the shader to let it manipulate texture mappings on the fly. The second best approach is to alter texture mappings.

EDIT: Ninjad by pspeed :slight_smile:

pspeed, TextureAtlas could be the answer. I was unable to determin how I should use TextureAtlas to achieve my goal. Is there an example somewhere of TextureAtlas usage I could use to get a better understanding?

A texture atlas is an abstract concept, its basically a texture with separate images in multiple areas. If you have for example a texture with four quarters each representing one image you’d set the texture coordinates of your mesh to 0-0.5 to use the upper left quarter. 0.5-1 for the lower left etc.

1 Like

To add to that:

1 Like

Hmm, it seems a bit overkill for what I need.

I was just reviewing my code in regard to the previous 3D engine that I was using, Ardor3D which was a branch of JME2, hence I am thinking it isn’t going to be a major step to migrate to JME3.

I notice that I was using the renderer to project parts on an image directly to the screen/canvas in ortho mode and hence not using any scene graph or other objects.

I initially looked at ui.Picture but it uses a Quad and hence my initial query to see if a texture could reference part of an image.

If I could project parts of a 2D image directly to the canvas in ortho mode this would be exactly what I am looking for. I wonder if there is this capability?


I guess altering the texture mapping of a quad, 4 points, isnt going to be the end of the world, I just wonder how expensive it would be if the geoms need to be packed every frame.

The “sub image” is likely to change every frame.

rendering the “sub image” directly to the canvas would be ideal.


That sounds like sprite animations, and should be done on the gpu without modifying vertex data.
Or you could pass a Vector2f[2] showing the real texture coordinates as material parameters to your shaders…

I delved into the source code and the utility that allowed me to render parts of a texture did in fact use a mesh and created the effect by altering the texture mappings.

I was quite happy with its performance so maybe I will extend Picture to include this ability.

jmaasing and zzuegg, is there an example codelet that demonstrates or similar to your suggested usage?

Thanks everyone for your help and advice. At this stage unless something better comes along it looks like I will be extending Picture to include the ability to alter the texture mapping of the quad to give the effect of displaying a sub image of the texture image.


You technically don’t even need to extend it to do it:

myPic.getMesh().setBuffer(Type.TexCoord, 2, new float[]{0, 0,
                                                         1, 0,
                                                         1, 1,
                                                         0, 1})

Though a utility method would be cleaner, certainly.

re: performance, skeleton based animation changes the whole position and normal buffers for a mesh every frame and performs fine… I think your 4-float buffers should perform ok.

1 Like

Thank you for that! I believe that is the solution I will adopt.