我正在寻找 glActiveTexture(...)
和 glBindTexture(...)
之间的关系,并发现了 awesome answer here ,最上面的答案(作者/用户 Alfonse)为我们提供了两个函数如何行为的伪代码,我理解了其中的大部分内容。但是,他在其中提到了这样的电话:
glActiveTexture(GL_TEXTURE0 + 5);
glBindTexture(GL_TEXTURE_2D, object);
glTexImage2D(GL_TEXTURE_2D, ...);
but one often binds a texture to the context just to upload some data or to modify it. It doesn’t matter at that point which texture unit you bind it to, so there’s no need to set the current texture unit. glTexImage2D doesn’t care if the current active texture is 0, 1, 40, or whatever.
所以我的问题是: 当生成两个纹理时,我们会执行以下操作:
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//some more code
//inside the render loop
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);
glDrawElements(...); glSwapBuffers();
//end of render loop
注意,在渲染循环之前的代码中,我使用了两个纹理glBindTexture(...)
,而没有调用glActiveTexture(...)
。由于默认的事件纹理单元是:GL_TEXTURE0
,这是否意味着 texture1
设置的参数被 texture2
覆盖>?
最佳答案
不,纹理参数(由glTexParameter
设置)是为特定纹理(当前绑定(bind)到事件纹理单元的纹理)设置的,而不是为纹理单元(不是为 >GL_TEXTUREi
,即)。
请注意,您对 glTextureParameteri
的使用不正确。它需要一个纹理句柄,而不是 GL_TEXTURE_2D
,由 glGenTextures
1 返回。您将其与 glTexParameteri
混淆了,后者确实可以使用 GL_TEXTURE_2D
调用。
1 正如 @derhass 所指出的,glGenTextures
(与 glCreateTextures
不同)仅保留句柄。仅当您将其传递给 glBindTexture
时,才会创建具有此句柄的纹理。如果您使用 glTexParameter
并不重要,但如果您想使用 glTextureParameteri
以及其他直接操作纹理句柄的函数,它可能很重要。
关于c++ - opengl - glBindTexture 是否覆盖事件纹理单元的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62371756/