com.jme3.scene.VertexBuffer.Format Java Examples

The following examples show how to use com.jme3.scene.VertexBuffer.Format. 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: GLRenderer.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private int convertFormat(Format format) {
    switch (format) {
        case Byte:
            return GL.GL_BYTE;
        case UnsignedByte:
            return GL.GL_UNSIGNED_BYTE;
        case Short:
            return GL.GL_SHORT;
        case UnsignedShort:
            return GL.GL_UNSIGNED_SHORT;
        case Int:
            return GL.GL_INT;
        case UnsignedInt:
            return GL.GL_UNSIGNED_INT;
        case Float:
            return GL.GL_FLOAT;
        case Double:
            return GL.GL_DOUBLE;
        default:
            throw new UnsupportedOperationException("Unknown buffer format.");

    }
}
 
Example #2
Source File: SkeletonPoints.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 #3
Source File: SkeletonInterBoneWire.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates buffers for points. Each line has POINT_AMOUNT of points.
 * @param skeleton
 *            the skeleton that will be showed
 * @param boneLengths
 *            the lengths of the bones
 */
public SkeletonInterBoneWire(Skeleton skeleton, Map<Integer, Float> boneLengths) {
    this.skeleton = skeleton;

    for (Bone bone : skeleton.getRoots()) {
        this.countConnections(bone);
    }

    this.setMode(Mode.Points);
    this.boneLengths = boneLengths;

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

    this.updateCounts();
}
 
Example #4
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Scales the texture coordinate buffer on this mesh by the given
 * scale factor. 
 * <p>
 * Note that values above 1 will cause the 
 * texture to tile, while values below 1 will cause the texture 
 * to stretch.
 * </p>
 * 
 * @param scaleFactor The scale factor to scale by. Every texture
 * coordinate is multiplied by this vector to get the result.
 * 
 * @throws IllegalStateException If there's no texture coordinate
 * buffer on the mesh
 * @throws UnsupportedOperationException If the texture coordinate
 * buffer is not in 2D float format.
 */
public void scaleTextureCoordinates(Vector2f scaleFactor){
    VertexBuffer tc = getBuffer(Type.TexCoord);
    if (tc == null)
        throw new IllegalStateException("The mesh has no texture coordinates");

    if (tc.getFormat() != VertexBuffer.Format.Float)
        throw new UnsupportedOperationException("Only float texture coord format is supported");

    if (tc.getNumComponents() != 2)
        throw new UnsupportedOperationException("Only 2D texture coords are supported");

    FloatBuffer fb = (FloatBuffer) tc.getData();
    fb.clear();
    for (int i = 0; i < fb.limit() / 2; i++){
        float x = fb.get();
        float y = fb.get();
        fb.position(fb.position()-2);
        x *= scaleFactor.getX();
        y *= scaleFactor.getY();
        fb.put(x).put(y);
    }
    fb.clear();
    tc.updateData(fb);
}
 
Example #5
Source File: LwjglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private int convertFormat(Format format) {
        switch (format) {
            case Byte:
                return GL_BYTE;
            case UnsignedByte:
                return GL_UNSIGNED_BYTE;
            case Short:
                return GL_SHORT;
            case UnsignedShort:
                return GL_UNSIGNED_SHORT;
            case Int:
                return GL_INT;
            case UnsignedInt:
                return GL_UNSIGNED_INT;
            case Half:
                return NVHalfFloat.GL_HALF_FLOAT_NV;
//                return ARBHalfFloatVertex.GL_HALF_FLOAT;
            case Float:
                return GL_FLOAT;
            case Double:
                return GL_DOUBLE;
            default:
                throw new UnsupportedOperationException("Unknown buffer format.");

        }
    }
 
