It is currently Thu Nov 21, 2019 8:09 am


All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: marching cubes normals all messed up?
PostPosted: Sun May 11, 2014 12:54 am 

Joined: Sun Apr 13, 2014 11:45 pm
Posts: 8
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 5189 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 5189 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 5189 times ]


Last edited by seshbot on Sun May 11, 2014 1:13 am, edited 1 time in total.

Top
Offline Profile  
Reply with quote  
 Post subject: Re: marching cubes normals all messed up?
PostPosted: Sun May 11, 2014 1:02 am 

Joined: Sun Apr 13, 2014 11:45 pm
Posts: 8
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 5188 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 5188 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 5188 times ]


Top
Offline Profile  
Reply with quote  
 Post subject: Re: marching cubes normals all messed up?
PostPosted: Sun May 11, 2014 1:37 am 

Joined: Sun Apr 13, 2014 11:45 pm
Posts: 8
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 5188 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 5188 times ]


Top
Offline Profile  
Reply with quote  
 Post subject: Re: marching cubes normals all messed up?
PostPosted: Sun May 11, 2014 4:09 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
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


Top
Offline Profile  
Reply with quote  
 Post subject: Re: marching cubes normals all messed up?
PostPosted: Mon May 12, 2014 7:16 am 

Joined: Sun Apr 13, 2014 11:45 pm
Posts: 8
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)


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net