افزودن حرکت

ترسیم اشیاء روی صفحه یکی از ویژگی‌های بسیار ابتدایی OpenGL است، اما می‌توانید این کار را با کلاس‌های چارچوب گرافیکی دیگر Android، ازجمله اشیاء Canvas و Drawable انجام دهید. OpenGL ES قابلیت‌های بیشتری برای جابه‌جایی و تبدیل اشیای ترسیم‌شده به‌صورت سه‌بعدی یا روش‌های منحصربه‌فرد دیگر برای ایجاد تجربه‌های کاربری جذاب ارائه می‌دهد.

در این درس، با یادگیری نحوه افزودن حرکت به شکل با چرخش، گام دیگری در استفاده از OpenGL ES برمی‌دارید.

چرخاندن شکل

چرخاندن شیء ترسیمی با OpenGL ES 2.0 نسبتاً ساده است. در پردازنده، ماتریس تبدیل دیگری (ماتریس چرخش) ایجاد کنید و سپس آن را با ماتریس‌های تبدیل نمای دوربین و طرح‌ریزی ترکیب کنید:

کاتلین

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

جاوا

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 می‌ماند:

کاتلین

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

جاوا

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

مگر اینکه اشیایی داشته باشید که بدون هیچ‌گونه تعامل کاربر تغییر کنند، معمولاً ایده خوبی است که این پرچم روشن باشد. آماده باشید تا این کد را از حالت توضیح خارج کنید، زیرا درس بعدی این فراخوانی را دوباره قابل‌اجرا می‌کند.