Example #6
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected int convertFormat(Format format) {
    switch (format) {
        case Byte:
            return GL.GL_BYTE;
        case UnsignedByte:
            return GL.GL_UNSIGNED_BYTE;
        case Short:
            return GL.GL_SHORT;
        case UnsignedShort:
            return GL.GL_UNSIGNED_SHORT;
        case Int:
            return GL2ES2.GL_INT;
        case UnsignedInt:
            return GL2ES2.GL_UNSIGNED_INT;
        case Half:
            return GL.GL_HALF_FLOAT;
        case Float:
            return GL.GL_FLOAT;
        case Double:
            return GL2GL3.GL_DOUBLE;
        default:
            throw new RuntimeException("Unknown buffer format.");

    }
}
 
Example #7
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Creates a {@link VertexBuffer} for the mesh or modifies
 * the existing one per the parameters given.
 * 
 * @param type The type of the buffer
 * @param components Number of components
 * @param format Data format
 * @param buf The buffer data
 * 
 * @throws UnsupportedOperationException If the buffer already set is 
 * incompatible with the parameters given.
 */
public void setBuffer(Type type, int components, Format format, Buffer buf){
    VertexBuffer vb = buffers.get(type.ordinal());
    if (vb == null){
        vb = new VertexBuffer(type);
        vb.setupData(Usage.Dynamic, components, format, buf);
        setBuffer(vb);
    }else{
        if (vb.getNumComponents() != components || vb.getFormat() != format){
            throw new UnsupportedOperationException("The buffer already set "
                    + "is incompatible with the given parameters");
        }
        vb.updateData(buf);
        updateCounts();
    }
}
 
Example #8
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Gets the triangle vertex positions at the given triangle index 
 * and stores them into the v1, v2, v3 arguments.
 * 
 * @param index The index of the triangle. 
 * Should be between 0 and {@link #getTriangleCount()}.
 * 
 * @param v1 Vector to contain first vertex position
 * @param v2 Vector to contain second vertex position
 * @param v3 Vector to contain third vertex position
 */
public void getTriangle(int index, Vector3f v1, Vector3f v2, Vector3f v3){
    VertexBuffer pb = getBuffer(Type.Position);
    IndexBuffer ib = getIndicesAsList();
    if (pb != null && pb.getFormat() == Format.Float && pb.getNumComponents() == 3){
        FloatBuffer fpb = (FloatBuffer) pb.getData();

        // aquire triangle's vertex indices
        int vertIndex = index * 3;
        int vert1 = ib.get(vertIndex);
        int vert2 = ib.get(vertIndex+1);
        int vert3 = ib.get(vertIndex+2);

        BufferUtils.populateFromBuffer(v1, fpb, vert1);
        BufferUtils.populateFromBuffer(v2, fpb, vert2);
        BufferUtils.populateFromBuffer(v3, fpb, vert3);
    }else{
        throw new UnsupportedOperationException("Position buffer not set or "
                                              + " has incompatible format");
    }
}
 
Example #9
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected int convertVertexFormat(VertexBuffer.Format fmt) {
    switch (fmt) {
        case Byte:
            return GL.GL_BYTE;
        case Double:
            return GL2GL3.GL_DOUBLE;
        case Float:
            return GL.GL_FLOAT;
        case Half:
            return GL.GL_HALF_FLOAT;
        case Int:
            return GL2ES2.GL_INT;
        case Short:
            return GL.GL_SHORT;
        case UnsignedByte:
            return GL.GL_UNSIGNED_BYTE;
        case UnsignedInt:
            return GL2ES2.GL_UNSIGNED_INT;
        case UnsignedShort:
            return GL.GL_UNSIGNED_SHORT;
        default:
            throw new UnsupportedOperationException("Unrecognized vertex format: " + fmt);
    }
}
 
Example #10
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 #11
Source File: MeshLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void startBoneAssigns() {
    if (mesh != sharedMesh && usesSharedVerts) {
        // will use bone assignments from shared mesh (?)
        return;
    }

    // current mesh will have bone assigns
    //int vertCount = mesh.getVertexCount();
    // each vertex has
    // - 4 bone weights
    // - 4 bone indices
    // create array-backed buffers for software skinning for access speed
    weightsFloatData = FloatBuffer.allocate(vertCount * 4);
    indicesData = ByteBuffer.allocate(vertCount * 4);

    VertexBuffer weights = new VertexBuffer(Type.BoneWeight);
    VertexBuffer indices = new VertexBuffer(Type.BoneIndex);

    weights.setupData(Usage.CpuOnly, 4, Format.Float, weightsFloatData);
    indices.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indicesData);
    
    mesh.setBuffer(weights);
    mesh.setBuffer(indices);
    
    //creating empty buffers for HW skinning 
    //the buffers will be setup if ever used.
    VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
    VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
    //setting usage to cpuOnly so that the buffer is not send empty to the GPU
    indicesHW.setUsage(Usage.CpuOnly);
    weightsHW.setUsage(Usage.CpuOnly);
    mesh.setBuffer(weightsHW);
    mesh.setBuffer(indicesHW);
}
 
