1
votes

In OpenGL fix function programming, can I possibly map different textures on different objects, but that texture in generated from one image only. For e.g. I have 1024 X 1024 image. I have four rectangles in my scene. Now I would want to slice image into 256 X 256 *4 and map these sliced images as textures.

How can I do this. One option is to off course pre-slice the image. But can this be done using glTexSubImage2D or some similar/different API?

3

3 Answers

3
votes

Yes, you can use texture coordinates, which indicate which parts of the texture you wish to be mapped onto your object, rather than mapping the whole thing.

Read more: http://www.glprogramming.com/red/chapter09.html#name6

2
votes

EDIT : Shaders are required to use array textures : http://www.opengl.org/registry/specs/EXT/texture_array.txt. I'll leave the answer as it might still be useful info.

I guess you can use an Array Texture for this : http://www.opengl.org/wiki/Array_Texture

You make a 256 x 2048 texture. When loading the texture you specify a layer size (256 x 256) and layer count (4). Your texture will then be split up in four layers.

You can access the texture using UVs : [x, y, LayerId]

Note that if you want to generate mipmaps, you need to define the number of levels when you allocate the storage with glTexStorage3D.

1
votes

I think you have three options.

  1. Use different texture coordinates for each different rectangle.

  2. Transform the texture coordinates using glMatrixMode(GL_TEXTURE) and a different matrix between drawing each rectangle.

  3. Create four different OpenGL textures from your original big texture. I don't think OpenGL offers you any help here. You have to either use a paint package to do it (easiest option if you only have to do this a few times), or copy parts of the image into a new buffer before calling glTexImage2D.

I think the first option is the easiest, with the advantage that you don't have to change any state between drawing the rectangles.