我正在尝试制作多纹理地形。但是,当我尝试绑定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});
没有效果,仅显示一种纹理。
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开头的纹理名称。因此,您可能得到的纹理名称恰好也与您将纹理绑定到的纹理单元相匹配。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句