2011-11-18 80 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]}; 
}