简体   繁体   English

标准化函数渲染脚本

[英]normalize function renderscript

I am learning android renderscript and currently looking at the carousel example. 我正在学习android renderscript,目前正在看轮播示例。 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. 我也在学习一些OpenGl ES 2.0,并且遇到了使用normalize标志但从未使用过的函数(该标志通常是-false,所以它做了一些类似将非float值强制转换为float的操作)..因此,如果有人可以给出我很好的解释,我将不胜感激。

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. 另外,我需要将大多数代码从renderscript移植到opengl es 2.0,因此请记住,我也必须在Java中使用此函数。 (and maybe write it?) Thx! (也许写出来?)谢谢!

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). 我不确定RenderScript,但是在GLSL中,normalize(x)返回的矢量与x方向相同,但单位长度(长度为1)。

Generally normalize means casting a value to be in some range. 通常归一化意味着将值转换为某个范围内的值。 For example in Time.normalize(bool) 例如在Time.normalize(bool)中

I've managed to implement the normalize function, for normalizing a 3d vector. 我设法实现了标准化功能,用于标准化3d向量。 For normalizing you need to divide each value of the vector (x, y and z) with it's magnitude. 为了进行归一化,您需要将向量的每个值(x,y和z)除以其大小。 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]};

}

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM