如何使用GL_REPEAT仅重复选择纹理图集? (OpenGL的) [英] How to use GL_REPEAT to repeat only a selection of a texture atlas? (OpenGL)

查看:101
本文介绍了如何使用GL_REPEAT仅重复选择纹理图集? (OpenGL的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,我的精灵(选区)位于纹理坐标内:

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

然后我想重复这个sprite N次到一个三角形条或者四边形):

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

我知道它与 GL_REPEAT 和textureCoords会通过范围 [0,1] 。然而,这是行不通的:(试图重复N = 10)

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

我们看到我们的纹理图集全部重复...

我该如何做到这一点? 按照问题描述的方式完成。 OpenGL的纹理坐标模式仅适用于整个纹理。



通常,要重复一个纹理,可以绘制一个比纹理所暗示的更大的多边形。例如,如果你有一个方形的纹理,你想在一个更大的区域重复多次(比如说六次),你会画出一个六倍于高度的矩形。然后,您将纹理坐标设置为(0,0) - (6,1),并将纹理模式设置为重复。当在多边形上插值时,超过1的纹理坐标将因为重复被启用而在纹理中环绕,导致纹理在矩形上映射六次。



纹理环绕模式都不支持问题中所述的操作类型,即它们都映射到完整[0,1]范围,而不是某个任意子集。当只使用纹理的一部分进行纹理化处理时,无法指定较大的纹理坐标,因此OpenGL只能在子矩形内重复使用它。



您基本上有两种选择:创建一个新的纹理,它只包含您需要从现有纹理中获取的精灵,或者编写一个GLSL顶点程序来适当地映射纹理坐标。


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?

解决方案

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.

这篇关于如何使用GL_REPEAT仅重复选择纹理图集? (OpenGL的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