মোশন যোগ করা

স্ক্রিনে অবজেক্ট আঁকা 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)
}

জাভা

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 আকৃতিটিকে শুধুমাত্র একটি ইনক্রিমেন্ট ঘোরায় এবং তারপর GLSurfaceView কন্টেনার থেকে requestRender()-এ কলের জন্য অপেক্ষা করে:

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

জাভা

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

কোনও ব্যবহারকারীর ইন্টার‌্যাকশন ছাড়াই অবজেক্ট পরিবর্তন না হলে, সাধারণত এই ফ্ল্যাগ চালু রাখাই ভাল। এই কোডটি আনকমেন্ট করার জন্য প্রস্তুত থাকুন, কারণ পরবর্তী পাঠে এই কলটি আবার প্রযোজ্য হবে।