Java Code Examples for com.jme3.util.BufferUtils

The following examples show how to use com.jme3.util.BufferUtils. These examples are extracted from open source projects. 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 Project: jmonkeybuilder   Source File: PaintTerrainToolControl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Start making changes.
 */
@JmeThread
private void startChange() {

    final Array<ColorPoint> colorPoints = getColorPoints();
    colorPoints.clear();

    final Texture alphaTexture = notNull(getAlphaTexture());
    final Image image = alphaTexture.getImage();
    final ByteBuffer data = image.getData(0);

    if (prevBuffer == null) {
        prevBuffer = BufferUtils.createByteBuffer(data.capacity());
    } else if (prevBuffer.capacity() < data.capacity()) {
        BufferUtils.destroyDirectBuffer(prevBuffer);
        prevBuffer = BufferUtils.createByteBuffer(data.capacity());
    }

    final int position = data.position();
    data.position(0);
    prevBuffer.clear();
    prevBuffer.put(data);
    prevBuffer.flip();
    data.position(position);
}
 
Example 2
@Override
public void setMeshes(List<Mesh> meshes) {
    this.vertices = new ArrayList<List<Vector3f>>(meshes.size());
    this.normals = new ArrayList<List<Vector3f>>(meshes.size());
    for (Mesh mesh : meshes) {
        Vector3f[] vertexTable = BufferUtils.getVector3Array(mesh.getFloatBuffer(Type.Position));
        int[] indices = new int[3];
        List<Vector3f> vertices = new ArrayList<Vector3f>(mesh.getTriangleCount() * 3);
        List<Vector3f> normals = new ArrayList<Vector3f>(mesh.getTriangleCount());
        for (int i = 0; i < mesh.getTriangleCount(); ++i) {
            mesh.getTriangle(i, indices);
            vertices.add(vertexTable[indices[0]]);
            vertices.add(vertexTable[indices[1]]);
            vertices.add(vertexTable[indices[2]]);
            normals.add(FastMath.computeNormal(vertexTable[indices[0]], vertexTable[indices[1]], vertexTable[indices[2]]));
        }
        this.vertices.add(vertices);
        this.normals.add(normals);
    }
}
 
Example 3
Source Project: MikuMikuStudio   Source File: Pose.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Applies the offsets of this pose to the vertex buffer given by the blend factor.
 *
 * @param blend Blend factor, 0 = no change to vertex buffer, 1 = apply full offsets
 * @param vertbuf Vertex buffer to apply this pose to
 */
public void apply(float blend, FloatBuffer vertbuf){
    for (int i = 0; i < indices.length; i++){
        Vector3f offset = offsets[i];
        int vertIndex   = indices[i];

        tempVec.set(offset).multLocal(blend);

        // acquire vertex
        BufferUtils.populateFromBuffer(tempVec2, vertbuf, vertIndex);

        // add offset multiplied by factor
        tempVec2.addLocal(tempVec);

        // write modified vertex
        BufferUtils.setInBuffer(tempVec2, vertbuf, vertIndex);
    }
}
 
Example 4
Source Project: OpenRTS   Source File: Circle.java    License: MIT License 6 votes vote down vote up
protected void updateGeometry() {
	FloatBuffer positions = BufferUtils.createFloatBuffer(samples * 3);
	FloatBuffer normals = BufferUtils.createFloatBuffer(samples * 3);
	short[] indices = new short[samples * 2];

	float rate = FastMath.TWO_PI / samples;
	float angle = 0;
	for (int i = 0; i < samples; i++) {
		float x = FastMath.cos(angle) + center.x;
		float y = FastMath.sin(angle) + center.y;
		positions.put(x * radius).put(y * radius).put(center.z);
		normals.put(new float[] { 0, 1, 0 });
		indices[i * 2] = (short) i;
		indices[i * 2 + 1] = (short) ((i + 1) % samples);
		angle += rate;
	}

	setBuffer(Type.Position, 3, positions);
	setBuffer(Type.Normal, 3, normals);
	setBuffer(Type.Index, 2, indices);

	setBuffer(Type.TexCoord, 2, new float[] { 0, 0, 1, 1 });

	updateBound();
}
 
Example 5
public Batch() {
    // setup mesh
    vertexPos.setupData(Usage.Stream, 2, VertexBuffer.Format.Float, BufferUtils.createFloatBuffer(BATCH_MAX_VERTICES * 2));
    vertexPosBuffer = (FloatBuffer) vertexPos.getData();
    mesh.setBuffer(vertexPos);

    vertexTexCoord.setupData(Usage.Stream, 2, VertexBuffer.Format.Float, BufferUtils.createFloatBuffer(BATCH_MAX_VERTICES * 2));
    vertexTexCoordBuffer = (FloatBuffer) vertexTexCoord.getData();
    mesh.setBuffer(vertexTexCoord);

    vertexColor.setupData(Usage.Stream, 4, VertexBuffer.Format.Float, BufferUtils.createFloatBuffer(BATCH_MAX_VERTICES * 4));
    vertexColorBuffer = (FloatBuffer) vertexColor.getData();
    mesh.setBuffer(vertexColor);

    indexBuffer.setupData(Usage.Stream, 3, VertexBuffer.Format.UnsignedShort, BufferUtils.createShortBuffer(BATCH_MAX_QUADS * 2 * 3));
    indexBufferBuffer = (ShortBuffer) indexBuffer.getData();
    mesh.setBuffer(indexBuffer);

    material = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    material.setBoolean("VertexColor", true);

    renderState.setDepthTest(false);
    renderState.setDepthWrite(false);
}
 
Example 6
Source Project: MikuMikuStudio   Source File: WireFrustum.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void update(Vector3f[] points){
    VertexBuffer vb = getBuffer(Type.Position);
    if (vb == null){
        setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(points));
        return;
    }

    FloatBuffer b = BufferUtils.createFloatBuffer(points);
    FloatBuffer a = (FloatBuffer) vb.getData();
    b.rewind();
    a.rewind();
    a.put(b);
    a.rewind();

    vb.updateData(a);
    
    updateBound();
}
 
Example 7
@Override
public void setMeshes(List<Mesh> meshes) {
    this.vertices = new ArrayList<List<Vector3f>>(meshes.size());
    this.normals = new ArrayList<List<Vector3f>>(meshes.size());
    for (Mesh mesh : meshes) {
        Vector3f[] vertexTable = BufferUtils.getVector3Array(mesh.getFloatBuffer(Type.Position));
        int[] indices = new int[3];
        List<Vector3f> vertices = new ArrayList<Vector3f>(mesh.getTriangleCount() * 3);
        List<Vector3f> normals = new ArrayList<Vector3f>(mesh.getTriangleCount());
        for (int i = 0; i < mesh.getTriangleCount(); ++i) {
            mesh.getTriangle(i, indices);
            vertices.add(vertexTable[indices[0]]);
            vertices.add(vertexTable[indices[1]]);
            vertices.add(vertexTable[indices[2]]);
            normals.add(FastMath.computeNormal(vertexTable[indices[0]], vertexTable[indices[1]], vertexTable[indices[2]]));
        }
        this.vertices.add(vertices);
        this.normals.add(normals);
    }
}
 
Example 8
/**
     * Instantiate the configured shape in Bullet.
     */
    protected void createShape() {
//        ObjectArrayList<Vector3f> pointList = new ObjectArrayList<Vector3f>();
//        for (int i = 0; i < points.length; i += 3) {
//            pointList.add(new Vector3f(points[i], points[i + 1], points[i + 2]));
//        }
//        objectId = new ConvexHullShape(pointList);
//        objectId.setLocalScaling(Converter.convert(getScale()));
//        objectId.setMargin(margin);
        ByteBuffer bbuf=BufferUtils.createByteBuffer(points.length * 4); 
//        fbuf = bbuf.asFloatBuffer();
//        fbuf.rewind();
//        fbuf.put(points);
        for (int i = 0; i < points.length; i++) {
            float f = points[i];
            bbuf.putFloat(f);
        }
        bbuf.rewind();
        objectId = createShape(bbuf);
        Logger.getLogger(this.getClass().getName()).log(Level.FINE, "Created Shape {0}", Long.toHexString(objectId));
        setScale(scale);
        setMargin(margin);
    }
 
