Mesh normals not changing or messed up after altering mesh?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
panto
Posts: 22
Joined: Thu Mar 17, 2011 5:44 pm

Mesh normals not changing or messed up after altering mesh?

Post by panto »

Hi,

So I wrote a basic terrain editor. My terrain has a shader which uses normals (NORMAL semantic) to calculate lighting and the texture blending. However, whenever I alter the mesh's vertices's positions manually, then the shader won't update the lighting/texture on the mesh itself until I've completely deleted the mesh and re-loaded it.

Example below.
1. I begin editing the terrain mesh: http://img23.imageshack.us/img23/4955/ss23742013.png .
2. I finish editing my terrain mesh, but the shader won't "update" the mesh. I save the terrain as a heightmap: http://img27.imageshack.us/img27/7295/ss23754945.png .
3. I re-load the terrain scenenode completely from the new heightmap and voila, this is what I see: http://img828.imageshack.us/img828/2070/ss23769729.png .

How would I solve it? I scoured over the forums and I've tried the meshBuffer->setDirty() method and also sceneManager->getMeshManipulator()->recalculateNormals( meshBuffer ) but neither of those seem to work. :?
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Re: Mesh normals not changing or messed up after altering me

Post by shadowslair »

Just a quick guess, but you can try calling

Code: Select all

terrainNode->setScale(terrainNode->getScale());
after you`ve altered the mesh in order to force normals recalculation. If it works this way, you can call it only if a key/button was pressed or sth alike, as it may be a bit costly operation to call frequently.
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
panto
Posts: 22
Joined: Thu Mar 17, 2011 5:44 pm

Re: Mesh normals not changing or messed up after altering me

Post by panto »

Thanks, it worked. It is quite costly. For now I've figured out an alternate (call setScale only once I'm doing editing vertices).

However, why doesn't sceneManager->getMeshManipulator()->recalculateNormals( meshBuffer); work? It doesn't seem to be as costly as setScale.
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Re: Mesh normals not changing or messed up after altering me

Post by shadowslair »

Shortly said, its because it operates the buffer in a bit different way, which in your case doesn`t do what you need as you have discovered yourself. Note that after you edit the vertices you call setPosition() on the terrain to apply the changes to the terrain (you`re using the example from the code snippets), you don`t need to call both setPosition() and setScale() or it will be even slower. If you call setScale() the current frame, make sure you don`t call setPosition() as well, because you don`t really need to.
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
Post Reply