Example #12
Source File: MeshLoader.java    From MikuMikuStudio with 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 #13
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static VertexBuffer convertToFloat(VertexBuffer vb){
    if (vb.getFormat() == Format.Float)
        return vb;

    IntBuffer ib = (IntBuffer) vb.getData();
    FloatBuffer fb = BufferUtils.createFloatBuffer(ib.capacity());
    convertToFloat(ib, fb);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.Float,
                    fb);
    return newVb;
}
 
Example #14
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static VertexBuffer convertToFixed(VertexBuffer vb){
    if (vb.getFormat() == Format.Int)
        return vb;

    FloatBuffer fb = (FloatBuffer) vb.getData();
    IntBuffer ib = BufferUtils.createIntBuffer(fb.capacity());
    convertToFixed(fb, ib);

    VertexBuffer newVb = new VertexBuffer(vb.getBufferType());
    newVb.setupData(vb.getUsage(),
                    vb.getNumComponents(),
                    Format.Int,
                    ib);
    return newVb;
}
 
Example #15
Source File: GdxRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int convertFormat(Format format) {
        switch (format) {
            case Byte:
                return GL20.GL_BYTE;
            case UnsignedByte:
                return GL20.GL_UNSIGNED_BYTE;
            case Short:
                return GL20.GL_SHORT;
            case UnsignedShort:
                return GL20.GL_UNSIGNED_SHORT;
            case Int:
                return GL20.GL_INT;
            case UnsignedInt:
                return GL20.GL_UNSIGNED_INT;
            /*
            case Half:
            return NVHalfFloat.GL_HALF_FLOAT_NV;
            //                return ARBHalfFloatVertex.GL_HALF_FLOAT;
             */
            case Float:
                return GL20.GL_FLOAT;
//            case Double:
//                return GLES20.GL_DOUBLE;
            default:
                throw new RuntimeException("Unknown buffer format.");

        }
    }
 
Example #16
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected void drawElementsInstanced(Mode mode, int indices_count, Format format,
        long indices_buffer_offset, int primcount) {
    // GL gl = GLContext.getCurrentGL();
    // FIXME: not yet in JOGL
    // gl.getGL2GL3().glDrawElementsInstanced(convertElementMode(mode), indices_count,
    // convertVertexFormat(format), indices_buffer_offset, primcount);
}
 
