Distorted textures with customized HeightBasedTerrain material

Hi,

I want to texture a spherical terrain with the HeightBasedTerrain Material. So I customized it a little for my needs. I made an own material definition and changed two lines of the HeightBasedTerrain.frag :

line 19:
[java]
//float height = position.y;
float height = sqrt((position.x * position.x) + (position.y * position.y) + (position.z * position.z));[/java]

line 70
[java]return (blend.y * terrainColor + blend.x * terrainColor + blend.z * terrainColor);[/java]

The octahedron from which the sphere is build, already has distorted textures. The upper and lower triangles are okay. On the sides you see the distortion. Here is a picture:
<br />
<br />

<br /><br />
These are the initial buffer values. Here the vertex positions:

[java] //Intial vertex positions.
vertPositions[0][0] = new Vector3f(0, 0, 1);
vertPositions[0][1] = new Vector3f(1, 1, 0);
vertPositions[0][2] = new Vector3f(-1, 1, 0);
vertPositions[1][0] = new Vector3f(0, 0, 1);
vertPositions[1][1] = new Vector3f(1, 1, 0);
vertPositions[1][2] = new Vector3f(1, -1, 0);
vertPositions[2][0] = new Vector3f(0, 0, 1);
vertPositions[2][1] = new Vector3f(1, -1, 0);
vertPositions[2][2] = new Vector3f(-1, -1, 0);
vertPositions[3][0] = new Vector3f(0, 0, 1);
vertPositions[3][1] = new Vector3f(-1, -1, 0);
vertPositions[3][2] = new Vector3f(-1, 1, 0);
vertPositions[4][0] = new Vector3f(0, 0, -1);
vertPositions[4][1] = new Vector3f(1, 1, 0);
vertPositions[4][2] = new Vector3f(-1, 1, 0);
vertPositions[5][0] = new Vector3f(0, 0, -1);
vertPositions[5][1] = new Vector3f(1, 1, 0);
vertPositions[5][2] = new Vector3f(1, -1, 0);
vertPositions[6][0] = new Vector3f(0, 0, -1);
vertPositions[6][1] = new Vector3f(1, -1, 0);
vertPositions[6][2] = new Vector3f(-1, -1, 0);
vertPositions[7][0] = new Vector3f(0, 0, -1);
vertPositions[7][1] = new Vector3f(-1, -1, 0);
vertPositions[7][2] = new Vector3f(-1, 1, 0);[/java]

Here the intial values for the index buffer:

[java]
indexParts[0][0] = 0; indexParts[0][1] = 1; indexParts[0][2] = 2;
indexParts[1][0] = 1; indexParts[1][1] = 0; indexParts[1][2] = 2;
indexParts[2][0] = 2; indexParts[2][1] = 1; indexParts[2][2] = 0;
indexParts[3][0] = 1; indexParts[3][1] = 0; indexParts[3][2] = 2;
indexParts[4][0] = 1; indexParts[4][1] = 0; indexParts[4][2] = 2;
indexParts[5][0] = 0; indexParts[5][1] = 1; indexParts[5][2] = 2;
indexParts[6][0] = 0; indexParts[6][1] = 1; indexParts[6][2] = 2;
indexParts[7][0] = 0; indexParts[7][1] = 1; indexParts[7][2] = 2;[/java]

And this is how I compute the texture coordinates. I guess here must be the problem. I got the formula from the german wikipedia de.wikipedia.org/wiki/UV-Koordinaten. I also tried the formula from the english wikipedia en.wikipedia.org/wiki/UV_mapping. Same problem, might be just another notation? Not able to determine that :smiley:

[java]new Vector2f(vertPositions[i][0].getX() / vertPositions[i][0].length(), vertPositions[i][0].getY() / vertPositions[i][0].length());[/java]

I hope the two dimensional arrays are not too confusing. I split up the octahedron in to 8 meshes. Has someone an idea what could be wrong with my way of computing the texture coordinates?

Greetings

Or in other words, does someone know different formulars for computing uv coordinates on an uneven sphere? I have some trouble to find and figure out a more appropriate way to do that. Or maybe a good website or a collection of different approaches to find uv’s on a sphere?

Greetings

Look at mapping (cartography) and how they do it.

There are a lot of ways to translate from a 2d plane (the map) to a sphere (the world) but all involve either distortion or wasted space.

The uv coordinates are fine. I figured that I need to customize the HeightBasedTerrain.frag a little more in order to make it work for spherical terrain.