2016-01-10 54 views
0

我在球体中渲染几个方形物体(我在球体的中心,物体在我的周围)。我正在使用手机的旋转传感器来查看球体中的物体。如何获取场景中每个对象的当前位置?

所有对象都从(0,0,0)左右的位置开始,但在渲染时我将每个对象旋转到球体中的不同角度。

这是我如何处理矩阵,直到从时刻我得到的旋转矩阵:与此MVP矩阵

public void position(float[] rotationMatrix , float[] projectionMatrix , float rotationAngle , float xRotation , float yRotation , float zRotation , float xTranslation , float yTranslation , float zTranslation){ 


     float[] MVP = new float[16]; 

     float[] modelMatrix = new float[16]; 

     System.arraycopy(rotationMatrix, 0, modelMatrix, 0, rotationMatrix.length); 

     Matrix.rotateM(modelMatrix, 0, -90f, 1f, 0f, 0f); //correct the axis so that the direction of Y axis is to the sky and Z is to the front 

     Matrix.rotateM(modelMatrix, 0, rotationAngle, xRotation, yRotation, zRotation); //rotate the object around axis 

     // used to control the distance of viewing the object (currently only z translation is used) 
     Matrix.translateM(modelMatrix, 0, xTranslation, yTranslation, zTranslation); 



     Matrix.multiplyMM(MVP , 0 , projectionMatrix , 0 , modelMatrix , 0); 

     textureShaderProgram.setUniforms(MVP , texture); 

     return; 
    } 

然后在着色器我乘以每个对象的位置(这是相同的位置基本上),它们被渲染成像世界一样的球体。

这个很好用。现在我想要做的是确定一个物体何时位于我面前。随时获取每个对象的位置,并且当我查看某个对象时,可以选择或点亮它。

但是由于每个对象都被乘以好几次,我怎么能知道它的位置以及我现在实际查看它的时间?

回答

1

翻译总是存储在转换矩阵的最后一列(或最后一行,取决于矩阵是存储列主要还是主要行)。

因此,worldspace中对象的位置是模型矩阵的最后一列。