I am learning android renderscript and currently looking at the carousel example. Here, a function called "normalize" is used many times. For example:

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

I can't find what this function mean and does. I was learning a bit OpenGl ES 2.0 as well and came across functions that use normalize flag but never used one (the flag was usually - false so it did something like casting a non-float value to float) .. So if someone can give me a good explanation, i would appreciate it.

Also, i need to port most of the code from renderscript to opengl es 2.0 so keep in mind that i would have to use this function in java as well. (and maybe write it?) Thx!

有帮助吗?

解决方案 3

I've managed to implement the normalize function, for normalizing a 3d vector. For normalizing you need to divide each value of the vector (x, y and z) with it's magnitude. Here is the code:

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]};

}

其他提示

I'm not sure about RenderScript, but in GLSL normalize(x) returns vector in the same direction as x but with a unit length (the length is 1).

Generally normalize means casting a value to be in some range. For example in Time.normalize(bool)

A slight correction to the formula in the accepted answer:

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]};
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top