![]() |
|
| 矩形のZ軸での回転 | ||||
1. 画面をタッチすると、色をランダムに変化させ、回転を反転させる |
||||
|
| GLSurfaceViewの使用例 | |
private GLSurfaceView glsurfaceview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); glsurfaceview = new GLSurfaceView(this); glsurfaceview.setRenderer(new MyRenderer()); setContentView(glsurfaceview); } |
|
.MyRendererには以下のメソッドを実装する void onDrawFrame(GL10 gl); // 実際の描画処理 void onSurfaceChanged(GL10 gl, int width, int height); void onSurfaceCreated(GL10 gl, EGLConfig config); |
| 矩形(頂点)の定義 | |
| FloatBuffer mObject; float[] vertices = { // 四角形 -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f }; |
|
| GL初期化 | |
| public void onInit(GL10 gl, EGLConfig config) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length*4); byteBuffer.order(ByteOrder.nativeOrder()); mObject = byteBuffer.asFloatBuffer(); mObject.put(vertices); mObject.position(0); gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); GLU.gluOrtho2D(gl, -1.0f, 1.0f, -1.0f, 1.0f); } |
|
| シーンの描画 | |
| public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f); // 赤 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mObject); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); } |
| 回転を反転するには、回転角をマイナス指定する。 | |
gl.glRotatef(-angle, 0, 0, 1.0f); // Z軸で回転 |
|
|
Copyright (C) CRIMSON Systems |