简体   繁体   中英

Translation of OpenGL ES 2.0 Shapes in Android

I recently looked at the OpenGL ES 2.0 Tutorial provided by developers.android.com. I succesfully finished it (even if it wasn't very clear) but then I bumped into a problem. Once I finished it, I was never told how to translate or scale objects. I tried different options that seemed logical at the moment but they didn't work. I am not very expirienced in OpenGL ES 2.0 in android.

Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);

long time = SystemClock.uptimeMillis() % 4000L;
mAngle = 0.090f * ((int) time);
Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

t.draw(mMVPMatrix);

All of these matrices are size 16 float arrays. My question is, how could I do a translation with an x and y position and sam for scale (a float with the scale)? There seems to be mo setTranslateM method and, when I tried the alternative methods I was not able to make them work. What do I do?

To translate/scale/rotate your objects, you should apply these operations to your Model Matrix.

Example:

Matrix.setIdentityM(mMMatrix, 0); // reset transformations
Matrix.setRotateM(mMMatrix, 0, 0, 1.0f, 0, 0); // set rotation
Matrix.translateM(mMMatrix, 0, 10.0f, 20.0f, 0); // apply translation
Matrix.scaleM(mMMatrix, 0, 0.25f, 0.25f, 0.25f); // apply scale

Please note that certain static methods of Matrix class do memory allocations so if you do objects transformation on each frame you may want not to use them. Read more about this here http://groups.google.com/group/android-developers/browse_thread/thread/b30dd2a437cfb076?pli=1 and in my blog post here http://androidworks-kea.blogspot.com/2012/05/developers-notes-about-opengl-es.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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