Java Code Examples for java.nio.ByteBuffer#asShortBuffer()

The following examples show how to use java.nio.ByteBuffer#asShortBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicCustomVideoRenderer.java    From opentok-android-sdk-samples with MIT License 6 votes vote down vote up
public MyRenderer(BasicCustomVideoRenderer parent) {

            this.mCustomVideoRenderer = parent;

            ByteBuffer bb = ByteBuffer.allocateDirect(mXYZCoords.length * 4);
            bb.order(ByteOrder.nativeOrder());
            mVertexBuffer = bb.asFloatBuffer();
            mVertexBuffer.put(mXYZCoords);
            mVertexBuffer.position(0);

            ByteBuffer tb = ByteBuffer.allocateDirect(mUVCoords.length * 4);
            tb.order(ByteOrder.nativeOrder());
            mTextureBuffer = tb.asFloatBuffer();
            mTextureBuffer.put(mUVCoords);
            mTextureBuffer.position(0);

            ByteBuffer dlb = ByteBuffer.allocateDirect(mVertexIndex.length * 2);
            dlb.order(ByteOrder.nativeOrder());
            mDrawListBuffer = dlb.asShortBuffer();
            mDrawListBuffer.put(mVertexIndex);
            mDrawListBuffer.position(0);
        }
 
Example 2
Source File: TrueTypeFont.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
    if (postTable == null || postTable.capacity() < 12 || upem < 0) {
        ulSize = .05f;
        ulPos = .1f;
        return;
    }
    ShortBuffer sb = postTable.asShortBuffer();
    ulSize = sb.get(5) / (float)upem;
    ulPos = -sb.get(4) / (float)upem;
}
 
Example 3
Source File: QuadRenderSystem.java    From BobEngine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Change the size of the vertex, texture, and index buffers.
 *
 * @param quads The number of quads the buffers should be able to hold.
 */
public void resizeBuffers(int quads) {
	// Set up vertex buffer
	ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(VERTEX_BYTES * quads);    // a float has 4 bytes so we allocate for each coordinate 4 bytes
	vertexByteBuffer.order(ByteOrder.nativeOrder());
	vertexBuffer = vertexByteBuffer.order(ByteOrder.nativeOrder()).asFloatBuffer();   // allocates the memory from the byte buffer
	vertexBuffer.position(0);                                                         // puts the cursor position at the beginning of the buffer

	// Set up texture buffer
	vertexByteBuffer = ByteBuffer.allocateDirect(TEX_BYTES * quads);
	vertexByteBuffer.order(ByteOrder.nativeOrder());
	textureBuffer = vertexByteBuffer.asFloatBuffer();
	textureBuffer.position(0);

	// Set up index buffer
	int layers = Room.DEF_LAYERS;

	if (getRoom() != null) {
		layers = getRoom().getNumLayers();
	}

	vertexByteBuffer = ByteBuffer.allocateDirect(INDEX_BYTES * quads);
	vertexByteBuffer.order(ByteOrder.nativeOrder());
	indexBuffer = new ShortBuffer[layers];
	for (int i = 0; i < layers; i++) {
		indexBuffer[i] = vertexByteBuffer.asShortBuffer();
		indexBuffer[i].position(0);
	}

	bufferSize = quads;
}
 
Example 4
Source File: TrueTypeFont.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
    if (postTable == null || postTable.capacity() < 12 || upem < 0) {
        ulSize = .05f;
        ulPos = .1f;
        return;
    }
    ShortBuffer sb = postTable.asShortBuffer();
    ulSize = sb.get(5) / (float)upem;
    ulPos = -sb.get(4) / (float)upem;
}
 
Example 5
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void testBuffersIndependentLimit(ByteBuffer b) {
    CharBuffer c = b.asCharBuffer();
    b.limit(b.capacity()); c.put(1, (char)1);
    b.limit(0);  c.put(1, (char)1);

    b.limit(b.capacity());
    ShortBuffer s = b.asShortBuffer();
    b.limit(b.capacity()); s.put(1, (short)1);
    b.limit(0);  s.put(1, (short)1);

    b.limit(b.capacity());
    IntBuffer i = b.asIntBuffer();
    i.put(1, (int)1);
    b.limit(0);  i.put(1, (int)1);

    b.limit(b.capacity());
    LongBuffer l = b.asLongBuffer();
    l.put(1, (long)1);
    b.limit(0);  l.put(1, (long)1);

    b.limit(b.capacity());
    FloatBuffer f = b.asFloatBuffer();
    f.put(1, (float)1);
    b.limit(0);  f.put(1, (float)1);

    b.limit(b.capacity());
    DoubleBuffer d = b.asDoubleBuffer();
    d.put(1, (double)1);
    b.limit(0);  d.put(1, (double)1);
}
 
