How to use GL_REPEAT to repeat only a selection of a texture atlas? (OpenGL)

前端 未结 6 1772
臣服心动
臣服心动 2021-02-12 19:00

How can I repeat a selection of a texture atlas?

For example, my sprite (selection) is within the texture coordinates:

GLfloat textureC         


        
相关标签:
6条回答
  • 2021-02-12 19:10

    It can't be done the way it's described in the question. OpenGL's texture coordinate modes only apply for the entire texture.

    Normally, to repeat a texture, you'd draw a polygon that is "larger" than your texture implies. For instance, if you had a square texture that you wanted to repeat a number of times (say six) over a bigger area, you'd draw a rectangle that's six times as wide as it is tall. Then you'd set the texture coordinates to (0,0)-(6,1), and the texture mode to "repeat". When interpolating across the polygon, the texture coordinate that goes beyond 1 will, due to repeat being enabled, "wrap around" in the texture, causing the texture to be mapped six times across the rectangle.

    None of the texture wrap modes support the kind of operation as described in the question, i.e. they all map to the full [0,1] range, not some arbitrary subset. when you're texturing using just a part of the texture, there's no way to specify that larger texture coordinate in a way that makes OpenGL repeat it inside only the sub-rectangle.

    You basically have two choices: Either create a new texture that only has the sprite you need from the existing texture or write a GLSL vertex program to map the texture coordinates appropriately.

    0 讨论(0)
  • 2021-02-12 19:28

    I'm not sure you can do that. I think OpenGL's texture coordinate modes only apply for the entire texture. When using an atlas, you're using "sub-textures", so that your texture coordinates never come close to 0 and 1, the normal limits where wrapping and clamping occurs.

    There might be extensions to deal with this, I haven't checked.

    EDIT: Normally, to repeat a texture, you'd draw a polygon that is "larger" than your texture implies. For instance, if you had a square texture that you wanted to repeat a number of times (say six) over a bigger area, you'd draw a rectangle that's six times as wide as it is tall. Then you'd set the texture coordinates to (0,0)-(6,1), and the texture mode to "repeat". When interpolating across the polygon, the texture coordinate that goes beyond 1 will, due to repeat being enabled, "wrap around" in the texture, causing the texture to be mapped six times across the rectangle.

    This is a bit crude to explain without images.

    Anyway, when you're texturing using just a part of the texture, there's no way to specify that larger texture coordinate in a way that makes OpenGL repeat it inside only the sub-rectangle.

    0 讨论(0)
  • 2021-02-12 19:30

    None of the texture wrap modes support the kind of operation you are looking for, i.e. they all map to the full [0,1] range, not some arbitrary subset. You basically have two choices: Either create a new texture that only has the sprite you need from the existing texture or write a GLSL pixel program to map the texture coordinates appropriately.

    0 讨论(0)
  • 2021-02-12 19:30

    While this may be an old topic; here's how I ended up doing it:

    A workaround would be to create multiple meshes, glued together containing the subset of the Texture UV's.

    E.g.: I have a laser texture contained within a larger texture atlas, at U[0.05 - 0.1] & V[0.05-0.1].

    I would then construct N meshes, each having U[0.05-0.1] & V[0.05-0.1] coordinates. (N = length / texture.height; height being the dimension of the texture I would like to repeat. Or easier: the amount of times I want to repeat the texture.)

    This solution would be more cost effective than having to reload texture after texture. Especially if you batch all render calls (as you should).

    (OpenGL ES 1.0,1.1,2.0 - Mobile Hardware 2011)

    0 讨论(0)
  • 2021-02-12 19:34

    Can be done with modulo of your tex-coords in shader. The mod will repeat your sub range coords.

    0 讨论(0)
  • 2021-02-12 19:36

    Can't be done...

    0 讨论(0)
提交回复
热议问题