My sample of Marching Cube Algorithm

http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
http://users.polytech.unice.fr/~lingrand/MarchingCubes/applet.html

Marching Cube algorithm is a traditional volume-rendering algorithm. The key of this algorithm is the matching judgment of the 15 meta-situations. Since these 15 cases are not variable so people tend to hard-code this information (actually most people will hard-code the whole 256 cases), which can also improve the runtime performance.

In my sample code, I just use the look up table from the first URL. It is a pity that there are no normals information. But the second URL offers the normal calculation method in Java, so that we can add lighting.

Let’s take a look at the core components of the program:

1. Geometry expression
I use a funtion vex_hit(GLfloat x, GLfloat y, GLfloat z) to judge when the 3D point is inside the geometry or not. Mathematically, if the calculated value is minus, the point is in the geometry, as f(X, Y, Z) = ((X-2)/2)^2 + Y^2 + (Z/2)^2 – 1. This is function is the most creative one in the whole program, you can use any valid 3D geometry maths expression here to generate more interesting shapes.

2. Vertex preprocessing
After indicating the marching volume and the unit cube size, what we need is to judge all the vertices by step 1. To eliminate calculation redundance, we can do the processing first before rendering. Consider this: if we handle the vertices cube by cube, all non-edge vertices will be calculated twice.

3. Unit cube case judgment
This is the most critical part of the whole program. By retrieving which vertices of one unit cube are IN the geometry, we can determine the specific case from the look-up table.

4. Normal calculation
To support lighting, we need to calculate the normal for each unit triangle. Since we know all three vertices of the triangle, we can generate the normal by using the cross multiplication. Actually I think it needs more consideration for some specific case. But just using this over-generalized method, the final effect is satisfactory.

5. Marching!
Draw all the triangles cube by cube. I think it is a O(n^3) process.

Take a look at a simple geometry:
((X-2)/2)^2 + Y^2 + (Z/2)^2 = 1 OR ((X+2)/2)^2 + Y^2 + (Z/2)^2 = 1
 

16 Division
16 Division
 
32 Division

32 Division

 

48 Division

48 Division

 

How about 180? It looks much better, but it takes a long time!

180 Division

 

180 Division with Mesh


Let’s have a look at some more interesting model:

Fun

Its geometric expression:

0.0260516*x*x*z*z + 0.028689*y*y + 0.0100072*z*z
- 0.057882*x*y + 0.0217817*x*z - 0.0187733*y*z
- 0.0812969*x + 0.086143*y + 0.00396285*z =0.04


TODO


Interpolation
Linear interpolation is enough for this rendering. We can put interpolation calculation on normals, also known as Vertices Merging. This technique will make the geometries much more smooth.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值