Example 6
Source File: AudioChannel.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public void drainDecoderBufferAndQueue(final int bufferIndex, final long presentationTimeUs) {
    if (mActualDecodedFormat == null) {
        throw new RuntimeException("Buffer received before format!");
    }

    final ByteBuffer data =
            bufferIndex == BUFFER_INDEX_END_OF_STREAM ?
                    null : mDecoderBuffers.getOutputBuffer(bufferIndex);

    AudioBuffer buffer = mEmptyBuffers.poll();
    if (buffer == null) {
        buffer = new AudioBuffer();
    }

    buffer.bufferIndex = bufferIndex;
    buffer.presentationTimeUs = presentationTimeUs;
    buffer.data = data == null ? null : data.asShortBuffer();

    if (mOverflowBuffer.data == null) {
        mOverflowBuffer.data = ByteBuffer
                .allocateDirect(data.capacity())
                .order(ByteOrder.nativeOrder())
                .asShortBuffer();
        mOverflowBuffer.data.clear().flip();
    }

    mFilledBuffers.add(buffer);
}
 
Example 7
Source File: TrueTypeFont.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
    if (postTable == null || postTable.capacity() < 12 || upem < 0) {
        ulSize = .05f;
        ulPos = .1f;
        return;
    }
    ShortBuffer sb = postTable.asShortBuffer();
    ulSize = sb.get(5) / (float)upem;
    ulPos = -sb.get(4) / (float)upem;
}
 
Example 8
Source File: TrueTypeFont.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void getStyleMetrics(float pointSize, float[] metrics, int offset) {

    if (ulSize == 0f && ulPos == 0f) {

        ByteBuffer head_Table = getTableBuffer(headTag);
        int upem = -1;
        if (head_Table != null && head_Table.capacity() >= 18) {
            ShortBuffer sb = head_Table.asShortBuffer();
            upem = sb.get(9) & 0xffff;
            if (upem < 16 || upem > 16384) {
                upem = 2048;
            }
        }

        ByteBuffer os2_Table = getTableBuffer(os_2Tag);
        setStrikethroughMetrics(os2_Table, upem);

        ByteBuffer post_Table = getTableBuffer(postTag);
        setUnderlineMetrics(post_Table, upem);
    }

    metrics[offset] = stPos * pointSize;
    metrics[offset+1] = stSize * pointSize;

    metrics[offset+2] = ulPos * pointSize;
    metrics[offset+3] = ulSize * pointSize;
}
 
Example 9
Source File: LittleEndianBinUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] shortToByteArray(short inShort) {
	byte[] bArray = new byte[2];
	ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
	bBuffer.order(ByteOrder.LITTLE_ENDIAN);
	ShortBuffer lBuffer = bBuffer.asShortBuffer();
	lBuffer.put(inShort);
	return bArray;
}
 
Example 10
Source File: Square.java    From opengl with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the drawing object data for use in an OpenGL ES context.
 */
public Square() {
    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
    // (# of coordinate values * 4 bytes per float)
            squareCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(squareCoords);
    vertexBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);

    // prepare shaders and OpenGL program
    int vertexShader = MyGLRenderer.loadShader(
            GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = MyGLRenderer.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
}
 
Example 11
Source File: TrueTypeFont.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
    if (postTable == null || postTable.capacity() < 12 || upem < 0) {
        ulSize = .05f;
        ulPos = .1f;
        return;
    }
    ShortBuffer sb = postTable.asShortBuffer();
    ulSize = sb.get(5) / (float)upem;
    ulPos = -sb.get(4) / (float)upem;
}
 
Example 12
Source File: IpUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Performs an IP checksum (used in IP header and across UDP
 * payload) on the specified portion of a ByteBuffer.  The seed
 * allows the checksum to commence with a specified value.
 */
