GLSL 纹理化多个片段
我正在尝试将固定管道代码 (OpenGL ES 1.1) 转换为使用着色器管道 (OpenGL ES 2.0) 的代码。然而,似乎只有一个纹理有效,我收到错误:
Validation Failed: Sampler error:
Samplers of different types use the same texture image unit.
- or -
A sampler's texture unit is out of range (greater than max allowed or negative).
在我的片段着色器中,我有一个统一的纹理 ID 和来自我的顶点着色器的纹理坐标:
varying lowp vec4 colorVarying;
varying lowp vec2 texCoordsVarying;
uniform sampler2D texID;
void main()
{
lowp vec4 colour = texture2D(texID, texCoordsVarying);
gl_FragColor = colour * colorVarying;
}
据我了解,因为我只使用一个纹理,所以我只需要一个活动纹理单元。因此,在我的管道中,我通过激活适当的纹理单元、绑定当前纹理并将纹理 ID 的统一值设置为纹理单元来设置纹理。然后我继续绘制顶点。
这是我的管道的纹理部分:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ((MNTexturedRect*)object).texID);
glUniform1i(uniforms[UNIFORM_TEXTURE], GL_TEXTURE0);
glVertexAttribPointer(ATTRIB_TEXTURE_COORDS, 2, GL_FLOAT, GL_FALSE, object.dataPerPoint*sizeof(GLfloat), &points[((MNTexturedRect*)object).texCoordOffset]);
glEnableVertexAttribArray(ATTRIB_TEXTURE_COORDS);
我知道纹理 ID 统一这一事实导致了问题,但不确定如何解决它。我有几个对象,似乎只有加载的第一个纹理实际上似乎有效。我只有采样器,不太确定为什么它会抱怨使用相同的纹理单元,并且我的纹理单元在范围内,因为它只是 GL_TEXTURE0。
有人知道为什么会发生这种情况吗?
I am attempting to convert my fixed pipeline code (OpenGL ES 1.1) to one using a shader pipeline (OpenGL ES 2.0). However only one texture seems to work and I get the error:
Validation Failed: Sampler error:
Samplers of different types use the same texture image unit.
- or -
A sampler's texture unit is out of range (greater than max allowed or negative).
In my fragment shader I have a uniform texture ID and the texture coordinates coming through from my vertex shader:
varying lowp vec4 colorVarying;
varying lowp vec2 texCoordsVarying;
uniform sampler2D texID;
void main()
{
lowp vec4 colour = texture2D(texID, texCoordsVarying);
gl_FragColor = colour * colorVarying;
}
As I understand it since I'm only using one texture I only need one active texture unit. So in my pipeline I'm setting up the textures by activating the appropriate texture unit, binding the current texture and setting the uniform value for the texture ID to the texture unit. I then proceed to draw the vertexes.
Here's the texturing part of my pipeline:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, ((MNTexturedRect*)object).texID);
glUniform1i(uniforms[UNIFORM_TEXTURE], GL_TEXTURE0);
glVertexAttribPointer(ATTRIB_TEXTURE_COORDS, 2, GL_FLOAT, GL_FALSE, object.dataPerPoint*sizeof(GLfloat), &points[((MNTexturedRect*)object).texCoordOffset]);
glEnableVertexAttribArray(ATTRIB_TEXTURE_COORDS);
I've an idea that the fact the texture ID is uniform is causing the problem, but not sure how to tackle it. I have several objects, and it appears only the first texture loaded actually seems to work. I only have on sampler and not quite sure why it would complain about using the same texture unit, and my texture unit is in range as it is just GL_TEXTURE0.
Anyone any ideas as to why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
采样器的 glUniform1i 参数的范围是
[0:MAX[
],而不是[GL_TEXTURE0:GL_TEXTURE0+MAX[
]。所以将您的代码更改为:
The parameter for glUniform1i for samplers is in the range
[0:MAX[
, not[GL_TEXTURE0:GL_TEXTURE0+MAX[
.so change your code to: