OpenGL ES Android Matrix Transformations

前端 未结 3 1191
再見小時候
再見小時候 2020-12-08 23:17

I have a renderer implementing GLSurfaceView.Renderer interface; a subclass of GLSurfaceView and some classes representing my objects I want to draw. I have the code from ht

3条回答
  •  忘掉有多难
    2020-12-09 00:18

    If you want to see movement, you should update mTriangle.mAngle each frame (preferably as a function of time to combat speed differences or delays caused by other processes...).

    Note that Matrix.setIdentityM(mModelMatrix, 0); restores all the accumulated rotations and translations to "zero" or actually to Identity Matrix... The same convention applies btw to all set functions. To accumulate all the transformations, one has to

    • setIdentity(model);
    • translate(trans); // to get the origin of rotation
    • rotate(rotmatrix); // to accumulate the rotation
    • translate(t2); // to translate again to some better position...

    Also one should keep the values of object translation vector [ox,oy,oz] between each call and feed them to Matrix.translateM(mModelMatrix, ox, oy, oz, 0);

    Typically one concatenates all 'translate, rotate, scale', etc. matrices as early as possible and cache them per object, or hierarchically per a complex container having multiple objects and having a bounding box, so that multiple objects can be culled when on behind the camera (or generally outside the viewing frustum).

    Also typically one keeps a moving camera in one matrix and per frame multiplies it with the projection matrix.

    You can start with something like:

    float Time = System.currentTimeMillis() * 0.01f;  // 10 radians / second == fast!
    Matrix.translateM(mModelMatrix, Math.sin(Time)*2.0f, 0, 1f, 0);
    ...
    

    As Tim noticed, there is no projection matrix involved, meaning that all z-values behave in this code exactly, even though changing x & y values would make a difference.

    I'd be tempted to say, that MVP matrix would mean multiplying in order M * V * P = (M*V) * P = M * (V*P).

提交回复
热议问题