2011-11-18 79 views
0

我正在学习android renderscript,目前正在寻找旋转木马示例。这里,多次使用称为“正常化”的功能。例如:正常化函数渲染脚本

float3 eye, float3 center; 
float3 f = normalize(center - eye); 

我找不到这个函数的意思和作用。我也学习了一些OpenGl ES 2.0,并且遇到了使用normalize标志但从未使用过的函数(该标志通常是 - false,因此它执行了类似将非浮点值赋值为float的操作)。因此,如果某人可以给我一个很好的解释,我将不胜感激。

此外,我需要将大部分代码从renderscript移植到opengl es 2.0中,所以请记住,我也必须在java中使用这个函数。 (也许写它?)Thx!

回答

0

我已经设法实现标准化函数,用于标准化3d矢量。为了正常化,您需要将矢量(x,y和z)的每个值与其大小相除。 下面是代码:

private static float[] normalize(float[] _vector){ 
float magnitude; 
magnitude = (float)(Math.sqrt(_vector[0]*_vector[0] + _vector[1]*_vector[1] + _vector[2]*_vector[2])); 
_vector[0] = _vector[0]/magnitude; 
_vector[1] = _vector[1]/magnitude; 
_vector[2] = _vector[2]/magnitude; 

return new float[]{_vector[0], _vector[1], _vector[2]}; 

} 
0

我不确定RenderScript,但是在GLSL中normalize(x)返回的方向与x相同,但是单位长度(长度为1)。

通常规范化意味着将某个值转换为某个范围。例如在Time.normalize(bool)

+0

如何实现这是Java?因为我正在编写使用OpenGL的Android应用程序.. – Sandra

0

出现小幅回调到公式中接受的答案:

private static float[] normalize(float[] _vector){ 
float magnitude; 
magnitude = (float)(Math.sqrt(_vector[0]*_vector[0] + _vector[1]*_vector[1] + _vector[2]*_vector[2])); 
_vector[0] = _vector[0]/magnitude; 
_vector[1] = _vector[1]/magnitude; 
_vector[2] = _vector[2]/magnitude; 

return new float[]{_vector[0], _vector[1], _vector[2]}; 
}