简体   繁体   English

如何获取场景中每个对象的当前位置?

[英]how to get current position of every object in scene?

I am rendering several square objects in sphere (I am in the center of the sphere and the objects are around me). 我正在渲染球体中的几个正方形对象(我位于球体的中心,并且这些对象围绕着我)。 I am using the phone's Rotation sensor to view the the objects in the sphere. 我正在使用手机的旋转传感器来查看球体中的对象。

all the objects start from position around (0,0,0) , but at rendering time I'm rotating each object to a different angle in the sphere . 所有对象都从(0,0,0)周围的位置开始,但是在渲染时,我将每个对象旋转到球体中的不同角度。

this is how I handle the matrices until from the moment I get the rotation matrix : 这就是我处理矩阵的方式,直到获得旋转矩阵为止:

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

then in the shader I multiply each object location(which is the same location basically) with this MVP matrix , and they are rendered around in a sphere like world. 然后在着色器中,将每个对象位置(基本上是相同的位置)与此MVP矩阵相乘,然后将它们渲染在像世界这样的球体中。

this works good . 这很好。 now what I like to do is identify when an object is right in front of me . 现在我想做的是确定什么时候物体就在我面前。 get each object's location at all times , and when I view a certain object make it selectable or lighted. 随时获取每个对象的位置,当我查看某个对象时,使其变为可选择或点亮状态。

but since every object is multiplied several times , how can I know it's location and when I am actually viewing it now ? 但是,由于每个对象都会被倍增,所以我如何知道它的位置以及现在实际查看它的时间?

The translation is always stored in the last column of a transformation matrix (or in the last row depending whether the matrix is stored column major or row major). 转换始终存储在转换矩阵的最后一列中(或存储在最后一行中,具体取决于矩阵是存储列专业还是行专业)。

Thus the position of an object in worldspace is the last column of the model matrix. 因此,对象在世界空间中的位置是模型矩阵的最后一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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