示例#1
0
 public void glPaint() {
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glEnable(GL_LIGHTING);
   glEnable(GL_LIGHT0);
   glEnable(GL_DEPTH_TEST);
   glEnableClientState(GL_NORMAL_ARRAY);
   glEnableClientState(GL_VERTEX_ARRAY);
   glVertexPointer(3, GL_FLOAT, 0, _buffer);
   glNormalPointer(GL_FLOAT, 0, _buffer);
   glColor3f(1.0f, 1.0f, 1.0f);
   glDrawArrays(GL_TRIANGLES, 0, _buffer.capacity() / 3);
   glFlush();
 }
示例#2
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();
        }