Ekranda nesne çizmek OpenGL'nin oldukça temel bir özelliğidir. Ancak bunu Canvas
ve Drawable
nesneleri dahil olmak üzere diğer Android grafik çerçevesi sınıflarıyla yapabilirsiniz. OpenGL ES, çizilen nesneleri üç boyutlu olarak veya başka benzersiz yöntemlerle taşımak ve dönüştürmek için ek özellikler sunar. Böylece, cazip kullanıcı deneyimleri oluşturabilir.
Bu derste, döndürme özelliğiyle bir şekle nasıl hareket ekleyeceğinizi öğrenerek OpenGL ES'yi kullanma yolunda bir adım daha atacaksınız.
Şekli döndürme
Bir çizim nesnesini OpenGL ES 2.0 ile döndürmek nispeten basittir. Oluşturucunuzda başka bir dönüşüm matrisi (döndürme matrisi) oluşturun, ardından bunu projeksiyon ve kamera görünümü dönüşüm matrisleriyle birleştirin:
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); }
Bu değişiklikleri yaptıktan sonra üçgeniniz dönmüyorsa bir sonraki bölümde açıklandığı gibi GLSurfaceView.RENDERMODE_WHEN_DIRTY
ayarıyla ilgili yorum yaptığınızdan emin olun.
Sürekli oluşturmayı etkinleştir
Bu sınıftaki örnek kodu bu noktaya kadar dikkatli bir şekilde takip ettiyseniz, oluşturma modunu yalnızca kirli olduğunda çizen çizgiyi açıkladığınızdan emin olun. Aksi takdirde, OpenGL şekli yalnızca bir artış döndürür ve ardından GLSurfaceView
kapsayıcısından requestRender()
çağrısının gelmesini bekler:
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); }
Nesneleriniz herhangi bir kullanıcı etkileşimi olmadan değişmediği sürece bu işaretin açık olması genellikle iyi bir fikirdir. Bir sonraki derste bu çağrıyı bir kez daha geçerli hale getireceğinden, kodun açıklamasını kaldırmaya hazır olun.