Example #17
Source File: ModelConverter.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void optimize(Mesh mesh, boolean toFixed){
        // update any data that need updating
        mesh.updateBound();
        mesh.updateCounts();

        // set all buffers into STATIC_DRAW mode
        mesh.setStatic();

        if (mesh.getBuffer(Type.Index) != null){
            // compress index buffer from UShort to UByte (if possible)
            FloatToFixed.compressIndexBuffer(mesh);

            // generate triangle strips stitched with degenerate tris
            generateStrips(mesh, false, false, 16, 0);
        }

        IntMap<VertexBuffer> bufs = mesh.getBuffers();
        for (Entry<VertexBuffer> entry : bufs){
            VertexBuffer vb = entry.getValue();
            if (vb == null || vb.getBufferType() == Type.Index)
                continue;

             if (vb.getFormat() == Format.Float){
                if (vb.getBufferType() == Type.Color){
                    // convert the color buffer to UByte
                    vb = FloatToFixed.convertToUByte(vb);
                    vb.setNormalized(true);
                }else if (toFixed){
                    // convert normals, positions, and texcoords
                    // to fixed-point (16.16)
                    vb = FloatToFixed.convertToFixed(vb);
//                    vb = FloatToFixed.convertToFloat(vb);
                }
                mesh.clearBuffer(vb.getBufferType());
                mesh.setBuffer(vb);
            }
        }
        mesh.setInterleaved();
    }
 
Example #18
Source File: FloatToFixed.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void compressIndexBuffer(Mesh mesh){
    int vertCount = mesh.getVertexCount();
    VertexBuffer vb = mesh.getBuffer(Type.Index);
    Format targetFmt;
    if (vb.getFormat() == Format.UnsignedInt && vertCount <= 0xffff){
        if (vertCount <= 256)
            targetFmt = Format.UnsignedByte;
        else
            targetFmt = Format.UnsignedShort;
    }else if (vb.getFormat() == Format.UnsignedShort && vertCount <= 0xff){
        targetFmt = Format.UnsignedByte;
    }else{
        return;
    }

    IndexBuffer src = mesh.getIndexBuffer();
    Buffer newBuf = VertexBuffer.createBuffer(targetFmt, vb.getNumComponents(), src.size());

    VertexBuffer newVb = new VertexBuffer(Type.Index);
    newVb.setupData(vb.getUsage(), vb.getNumComponents(), targetFmt, newBuf);
    mesh.clearBuffer(Type.Index);
    mesh.setBuffer(newVb);

    IndexBuffer dst = mesh.getIndexBuffer();
    for (int i = 0; i < src.size(); i++){
        dst.put(i, src.get(i));
    }
}
 
Example #19
Source File: OGLESShaderRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private int convertFormat(Format format) {
        switch (format) {
            case Byte:
                return GLES20.GL_BYTE;
            case UnsignedByte:
                return GLES20.GL_UNSIGNED_BYTE;
            case Short:
                return GLES20.GL_SHORT;
            case UnsignedShort:
                return GLES20.GL_UNSIGNED_SHORT;
            case Int:
                return GLES20.GL_INT;
            case UnsignedInt:
                return GLES20.GL_UNSIGNED_INT;
            /*
            case Half:
            return NVHalfFloat.GL_HALF_FLOAT_NV;
            //                return ARBHalfFloatVertex.GL_HALF_FLOAT;
             */
            case Float:
                return GLES20.GL_FLOAT;
//            case Double:
//                return GLES20.GL_DOUBLE;
            default:
                throw new RuntimeException("Unknown buffer format.");

        }
    }
 
Example #20
Source File: RenderDeviceJme.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);

    niftyMat = new Material(display.getAssetManager(), "Common/MatDefs/Nifty/Nifty.j3md");
    niftyMat.getAdditionalRenderState().setDepthTest(false);
}
 
Example #21
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 5 votes vote down vote up
private static void putValue(VertexBuffer.Format format, Buffer buf1, Buffer buf2, int index) {
	switch (format) {
		case Byte:
		case Half:
		case UnsignedByte:
			byte b = ((ByteBuffer) buf2).get(index);
			((ByteBuffer) buf1).put(b);
			break;
		case Short:
		case UnsignedShort:
			short s = ((ShortBuffer) buf2).get(index);
			((ShortBuffer) buf1).put(s);
			break;

		case Int:
		case UnsignedInt:
			int i = ((IntBuffer) buf2).get(index);
			((IntBuffer) buf1).put(i);
			break;
		case Float:
			float f = ((FloatBuffer) buf2).get(index);
			((FloatBuffer) buf1).put(f);
			break;
		case Double:
			double d = ((DoubleBuffer) buf2).get(index);
			((DoubleBuffer) buf1).put(d);
			break;
		default:
			throw new UnsupportedOperationException("Unrecoginized buffer format: " + format);
	}
}
 
