????
OpenGL ES Square   ( ES: for Embedded Systems )
 OpenGL ES 2.0 の実装から OpenGLContext が廃止されたので、GLSurfaceView class を使うこと。
 → Android.opengl Packageのリファレンス

  

 市松模様の表示

 1. 画面をタッチすると、ランダムで分割数を変更する

▲ 上へ


Squareの作成部分

public void createPlane(float width,float height,int widthSegments,int heightSegments) {

  float[] vertices = new float[(widthSegments + 1) * (heightSegments + 1) * 3];
  short[] indices = new short[(widthSegments + 1) * (heightSegments + 1) * 6];
  float[] colors = new float[(widthSegments + 1) * (heightSegments + 1) * 8];

  float xOffset = width / -2;
  float yOffset = height / -2;
  float xWidth = width / (widthSegments);
  float yHeight = height / (heightSegments);
  int currentVertex = 0;
  int currentIndex = 0;

  short w = (short) (widthSegments + 1);

  for (int y = 0; y < heightSegments + 1; y++) {
    for (int x = 0; x < widthSegments + 1; x++) {
      vertices[currentVertex] = xOffset + x * xWidth;
      vertices[currentVertex + 1] = yOffset + y * yHeight;
      vertices[currentVertex + 2] = 0;
      currentVertex += 3;

      int n = y * (widthSegments + 1) + x;
      if (y < heightSegments && x < widthSegments) {
        // Face one
        indices[currentIndex] = (short) n;
        indices[currentIndex + 1] = (short) (n + 1);
        indices[currentIndex + 2] = (short) (n + w);

        // Face two
        indices[currentIndex + 3] = (short) (n + 1);
        indices[currentIndex + 4] = (short) (n + 1 + w);
        indices[currentIndex + 5] = (short) (n + 1 + w - 1);

        currentIndex += 6;
      }
    }
  }

  // カラー配列
  int cnt = 0;
  for (int y =0; y<(heightSegments + 1); y++) {
    for (int x=0; x<(widthSegments + 1); x++) {
      if ((x+y)%2 == 0) {
        colors[cnt] = 1;
        colors[cnt+1] = 0;
        colors[cnt+2] = 0;
        colors[cnt+3] = 1;
      }
      else {
        colors[cnt] = 0;
        colors[cnt+1] = 1;
        colors[cnt+2] = 0;
        colors[cnt+3] = 1;
      }

      cnt+=4;
    }
  }


  // 頂点配列
  ByteBuffer v_buffer = ByteBuffer.allocateDirect(vertices.length << 2);
  v_buffer.order(ByteOrder.nativeOrder());
  mVertexBuffer = v_buffer.asFloatBuffer();
  mVertexBuffer.put(vertices);
  mVertexBuffer.position(0);

  // インデックス配列
  ByteBuffer i_buffer = ByteBuffer.allocateDirect(indices.length << 1);
  i_buffer.order(ByteOrder.nativeOrder());
  mIndexBuffer = i_buffer.asShortBuffer();
  mIndexBuffer.put(indices);
  mIndexBuffer.position(0);

  // カラー配列
  ByteBuffer c_buffer = ByteBuffer.allocateDirect(colors.length << 2);
  c_buffer.order(ByteOrder.nativeOrder());
  mColorBuffer = c_buffer.asFloatBuffer();
  mColorBuffer.put(colors);
  mColorBuffer.position(0);
}





CRIMSON Systems Homeへ Copyright (C) CRIMSON Systems