Search code examples
dictionaryopenglheightterrainnormals

Normals of height map dont work


Iam trying to implement normals for my height map but they dont seems to work.

Look at these: Normals on slope

enter image description here

enter image description here

Note that the pattern occurs along the edges. Why? Vertices are shared (indexing) and normals are average for vertex from all triangles that vertex is part of.

Algorithm for normals looks like that:

 float size=Size;
  int  WGidY=int(gl_WorkGroupID.y);
  int  WGidX=int(gl_WorkGroupID.x);

    vec4 tempVertices[3];
    tempVertices[0]=imageLoad(HeightMap, ivec2(WGidX, WGidY));
    tempVertices[1]=imageLoad(HeightMap, ivec2(WGidX, WGidY+1));
    tempVertices[2]=imageLoad(HeightMap, ivec2(WGidX+1, WGidY));
    vec4 LoadedNormal=imageLoad(NormalMap, ivec2(WGidX, WGidY));
    vec4 Normal=vec4(0.0f);
    Normal.xyz=cross((tempVertices[0].xyz-tempVertices[1].xyz),  (tempVertices[0].xyz-tempVertices[2].xyz));
    Normal.w=1; 
    imageStore(NormalMap, ivec2(WGidX,WGidY),          Normal+LoadedNormal);

Solution

  • ok guys, I found a problem. This is symptom of "greedy triangulation". The normals inside a triangle are interpolated by barycentric algorithm but the edges are interpolated linearly to prevent color differences between adjacting triangles. Thank you, again, Paul Bourke: http://paulbourke.net/texture_colour/interpolation/

    If you dont have enough triangles dont use Phong Shading (maybe normal mapping?). After tweaks:

    http://prntscr.com/dadrue

    http://prntscr.com/dadtum

    http://prntscr.com/dadugf