Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OpenTK在着色器上绑定多个纹理_C#_Opengl_Opengl Es - Fatal编程技术网

C# OpenTK在着色器上绑定多个纹理

C# OpenTK在着色器上绑定多个纹理,c#,opengl,opengl-es,C#,Opengl,Opengl Es,我正在尝试制作多纹理地形。 但当我尝试绑定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 sh

我正在尝试制作多纹理地形。 但当我尝试绑定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});
有一个着色器片段程序:

#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});
没有效果,只显示一个纹理

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

这意味着:

这段代码现在没有意义了

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

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

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


我的猜测是,为什么这似乎对一个案例有效,是因为你运气好。许多OpenGL实现将返回以1开头的纹理名称。因此,您可能得到了一个纹理名称,该名称恰好与您将纹理绑定到的纹理单元相匹配。

我只是想确保我能理解。你在问为什么会发生这种情况,是吗?是的,但现在我解决了它(阅读下一篇文章)
diffuse.SetUniform("mainTexture",grass.glId);
diffuse.SetUniform("gNormalMap",grass_normal.glId);
diffuse.SetUniform("stoneTexture",stone.glId);
diffuse.SetUniform("mainTexture", 0);
diffuse.SetUniform("gNormalMap", 1);
diffuse.SetUniform("stoneTexture", 2);