緑本から。
PolygonSample.java(メインクラス)
package opengl_es.test.polygonsample; import android.app.Activity; import android.os.Bundle; import android.opengl.GLSurfaceView; import android.opengl.GLU; import android.view.Window; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; public class PolygonSample extends Activity implements GLSurfaceView.Renderer { private GLSurfaceView gLSurfaceView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); requestWindowFeature(Window.FEATURE_NO_TITLE); gLSurfaceView = new GLSurfaceView(this); gLSurfaceView.setRenderer(this); setContentView(gLSurfaceView); } public void onDrawFrame(GL10 gl){ gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); GLU.gluLookAt(gl, 0.0f, 0.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); PolygonDrawer.drawBoard(gl,0.0f,0.0f,128,128,255,0,0,255); PolygonDrawer.drawBoard(gl,-60.0f,-120.0f,64,64,0,0,255,255); } public void onSurfaceChanged(GL10 gl,int width, int height){ gl.glViewport(0,0,width,height); float ratio = (float) width / height; gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.0f, 1000.0f); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // TODO Auto-generated method stub gl.glDisable(GL10.GL_DITHER); gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_FASTEST); gl.glClearColor(0,0,0,1); gl.glEnable(GL10.GL_CULL_FACE); gl.glFrontFace(GL10.GL_CCW); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glShadeModel(GL10.GL_SMOOTH); } protected void onResume(){ super.onResume(); gLSurfaceView.onResume(); } protected void onPause() { super.onPause(); gLSurfaceView.onPause(); } }
PolygonDrawer.java(描画メソッドを定義)
package opengl_es.test.polygonsample; import java.nio.IntBuffer; import javax.microedition.khronos.opengles.GL10; public class PolygonDrawer { /* @param gl @param x, y @param width, height @param red, green, blue, alpha */ static void drawBoard(GL10 gl, float x, float y, int width, int height, int red, int green, int blue , int alpha) { int one = 0x10000; int vertices[] = { -width*one/2, -height*one/2,height*one/2, width*one/2, -height*one/2,-width*one/2, -width*one/2,height*one/2,width*one/2, width*one/2,height*one/2,-height*one/2, }; int colors[] = { one*red/255,one*green/255,one*blue/255,one*alpha/255, one*red/255,one*green/255,one*blue/255,one*alpha/255, one*red/255,one*green/255,one*blue/255,one*alpha/255, one*red/255,one*green/255,one*blue/255,one*alpha/255, }; gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10.GL_TEXTURE_2D); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); gl.glPushMatrix(); gl.glTranslatef(x, y, 0); gl.glVertexPointer(3, GL10.GL_FIXED, 0, IntBuffer.wrap(vertices)); gl.glColorPointer(4, GL10.GL_FIXED, 0, IntBuffer.wrap(colors)); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glPopMatrix(); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); gl.glDisableClientState(GL10.GL_COLOR_ARRAY); } }