Search code examples
androidopengl-esnormalizerenderscript

normalize function renderscript


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!


Solution

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