JavaとJavaEEプログラマのブログ

JavaEEを中心にしたをソフトウェア開発についてのブログ

5x5のチェッカーボードを描画

package jp.mprog;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView;

import static  jp.mprog.GraphicUtils.drawSquare;
import static  jp.mprog.GraphicUtils.drawRectangle;

public class GLRenderer implements GLSurfaceView.Renderer  {

	private float getPosition(int count){
		return (float)count * 0.4f - 0.8f;
	}

	@Override
	public void onDrawFrame(GL10 gl) {

		GraphicUtils.makeWorld(gl);

		int one = 0x10000;
		int half = 0x08000;

		drawSquare(gl, 0.5f, 0.0f, one, one, 0, one );

		drawRectangle(gl, 0.0f, 0.0f, 1.0f, 1.0f, one, half, 0, one );
		drawRectangle(gl, -0.8f, 0.0f, 0.2f, 2.0f, one, half, 0, one );

		//チェッカーボードを描画する。
		final int c = 5;
		for(int i=0; i < c; i++){
			for (int j = 0; j < c; j++) {
				int brightness = (i + j) % 2 * one;
				drawRectangle(gl,
						getPosition(i),
						getPosition(j),
						0.4f,
						0.4f,
						brightness, brightness, brightness, one);

			}
		}

	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {

	}
}