Java Code Examples for java.nio.ShortBuffer#flip()

The following examples show how to use java.nio.ShortBuffer#flip() . 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: OpusDecoder.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
/**
 * Encode the input buffer to output.
 * @param directInput Input byte buffer
 * @param directOutput Output sample buffer
 * @return Number of bytes written to the output
 */
public int decode(ByteBuffer directInput, ShortBuffer directOutput) {
  checkNotReleased();

  if (!directInput.isDirect() || !directOutput.isDirect()) {
    throw new IllegalArgumentException("Arguments must be direct buffers.");
  }

  directOutput.clear();
  int result = library.decode(instance, directInput, directInput.remaining(), directOutput, directOutput.remaining() / channels);

  if (result < 0) {
    throw new IllegalStateException("Decoding failed with error " + result);
  }

  directOutput.position(result * channels);
  directOutput.flip();

  return result;
}
 
Example 2
Source File: AudioChannel.java    From Mp4Composer-android with MIT License 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = overflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        outBuff.put(inBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs =
                sampleCountToDurationUs(inBuff.position(), inputSampleRate, inputChannelCount);
        overflowBuff.put(inBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        overflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        outBuff.put(inBuff);
    }
    return input.presentationTimeUs;
}
 
Example 3
Source File: AudioChannel.java    From android-transcoder with Apache License 2.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        mRemixer.remix(inBuff, outBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs =
                sampleCountToDurationUs(inBuff.position(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 4
Source File: BufferUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ShortBuffer createShortBuffer(short... data) {
    if (data == null) {
        return null;
    }
    ShortBuffer buff = createShortBuffer(data.length);
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}
 
Example 5
Source File: AudioChannel.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        mRemixer.remix(inBuff, outBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs =
                sampleCountToDurationUs(inBuff.position(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 6
Source File: OggOpusEnc.java    From speech-android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Encode raw audio data into Opus format then call OpusWriter to write the Ogg packet
 *
 * @param rawAudio
 * @return
 * @throws IOException
 */
public int encodeAndWrite(byte[] rawAudio) throws IOException {
    int uploadedAudioSize = 0;
    ByteArrayInputStream ios = new ByteArrayInputStream(rawAudio);

    byte[] data = new byte[SpeechConfiguration.FRAME_SIZE*2];
    int bufferSize, read;

    while((read = ios.read(data)) > 0){
        bufferSize = read;
        byte[] pcmBuffer = new byte[read];
        System.arraycopy(data, 0, pcmBuffer, 0, read);

        ShortBuffer shortBuffer = ShortBuffer.allocate(bufferSize);
        for (int i = 0; i < read; i += 2) {
            int b1 = pcmBuffer[i] & 0xff;
            int b2 = pcmBuffer[i+1] << 8;
            shortBuffer.put((short) (b1 | b2));
        }
        shortBuffer.flip();
        ByteBuffer opusBuffer = ByteBuffer.allocate(bufferSize);

        int opus_encoded = JNAOpus.INSTANCE.opus_encode(this.opusEncoder, shortBuffer, SpeechConfiguration.FRAME_SIZE, opusBuffer, bufferSize);

        opusBuffer.position(opus_encoded);
        opusBuffer.flip();

        byte[] opusData = new byte[opusBuffer.remaining()];
        opusBuffer.get(opusData, 0, opusData.length);

        if (opus_encoded > 0) {
            uploadedAudioSize += opusData.length;
            writer.writePacket(opusData, 0, opusData.length);
        }
    }

    ios.close();
    return uploadedAudioSize;
}
 
Example 7
Source File: AudioChannel.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        mRemixer.remix(inBuff, outBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs =
                sampleCountToDurationUs(inBuff.position(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 8
Source File: AudioChannel.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input,
                                       final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        mRemixer.remix(inBuff, outBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs =
                sampleCountToDurationUs(inBuff.position(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 9
Source File: BufferUtils.java    From Ultraino with MIT License 5 votes vote down vote up
public static ShortBuffer createShortBuffer(short... data) {
    if (data == null) {
        return null;
    }
    ShortBuffer buff = createShortBuffer(data.length);
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}
 
Example 10
Source File: AudioChannel.java    From EZFilter with MIT License 5 votes vote down vote up
private long remixAndMaybeFillOverflow(final AudioBuffer input, final ShortBuffer outBuff) {
    final ShortBuffer inBuff = input.data;
    final ShortBuffer overflowBuff = mOverflowBuffer.data;

    outBuff.clear();

    // Reset position to 0, and set limit to capacity (Since MediaCodec doesn't do that for us)
    inBuff.clear();

    if (inBuff.remaining() > outBuff.remaining()) {
        // Overflow
        // Limit inBuff to outBuff's capacity
        inBuff.limit(outBuff.capacity());
        mRemixer.remix(inBuff, outBuff);

        // Reset limit to its own capacity & Keep position
        inBuff.limit(inBuff.capacity());

        // Remix the rest onto overflowBuffer
        // NOTE: We should only reach this point when overflow buffer is empty
        final long consumedDurationUs = sampleCountToDurationUs(inBuff.position(), mInputSampleRate, mInputChannelCount);
        mRemixer.remix(inBuff, overflowBuff);

        // Seal off overflowBuff & mark limit
        overflowBuff.flip();
        mOverflowBuffer.presentationTimeUs = input.presentationTimeUs + consumedDurationUs;
    } else {
        // No overflow
        mRemixer.remix(inBuff, outBuff);
    }

    return input.presentationTimeUs;
}
 
Example 11
Source File: BufferUtils.java    From aion-germany with GNU General Public License v3.0 5 votes vote down vote up
public static ShortBuffer createShortBuffer(short... data) {
	if (data == null) {
		return null;
	}
	ShortBuffer buff = createShortBuffer(data.length);
	buff.clear();
	buff.put(data);
	buff.flip();
	return buff;
}
 
Example 12
Source File: BufferUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static ShortBuffer createShortBuffer(short... data) {
    if (data == null) {
        return null;
    }
    ShortBuffer buff = createShortBuffer(data.length);
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}
 
Example 13
Source File: ParticleTriMesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void initParticleData(ParticleEmitter emitter, int numParticles) {
    setMode(Mode.Triangles);

    this.emitter = emitter;

    particlesCopy = new Particle[numParticles];

    // set positions
    FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles * 4);
    VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
    pvb.setupData(Usage.Stream, 3, Format.Float, pb);
    
    //if the buffer is already set only update the data
    VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
    if (buf != null) {
        buf.updateData(pb);
    } else {
        setBuffer(pvb);
    }
    
    // set colors
    ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4);
    VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
    cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
    cvb.setNormalized(true);
    
    buf = getBuffer(VertexBuffer.Type.Color);
    if (buf != null) {
        buf.updateData(cb);
    } else {
        setBuffer(cvb);
    }

    // set texcoords
    VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
    FloatBuffer tb = BufferUtils.createVector2Buffer(numParticles * 4);
    
    uniqueTexCoords = false;
    for (int i = 0; i < numParticles; i++){
        tb.put(0f).put(1f);
        tb.put(1f).put(1f);
        tb.put(0f).put(0f);
        tb.put(1f).put(0f);
    }
    tb.flip();
    tvb.setupData(Usage.Static, 2, Format.Float, tb);
    
    buf = getBuffer(VertexBuffer.Type.TexCoord);
    if (buf != null) {
        buf.updateData(tb);
    } else {
        setBuffer(tvb);
    }

    // set indices
    ShortBuffer ib = BufferUtils.createShortBuffer(numParticles * 6);
    for (int i = 0; i < numParticles; i++){
        int startIdx = (i * 4);

        // triangle 1
        ib.put((short)(startIdx + 1))
          .put((short)(startIdx + 0))
          .put((short)(startIdx + 2));

        // triangle 2
        ib.put((short)(startIdx + 1))
          .put((short)(startIdx + 2))
          .put((short)(startIdx + 3));
    }
    ib.flip();
    
    VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index);
    ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib);
    
    buf = getBuffer(VertexBuffer.Type.Index);
    if (buf != null) {
        buf.updateData(ib);
    } else {
        setBuffer(ivb);
    }
    
}
 
Example 14
Source File: GLMesh.java    From WraithEngine with Apache License 2.0 4 votes vote down vote up
@Override
public void update(VertexData vertexData)
{
    if (isDisposed())
        throw new IllegalStateException("Mesh already disposed!");

    FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertexData.getDataArray().length);
    vertexBuffer.put(vertexData.getDataArray());
    vertexBuffer.flip();

    ShortBuffer indexBuffer = BufferUtils.createShortBuffer(vertexData.getTriangles().length);
    indexBuffer.put(vertexData.getTriangles());
    indexBuffer.flip();

    if (!created)
    {
        vaoId = opengl.generateVertexArray();
        vboId = opengl.generateBuffer();
        indexId = opengl.generateBuffer();
    }

    bindStates.bindVao(vaoId);

    bindStates.bindBuffer(false, vboId);
    opengl.uploadBufferData(vertexBuffer);

    int stride = vertexData.getVertexByteSize();
    int offset = 0;

    ShaderAttributes attributes = vertexData.getAttributeSizes();
    for (int i = 0; i < attributes.getCount(); i++)
    {
        opengl.setVertexAttributePointer(i, attributes.getAttributeSize(i), stride, offset);
        offset += attributes.getAttributeSize(i) * 4;
    }

    bindStates.bindBuffer(true, indexId);
    opengl.uploadBufferData(indexBuffer);

    created = true;
    indexCount = vertexData.getTriangles().length;
}
 
Example 15
Source File: OggOpusEnc.java    From android-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Encode raw audio data into Opus format then call OpusWriter to write the Ogg packet.
 *
 * @param rawAudio the raw audio
 * @return the int
 * @throws IOException Signals that an I/O exception has occurred.
 */
public int encodeAndWrite(byte[] rawAudio) throws IOException {
  int uploadedAudioSize = 0;
  ByteArrayInputStream ios = new ByteArrayInputStream(rawAudio);

  byte[] data = new byte[SpeechConfiguration.FRAME_SIZE * 2];
  int bufferSize, read;

  while ((read = ios.read(data)) > 0) {
    bufferSize = read;
    byte[] pcmBuffer = new byte[read];
    System.arraycopy(data, 0, pcmBuffer, 0, read);

    ShortBuffer shortBuffer = ShortBuffer.allocate(bufferSize);
    for (int i = 0; i < read; i += 2) {
      int b1 = pcmBuffer[i] & 0xff;
      int b2 = pcmBuffer[i + 1] << 8;
      shortBuffer.put((short) (b1 | b2));
    }
    shortBuffer.flip();
    ByteBuffer opusBuffer = ByteBuffer.allocate(bufferSize);

    int opus_encoded = JNAOpus.INSTANCE.opus_encode(this.opusEncoder, shortBuffer, SpeechConfiguration.FRAME_SIZE,
            opusBuffer, bufferSize);

    opusBuffer.position(opus_encoded);
    opusBuffer.flip();

    byte[] opusData = new byte[opusBuffer.remaining()];
    opusBuffer.get(opusData, 0, opusData.length);

    if (opus_encoded > 0) {
      uploadedAudioSize += opusData.length;
      // System.out.println("This is where I'd write some data. " + uploadedAudioSize + " to be specific.");
      writer.writePacket(opusData, 0, opusData.length);
    }
  }

  ios.close();

  return uploadedAudioSize;
}
 
Example 16
Source File: MBox.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected void refreshGeometry() {
    // The nunmber of quads along a side is 1
    // plus the number of "slices"... or splits.
    // A box with 0 slices is just a regular 6 quad box.
    // A box with 1 slice all around has four quads per side, etc.
    //
    // Vertex count is quads + 1
    // Total number of vertexes if all sides are on is:
    // top/bottom = (xSlices + 2) * (zSlices + 2) * 2
    // front/back = (xSlices + 2) * (ySlices + 2) * 2
    // left/right = (zSlices + 2) * (ySlices + 2) * 2

    int xVertCount = slices[0] + 2;
    int yVertCount = slices[1] + 2;
    int zVertCount = slices[2] + 2;
    int xQuadCount = slices[0] + 1;
    int yQuadCount = slices[1] + 1;
    int zQuadCount = slices[2] + 1;

    int upVertCount = xVertCount * zVertCount;
    int frontVertCount = xVertCount * yVertCount;
    int sideVertCount = zVertCount * yVertCount;
    int upTriCount = xQuadCount * zQuadCount * 2;
    int frontTriCount = xQuadCount * yQuadCount * 2;
    int sideTriCount = zQuadCount * yQuadCount * 2;

    int vertCount = 0;
    int triCount = 0;

    if( (sideMask & TOP_MASK) != 0 ) {
        vertCount += upVertCount;
        triCount += upTriCount;
    }
    if( (sideMask & BOTTOM_MASK) != 0 ) {
        vertCount += upVertCount;
        triCount += upTriCount;
    }
    if( (sideMask & FRONT_MASK) != 0 ) {
        vertCount += frontVertCount;
        triCount += frontTriCount;
    }
    if( (sideMask & BACK_MASK) != 0 ) {
        vertCount += frontVertCount;
        triCount += frontTriCount;
    }
    if( (sideMask & LEFT_MASK) != 0 ) {
        vertCount += sideVertCount;
        triCount += sideTriCount;
    }
    if( (sideMask & RIGHT_MASK) != 0 ) {
        vertCount += sideVertCount;
        triCount += sideTriCount;
    }

    FloatBuffer verts = BufferUtils.createFloatBuffer(vertCount * 3);
    FloatBuffer norms = BufferUtils.createFloatBuffer(vertCount * 3);
    FloatBuffer texes = BufferUtils.createFloatBuffer(vertCount * 2);
    ShortBuffer index = BufferUtils.createShortBuffer(triCount * 3);

    int lastIndex = 0;
    if( (sideMask & TOP_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, TOP, 0, xVertCount, 2, zVertCount, 1,
                             verts, norms, texes, index);
    }
    if( (sideMask & BOTTOM_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, BOTTOM, 0, xVertCount, 2, zVertCount, 1,
                             verts, norms, texes, index);
    }
    if( (sideMask & FRONT_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, FRONT, 0, xVertCount, 1, yVertCount, 2,
                             verts, norms, texes, index);
    }
    if( (sideMask & BACK_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, BACK, 0, xVertCount, 1, yVertCount, 2,
                             verts, norms, texes, index);
    }
    if( (sideMask & LEFT_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, LEFT, 2, zVertCount, 1, yVertCount, 0,
                              verts, norms, texes, index);
    }
    if( (sideMask & RIGHT_MASK) != 0 ) {
        lastIndex = fillSide(lastIndex, RIGHT, 2, zVertCount, 1, yVertCount, 0,
                             verts, norms, texes, index);
    }

    index.flip();
    norms.flip();
    verts.flip();
    texes.flip();

    setBuffer(Type.Index, 3, index);

    setBuffer(Type.Position, 3, verts);
    setBuffer(Type.TexCoord, 2, texes);

    setBuffer(Type.Normal, 3, norms);

    updateBound();
    clearCollisionData();

}
 
Example 17
Source File: MapRenderer.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public void onSurfaceChanged(int width, int height) {
    //log.debug("onSurfaceChanged: new={}, {}x{}", mNewSurface, width, height);

    if (width <= 0 || height <= 0)
        return;

    GLState.viewport(width, height);

    //GL.scissor(0, 0, width, height);
    //GL.enable(GL20.SCISSOR_TEST);

    gl.clearStencil(0x00);

    gl.disable(GL.CULL_FACE);
    gl.blendFunc(GL.ONE, GL.ONE_MINUS_SRC_ALPHA);

    gl.frontFace(GL.CW);
    gl.cullFace(GL.BACK);

    if (!mNewSurface) {
        mMap.updateMap(false);
        return;
    }

    mNewSurface = false;

    /* initialize quad indices used by Texture- and LineTexRenderer */
    int[] vboIds = GLUtils.glGenBuffers(2);

    mQuadIndicesID = vboIds[0];

    short[] indices = new short[MAX_INDICES];
    for (int i = 0, j = 0; i < MAX_INDICES; i += 6, j += 4) {
        indices[i + 0] = (short) (j + 0);
        indices[i + 1] = (short) (j + 1);
        indices[i + 2] = (short) (j + 2);

        indices[i + 3] = (short) (j + 2);
        indices[i + 4] = (short) (j + 1);
        indices[i + 5] = (short) (j + 3);
    }
    ShortBuffer buf = MapRenderer.getShortBuffer(indices.length);
    buf.put(indices);
    buf.flip();

    GLState.bindElementBuffer(mQuadIndicesID);
    gl.bufferData(GL.ELEMENT_ARRAY_BUFFER,
            indices.length * 2, buf,
            GL.STATIC_DRAW);
    GLState.bindElementBuffer(GLState.UNBIND);

    /* initialize default quad */
    FloatBuffer floatBuffer = MapRenderer.getFloatBuffer(8);
    float[] quad = new float[]{-1, -1, -1, 1, 1, -1, 1, 1};
    floatBuffer.put(quad);
    floatBuffer.flip();
    mQuadVerticesID = vboIds[1];

    GLState.bindVertexBuffer(mQuadVerticesID);
    gl.bufferData(GL.ARRAY_BUFFER,
            quad.length * 4, floatBuffer,
            GL.STATIC_DRAW);
    GLState.bindVertexBuffer(GLState.UNBIND);

    GLState.init();

    mMap.updateMap(true);
}
 
Example 18
Source File: Grid.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Creates a grid debug shape.
 * @param xLines
 * @param yLines
 * @param lineDist 
 */
public Grid(int xLines, int yLines, float lineDist){
    xLines -= 2;
    yLines -= 2;
    int lineCount = xLines + yLines + 4;

    FloatBuffer fpb = BufferUtils.createFloatBuffer(6 * lineCount);
    ShortBuffer sib = BufferUtils.createShortBuffer(2 * lineCount);

    float xLineLen = (yLines + 1) * lineDist;
    float yLineLen = (xLines + 1) * lineDist;
    int curIndex = 0;

    // add lines along X
    for (int i = 0; i < xLines + 2; i++){
        float y = (i) * lineDist;

        // positions
        fpb.put(0)       .put(0).put(y);
        fpb.put(xLineLen).put(0).put(y);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    // add lines along Y
    for (int i = 0; i < yLines + 2; i++){
        float x = (i) * lineDist;

        // positions
        fpb.put(x).put(0).put(0);
        fpb.put(x).put(0).put(yLineLen);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    fpb.flip();
    sib.flip();

    setBuffer(Type.Position, 3, fpb);
    setBuffer(Type.Index, 2, sib);
    
    setMode(Mode.Lines);

    updateBound();
    updateCounts();
}
 
Example 19
Source File: Grid.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Creates a grid debug shape.
 * @param xLines
 * @param yLines
 * @param lineDist 
 */
public Grid(int xLines, int yLines, float lineDist){
    xLines -= 2;
    yLines -= 2;
    int lineCount = xLines + yLines + 4;

    FloatBuffer fpb = BufferUtils.createFloatBuffer(6 * lineCount);
    ShortBuffer sib = BufferUtils.createShortBuffer(2 * lineCount);

    float xLineLen = (yLines + 1) * lineDist;
    float yLineLen = (xLines + 1) * lineDist;
    int curIndex = 0;

    // add lines along X
    for (int i = 0; i < xLines + 2; i++){
        float y = (i) * lineDist;

        // positions
        fpb.put(0)       .put(0).put(y);
        fpb.put(xLineLen).put(0).put(y);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    // add lines along Y
    for (int i = 0; i < yLines + 2; i++){
        float x = (i) * lineDist;

        // positions
        fpb.put(x).put(0).put(0);
        fpb.put(x).put(0).put(yLineLen);

        // indices
        sib.put( (short) (curIndex++) );
        sib.put( (short) (curIndex++) );
    }

    fpb.flip();
    sib.flip();

    setBuffer(Type.Position, 3, fpb);
    setBuffer(Type.Index, 2, sib);
    
    setMode(Mode.Lines);

    updateBound();
    updateCounts();
    setStatic();
}
 
Example 20
Source File: ParticleTriMesh.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
    public void initParticleData(ParticleEmitter emitter, int numParticles) {
        setMode(Mode.Triangles);

        this.emitter = emitter;

//        particlesCopy = new Particle[numParticles];

        // set positions
        FloatBuffer pb = BufferUtils.createVector3Buffer(numParticles * 4);
        // if the buffer is already set only update the data
        VertexBuffer buf = getBuffer(VertexBuffer.Type.Position);
        if (buf != null) {
            buf.updateData(pb);
        } else {
            VertexBuffer pvb = new VertexBuffer(VertexBuffer.Type.Position);
            pvb.setupData(Usage.Stream, 3, Format.Float, pb);
            setBuffer(pvb);
        }
        
        // set colors
        ByteBuffer cb = BufferUtils.createByteBuffer(numParticles * 4 * 4);
        buf = getBuffer(VertexBuffer.Type.Color);
        if (buf != null) {
            buf.updateData(cb);
        } else {
            VertexBuffer cvb = new VertexBuffer(VertexBuffer.Type.Color);
            cvb.setupData(Usage.Stream, 4, Format.UnsignedByte, cb);
            cvb.setNormalized(true);
            setBuffer(cvb);
        }

        // set texcoords
        FloatBuffer tb = BufferUtils.createVector2Buffer(numParticles * 4);
        uniqueTexCoords = false;
        for (int i = 0; i < numParticles; i++){
            tb.put(0f).put(1f);
            tb.put(1f).put(1f);
            tb.put(0f).put(0f);
            tb.put(1f).put(0f);
        }
        tb.flip();
        
        buf = getBuffer(VertexBuffer.Type.TexCoord);
        if (buf != null) {
            buf.updateData(tb);
        } else {
            VertexBuffer tvb = new VertexBuffer(VertexBuffer.Type.TexCoord);
            tvb.setupData(Usage.Static, 2, Format.Float, tb);
            setBuffer(tvb);
        }

        // set indices
        ShortBuffer ib = BufferUtils.createShortBuffer(numParticles * 6);
        for (int i = 0; i < numParticles; i++){
            int startIdx = (i * 4);

            // triangle 1
            ib.put((short)(startIdx + 1))
              .put((short)(startIdx + 0))
              .put((short)(startIdx + 2));

            // triangle 2
            ib.put((short)(startIdx + 1))
              .put((short)(startIdx + 2))
              .put((short)(startIdx + 3));
        }
        ib.flip();

        buf = getBuffer(VertexBuffer.Type.Index);
        if (buf != null) {
            buf.updateData(ib);
        } else {
            VertexBuffer ivb = new VertexBuffer(VertexBuffer.Type.Index);
            ivb.setupData(Usage.Static, 3, Format.UnsignedShort, ib);
            setBuffer(ivb);
        }
        
        updateCounts();
    }