Ejemplo n.º 1
0
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      // 设置屏幕背景色RGBA
      GLES20.glClearColor(0.9f, 0.9f, 0.9f, 1.0f);
      // 启用深度测试
      GLES20.glEnable(GLES20.GL_DEPTH_TEST);
      // 设置为打开背面剪裁
      GLES20.glEnable(GLES20.GL_CULL_FACE);
      // 初始化变换矩阵
      MatrixState.setInitStack();

      float[] colorValue = {1, 0, 0, 1}; // 创建颜色数组
      ball = new Ball(MySurfaceView.this, Constant.BALL_R, colorValue); // 创建球对象
      colorValue = new float[] {1, 1, 0, 1};
      stick =
          new Stick(
              MySurfaceView.this,
              Constant.LENGTH,
              Constant.R,
              Constant.ANGLE_SPAN,
              colorValue); // 创建圆管对象

      double[] initPoint = Utils.getFirstPoint(Constant.LENGTH); // 得到第一个五边形左下点的坐标
      double[] initVector = {1, 0, 0, 1}; // 初始化方向向量
      double[] zPivot = {0, 0, 1, 1}; // 以z轴为旋转轴
      int[] vertices = {0, 1, 2, 3, 4}; // 球的索引
      int[] borders = {0, 1, 2, 3, 4}; // 圆管的索引
      first =
          new RegularPolygon(
              MySurfaceView.this,
              5,
              72,
              Constant.LENGTH,
              initPoint,
              initVector,
              zPivot,
              vertices,
              borders); // 1

      vertices = new int[] {2, 3, 4}; // 球的索引
      borders = new int[] {1, 2, 3, 4}; // 圆管的索引
      RegularPolygon rp2 = first.buildChild(6, -60, 1, vertices, borders); // 2

      vertices = new int[] {2, 3, 4, 5};
      borders = new int[] {1, 2, 3, 4, 5};
      RegularPolygon rp4 = rp2.buildChild(6, 60, 3, vertices, borders); // 4

      vertices = new int[] {};
      borders = new int[] {1, 5};
      rp4.buildChild(6, -60, 2, vertices, borders); // 5

      vertices = new int[] {2};
      borders = new int[] {1, 2};
      RegularPolygon rp6 = rp4.buildChild(5, -72, 3, vertices, borders); // 6

      vertices = new int[] {3, 4, 5};
      borders = new int[] {2, 3, 4, 5};
      rp6.buildChild(6, 60, 2, vertices, borders); // 7
    }
Ejemplo n.º 2
0
    public void onDrawFrame(GL10 gl) {
      // 清除深度缓冲与颜色缓冲
      GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

      MatrixState.pushMatrix();
      MatrixState.translate(0, 0, -6f);
      MatrixState.pushMatrix();
      MatrixState.rotate(xAngle, 1, 0, 0);
      MatrixState.rotate(yAngle, 0, 1, 0);
      MatrixState.rotate(zAngle, 0, 0, 1);
      for (int i = 0; i < 5; i++) { // 五部分循环
        MatrixState.pushMatrix();
        MatrixState.rotate(72 * i, 0, 0, 1); // 根据绘制的为第几部分,旋转72*i度
        first.drawSelf(0, 0); // 绘制足球碳的五分之一部分,最后的五边形由五部分组合形成,所以不用绘制
        MatrixState.popMatrix();
      }
      MatrixState.popMatrix();
      MatrixState.popMatrix();
      Utils.drawnVertices.clear();
    }