Example #22
Source File: SilentTangentBinormalGenerator.java    From OpenRTS with MIT License 5 votes vote down vote up
private static void bulkPut(VertexBuffer.Format format, Buffer buf1, Buffer buf2) {
	switch (format) {
		case Byte:
		case Half:
		case UnsignedByte:
			((ByteBuffer) buf1).put((ByteBuffer) buf2);
			break;
		case Short:
		case UnsignedShort:

			((ShortBuffer) buf1).put((ShortBuffer) buf2);
			break;

		case Int:
		case UnsignedInt:
			((IntBuffer) buf1).put((IntBuffer) buf2);
			break;
		case Float:

			((FloatBuffer) buf1).put((FloatBuffer) buf2);
			break;
		case Double:
			((DoubleBuffer) buf1).put((DoubleBuffer) buf2);
			break;

		default:
			throw new UnsupportedOperationException("Unrecoginized buffer format: " + format);
	}
}
 
Example #23
Source File: MeshLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void startLodFaceList(String submeshindex, String numfaces) {
    int index = Integer.parseInt(submeshindex);
    mesh = geoms.get(index).getMesh();
    int faceCount = Integer.parseInt(numfaces);

    VertexBuffer originalIndexBuffer = mesh.getBuffer(Type.Index);
    vb = new VertexBuffer(VertexBuffer.Type.Index);
    if (originalIndexBuffer.getFormat() == Format.UnsignedInt) {
        // LOD buffer should also be integer
        ib = BufferUtils.createIntBuffer(faceCount * 3);
        sb = null;
        vb.setupData(Usage.Static, 3, Format.UnsignedInt, ib);
    } else {
        sb = BufferUtils.createShortBuffer(faceCount * 3);
        ib = null;
        vb.setupData(Usage.Static, 3, Format.UnsignedShort, sb);
    }

    List<VertexBuffer> levels = lodLevels.get(index);
    if (levels == null) {
        // Create the LOD levels list
        levels = new ArrayList<VertexBuffer>();

        // Add the first LOD level (always the original index buffer)
        levels.add(originalIndexBuffer);
        lodLevels.put(index, levels);
    }
    levels.add(vb);
}
 
Example #24
Source File: SkeletonPoints.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SkeletonPoints(Skeleton skeleton){
    this.skeleton = skeleton;

    setMode(Mode.Points);

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

    setPointSize(7);

    updateCounts();
}
 
Example #25
Source File: RenderDeviceJme.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 #26
Source File: MeshLoader.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void startVertexBuffer(Attributes attribs) throws SAXException {
    if (parseBool(attribs.getValue("positions"), false)) {
        vb = new VertexBuffer(Type.Position);
        fb = BufferUtils.createFloatBuffer(vertCount * 3);
        vb.setupData(Usage.Static, 3, Format.Float, fb);
        mesh.setBuffer(vb);
    }
    if (parseBool(attribs.getValue("normals"), false)) {
        vb = new VertexBuffer(Type.Normal);
        fb = BufferUtils.createFloatBuffer(vertCount * 3);
        vb.setupData(Usage.Static, 3, Format.Float, fb);
        mesh.setBuffer(vb);
    }
    if (parseBool(attribs.getValue("colours_diffuse"), false)) {
        vb = new VertexBuffer(Type.Color);
        fb = BufferUtils.createFloatBuffer(vertCount * 4);
        vb.setupData(Usage.Static, 4, Format.Float, fb);
        mesh.setBuffer(vb);
    }
    if (parseBool(attribs.getValue("tangents"), false)) {
        int dimensions = parseInt(attribs.getValue("tangent_dimensions"), 3);
        vb = new VertexBuffer(Type.Tangent);
        fb = BufferUtils.createFloatBuffer(vertCount * dimensions);
        vb.setupData(Usage.Static, dimensions, Format.Float, fb);
        mesh.setBuffer(vb);
    }
    if (parseBool(attribs.getValue("binormals"), false)) {
        vb = new VertexBuffer(Type.Binormal);
        fb = BufferUtils.createFloatBuffer(vertCount * 3);
        vb.setupData(Usage.Static, 3, Format.Float, fb);
        mesh.setBuffer(vb);
    }

    int texCoords = parseInt(attribs.getValue("texture_coords"), 0);
    for (int i = 0; i < texCoords; i++) {
        int dims = parseInt(attribs.getValue("texture_coord_dimensions_" + i), 2);
        if (dims < 1 || dims > 4) {
            throw new SAXException("Texture coord dimensions must be 1 <= dims <= 4");
        }

        if (i <= 7) {
            vb = new VertexBuffer(TEXCOORD_TYPES[i]);
        } else {
            // more than 8 texture coordinates are not supported by ogre.
            throw new SAXException("More than 8 texture coordinates not supported");
        }
        fb = BufferUtils.createFloatBuffer(vertCount * dims);
        vb.setupData(Usage.Static, dims, Format.Float, fb);
        mesh.setBuffer(vb);
    }
}
 
