简体   繁体   中英

Best OpenGL ES syntax for android

I run a SurfaceView game where characters and sprites can either move N/S or E/W along a grid. Nowadays this isn't good enough so I've decided to spend the next few months converting it to GLSurfaceView. I'm 57 years old and figure I won't live long enough to become expert in OpenGL. To draw a bitmap texture to a device screen required 2 lines of code in SurfaceView:

Bitmap bmp_background = BitmapFactory.decodeResource(getResources(), R.drawable.backgroundimage_2000_1000,options);
canvas.drawBitmap(bmp_background, null, new Rect(0, 0, screenWidth, screenHeight), null);

The bare bones 'OpenGL ES Texture Mapping' example at Java Code Geeks requires over 300 lines of obsure code to do this same thing in GLSurfaceView.

Thus my strategy has been to look for complete code examples, that include all code from the Main Activity on down, and work from there. My main sources for complete code examples have been the tutorial on the Android Developer site and the Java Code Geeks site. Unfortunately the syntax each site uses is quite different. I was wondering what the differences in syntax mean and advantages of using one type of syntax over another. Here are two code snippets from the GLRenderer onDrawFrame method from both sites:

developer.android.com:

public void onDrawFrame(GL10 unused) {
   GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
   android.opengl.Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
   android.opengl.Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
   mSquare.draw(mMVPMatrix);

javacodegeeks.com:

public void onDrawFrame(GL10 gl) {
   gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
   gl.glLoadIdentity();
   gl.glTranslatef(0.0f, 0.0f, -5.0f);
   square.draw(gl);

Examples you have provided are actually for OpenGL ES 1.x and 2.x. They are completely different: ES 1 (javacodegeeks example) is an old version with fixed function pipeline, while the ES 2 (developer.android example) is newer, with programmable pipeline. The programmable pipeline gives an advantage of better graphics controls, allowing you to implement more graphics effects, and it's not as limited as ES 1, although it may require a bit more code to setup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM