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

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

「OpenGL]円を描くメソッドを追加

	/**
	 * 円を描画
	 */
	public static void drawCircle(GL10 gl,final float x, final float y,
			final int divides, final  float radius,
			final int red, final int green, final int blue, final int alpha){
		float [] vertices = new float[divides * 3 * 2];//頂点の数はn角形の場合はn*3*2になる。

		int vertexId = 0;

		for(int i=0; i < divides; i++){
			float theta1  = getRadian(divides, i);
			float theta2  = getRadian(divides, i+1);

			vertices[vertexId++] = x;
			vertices[vertexId++] = y;

			vertices[vertexId++] = (float) (cos(theta1) * radius + x);
			vertices[vertexId++] = (float) (sin(theta1) * radius + y);

			vertices[vertexId++] = (float) (cos(theta2) * radius + x);
			vertices[vertexId++] = (float) (sin(theta2) * radius + y);
		}

		//色指定
		gl.glColor4x(red, green, blue, alpha);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

		//ポリゴン描画
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, getFloatBuffer(vertices) );
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		//3つの頂点を持つポリゴンn個で構成されている。
		final int polygonNum =  divides * 3;
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0, polygonNum);

	}

レンダラークラスで以下のように使う

        //円を描画する。
		drawCircle(gl, 0.0f, 0.0f, 8, 1.0f, one, one, 0, one);
		drawCircle(gl, 0.0f, 0.0f, 16, 0.8f, one, half, 0, one);
		drawCircle(gl, 0.0f, 0.0f, 32, 0.6f, one, 0, half, one);
		drawCircle(gl, 0.0f, 0.0f, 64, 0.4f, half, one, 0, one);