opengl - Blending multiple textures in GLSL -
this long promise it's interesting. :)
i'm trying mimic appearance of application's texturing using jmonkeyengine. have list of vertices, , faces (triangles) making "landscape mesh" should textured 7-15 different textures (depending on terrain of "landscape"). each triangle has texture code associated it, signifying texture particular triangle should consist of. , of course, textures should blend smoothly between each face.
so i'm trying develop strategy allows (which not utilize pre-made alpha map png files, texture alphas need done @ run time). right figure if calculate "strength" of each texture @ each vertex (in vertex shader)--by factoring in terrain types of it's neighboring faces (unsure how yet)--i should able set alpha values based on how far pixel vertex. generated 'alpha map' used frag shader blend each texture per pixel.
is feasible, or should looking @ totally different strategy? have shader code application i'm trying mimic (but hlsl , i'm using glsl), seems they're doing blending step elsewhere:
sampler meshtexturesampler = sampler_state { texture = diffuse_texture; addressu = wrap; addressv = wrap; minfilter = linear; magfilter = linear; }; i'm not sure hlsl "meshtexturesampler" seems application may have pre-blended textures needed, , created single texture entire mesh based on face/terrain code data. in pixel/fragment shader seem this:
float4 tex_col = tex2d(meshtexturesampler, in.tex0); after it's shadows, lighting, etc -- no sort of texture blending @ far can tell, leads me believe texture blending work being done on cpu beforehand, suppose. suggestions welcome.
if understand correctly, here first shot be:
your problem is, more or less, how distribute your per-face value on vertices. similar normal generation on mesh: first generate normal each triangle, , calculate them per vertex. google "normal generation" , you'll there, here's gist. each adjacent triangle, find weighing factor (often angle of corner uses vertex, or surface area of triangle, or combination), , sum value (be normal or "strengths") multiplied weighing factor total result. normalize , you're done.
so have texture "strengths" can send vertex shader. modern solution use chars , sample texture array in pixel shader, after you've fudged blend values bit give nicer transfers.
so, if problem correctly :
preprocess:
forearch vertex in mesh vertexvalue = 0 normalization = 0 foreach adjacent triangle of vertex angle = calculateanglebetween3vertices(vertex,triangle.someothervertex,triangle.theotherothervertex) vertexvalue += triangle.value * angle normalization += angle vertexvalue/=normalization rendering time:
pipe value(s) of each vertex fragmentshader, , in fragment shader:
basecolour = 0; foreach value basecolour = mix(basecolour, texture2d(texturesamplerforthisvalue,uv), value) //this simple, better once have working or, alternatively, can take @ geometry. if have combination of big triangles , tiny ones, have unequal spread of data, , since data per vertex, have more detail more geometry. in case ,you want else doing , decouple texturing geometry using blend maps. these can low-resolution , shouldn't increase memory consumption or shader execution time much.
Comments
Post a Comment