c# - OpenTK bind multiple textures on shader -
i'm trying make multitextured terrain. when trying bind 3 textures, there 1 texture displayed. c# code here :
gl.useprogram(program); //active textures, glid - texture id, generated gl.gentexture() gl.bindtextures(0,3,new int[3]{grass.glid,grass_normal.glid,stone.glid}); //set shaders uniform, diffuse shader class diffuse.setuniform("maintexture",grass.glid); diffuse.setuniform("gnormalmap",grass_normal.glid); diffuse.setuniform("stonetexture",stone.glid); //just render code, example gl.bindvertexarray(mesh.vao); gl.drawelements(primitivetype.triangles, mesh.triangles.count,drawelementstype.unsignedint, intptr.zero); //unbind 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});
there shader fragment program:
#version 140 uniform sampler2d maintexture;//grass uniform sampler2d gnormalmap;//grass normal uniform sampler2d stonetexture;//stone out vec4 fragcolor; //hide other uniforms , 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, want mix textures later //i don't know why, there no difference, //if use stone - 'tex = x', or grass - 'tex = y' //in both cases, grass displayed fragcolor = color*tex*(diffuse+ambient);//color }
i tried first bind stone grass, , it's displayed stone without grass. mean this:
gl.bindtextures(0,3,new int[3]{stone.glid,grass_normal.glid,grass.glid});
there no effect, 1 texture displayed.
gl.bindtextures(0,3,new int[3]{grass.glid,grass_normal.glid,stone.glid});
if function makes sense, means glid
fields opengl object names.
which means:
diffuse.setuniform("maintexture",grass.glid); diffuse.setuniform("gnormalmap",grass_normal.glid); diffuse.setuniform("stonetexture",stone.glid);
this code not make sense.
sampler uniform values not actual opengl texture names. after all, if were, there no need bind them context.
sampler uniforms set texture unit indices. thus, sampler fetch whatever texture bound given texture unit. since glbindtextures
call used texture image units [0,2], uniform setting should reflect that:
diffuse.setuniform("maintexture", 0); diffuse.setuniform("gnormalmap", 1); diffuse.setuniform("stonetexture", 2);
my guess why seemed work 1 case got lucky. many opengl implementations return texture names starting 1. got texture name happened match texture unit bound texture to.
Comments
Post a Comment