OpenGL ES 纹理映射问答中的领悟

简介: OpenGL ES 纹理映射问答中的领悟 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。

OpenGL ES 纹理映射问答中的领悟

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。


一堆的问与答,有些混乱,不过这混乱中,似乎有些东西正点醒着人们昏睡中的头脑;

也许吧,有些事情是没法用语法来真切地形容的,

而有些事情,非得等到积累了一定的实践之后,才能够理解,

但理解之后,也不一定能表述出来或者表述得很清楚,

除非你真的纯熟到一定境界,

那时,你可以用你自已的语言,

你自已的理解,

你自已明白的其它事物来承载这种领悟,

以传达给更多的人们。



转自:http://stackoverflow.com/questions/662107/how-to-use-gl-repeat-to-repeat-only-a-selection-of-a-texture-atlas-opengl

How can I repeat a selection of a texture atlas?

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

GLfloat textureCoords[]=
{
    .1f,  .1f,
    .3f,  .1f,
    .1f,  .3f,
    .3f,  .3f
};

Then I want to repeat that sprite N times to a triangle strip (or quad) defined by:

GLfloat vertices[]=
{
   -100.f, -100.f,
    100.f, -100.f,
   -100.f,  100.f,
    100.f,  100.f
};

I know it has something to do with GL_REPEAT and textureCoords going passed the range [0,1]. This however, doesn't work: (trying to repeat N = 10)

GLfloat textureCoords[]=
{
    10.1f,  10.1f,
    10.3f,  10.1f,
    10.1f,  10.3f,
    10.3f,  10.3f
};

We're seeing our full texture atlas repeated...

How would I do this the right way?

share | improve this question
  add comment

5 Answers

up vote 11 down vote accepted

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.

share | improve this answer
 
 
Thanks to "unwind", "Jimmy J" and "TrayMan" for your contributions. I voted your answers up and compiled this answer from yours which now describes very well what's going on. –   Kriem  Mar 19 '09 at 15:30
1  
>"or write a GLSL vertex program to map the texture coordinates appropriately." There must be fragmentprogram instead. –   Sarge Borsch  Feb 2 '13 at 14:21 
add comment

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.

share | improve this answer
 
 
Hmm, GL_CLAMP for example affects the sub-textures you're describing, leading me to the idea texture coordinate mode do apply to parts of a texture atlas. Or am I in the wrong here? (Of course, seeing that I can't repeat the way I'd like, implies I indeed am in the wrong:)) –   Kriem  Mar 19 '09 at 13:45
add comment

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.

share | improve this answer
  add comment

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)

share | improve this answer
 
 
 

目录
相关文章
|
5月前
|
XML 小程序 Java
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
【Android App】三维投影OpenGL ES的讲解及着色器实现(附源码和演示 超详细)
55 0
|
存储 编解码 算法
Opengl ES之LUT滤镜(上)
Opengl ES之连载系列
343 0
|
数据安全/隐私保护 开发者
OpenGL ES 多目标渲染(MRT)
Opengl ES连载系列
221 0
|
数据安全/隐私保护 索引
Opengl ES之纹理数组
Opengl ES连载系列
180 0
|
数据安全/隐私保护
Opengl ES之水印贴图
Opengl ES之连载系列
88 0
|
Java 数据安全/隐私保护 Android开发
Opengl ES之矩阵变换(下)
Opengl ES连载系列
84 0
|
Java API 数据安全/隐私保护
Opengl ES之矩阵变换(上)
Opengl ES连载系列
87 0
|
存储
Opengl ES之踩坑记
Opengl ES之连载系列
91 0
|
存储 编解码 算法
Opengl ES之RGB转NV21
Opengl ES连载系列
117 0
|
并行计算 C++
Opengl ES之YUV数据渲染
Opengl ES连载系列
131 0