Example 9
Source Project: MikuMikuStudio   Source File: MeshBuilder.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * @param materialNumber
 *            the material index
 * @return the vertices colors buffer or null if no vertex colors is set
 */
public ByteBuffer getVertexColorsBuffer(int materialNumber) {
    ByteBuffer result = null;
    if (verticesColors != null && vertexColorsMap.get(materialNumber) != null) {
        List<byte[]> data = vertexColorsMap.get(materialNumber);
        result = BufferUtils.createByteBuffer(4 * data.size());
        for (byte[] v : data) {
            if (v != null) {
                result.put(v[0]).put(v[1]).put(v[2]).put(v[3]);
            } else {
                result.put((byte) 0).put((byte) 0).put((byte) 0).put((byte) 0);
            }
        }
        result.flip();
    }
    return result;
}
 
Example 10
/**
 * De-serialize this shape, for example when loading from a J3O file.
 *
 * @param im importer (not null)
 * @throws IOException from importer
 */
@Override
public void read(final JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule capsule = im.getCapsule(this);
    this.numVertices = capsule.readInt(MeshCollisionShape.NUM_VERTICES, 0);
    this.numTriangles = capsule.readInt(MeshCollisionShape.NUM_TRIANGLES, 0);
    this.vertexStride = capsule.readInt(MeshCollisionShape.VERTEX_STRIDE, 0);
    this.triangleIndexStride = capsule.readInt(MeshCollisionShape.TRIANGLE_INDEX_STRIDE, 0);

    this.triangleIndexBase = BufferUtils.createByteBuffer(capsule.readByteArray(MeshCollisionShape.TRIANGLE_INDEX_BASE, null));
    this.vertexBase = BufferUtils.createByteBuffer(capsule.readByteArray(MeshCollisionShape.VERTEX_BASE, null));

    byte[] nativeBvh = capsule.readByteArray(MeshCollisionShape.NATIVE_BVH, null);
    memoryOptimized=nativeBvh != null;
    createShape(nativeBvh);
}
 
Example 11
/**
 * Inits the mip maps of a cube map with the given number of mip maps
 * @param nbMipMaps the number of mip maps to initialize
 */
public void initMipMaps(int nbMipMaps) {
    int maxMipMap = (int) (Math.log(image.getWidth()) / Math.log(2) + 1);
    if (nbMipMaps > maxMipMap) {
        throw new IllegalArgumentException("Max mip map number for a " + image.getWidth() + "x" + image.getHeight() + " cube map is " + maxMipMap);
    }

    sizes = new int[nbMipMaps];

    int totalSize = 0;
    for (int i = 0; i < nbMipMaps; i++) {
        int size = (int) pow(2, maxMipMap - 1 - i);
        sizes[i] = size * size * image.getFormat().getBitsPerPixel() / 8;
        totalSize += sizes[i];
    }
    
    image.setMipMapSizes(sizes);        
    image.getData().clear();
    for (int i = 0; i < 6; i++) {
        image.addData(BufferUtils.createByteBuffer(totalSize));
    }
    mipMapRaster = new MipMapImageRaster(image, 0);        
}
 
Example 12
/**
 *  Retrieves the vertices from the Triangle buffer.
 */
public FloatBuffer getVertices() {
    // There are 3 floats needed for each vertex (x,y,z)
    final int numberOfFloats = vertices.size() * 3;
    FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(numberOfFloats); 

    // Force the limit, set the cap - most number of floats we will use the buffer for
    verticesBuffer.limit(numberOfFloats);

    // Copy the values from the list to the direct float buffer
    for (Vector3f v : vertices) {
        verticesBuffer.put(v.x).put(v.y).put(v.z);
    }

    vertices.clear();
    return verticesBuffer;
}
 
Example 13
Source Project: OpenRTS   Source File: OpenRTSApplication.java    License: MIT License 6 votes vote down vote up
@Override
public void onAction(String name, boolean value, float tpf) {
	if (!value) {
		return;
	}

	if (name.equals("SIMPLEAPP_Exit")) {
		stop();
	} else if (name.equals("SIMPLEAPP_CameraPos")) {
		if (cam != null) {
			Vector3f loc = cam.getLocation();
			Quaternion rot = cam.getRotation();
			System.out.println("Camera Position: (" + loc.x + ", " + loc.y + ", " + loc.z + ")");
			System.out.println("Camera Rotation: " + rot);
			System.out.println("Camera Direction: " + cam.getDirection());
		}
	} else if (name.equals("SIMPLEAPP_Memory")) {
		BufferUtils.printCurrentDirectMemory(null);
	}
}
 
Example 14
Source Project: jmonkeyengine   Source File: Pose.java    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Applies the offsets of this pose to the vertex buffer given by the blend factor.
 *
 * @param blend Blend factor, 0 = no change to vertex buffer, 1 = apply full offsets
 * @param vertbuf Vertex buffer to apply this pose to
 */
public void apply(float blend, FloatBuffer vertbuf){
    for (int i = 0; i < indices.length; i++){
        Vector3f offset = offsets[i];
        int vertIndex   = indices[i];

        tempVec.set(offset).multLocal(blend);

        // acquire vertex
        BufferUtils.populateFromBuffer(tempVec2, vertbuf, vertIndex);

        // add offset multiplied by factor
        tempVec2.addLocal(tempVec);

        // write modified vertex
        BufferUtils.setInBuffer(tempVec2, vertbuf, vertIndex);
    }
}
 
Example 15
/**
 * Creates a points with bone lengths data. If the data is supplied then the points will show both head and tail of each bone.
 * @param skeleton
 *            the skeleton that will be shown
 * @param boneLengths
 *            a map between the bone's index and the bone's length
 */
public SkeletonPoints(Skeleton skeleton, Map<Integer, Float> boneLengths) {
    this.skeleton = skeleton;
    this.setMode(Mode.Points);
    int pointsCount = skeleton.getBoneCount();

    if (boneLengths != null) {
        this.boneLengths = boneLengths;
        pointsCount *= 2;
    }

    VertexBuffer pb = new VertexBuffer(Type.Position);
    FloatBuffer fpb = BufferUtils.createFloatBuffer(pointsCount * 3);
    pb.setupData(Usage.Stream, 3, Format.Float, fpb);
    this.setBuffer(pb);

    this.updateCounts();

}
 
Example 16
public void update(Vector3f[] points){
    VertexBuffer vb = getBuffer(Type.Position);
    if (vb == null){
        setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(points));
        return;
    }

    FloatBuffer b = BufferUtils.createFloatBuffer(points);
    FloatBuffer a = (FloatBuffer) vb.getData();
    b.rewind();
    a.rewind();
    a.put(b);
    a.rewind();

    vb.updateData(a);
    
    updateBound();
}
 
Example 17
public WireSphere(float radius) {
        updatePositions(radius);
        ShortBuffer ib = BufferUtils.createShortBuffer(samples * 2 * 2 + zSamples * samples * 2 /*+ 3 * 2*/);
        setBuffer(Type.Index, 2, ib);

//        ib.put(new byte[]{
//            (byte) 0, (byte) 1,
//            (byte) 2, (byte) 3,
//            (byte) 4, (byte) 5,
//        });

//        int curNum = 3 * 2;
        int curNum = 0;
        for (int j = 0; j < 2 + zSamples; j++) {
            for (int i = curNum; i < curNum + samples - 1; i++) {
                ib.put((short) i).put((short) (i + 1));
            }
            ib.put((short) (curNum + samples - 1)).put((short) curNum);
            curNum += samples;
        }

        setMode(Mode.Lines);

        updateBound();
        updateCounts();
    }
 
Example 18
private static void convertNormals(FloatBuffer input, ByteBuffer output){
    if (output.capacity() < input.capacity())
        throw new RuntimeException("Output must be at least as large as input!");

    input.clear();
    output.clear();
    Vector3f temp = new Vector3f();
    int vertexCount = input.capacity() / 3;
    for (int i = 0; i < vertexCount; i++){
        BufferUtils.populateFromBuffer(temp, input, i);

        // offset and scale vector into -128 ... 127
        temp.multLocal(127).addLocal(0.5f, 0.5f, 0.5f);

        // quantize
        byte v1 = (byte) temp.getX();
        byte v2 = (byte) temp.getY();
        byte v3 = (byte) temp.getZ();

        // store
        output.put(v1).put(v2).put(v3);
    }
}
 
Example 19
/**
 * Creates a new atlas texture for the given map name.
 * @param mapName
 * @return the atlas texture
 */
public Texture getAtlasTexture(String mapName) {
    if (images == null) {
        return null;
    }
    byte[] image = images.get(mapName);
    if (image != null) {
        //TODO check if color space shouldn't be sRGB
        Texture2D tex = new Texture2D(new Image(format, atlasWidth, atlasHeight, BufferUtils.createByteBuffer(image), null, ColorSpace.Linear));
        tex.setMagFilter(Texture.MagFilter.Bilinear);
        tex.setMinFilter(Texture.MinFilter.BilinearNearestMipMap);
        tex.setWrap(Texture.WrapMode.EdgeClamp);
        return tex;
    }
    return null;
}
 
Example 20
Source Project: MikuMikuStudio   Source File: PMDSkinData.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public PMDSkinData(DataInputStreamLittleEndian is) throws IOException {
//        PMDSkinVertData skinVertData[];
        skinName = is.readString(20);
//        System.out.println("skinName = "+skinName);
        skinVertCount = is.readInt();
        skinType = is.readByte();
//        System.out.println("skinVertCount = "+skinVertCount);
//        skinVertData = new PMDSkinVertData[skinVertCount];
//        for(int i=0;i<skinVertCount;i++) {
//            skinVertData[i] = new PMDSkinVertData(is);
//        }
        indexBuf = BufferUtils.createShortBuffer(skinVertCount);
        skinBuf = BufferUtils.createFloatBuffer(skinVertCount * 3);
        for(int i=0;i<skinVertCount;i++) {
            indexBuf.put((short)is.readInt());
            skinBuf.put(is.readFloat());
            skinBuf.put(is.readFloat());
            skinBuf.put(-is.readFloat());
        }
    }
 
Example 21
Source Project: MikuMikuStudio   Source File: PMDSkinMesh.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
    public synchronized PMDSkinMesh clone() {
        PMDSkinMesh newMesh = (PMDSkinMesh)super.clone();
//        newMesh.boneMatrixArray = new Matrix4f[boneMatrixArray.length];
        newMesh.skinvb2 = new VertexBuffer(VertexBuffer.Type.Position);
        FloatBuffer skinvfb2 = BufferUtils.clone((FloatBuffer)this.skinvb2.getData());
        newMesh.skinvb2.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb2);
        
//        newMesh.skinnb2 = new VertexBuffer(VertexBuffer.Type.Normal);
//        FloatBuffer skinnfb2 = BufferUtils.clone((FloatBuffer)this.skinnb2.getData());
//        newMesh.skinnb2.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.Float, skinnfb2);
        
        VertexBuffer skinvb1 = new VertexBuffer(VertexBuffer.Type.Position);
