إنّ رسم العناصر على الشاشة هو ميزة أساسية جدًا في OpenGL، ولكن يمكنك إجراء ذلك مع فئات أخرى لإطار عمل رسومات Android، بما في ذلك الكائنات Canvas
وDrawable
. يوفّر OpenGL ES إمكانات إضافية لنقل
العناصر المرسومة وتحويلها بثلاثة أبعاد أو بطرق فريدة أخرى لإنشاء
تجارب مقنعة للمستخدمين.
في هذا الدرس، ستتقدم خطوة أخرى نحو استخدام OpenGL ES من خلال تعلم كيفية إضافة حركة إلى شكل باستخدام التدوير.
تدوير شكل
إنّ تدوير كائن رسم باستخدام OpenGL ES 2.0 أمر بسيط نسبيًا. في العارض الخاص بك، قم بإنشاء مصفوفة تحويل أخرى (مصفوفة دوران) ثم دمجها مع مصفوفات تحويل عرض الكاميرا والإسقاط:
Kotlin
private val rotationMatrix = FloatArray(16) override fun onDrawFrame(gl: GL10) { val scratch = FloatArray(16) ... // Create a rotation transformation for the triangle val time = SystemClock.uptimeMillis() % 4000L val angle = 0.090f * time.toInt() Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1.0f) // Combine the rotation matrix with the projection and camera view // Note that the vPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0) // Draw triangle mTriangle.draw(scratch) }
Java
private float[] rotationMatrix = new float[16]; @Override public void onDrawFrame(GL10 gl) { float[] scratch = new float[16]; ... // Create a rotation transformation for the triangle long time = SystemClock.uptimeMillis() % 4000L; float angle = 0.090f * ((int) time); Matrix.setRotateM(rotationMatrix, 0, angle, 0, 0, -1.0f); // Combine the rotation matrix with the projection and camera view // Note that the vPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0); // Draw triangle mTriangle.draw(scratch); }
إذا لم يتم تدوير المثلث بعد إجراء هذه التغييرات، تأكّد من أنّك قد علّقت على
إعداد GLSurfaceView.RENDERMODE_WHEN_DIRTY
كما هو موضّح في القسم التالي.
تفعيل العرض المستمر
إذا كنت قد اتبِعت الرمز البرمجي النموذجي في هذه الفئة بعناية إلى هذه النقطة، احرص على إضافة تعليق على السطر الذي يضبط وضع العرض عند عدم تسخينه فقط، وإلا سيتولى OpenGL
تدوير الشكل بمقدار جزء واحد فقط ثم انتظار اتصال requestRender()
من الحاوية GLSurfaceView
:
Kotlin
class MyGLSurfaceView(context: Context) : GLSurfaceView(context) { init { ... // Render the view only when there is a change in the drawing data. // To allow the triangle to rotate automatically, this line is commented out: // renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY } }
Java
public class MyGLSurfaceView(Context context) extends GLSurfaceView { ... // Render the view only when there is a change in the drawing data. // To allow the triangle to rotate automatically, this line is commented out: //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); }
إذا لم تكن هناك عناصر تتغيّر بدون أي تفاعل من المستخدم، من الأفضل عادةً تفعيل هذه العلامة. كن مستعدًا لإلغاء التعليق على هذا الرمز، لأن الدرس التالي يجعل هذه المكالمة سارية مرة أخرى.