Volumes Of Fun
http://www.volumesoffun.com/phpBB3/

marching cubes normals all messed up?
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=15&t=588
Page 1 of 1

Author:  seshbot [ Sun May 11, 2014 12:54 am ]
Post subject:  marching cubes normals all messed up?

Hi,

I am pretty much using the sample code to generate a marching cubes mesh, and disabled normal interpolation in my shader (using 'flat' vars).

I have attached three images (inline at the end of this post):
- the 'cubic' mesh generator,
- the 'marching cubes' generator with flat normal interpolation, and
- the 'marching cubes' generator with smooth normal interpolation (same as the samples)

The issue can be seen in pic 2 where the dark regions meet the light regions - the definition of the vertex normals at the edges become confused and lighting calculations seem to get very weird.

Is this a known issue? I dont think its caused by my code because it looks like the samples if I use smooth interpolation...

Thanks,

Attachment:
File comment: cubic mesh
Screen Shot 2014-05-11 at 9.33.41 AM.png
Screen Shot 2014-05-11 at 9.33.41 AM.png [ 78.61 KiB | Viewed 7163 times ]

Attachment:
File comment: marching cubes mesh with 'flat' normal interpolation
Screen Shot 2014-05-11 at 9.34.32 AM.png
Screen Shot 2014-05-11 at 9.34.32 AM.png [ 144.12 KiB | Viewed 7163 times ]

Attachment:
File comment: marching cubes with default normal interpolation
Screen Shot 2014-05-11 at 9.48.10 AM.png
Screen Shot 2014-05-11 at 9.48.10 AM.png [ 242.45 KiB | Viewed 7163 times ]

Author:  seshbot [ Sun May 11, 2014 1:02 am ]
Post subject:  Re: marching cubes normals all messed up?

Ive attached three more images showing a particularly stark problem - notice the dark triangles in the center seem to be floating out in space?

The problem seems to be disguised by the blurriness introduced by using the interpolated normals (pic 3)

Attachment:
File comment: cubic
Screen Shot 2014-05-11 at 9.57.23 AM.png
Screen Shot 2014-05-11 at 9.57.23 AM.png [ 46.4 KiB | Viewed 7162 times ]

Attachment:
File comment: flat normals marching cubes
Screen Shot 2014-05-11 at 9.57.41 AM.png
Screen Shot 2014-05-11 at 9.57.41 AM.png [ 85.63 KiB | Viewed 7162 times ]

Attachment:
File comment: interpolated normals marching cubes
Screen Shot 2014-05-11 at 10.00.48 AM.png
Screen Shot 2014-05-11 at 10.00.48 AM.png [ 146.61 KiB | Viewed 7162 times ]

Author:  seshbot [ Sun May 11, 2014 1:37 am ]
Post subject:  Re: marching cubes normals all messed up?

one last pair of images illustrating the normals - the second image is the normals rendered directly in the fragment shader
Attachment:
File comment: marching cubes
Screen Shot 2014-05-11 at 10.35.45 AM.png
Screen Shot 2014-05-11 at 10.35.45 AM.png [ 114.45 KiB | Viewed 7162 times ]

Attachment:
File comment: marching cubes normal render
Screen Shot 2014-05-11 at 10.36.03 AM.png
Screen Shot 2014-05-11 at 10.36.03 AM.png [ 98.58 KiB | Viewed 7162 times ]

Author:  David Williams [ Sun May 11, 2014 4:09 pm ]
Post subject:  Re: marching cubes normals all messed up?

I don't think this is a bug - I think you just shouldn't use the surface normals in this way. The normals are computed per-vertex and do not match any of the faces, instead they are roughly the average of their surrounding faces.

Consider the diagram below (ignore the text, I just found it on another website):

Image

The purple arrows are the per-vertex normals, whereas the red ones are the interpolated normls. I've never used OpenGL 'flat' interpolation but you are basically telling the system to use the first vertice's normal as the normal for the whole triangle. So depending how the triangles are defined, the image above may end up using the center normal for both triangles (making the shape appear flat) or some other configuration.

You might get more predicatble behaviour if you know what order the vertices are defined in (PolyVox may not define this in a predicatable way), but it still it cannot end up looking correct as none of the nomals match the faces.

So what is the solution? If you want the flat shaded look then I would suggest ignoring the normals completly and instead computing them in the fragment shader. This is what we do when using the CubicSurfaceExtractor as in that case normals aren't even generated. Have a look at the BasicExample (you'll want a recent version of the develop branch) and also at the documentation here: http://www.volumesoffun.com/polyvox/doc ... bic-meshes

Author:  seshbot [ Mon May 12, 2014 7:16 am ]
Post subject:  Re: marching cubes normals all messed up?

thanks your advice worked well.

Note that on my machine the code on the page you linked to does not do the correct thing. I believe this is driver specific.

vec3 worldNormal = cross(dFdy(inWorldPosition.xyz), dFdx(inWorldPosition.xyz));

should be changed to:

vec3 fdx = vec3(dFdx(vPosition_world.x), dFdx(vPosition_world.y), dFdx(vPosition_world.z));
vec3 fdy = vec3(dFdy(vPosition_world.x), dFdy(vPosition_world.y), dFdy(vPosition_world.z));
vec3 worldNormal = cross(fdx, fdy);

(I got that from http://stackoverflow.com/questions/2027 ... ectly-in-a)

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/