|
本帖最后由 oirrm 于 2014-9-23 00:18 编辑
- #include "stdafx.h"
- ;
- #define BM 0xff //255
- #define N 0x1000 //4096
- #define B 0x100 //256
- int b00, b10, b01, b11;
- int bx0, bx1, by0, by1, bz0, bz1;
- float rx0, rx1, ry0, ry1, rz0, rz1;
- float sx,sy,sz;
- float t,u,v,a,b,c,d;
- float* q;
- float vec[3];
- static int p[ B + B + 2];
- static float g3[B + B + 2][3];
- static float g2[B + B + 2][2];
- static float g1[B + B +2];
- int i,j;
- #define lerp(t, a, b) ( a + t * (b - a) )
- #define s_curve(t) ( t * t * (3. - 2. * t) )
- #define setup(i,b0,b1,r0,r1)\
- t = vec[i] + N;\
- b0 = ((int)t) & BM;\
- b1 = (b0+1) & BM;\
- r0 = t - (int)t;\
- r1 = r0 - 1.;
- #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
- int _tmain(int argc, _TCHAR* argv[])
- {
- setup(0, bx0, bx1, rx0, rx1); //0,1,0,-1
- setup(1, by0, by1, ry0, ry1); //0,1,0,-1
- setup(2, bz0, bz1, rz0, rz1); //0,1,0,-1
-
- i = p[bx0];
- j = p[bx1];
-
- b00 = p[i+by0];//得到四个随机值
- b10 = p[j+by0];
- b01 = p[i+by1];
- b11 = p[j+by1];
-
- sx = s_curve(rx0);
- sy = s_curve(ry0);
- sz = s_curve(rz0);
- q = g3[b00 + bz0];u = at3(rx0, ry0, rz0);//将(bx0,by0,bz0)处的梯度与中间点到该点的向量(rx0,ry0,rz0)相乘
- q = g3[b10 + bz0]; v = at3(rx1, ry0, rz0);//将(bx1,by0,bz0)处的梯度与中间点到该点的向量(rx1,ry0,rz0)相乘
- a = lerp(sx, u, v);
-
- q = g3[b01 + bz0]; u = at3(rx0, ry1, rz0);//将(bx0,by1,bz0)处的梯度与中间点到该点的向量(rx0,ry1,rz0)相乘
- q = g3[b11 + bz0];v = at3(rx1, ry1, rz0);//将(bx1,by1,bz0)处的梯度与中间点到该点的向量(rx1,ry1,rz0)相乘
- b = lerp(sx, u, v);
-
- c = lerp(sy, a, b);
- q = g3[b00 + bz1];u = at3(rx0, ry0, rz1);//将(bx0,by0,bz1)处的梯度与中间点到该点的向量(rx0,ry0,rz1)相乘
- q = g3[b10 + bz1];v = at3(rx1, ry0, rz1);//将(bx1,by0,bz1)处的梯度与中间点到该点的向量(rx1,ry0,rz1)相乘
- a = lerp(sx, u, v);
-
- q = g3[b01 + bz1];u = at3(rx0, ry1, rz1);//将(bx0,by1,bz1)处的梯度与中间点到该点的向量(rx0,ry1,rz1)相乘
- q = g3[b11 + bz0];v = at3(rx1, ry1, rz1);//将(bx1,by1,bz1)处的梯度与中间点到该点的向量(rx1,ry1,rz1)相乘
- b = lerp(sx, u, v);
-
- d = lerp(sy, a, b);
-
- lerp(sz, c, d);//进行三次线性插值
- getchar();
- return 0;
- }
-
复制代码 |
|