简体   繁体   English

了解纹理

[英]Understanding of textures

Let's say I've three different texture objects with different image data on them. 假设我有三个具有不同图像数据的不同纹理对象。 These three texture objects are all bound to texture unit 0. 这三个纹理对象都绑定到纹理单元0。

I've three models in my scene and each uses one of these textures. 我的场景中有三个模型,每个模型都使用其中一种纹理。

In my fragment shader I've a sampler2D variable. 在片段着色器中,我有一个sampler2D变量。 And all models use the same shader program for rendering. 并且所有模型都使用相同的着色器程序进行渲染。

Now I do the following operations in sequence 现在我依次执行以下操作

  1. Bind texture object 1 绑定纹理对象1
  2. Render model 1 渲染模型1

  3. Bind texture object 2 绑定纹理对象2

  4. Render model 2 渲染模型2

  5. Bind texture object 3 绑定纹理对象3

  6. Render model 3 渲染模型3

What is want to ask is this the way you texture different models with different textures and does the sampler in fragment shader reads data from currently bound texture. 想要问的是这种用不同纹理对不同模型进行纹理处理以及片段着色器中的采样器从当前绑定的纹理中读取数据的方式。 What happens if a texture is bound to a different texture unit, do I need a different sampler for that. 如果纹理绑定到其他纹理单元会发生什么,我是否需要其他采样器。

A sampler always reads from the currently bound texture at the texture unit it is set to. 采样器总是从当前绑定的纹理以其设置的纹理单位读取。 Since the default active texture unit is unit 0 and the default value of a sampler is also 0 your code should work unless you change either the sampler or the active texture unit. 由于默认的活动纹理单位为单位0,并且采样器的默认值也为0,因此除非更改采样器或活动纹理单位,否则代码应该可以工作。

If you bind the texture to another texture unit, you have to make sure that the active texture unit ( glActiveTexture ) matches with the value set for the sampler ( glUniform1i ). 如果纹理绑定到另一个纹理单元,你必须确保该活动的纹理单元( glActiveTexture )与采样(设定值相匹配glUniform1i )。 If you bind, For example, the texture to GL_TEXTURE2 , then you have to set your sampler to glUniform1i(sampler_location, 2) . 例如,如果将纹理绑定到GL_TEXTURE2 ,则必须将采样器设置为glUniform1i(sampler_location, 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM