Menggambar objek di layar adalah fitur yang cukup mendasar di OpenGL, tetapi Anda dapat melakukannya dengan
class framework grafis Android lainnya, termasuk objek Canvas
dan
Drawable
. OpenGL ES menyediakan kemampuan tambahan untuk
memindahkan dan mengubah objek yang digambar dalam tiga dimensi atau dengan cara unik lainnya untuk menciptakan
pengalaman pengguna yang menarik.
Pada tutorial ini, Anda mengambil langkah maju untuk menggunakan OpenGL ES dengan mempelajari cara menambahkan gerakan ke bentuk dengan rotasi.
Memutar bentuk
Memutar objek gambar dengan OpenGL ES 2.0 relatif sederhana. Di perender, buat matriks transformasi lain (matriks rotasi), lalu gabungkan dengan matriks transformasi proyeksi dan tampilan kamera Anda:
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); }
Jika segitiga tidak berputar setelah melakukan perubahan ini, pastikan Anda telah menjadikan setelan GLSurfaceView.RENDERMODE_WHEN_DIRTY
sebagai komentar, seperti yang dijelaskan di bagian berikutnya.
Mengaktifkan rendering berkelanjutan
Jika Anda telah dengan tekun mengikuti kode contoh di class ini hingga tahap ini, pastikan
Anda menjadikan baris yang menyetel mode render hanya menggambar saat kotor, jika tidak, OpenGL
akan memutar bentuk hanya satu kali secara bertahap, lalu menunggu panggilan ke requestRender()
dari penampung 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); }
Kecuali jika Anda memiliki objek yang berubah tanpa interaksi pengguna, sebaiknya aktifkan flag ini. Bersiaplah untuk menghapus tanda komentar pada kode ini, karena tutorial berikutnya akan membuat panggilan ini dapat diterapkan sekali lagi.