Hello Guest

Texture splatting

  • 5 Replies
  • 10973 Views
Texture splatting
« on: June 03, 2009, 11:12:24 »
Hi Guys
I'd like to texture a terrain. To get a nice result, i thought about texture splatting. What i already have is this:

It's a big alphamap an a grastile, put on the terrain using multitexturing. Now what i need is to get the black replaced with a rock texture.
Does anybody know how to do this?

*

Kai

Re: Texture splatting
« Reply #1 on: June 03, 2009, 12:33:01 »
Hi,

some google'ing around brought the following code snippet [http://wiki.delphigl.com/index.php/Texture_Splatting]:
Quote
GL.Color4d(1, 1, 1, 0);
GL.BindTexture(GL.TEXTURE_2D, tex1);
RenderQuad();
GL.Enable(GL.BLEND);
GL.Color4d(0, 0, 0, 1);
GL.BindTexture(GL.TEXTURE_2D, alpha2);
GL.BlendFunc(GL.ONE, GL.ONE);
RenderQuad();
GL.Color4d(1, 1, 1, 0);
GL.BindTexture(GL.TEXTURE_2D, tex2);
GL.BlendFunc(GL.DST_ALPHA, GL.ONE_MINUS_DST_ALPHA);
RenderQuad();
It renders the terrain mesh (here: RenderQuad()) multiple times with applied framebuffer blending.

As noted there, using a fragment shader for this work would be the faster alternative if your terrain rendering is expensive (many vertices), because then you only have to render it once and sample the two diffuse textures as well as the alpha channel of the masking texture and mix them together.

Re: Texture splatting
« Reply #2 on: June 03, 2009, 13:42:45 »
Yes i saw the solution from dlgwiki before, but i want to avoid double-rendering the terrain.
Isn't there a simple other solution without shaders?

*

Kai

Re: Texture splatting
« Reply #3 on: June 03, 2009, 13:52:20 »
Quote
Isn't there a simple other solution without shaders?
None that I know of.
But using shaders for that is not that hard and may look cleaner than struggling with the fixed-function-state-changing methods.
I would encourage everyone to use shaders, if I could, since they are so powerful and concise :-).
Sure, you have to build some (little) code in Java for them to work but then you can do everything that comes to your mind.
For your terrain renderer, you might then also want to use bump mapping and per-pixel lighting and such things, that can only be done with shaders.

Please have a look at http://www.lighthouse3d.com/opengl/glsl/index.php?minimal for an introduction to GLSL and some code samples.

Re: Texture splatting
« Reply #4 on: June 03, 2009, 14:59:42 »
Thanks for the link, i'll have a look at it.
Hope there are somewhere simple lwjgl examples.

Or could you think about an other approach for texturing terrain?

Re: Texture splatting
« Reply #5 on: June 19, 2009, 23:56:42 »
I almost got it to work now, but i have a strange error.
I can mix 2 textures and everything works fine.
As soon as i mix the third texture, the rest of the scene (plants etc) gets messed up, while terrain is fine. All the other Textures then get replaced by the alphamap  ???
Looks like this:

Shader:
Code: [Select]
uniform sampler2D Alpha;
uniform sampler2D Forest;
uniform sampler2D Grass;
uniform sampler2D Rock;

void main(void){
   vec4 alpha = texture2D(Alpha, gl_TexCoord[0].xy);
   vec4 tex0 = texture2D(Forest, gl_TexCoord[1].xy);
   vec4 tex1 = texture2D(Grass, gl_TexCoord[1].xy);
   vec4 tex2 = texture2D(Rock, gl_TexCoord[1].xy);

   tex0 *= alpha.r;
   tex1 = mix(tex0, tex1, alpha.g);
   vec4 outColor = mix(tex1, tex2, alpha.b);

   gl_FragColor = outColor;
}

Anybody knows some help?

/edit: working now, just some multitexturing issue...
« Last Edit: June 20, 2009, 01:18:23 by Rainer »