//        FloatBuffer skinvfb1 = BufferUtils.clone((FloatBuffer)this.skinvb2.getData());
        FloatBuffer skinvfb1 = BufferUtils.clone((FloatBuffer)this.getBuffer(VertexBuffer.Type.Position).getData());
        skinvb1.setupData(VertexBuffer.Usage.Dynamic, 3, VertexBuffer.Format.Float, skinvfb1);
        newMesh.clearBuffer(VertexBuffer.Type.Position);
        newMesh.setBuffer(skinvb1);
        
//        VertexBuffer skinnb1 = new VertexBuffer(VertexBuffer.Type.Normal);
//        FloatBuffer skinnfb1 = BufferUtils.clone((FloatBuffer)this.skinnb2.getData());
//        FloatBuffer skinnfb1 = BufferUtils.clone((FloatBuffer)this.getBuffer(VertexBuffer.Type.Normal).getData());
//        skinnb1.setupData(VertexBuffer.Usage.Stream, 3, VertexBuffer.Format.Float, skinnfb1);
//        newMesh.clearBuffer(VertexBuffer.Type.Normal);
//        newMesh.setBuffer(skinnb1);
        FloatBuffer newBoneMatrixBuffer = BufferUtils.createFloatBuffer(boneMatrixBuffer.capacity());
        boneMatrixBuffer.position(0);
        newBoneMatrixBuffer.put(boneMatrixBuffer);
        newBoneMatrixBuffer.position(0);
        newMesh.setBoneMatrixBuffer(newBoneMatrixBuffer);
        return newMesh;
    }
 
Example 22
Source Project: MikuMikuStudio   Source File: OGGLoader.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ByteBuffer readToBuffer() throws IOException{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buf = new byte[512];
    int read = 0;

    try {
        while ( (read = vorbisStream.readPcm(buf, 0, buf.length)) > 0){
            baos.write(buf, 0, read);
        }
    } catch (EndOfOggStreamException ex){
    }

   
    byte[] dataBytes = baos.toByteArray();
    swapBytes(dataBytes, 0, dataBytes.length);
    
    int bytesToCopy = getOggTotalBytes( dataBytes.length );

    ByteBuffer data = BufferUtils.createByteBuffer(bytesToCopy);
    data.put(dataBytes, 0, bytesToCopy).flip();

    vorbisStream.close();
    loStream.close();
    oggStream.close();

    return data;
}
 
Example 23
private Texture2D createAtlasTextureInternal(final int width, final int height) throws Exception {
    ByteBuffer initialData = BufferUtils.createByteBuffer(width * height * 4);
    for (int i = 0; i < width * height * 4; i++) {
        initialData.put((byte) 0x00);
    }
    initialData.rewind();

    Texture2D texture = new Texture2D(new com.jme3.texture.Image(Format.RGBA8, width, height, initialData, ColorSpace.sRGB));
    texture.setMinFilter(MinFilter.NearestNoMipMaps);
    texture.setMagFilter(MagFilter.Nearest);
    return texture;
}
 
Example 24
public RenderDeviceJme(NiftyJmeDisplay display) {
    this.display = display;

    quadColor = new VertexBuffer(Type.Color);
    quadColor.setNormalized(true);
    ByteBuffer bb = BufferUtils.createByteBuffer(4 * 4);
    quadColor.setupData(Usage.Stream, 4, Format.UnsignedByte, bb);
    quad.setBuffer(quadColor);

    quadModTC.setUsage(Usage.Stream);

    // Load the 3 material types separately to avoid
    // reloading the shader when the defines change.

    // Material with a single color (no texture or vertex color)
    colorMaterial = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");

    // Material with a texture and a color (no vertex color)
    textureColorMaterial = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");

    // Material with vertex color, used for gradients (no texture)
    vertexColorMaterial = new Material(display.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
    vertexColorMaterial.setBoolean("VertexColor", true);

    // Shared render state for all materials
    renderState.setDepthTest(false);
    renderState.setDepthWrite(false);
}
 
Example 25
public static VertexBuffer convertToUByte(VertexBuffer vb){
    FloatBuffer fb = (FloatBuffer) vb.getData();
    ByteBuffer bb = BufferUtils.createByteBuffer(fb.capacity());
    convertToUByte(fb, bb);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.UnsignedByte,
                    bb);
    newVb.setNormalized(true);
    return newVb;
}
 