private static int checksum(ByteBuffer buf, int seed, int start, int end) {
    int sum = seed;
    final int bufPosition = buf.position();

    // set position of original ByteBuffer, so that the ShortBuffer
    // will be correctly initialized
    buf.position(start);
    ShortBuffer shortBuf = buf.asShortBuffer();

    // re-set ByteBuffer position
    buf.position(bufPosition);

    final int numShorts = (end - start) / 2;
    for (int i = 0; i < numShorts; i++) {
        sum += intAbs(shortBuf.get(i));
    }
    start += numShorts * 2;

    // see if a singleton byte remains
    if (end != start) {
        short b = buf.get(start);

        // make it unsigned
        if (b < 0) {
            b += 256;
        }

        sum += b * 256;
    }

    sum = ((sum >> 16) & 0xFFFF) + (sum & 0xFFFF);
    sum = ((sum + ((sum >> 16) & 0xFFFF)) & 0xFFFF);
    int negated = ~sum;
    return intAbs((short) negated);
}
 
Example 13
Source File: TrueTypeFont.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setUnderlineMetrics(ByteBuffer postTable, int upem) {
    if (postTable == null || postTable.capacity() < 12 || upem < 0) {
        ulSize = .05f;
        ulPos = .1f;
        return;
    }
    ShortBuffer sb = postTable.asShortBuffer();
    ulSize = sb.get(5) / (float)upem;
    ulPos = -sb.get(4) / (float)upem;
}
 
Example 14
Source File: TrueTypeFont.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected void initAllNames(int requestedID, HashSet names) {

        byte[] name = new byte[256];
        ByteBuffer buffer = getTableBuffer(nameTag);

        if (buffer != null) {
            ShortBuffer sbuffer = buffer.asShortBuffer();
            sbuffer.get(); // format - not needed.
            short numRecords = sbuffer.get();

            /* The name table uses unsigned shorts. Many of these
             * are known small values that fit in a short.
             * The values that are sizes or offsets into the table could be
             * greater than 32767, so read and store those as ints
             */
            int stringPtr = ((int) sbuffer.get()) & 0xffff;
            for (int i=0; i<numRecords; i++) {
                short platformID = sbuffer.get();
                if (platformID != MS_PLATFORM_ID) {
                    sbuffer.position(sbuffer.position()+5);
                    continue; // skip over this record.
                }
                short encodingID = sbuffer.get();
                short langID     = sbuffer.get();
                short nameID     = sbuffer.get();
                int   nameLen    = ((int) sbuffer.get()) & 0xffff;
                int   namePtr    = (((int) sbuffer.get()) & 0xffff) + stringPtr;

                if (nameID == requestedID) {
                    buffer.position(namePtr);
                    buffer.get(name, 0, nameLen);
                    names.add(makeString(name, nameLen, encodingID));
                }
            }
        }
    }
 
Example 15
Source File: TrueTypeFont.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setStrikethroughMetrics(ByteBuffer os_2Table, int upem) {
    if (os_2Table == null || os_2Table.capacity() < 30 || upem < 0) {
        stSize = .05f;
        stPos = -.4f;
        return;
    }
    ShortBuffer sb = os_2Table.asShortBuffer();
    stSize = sb.get(13) / (float)upem;
    stPos = -sb.get(14) / (float)upem;
}
 
Example 16
Source File: BinUtils.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public static byte[] shortToByteArray(short inShort) {
	byte[] bArray = new byte[2];
	ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
	ShortBuffer lBuffer = bBuffer.asShortBuffer();
	lBuffer.put(inShort);
	return bArray;
}
 
Example 17
Source File: TriangleRenderer.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
public Triangle() {

        // Buffers to be passed to gl*Pointer() functions
        // must be direct, i.e., they must be placed on the
        // native heap where the garbage collector cannot
        // move them.
        //
        // Buffers with multi-byte datatypes (e.g., short, int, float)
        // must have their byte order set to native order

        ByteBuffer vbb = ByteBuffer.allocateDirect(VERTS * 3 * 4);
        vbb.order(ByteOrder.nativeOrder());
        mFVertexBuffer = vbb.asFloatBuffer();

        ByteBuffer tbb = ByteBuffer.allocateDirect(VERTS * 2 * 4);
        tbb.order(ByteOrder.nativeOrder());
        mTexBuffer = tbb.asFloatBuffer();

        ByteBuffer ibb = ByteBuffer.allocateDirect(VERTS * 2);
        ibb.order(ByteOrder.nativeOrder());
        mIndexBuffer = ibb.asShortBuffer();

        // A unit-sided equalateral triangle centered on the origin.
        float[] coords = {
                // X, Y, Z
                -0.5f, -0.25f, 0,
                 0.5f, -0.25f, 0,
                 0.0f,  0.559016994f, 0
        };

        for (int i = 0; i < VERTS; i++) {
            for(int j = 0; j < 3; j++) {
                mFVertexBuffer.put(coords[i*3+j] * 2.0f);
            }
        }

        for (int i = 0; i < VERTS; i++) {
            for(int j = 0; j < 2; j++) {
                mTexBuffer.put(coords[i*3+j] * 2.0f + 0.5f);
            }
        }

        for(int i = 0; i < VERTS; i++) {
            mIndexBuffer.put((short) i);
        }

        mFVertexBuffer.position(0);
        mTexBuffer.position(0);
        mIndexBuffer.position(0);
    }
 
Example 18
Source File: PointsMatrix.java    From MegviiFacepp-Android-SDK with Apache License 2.0 4 votes vote down vote up
public PointsMatrix(boolean isFaceCompare) {
	// FloatBuffer fb_0 = floatBufferUtil(squareCoords);
	// FloatBuffer fb_1 = floatBufferUtil(squareCoords_1);
	// vertexBuffers.add(fb_0);
	// vertexBuffers.add(fb_1);
	// initialize byte buffer for the draw list
	this.isFaceCompare = isFaceCompare;

	ByteBuffer dlb = ByteBuffer.allocateDirect(
			// (# of coordinate values * 2 bytes per short)
			drawOrder.length * 2);
	dlb.order(ByteOrder.nativeOrder());
	drawListBuffer = dlb.asShortBuffer();
	drawListBuffer.put(drawOrder);
	drawListBuffer.position(0);
	ByteBuffer line_dlb = ByteBuffer.allocateDirect(
			// (# of coordinate values * 2 bytes per short)
			drawLineOrder.length * 2);
	line_dlb.order(ByteOrder.nativeOrder());
	drawLineListBuffer = line_dlb.asShortBuffer();
	drawLineListBuffer.put(drawLineOrder);
	drawLineListBuffer.position(0);
	for (int i = 0; i < cubeOrders.length; ++i) {
		final short cubeOrder[] = cubeOrders[i];
		ByteBuffer cubedlb = ByteBuffer.allocateDirect(
				// (# of coordinate values * 2 bytes per short)
				cubeOrder.length * 2);
		cubedlb.order(ByteOrder.nativeOrder());
		cubeListBuffer[i] = cubedlb.asShortBuffer();
		cubeListBuffer[i].put(cubeOrder);
		cubeListBuffer[i].position(0);
	}

	ByteBuffer faceRectLDB = ByteBuffer.allocateDirect(drawFaceRectOrder.length * 2);
	faceRectLDB.order(ByteOrder.nativeOrder());
	faceRectListBuffer = faceRectLDB.asShortBuffer();
	faceRectListBuffer.put(drawFaceRectOrder);
	faceRectListBuffer.position(0);

	// prepare shaders and OpenGL program
	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
}
 
Example 19
Source File: MDStereoSphere3D.java    From MD360Player4Android with Apache License 2.0 4 votes vote down vote up
private static void generateSphere(float radius, int rings, int sectors, MDAbsObject3D object3D, MDDirection direction) {
    final float PI = (float) Math.PI;
    final float PI_2 = (float) (Math.PI / 2);

    float R = 1f/(float)rings;
    float S = 1f/(float)sectors;
    short r, s;
    float x, y, z;

    int numPoint = (rings + 1) * (sectors + 1);
    float[] vertexs = new float[numPoint * 3];
    float[] texcoords = new float[numPoint * 2];
    float[] texcoords2 = new float[numPoint * 2];
    short[] indices = new short[numPoint * 6];

    int t = 0, v = 0;
    for(r = 0; r < rings + 1; r++) {
        for(s = 0; s < sectors + 1; s++) {
            x = (float) (Math.cos(2*PI * s * S) * Math.sin( PI * r * R ));
            y = - (float) Math.sin( -PI_2 + PI * r * R );
            z = (float) (Math.sin(2*PI * s * S) * Math.sin( PI * r * R ));

            if (MDDirection.VERTICAL == direction){
                texcoords[t] = s*S;
                texcoords2[t] = s*S;
                t++;

                texcoords[t] = 1 - r*R/2;
                texcoords2[t] = 0.5f - r*R/2;
                t++;
            } else {
                texcoords[t] = s*S/2;
                texcoords2[t] = s*S/2 + 0.5f;
                t++;

                texcoords[t] = 1 - r*R;
                texcoords2[t] = 1 - r*R;
                t++;
            }

            vertexs[v++] = x * radius;
            vertexs[v++] = y * radius;
            vertexs[v++] = z * radius;
        }
    }

    int counter = 0;
    int sectorsPlusOne = sectors + 1;
    for(r = 0; r < rings; r++){
        for(s = 0; s < sectors; s++) {
            indices[counter++] = (short) (r * sectorsPlusOne + s);       //(a)
            indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s));    //(b)
            indices[counter++] = (short) ((r) * sectorsPlusOne + (s+1));  // (c)
            indices[counter++] = (short) ((r) * sectorsPlusOne + (s+1));  // (c)
            indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s));    //(b)
            indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s+1));  // (d)
        }
    }

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            vertexs.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(vertexs);
    vertexBuffer.position(0);

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer cc = ByteBuffer.allocateDirect(
            texcoords.length * 4);
    cc.order(ByteOrder.nativeOrder());
    FloatBuffer texBuffer = cc.asFloatBuffer();
    texBuffer.put(texcoords);
    texBuffer.position(0);

    // initialize vertex2 byte buffer for shape coordinates
    ByteBuffer cc2 = ByteBuffer.allocateDirect(
            texcoords.length * 4);
    cc2.order(ByteOrder.nativeOrder());
    FloatBuffer texBuffer2 = cc2.asFloatBuffer();
    texBuffer2.put(texcoords2);
    texBuffer2.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            indices.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    ShortBuffer indexBuffer = dlb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    object3D.setIndicesBuffer(indexBuffer);
    object3D.setTexCoordinateBuffer(0,texBuffer);
    object3D.setTexCoordinateBuffer(1,texBuffer2);
    object3D.setVerticesBuffer(0,vertexBuffer);
    object3D.setVerticesBuffer(1,vertexBuffer);
    object3D.setNumIndices(indices.length);
}
 
