Sign in to follow this  

Having a bigger voxel density in a sphere

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a very simple Voxel Raytracer for fun. The voxels are stored in a 3d array and here is the code that stores them to create a sphere.

 if ((Math.Pow((cx - 200), 2) + (Math.Pow((cy - 140), 2)) + (Math.Pow((i - 10), 2)) == Math.Pow(50, 2)))
                        {
                           
                                Map[cx+d, cy+d, i+d] = 2;
                        
                        }

[attachment=30289:sphere.png]

 

However, the sphere seems to be composed of an extremely tin pixel density. I suppose this has something to do with the pixels being integers. It still gives a nice effect but i was wondering if anyone here had any idea to improve the pixel density on the surface of the sphere.

 

PS: also do you know any resources on how to fill the surface of certain prism via general equations ? I can't seem to find any.

Edited by FFA702

Share this post


Link to post
Share on other sites

Maybe try doing something like this?

This will produce a solid voxel-sphere.. I think your issues is because you do a == compare, and when dealing with integers the radius from (cx, cy, i..) won't hit the radius (50) at every point on the edge, and thus you get holes in it :)

BTW: doing a x * x instead of pow(x, 2) will probably be faster :)

for (int z = -radius; z <= radius; z++)
{
   for (int y = -radius; y <= radius; y++)
   {
      for (int x = -radius; x <= radius; x++)
      {
         int dist = (x * x + y * y + z * z);
         if (dist <= radius_squared)    // radius_squared = radius * radius
            Map[cx + x, cy + y, cz + z] = 2;
      }
   }
}

Share this post


Link to post
Share on other sites

You could also check whether the individual voxels intersect the sphere surface, instead of whether their center is exactly on the surface.

 

For that youd need to calculate the furthest and closest point of the voxel from the sphere.

I dont know what the best way to do that is assuming a cube-shaped voxel, but for quick results you could just assume theyre spheres, which means just checking that the center is within radius +- some constant from the sphere center (where the constant is the voxel radius).

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this