Example 26
Source Project: MikuMikuStudio   Source File: MeshLoader.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void startFaces(String count) throws SAXException {
    int numFaces = parseInt(count);
    int indicesPerFace = 0;

    switch (mesh.getMode()) {
        case Triangles:
            indicesPerFace = 3;
            break;
        case Lines:
            indicesPerFace = 2;
            break;
        case Points:
            indicesPerFace = 1;
            break;
        default:
            throw new SAXException("Strips or fans not supported!");
    }

    int numIndices = indicesPerFace * numFaces;

    vb = new VertexBuffer(VertexBuffer.Type.Index);
    if (!usesBigIndices) {
        sb = BufferUtils.createShortBuffer(numIndices);
        ib = null;
        vb.setupData(Usage.Static, indicesPerFace, Format.UnsignedShort, sb);
    } else {
        ib = BufferUtils.createIntBuffer(numIndices);
        sb = null;
        vb.setupData(Usage.Static, indicesPerFace, Format.UnsignedInt, ib);
    }
    mesh.setBuffer(vb);
}
 
Example 27
public static void flipImage(Image img, int index){
    if (img.getFormat().isCompressed())
        throw new UnsupportedOperationException("Flipping compressed " +
                                                "images is unsupported.");

    int w = img.getWidth();
    int h = img.getHeight();
    int halfH = h / 2;

    // bytes per pixel
    int bpp = img.getFormat().getBitsPerPixel() / 8;
    int scanline = w * bpp;

    ByteBuffer data = img.getData(index);
    ByteBuffer temp = BufferUtils.createByteBuffer(scanline);
    
    data.rewind();
    for (int y = 0; y < halfH; y++){
        int oppY = h - y - 1;
        // read in scanline
        data.position(y * scanline);
        data.limit(data.position() + scanline);

        temp.rewind();
        temp.put(data);

    }
}
 
Example 28
/**
 * Dispose this renderer. The underlying frame buffer is also disposed.
 */
public void dispose() {
  while (!frameState.compareAndSet(WAITING_STATE, DISPOSING_STATE)) ;
  while (!imageState.compareAndSet(WAITING_STATE, DISPOSING_STATE)) ;
  frameBuffer.dispose();
  BufferUtils.destroyDirectBuffer(frameByteBuffer);
  frameState.compareAndSet(DISPOSING_STATE, DISPOSED_STATE);
  imageState.compareAndSet(DISPOSING_STATE, DISPOSED_STATE);
}
 
Example 29
private void setupOffBuffer() {
        image = new BufferedImage(width, height,
                BufferedImage.TYPE_4BYTE_ABGR);
        cpuBuf = BufferUtils.createByteBuffer(width * height * 4);
//        cpuArray = new byte[width * height * 4];
        offBuffer = new FrameBuffer(width, height, 0);
        //setup framebuffer to use texture
        offBuffer.setDepthBuffer(Format.Depth);
        offBuffer.setColorBuffer(Format.RGBA8);
        //set viewport to render to offscreen framebuffer
        viewPort.setOutputFrameBuffer(offBuffer);
        camera.resize(width, height, false);
    }
 
Example 30
private Texture createTexture(String name) {
    ByteBuffer bb = BufferUtils.createByteBuffer(3);
    Image image = new Image(Format.RGB8, 1, 1, bb, ColorSpace.sRGB);
    Texture2D texture = new Texture2D(image);
    texture.setName(name);
    return texture;
}