Ekranda nesne çizmek OpenGL’in oldukça temel bir özelliğidir, ancak bunu diğer
Canvas
ve dahil olmak üzere Android grafik çerçevesi sınıfları
Drawable
nesne. OpenGL ES, aşağıdakiler için ek özellikler sunar:
oluşturmak için, çizilmiş nesneleri üç boyutlu veya diğer benzersiz
ikna edici bir kullanıcı deneyimi sağlıyor.
Bu derste hareketin nasıl ekleneceğini öğrenerek OpenGL ES'yi kullanma konusunda döndüren bir şekle dönüştürülebilir.
Şekli döndürme
OpenGL ES 2.0 ile çizim nesnesini döndürmek nispeten basit bir işlemdir. Oluşturucunuzda başka bir dönüşüm matrisi (rotasyon matrisi) ve ardından projeksiyonunuzla birleştirerek kamera görünümü dönüştürme matrisleri:
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);
}
Bu değişiklikleri yaptıktan sonra üçgeniniz dönmezse
GLSurfaceView.RENDERMODE_WHEN_DIRTY
.
ayarını değiştirin.
Sürekli oluşturmayı etkinleştir
Bu noktaya kadar bu sınıftaki örnek kodu dikkatli bir şekilde takip ettiyseniz
oluşturma modunu yalnızca kirli olduğunda çizim yapan satırı yorumladığınızdan emin olun, aksi takdirde OpenGL
şekli yalnızca bir artışla döndürür ve ardından GLSurfaceView
kapsayıcısından requestRender()
çağrısının yapılmasını bekler:
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);
}
Kullanıcı etkileşimi olmadan değişen nesneleriniz yoksa bu genellikle iyi bir fikirdir bayrağı etkinleştirdik. Bir sonraki ders çağrının geçerli olmasını sağlayacağından, bu kodun açıklamasını kaldırmaya hazır olun bir kez daha gönderin.