【问题标题】:OpenTK bind multiple textures on shaderOpenTK 在着色器上绑定多个纹理
【发布时间】:2016-05-28 14:52:13
【问题描述】:

我正在尝试制作多纹理地形。 但是当我尝试绑定 3 个纹理时,只显示了一个纹理。 c#代码在这里:

GL.UseProgram(program);
//Active textures, glId - is texture id, generated by GL.GenTexture()
GL.BindTextures(0,3,new int[3]{grass.glId,grass_normal.glId,stone.glId});
//Set shaders uniform, diffuse is a shader class
diffuse.SetUniform("mainTexture",grass.glId);
diffuse.SetUniform("gNormalMap",grass_normal.glId);
diffuse.SetUniform("stoneTexture",stone.glId);    
//Just some render code, for example 
GL.BindVertexArray(mesh.vao);
GL.DrawElements(PrimitiveType.Triangles,
mesh.triangles.Count,DrawElementsType.UnsignedInt, IntPtr.Zero);        
//Unbind all
GL.UseProgram(0);
GL.BindVertexArray(0); 
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
GL.BindBuffer(BufferTarget.ElementArrayBuffer,0);
GL.BindTextures(0,3,new int[3]{0,0,0});

有shader片段程序:

#version 140
uniform sampler2D mainTexture;//grass
uniform sampler2D gNormalMap;//grass normal
uniform sampler2D stoneTexture;//stone
out vec4 FragColor;
//hide other uniforms and inputs

void main (void){ 
//Some light code
vec4 x = texture2D(stoneTexture, fract(vTexCoord.st * texSize));
vec4 y = texture2D(mainTexture, fract(vTexCoord.st * texSize));
vec4 tex = x;//display stone, but i want to mix textures later
//I don't know why, but there is no difference,
//if i use stone - 'tex = x', or grass - 'tex = y'
//in both cases, only grass is displayed  
FragColor = color*tex*(diffuse+ambient);//Color
}

我尝试先绑定石头再绑定草,显示的是没有草的石头。我的意思是这样的:

GL.BindTextures(0,3,new int[3]{stone.glId,grass_normal.glId,grass.glId});

没有效果,只显示一个纹理。

【问题讨论】:

  • 我只是想确保我理解。你在问为什么会这样,对吧?
  • 是的,但现在我修复了它(阅读下一篇文章)

标签: c# opengl opengl-es


【解决方案1】:
GL.BindTextures(0,3,new int[3]{grass.glId,grass_normal.glId,stone.glId});

如果此函数有意义,则意味着您的 glId 字段是 OpenGL 对象名称。

这意味着:

diffuse.SetUniform("mainTexture",grass.glId);
diffuse.SetUniform("gNormalMap",grass_normal.glId);
diffuse.SetUniform("stoneTexture",stone.glId);

这段代码现在没有意义了。

采样器统一值不是实际的 OpenGL 纹理名称。毕竟,如果它们是,就没有必要将它们绑定到上下文。

采样器制服设置为 纹理单元 索引。因此,采样器将从绑定到给定纹理单元的任何纹理中获取。由于您的 glBindTextures 调用使用了纹理图像单元 [0,2],因此您的统一设置应该反映:

diffuse.SetUniform("mainTexture", 0);
diffuse.SetUniform("gNormalMap", 1);
diffuse.SetUniform("stoneTexture", 2);

我猜为什么这似乎适用于一种情况,你很幸运。许多 OpenGL 实现将返回以 1 开头的纹理名称。因此,您可能得到一个纹理名称,恰好也与您绑定纹理的纹理单元匹配。

【讨论】:

    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    相关资源
    最近更新 更多