# OpenGL Per triangle normals generating..

This topic is 1932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there, i have small problem with normals for terrain generated from heightmap. I'm trying to generate normals for it, and its almost working, but i'm getting very strange result ( there are strange stripes everywhere ;f) :

Code for generation normals :

glm::vec3 x6ngine::cHeightMap::computeNormal(glm::vec3 v0, glm::vec3 v1, glm::vec3 v2)
{
return glm::normalize(glm::normalize(glm::cross(v2-v0,v1-v0)));
}


i dont know where's problem ;/ somewhere in opengl code?, or i'm just generating it wrong? Thanks for help. Regards x6itru.

##### Share on other sites

Normalizing 2 times is useless. You need to calculate vertex normals by assigning them an average of all surface normals of adjacent quads to not have those sudden changes.

##### Share on other sites

I've normalized it twice by accident thanks for help, it should look smoother with per vertex normals.

Edited by x6itru

##### Share on other sites

hello again ;/

I have really strange problem with vertex normals.

result :

code for compute vertex normal :

Result is very strange ;/ There are strange small "holes" - ofc they're from normals, terrain grid is smooth, so normals should be smooth too. ;/

Edited by x6itru

##### Share on other sites

Looks about right. Terrain with smooth normals and not per-pixel normals will still not look perfectly smoothed.

you are setting sum to 0 somewhere else I assume? Math looks fine.

##### Share on other sites

Yes of course i'm setting sum to 0, can you tell me something more about per-pixel normals(almost nothing in google about that) or any other way to make it smooth?

##### Share on other sites

Hello

f0 to f5 must be normalized before averaging them to get v0Normal.

Even if the points are aligned on x and z axes, the triangles have generally differents areas, so the norms of f0 to f5 are generally not the same.

( according to the cross product properties, the norm of f0 is 2x the area of the triangle (v0,v1,v2) )

f0=normalize(crossProduct(v1-v0,v2-v0))
.
.
.

v0Normal=(f0+f1+f2+f3+f4+f5)/6

should work better

I noticed a confusion between your formulas and your scheme, for example according to your scheme f0 should be the normal of the triangle (v0,v1,v6), not (v0,v1,v2). Hoping this is just wrong typo ?

Edited by Tournicoti

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 17
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631760
• Total Posts
3002173
×