如何栅格化球体 [英] How to Rasterize a Sphere

查看:168
本文介绍了如何栅格化球体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个在其外部具有块"的球体,就像在Minecraft中构建的一样. (我不知道圆外面的术语是什么).问题是,我不知道如何获得像中点圆算法这样的方程来为球体工作.最好是在lua或java中,这样我可以更轻松地阅读任何答案.而且我不想知道如何使用Trig计算球体上的点,我已经知道该怎么做.

So, I am trying to create a sphere that has "blocks" on its outside, kinda like as if built in Minecraft. (I don't know what the terminology for the outside of a circle is). The problem is, I can't figure out how to get an equation like the Midpoint Circle Algorithm to work for a sphere. Preferably in lua or java so I can read any answers easier. And I don't want to know how to calculate a point on the sphere with trig, I already know how to do that.

推荐答案

我认为最简单的方法是将Midpoint Circle算法扩展到3D.

I think the easiest way to do this is something like the Midpoint Circle Algorithm, extended to 3D.

首先,让我们找出我们要填充的块.假设原点位于块(0,0,0)和半径R的中间:

First, lets figure out which blocks we want to fill. Assuming an origin in the middle of block (0,0,0) and radius R:

  1. 我们只想填充球体内的框.这些恰好是(x,y,z)框,例如x²+y²+z² <= R²;和
  2. 我们只想用表情填充框.如果一个盒子上有一张面孔,那么它的至少一个邻居不在球体中,所以不是,所以:(|x|+1)²+y²+z² > R² OR x²+(|y|+1)²+z² > R² OR x²+y²+(|z|+1)² > R²
  1. We only want to fill boxes inside the sphere. Those are exactly the boxes (x,y,z) such that x²+y²+z² <= R²; and
  2. We only want to fill boxes with a face showing. If a box has a face showing, then at least one of its neighbors is not in the sphere, so: (|x|+1)²+y²+z² > R² OR x²+(|y|+1)²+z² > R² OR x²+y²+(|z|+1)² > R²

这是使它变得棘手的第二部分,但请记住(|a|+1)² = |a|² + 2|a| + 1.例如,如果z是球体内的框的最大坐标,并且如果该框具有显示的面,则将特别显示z面,因为x²+y²+(|z|+1)² = x²+y²+z²+2|z|+1,并且至少与xy的类似值一样大.

It's the 2nd part that makes it tricky, but remember that (|a|+1)² = |a|² + 2|a| + 1. If, say, z is the largest coordinate of a box that is inside the sphere, and if that box has a face showing, then the z face in particular will be showing, because x²+y²+(|z|+1)² = x²+y²+z²+2|z|+1, and that will be at least as big as the analogous values for x and y.

因此,很容易计算以下框:1)球体内部的框,2)将z作为其最大坐标,以及3)具有最大可能的z值,即将1加到z的结果中球外的盒子.此外,4)对所有x,y,z都具有正值.

So, it's pretty easy to calculate the boxes that are 1) inside the sphere, 2) have z as their largest coordinate, and 3) have the largest possible z value, i.e., adding 1 to z results in a box outside the sphere. Additionally, 4) have positive values for all x,y,z.

然后可以用24种不同的方式反映这些框的坐标,以在球体表面上生成所有框.这些都是坐标符号的8个组合乘以坐标轴最大的所有3个选择.

The coordinates of these boxes can then be reflected 24 different ways to generate all the boxes on the surface of the sphere. Those are all 8 combinations of signs of the coordinates times all 3 choices for which axis has the largest coordinate.

以下是生成具有最大x,y,zz正数的点的方法:

Here's how to generate the points with positive x,y,z and z largest:

maxR2 = floor(R*R);
zx = floor(R);
for (x=0; ;++x)
{
    //max z for this x value.
    while(x*x+zx*zx > maxR2 && zx>=x)
        --zx;
    if (zx<x)
        break; //with this x, z can't be largest

    z=zx;
    for(y=0; ;++y)
    {
        while(x*x+y*y+z*z > maxR2 && z>=x && z>=y)
            --z;
        if (z<x||z<y)
            break; //with this x and y, z can't be largest
        FILLBOX(x,y,z); //... and up to 23 reflections of it
    }
}

注意:如果对您来说很重要,请在计算反射时不要画图,例如(0,y,z) (-0,y,z),因为这是同一框的两次.另外,请勿交换具有相同值的轴,因为这将再次绘制同一框两次(例如,如果您具有(1,5,5),则不要交换yz并再次绘制.

NOTE: If it matters to you, be careful when you calculate the reflections so that you don't draw, for example, (0,y,z) and (-0,y,z), because that's the same box twice. Also don't swap axes with the same value, because again that would draw the same box twice (e.g., if you have (1,5,5), don't swap y and z and draw again.

请注意,R也不必是整数.如果添加0.5,它会看起来更好一些.

NOTE ALSO that R doesn't have to be an integer. It'll look a little nicer if you add 0.5 to it.

这是一个考虑到以上所有情况的示例(您需要支持webgl的浏览器) https: //jsfiddle.net/mtimmerm/ay2adpwb/

Here's an example that takes all of the above into account (you need a browser that supports webgl) https://jsfiddle.net/mtimmerm/ay2adpwb/

这篇关于如何栅格化球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