Texture Blending

I am developing a landscape editor in JOGL. I can place the textures in the scene but there is a sharp line where two textures join. I want the edge of the textures to blend together.

Can someone please suggest a method to do this?

That’ll depend heavily on what your landscape looks like (eg. heightmap vs. model), how you’re texturing it (procedural vs. tiles) and how your rendering it (multipass vs. texture spatting vs. megatexture). Basically you’ll have to give us a lot more info if you want a useful answer.

These days I’d suggest looking at either texture splatting or megatexture approaches if you want nice results.

My texture code:

Texture rock;
Texture grass;

rock = TextureIO.newTexture(new File(“rock.jpg”),false);
grass = TextureIO.newTexture(new File(“grass.jpg”),false);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

//Drawing quad with rock texture
gl.glEnable(GL.GL_TEXTURE_2D);
TextureCoords rockCoords = rock.getImageTexCoords();
rock.bind();
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(rockCoords.left(), rockCoords.top());
gl.glVertex3d(0,0,0);
gl.glTexCoord2f(rockCoords.right(), rockCoords.top());
gl.glVertex3d(1,0,0);
gl.glTexCoord2f(rockCoords.right(), rockCoords.bottom());
gl.glVertex3d(1,1,0);
gl.glTexCoord2f(rockCoords.left(), rockCoords.bottom());
gl.glVertex3d(0,1,0);
gl.glEnd();
gl.glDisable(GL.GL_TEXTURE_2D);

//Drawing quad with grass texture
gl.glEnable(GL.GL_TEXTURE_2D);
TextureCoords grassCoords = grass.getImageTexCoords();
grass.bind();
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(grassCoords.left(), grassCoords.top());
gl.glVertex3d(1,0,0);
gl.glTexCoord2f(grassCoords.right(), grassCoords.top());
gl.glVertex3d(2,0,0);
gl.glTexCoord2f(grassCoords.right(), grassCoords.bottom());
gl.glVertex3d(2,1,0);
gl.glTexCoord2f(grassCoords.left(), grassCoords.bottom());
gl.glVertex3d(1,1,0);
gl.glEnd();
gl.glDisable(GL.GL_TEXTURE_2D);

I want to blend the rock and grass together where the textures touch

Ugh, so much wrong with this.

  1. Use tga or png for textures, as jpeg artifacts are horribly obvious when on textures. Alternatively if you want compression use the various DXT formats.
  2. Don’t use quads, you’re just making extra work for the driver as they’ll be split into two tris anyway. Use indexed triangle lists.
  3. Don’t use immediate mode, use Vertex Buffer Objects (VBOs) or at least vertex arrays.
  4. Don’t submit your geometry as doubles since it’ll get converted to float precision anyway, all you’re doing is wasting bandwidth and adding extra processing time.
  5. Don’t draw every tile in it’s own batch, you’ll kill the performance.
  6. Pack your tiles into a single texture so you can submit the whole terrain in one batch.

If you just want to hardcode a transition you could just add some extra geometry and do the cross fade by setting the vertex alpha as appropriate, but it’d be pretty inefficient. I’d again suggest that you google “texture splatting”, but I suspect you’re not going to bother.

/hugs Orangy Tang

Thanks for your advice, I have researched texture splatting since you mentioned it.

What do you mean by “Don’t draw every tile in it’s own batch” and “Pack your tiles into a single texture”?

Look into VBOs and this should become apparent. You want to draw your entire terrain in one call. All your vertices and texture coords should be in one big buffer. This means all your texture coords will reference the same texture, so you need a single texture that has all your tiles.

Thanks for the explanation :slight_smile:

+1

I am myself learning openGL and now I’m trying to optimize my code, this info will surely help .

I don’t really get it. All your points are valid, but he has a problem, that is hard enough in itself (at least requiring multipass, multitexturing with register combiners or shaders).

You just exploded the magnitude of his problem. I expect he’ll have a lot of work on each and every step, and step 6 invalidates GL_REPEAT which he uses, multiple images in one texture only really works for sprites, not terrain tiles. Worst: after he did all that work, he will face exactly the same problem: the surface types don’t have smooth transitions. My advise would be: give multi-pass a try. Don’t care about performance, first try to achieve the results you’re after.

‘Make it work, make it faster.’

Everything else is premature optimization, making it much harder for yourself.

thanks for the advice Riven, I was just looking for techniques to research and included the code to try to further explain the situation. I’ve got some smooth transitions for the edges from the advice given. I’ll do some code optimisation next.

Im a female btw :stuck_out_tongue:

Good to hear. Once you get comfortable with OpenGL I’d advice you to skip all the ‘old’ ways, and start toying with shaders as soon as possible.

‘he’ was obviously a typo, normally I always refer to programmers as females. :persecutioncomplex:

Can you suggest any good websites to look at or books to buy?

http://www.lighthouse3d.com/opengl/glsl/index.php?shaders