Example #27
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 #28
Source File: TestCustomAnim.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void simpleInitApp() {

    AmbientLight al = new AmbientLight();
    rootNode.addLight(al);

    DirectionalLight dl = new DirectionalLight();
    dl.setDirection(Vector3f.UNIT_XYZ.negate());
    rootNode.addLight(dl);

    Box box = new Box(1, 1, 1);

    // Setup bone weight buffer
    FloatBuffer weights = FloatBuffer.allocate( box.getVertexCount() * 4 );
    VertexBuffer weightsBuf = new VertexBuffer(Type.BoneWeight);
    weightsBuf.setupData(Usage.CpuOnly, 4, Format.Float, weights);
    box.setBuffer(weightsBuf);

    // Setup bone index buffer
    ByteBuffer indices = ByteBuffer.allocate( box.getVertexCount() * 4 );
    VertexBuffer indicesBuf = new VertexBuffer(Type.BoneIndex);
    indicesBuf.setupData(Usage.CpuOnly, 4, Format.UnsignedByte, indices);
    box.setBuffer(indicesBuf);

    // Create bind pose buffers
    box.generateBindPose(true);

    // Create skeleton
    bone = new Bone("root");
    bone.setBindTransforms(Vector3f.ZERO, Quaternion.IDENTITY, Vector3f.UNIT_XYZ);
    bone.setUserControl(true);
    skeleton = new Skeleton(new Bone[]{ bone });

    // Assign all verticies to bone 0 with weight 1
    for (int i = 0; i < box.getVertexCount() * 4; i += 4){
        // assign vertex to bone index 0
        indices.array()[i+0] = 0;
        indices.array()[i+1] = 0;
        indices.array()[i+2] = 0;
        indices.array()[i+3] = 0;

        // set weight to 1 only for first entry
        weights.array()[i+0] = 1;
        weights.array()[i+1] = 0;
        weights.array()[i+2] = 0;
        weights.array()[i+3] = 0;
    }

    // Maximum number of weights per bone is 1
    box.setMaxNumWeights(1);

    // Create model
    Geometry geom = new Geometry("box", box);
    geom.setMaterial(assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m"));
    Node model = new Node("model");
    model.attachChild(geom);

    // Create skeleton control
    SkeletonControl skeletonControl = new SkeletonControl(skeleton);
    model.addControl(skeletonControl);

    rootNode.attachChild(model);
}
 
Example #29
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setBuffer(Type type, int components, IntBuffer buf) {
    setBuffer(type, components, Format.UnsignedInt, buf);
}
 
Example #30
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setBuffer(Type type, int components, ShortBuffer buf) {
    setBuffer(type, components, Format.UnsignedShort, buf);
}