Example 20
Source File: Sphere.java    From Pano360 with MIT License 4 votes vote down vote up
/**
 * modified from hzqiujiadi on 16/1/8.
 * original source code:
 * https://github.com/shulja/viredero/blob/a7d28b21d762e8479dc10cde1aa88054497ff649/viredroid/src/main/java/org/viredero/viredroid/Sphere.java
 * @param radius -between near plane and far plane.
 * @param rings
 * @param sectors
 */
public Sphere(float radius, int rings, int sectors) {
    final float PI = (float) Math.PI;
    final float PI_2 = (float) (Math.PI / 2);

    float R = 1f/(float)rings;
    float S = 1f/(float)sectors;
    short r, s;
    float x, y, z;

    int numPoint = (rings + 1) * (sectors + 1);
    float[] vertexs = new float[numPoint * 3];
    float[] texcoords = new float[numPoint * 2];
    short[] indices = new short[numPoint * 6];

    //map texture 2d-3d
    int t = 0, v = 0;
    for(r = 0; r < rings + 1; r++) {
        for(s = 0; s < sectors + 1; s++) {
            x = (float) (Math.cos(2*PI * s * S) * Math.sin( PI * r * R ));
            y = (float) Math.sin( -PI_2 + PI * r * R );
            z = (float) (Math.sin(2*PI * s * S) * Math.sin( PI * r * R ));

            texcoords[t++] = s*S;
            texcoords[t++] = r*R;

            vertexs[v++] = x * radius;
            vertexs[v++] = y * radius;
            vertexs[v++] = z * radius;
        }
    }

    //glDrawElements
    int counter = 0;
    int sectorsPlusOne = sectors + 1;
    for(r = 0; r < rings; r++){
        for(s = 0; s < sectors; s++) {
            indices[counter++] = (short) (r * sectorsPlusOne + s);       //(a)
            indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s));    //(b)
            indices[counter++] = (short) ((r) * sectorsPlusOne + (s+1));  // (c)
            indices[counter++] = (short) ((r) * sectorsPlusOne + (s+1));  // (c)
            indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s));    //(b)
            indices[counter++] = (short) ((r+1) * sectorsPlusOne + (s+1));  // (d)
        }
    }

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            vertexs.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(vertexs);
    vertexBuffer.position(0);

    // initialize vertex byte buffer for shape coordinates
    ByteBuffer cc = ByteBuffer.allocateDirect(
            texcoords.length * 4);
    cc.order(ByteOrder.nativeOrder());
    FloatBuffer texBuffer = cc.asFloatBuffer();
    texBuffer.put(texcoords);
    texBuffer.position(0);

    // initialize byte buffer for the draw list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            indices.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    indexBuffer = dlb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    mTexCoordinateBuffer=texBuffer;
    mVerticesBuffer=vertexBuffer;
    mNumIndices=indices.length;
}