Beispiel #1
0
  public static void main(String args[]) throws Exception {
    FloatBuffer buffer = FloatBuffer.allocate(10);

    for (int i = 0; i < buffer.capacity(); ++i) {
      float f = (float) Math.sin((((float) i) / 10) * (2 * Math.PI));
      buffer.put(f);
    }

    /** flip()源码: */
    //		public final Buffer flip() {
    //        	limit = position;
    //        	position = 0;
    //        	mark = -1;
    //        	return this;
    //    	}
    /**
     * 反转缓冲区。首先将限制设置为当前位置,然后将位置设置为 0。 如果已定义了标记,则丢弃该标记。 常与compact方法一起使用。 通常情况下,在准备从缓冲区中读取数据时调用flip方法。
     */
    buffer.flip();

    while (buffer.hasRemaining()) {
      float f = buffer.get();
      System.out.println(f);
    }
  }
Beispiel #2
0
    public circle(float r) {
      transMatrix = new float[16];
      int sides = 24;
      vertexCoords = new float[sides * 3];
      double angle = 2 * Math.PI / sides;

      for (int i = 0; i < sides; i++) {
        vertexCoords[3 * i] = (float) (Math.cos(i * angle) * r);
        vertexCoords[3 * i + 1] = (float) (Math.sin(i * angle) * r);
        vertexCoords[3 * i + 2] = 0f;
      }
      vertexCount = vertexCoords.length / COORDS_PER_VERTEX;
      vertexStride = COORDS_PER_VERTEX * 4;
      ByteBuffer bb = ByteBuffer.allocateDirect(vertexCoords.length * 4);
      bb.order(ByteOrder.nativeOrder());
      vertexBuffer = bb.asFloatBuffer();
      vertexBuffer.put(vertexCoords);
      vertexBuffer.position(0);
      int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
      int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

      mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
      GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
      GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
      GLES20.glLinkProgram(mProgram); // create OpenGL program executables
    }
Beispiel #3
0
 public void fellipse(Coord c, Coord r, int a1, int a2) {
   st.set(cur2d);
   apply();
   gl.glBegin(GL.GL_TRIANGLE_FAN);
   vertex(c);
   for (int i = a1; i <= a2; i += 5) {
     double a = (i * Math.PI * 2) / 360.0;
     vertex(c.add((int) (Math.cos(a) * r.x), -(int) (Math.sin(a) * r.y)));
   }
   gl.glEnd();
   checkerr();
 }
Beispiel #4
0
        /**
         * Computes the vertices of the triangles for rendering a sphere and stores them in _buffer.
         * The vertices are repeated as necessary so that the buffer can be used by rendering with
         * glDrawArrays.
         */
        private void computeVertices() {
          final int nphi = 50;
          final int ntheta = 100;

          final float dphi = (float) Math.PI / nphi;
          final float dtheta = 2.0f * (float) Math.PI / ntheta;

          float[] vertices = new float[2 * 3 * 3 * ntheta + (nphi - 2) * 3 * 6 * ntheta];

          // Precompute trigonometry.
          float[] phiSin = new float[nphi];
          float[] phiCos = new float[nphi];
          for (int i = 0; i < nphi; ++i) {
            float phi = i * dphi;
            phiSin[i] = (float) Math.sin(phi);
            phiCos[i] = (float) Math.cos(phi);
          }
          float[] thetaSin = new float[ntheta];
          float[] thetaCos = new float[ntheta];
          for (int i = 0; i < ntheta; ++i) {
            float theta = i * dtheta;
            thetaSin[i] = (float) Math.sin(theta);
            thetaCos[i] = (float) Math.cos(theta);
          }

          // Fan at north pole.
          int index = 0;
          for (int i = 0; i < ntheta; ++i) {
            // Pole.
            vertices[index++] = 0.0f; // x
            vertices[index++] = 0.0f; // y
            vertices[index++] = 1.0f; // z

            vertices[index++] = thetaCos[i] * phiSin[1]; // x
            vertices[index++] = thetaSin[i] * phiSin[1]; // y
            vertices[index++] = phiCos[1]; // z

            int j = (i + 1) % ntheta;
            vertices[index++] = thetaCos[j] * phiSin[1];
            vertices[index++] = thetaSin[j] * phiSin[1];
            vertices[index++] = phiCos[1];
          }

          // Latitude strips.
          for (int ip = 1; ip < nphi - 1; ++ip) {
            for (int it = 0; it < ntheta; ++it) {
              int jt = (it + 1) % ntheta;
              vertices[index++] = thetaCos[it] * phiSin[ip];
              vertices[index++] = thetaSin[it] * phiSin[ip];
              vertices[index++] = phiCos[ip];

              vertices[index++] = thetaCos[it] * phiSin[ip + 1];
              vertices[index++] = thetaSin[it] * phiSin[ip + 1];
              vertices[index++] = phiCos[ip + 1];

              vertices[index++] = thetaCos[jt] * phiSin[ip];
              vertices[index++] = thetaSin[jt] * phiSin[ip];
              vertices[index++] = phiCos[ip];

              vertices[index] = vertices[index - 3];
              ++index;
              vertices[index] = vertices[index - 3];
              ++index;
              vertices[index] = vertices[index - 3];
              ++index;

              vertices[index] = vertices[index - 9];
              ++index;
              vertices[index] = vertices[index - 9];
              ++index;
              vertices[index] = vertices[index - 9];
              ++index;

              vertices[index++] = thetaCos[jt] * phiSin[ip + 1];
              vertices[index++] = thetaSin[jt] * phiSin[ip + 1];
              vertices[index++] = phiCos[ip + 1];
            }
          }

          // Fan at south pole.
          for (int i = 0; i < ntheta; ++i) {
            vertices[index++] = thetaCos[i] * phiSin[nphi - 1];
            vertices[index++] = thetaSin[i] * phiSin[nphi - 1];
            vertices[index++] = phiCos[nphi - 1];

            // Pole.
            vertices[index++] = 0.0f;
            vertices[index++] = 0.0f;
            vertices[index++] = 1.0f;

            int j = (i + 1) % ntheta;
            vertices[index++] = thetaCos[j] * phiSin[nphi - 1];
            vertices[index++] = thetaSin[j] * phiSin[nphi - 1];
            vertices[index++] = phiCos[nphi - 1];
          }

          _buffer =
              ByteBuffer.allocateDirect(vertices.length * 4)
                  .order(ByteOrder.nativeOrder())
                  .asFloatBuffer();
          _buffer.put(vertices);
          _buffer.rewind();
        }