Search in sources :

Example 16 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class LodGenerator method gatherVertexData.

private void gatherVertexData(Mesh mesh, List<Vertex> vertexLookup) {
    //in case the model is currently animating with software animation
    //attempting to retrieve the bind position instead of the position.
    VertexBuffer position = mesh.getBuffer(VertexBuffer.Type.BindPosePosition);
    if (position == null) {
        position = mesh.getBuffer(VertexBuffer.Type.Position);
    }
    FloatBuffer pos = (FloatBuffer) position.getDataReadOnly();
    pos.rewind();
    while (pos.remaining() != 0) {
        Vertex v = new Vertex();
        v.position.setX(pos.get());
        v.position.setY(pos.get());
        v.position.setZ(pos.get());
        v.isSeam = false;
        Vertex existingV = findSimilar(v);
        if (existingV != null) {
            //vertex position already exists
            existingV.isSeam = true;
            v.isSeam = true;
        } else {
            vertexList.add(v);
        }
        vertexLookup.add(v);
    }
    pos.rewind();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer)

Example 17 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class TextureAtlas method applyCoords.

/**
     * Applies the texture coordinates to the given output mesh
     * if the DiffuseMap or ColorMap of the input geometry exist in the atlas.
     * @param geom The geometry to change the texture coordinate buffer on.
     * @param offset Target buffer offset.
     * @param outMesh The mesh to set the coords in (can be same as input).
     * @return true if texture has been found and coords have been changed, false otherwise.
     */
public boolean applyCoords(Geometry geom, int offset, Mesh outMesh) {
    Mesh inMesh = geom.getMesh();
    geom.computeWorldMatrix();
    VertexBuffer inBuf = inMesh.getBuffer(Type.TexCoord);
    VertexBuffer outBuf = outMesh.getBuffer(Type.TexCoord);
    if (inBuf == null || outBuf == null) {
        throw new IllegalStateException("Geometry mesh has no texture coordinate buffer.");
    }
    Texture tex = getMaterialTexture(geom, "DiffuseMap");
    if (tex == null) {
        tex = getMaterialTexture(geom, "ColorMap");
    }
    if (tex != null) {
        TextureAtlasTile tile = getAtlasTile(tex);
        if (tile != null) {
            FloatBuffer inPos = (FloatBuffer) inBuf.getData();
            FloatBuffer outPos = (FloatBuffer) outBuf.getData();
            tile.transformTextureCoords(inPos, offset, outPos);
            return true;
        } else {
            return false;
        }
    } else {
        throw new IllegalStateException("Geometry has no proper texture.");
    }
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer) MatParamTexture(com.jme3.material.MatParamTexture) Texture(com.jme3.texture.Texture)

Example 18 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class ObjectHelper method flipMeshIfRequired.

/**
     * The method flips the mesh if the scale is mirroring it. Mirroring scale has either 1 or all 3 factors negative.
     * If two factors are negative then there is no mirroring because a rotation and translation can be found that will
     * lead to the same transform when all scales are positive.
     * 
     * @param geometry
     *            the geometry that is being flipped if necessary
     * @param scale
     *            the scale vector of the given geometry
     */
private void flipMeshIfRequired(Geometry geometry, Vector3f scale) {
    float s = scale.x * scale.y * scale.z;
    if (s < 0 && geometry.getMesh() != null) {
        // negative s means that the scale is mirroring the object
        FloatBuffer normals = geometry.getMesh().getFloatBuffer(Type.Normal);
        if (normals != null) {
            for (int i = 0; i < normals.limit(); i += 3) {
                if (scale.x < 0) {
                    normals.put(i, -normals.get(i));
                }
                if (scale.y < 0) {
                    normals.put(i + 1, -normals.get(i + 1));
                }
                if (scale.z < 0) {
                    normals.put(i + 2, -normals.get(i + 2));
                }
            }
        }
        if (geometry.getMesh().getMode() == Mode.Triangles) {
            // there is no need to flip the indexes for lines and points
            LOGGER.finer("Flipping index order in triangle mesh.");
            Buffer indexBuffer = geometry.getMesh().getBuffer(Type.Index).getData();
            for (int i = 0; i < indexBuffer.limit(); i += 3) {
                if (indexBuffer instanceof ShortBuffer) {
                    short index = ((ShortBuffer) indexBuffer).get(i + 1);
                    ((ShortBuffer) indexBuffer).put(i + 1, ((ShortBuffer) indexBuffer).get(i + 2));
                    ((ShortBuffer) indexBuffer).put(i + 2, index);
                } else {
                    int index = ((IntBuffer) indexBuffer).get(i + 1);
                    ((IntBuffer) indexBuffer).put(i + 1, ((IntBuffer) indexBuffer).get(i + 2));
                    ((IntBuffer) indexBuffer).put(i + 2, index);
                }
            }
        }
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) CullHint(com.jme3.scene.Spatial.CullHint)

Example 19 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class TestCustomAnim method simpleInitApp.

@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);
    VertexBuffer weightsHW = new VertexBuffer(Type.HWBoneWeight);
    VertexBuffer indicesHW = new VertexBuffer(Type.HWBoneIndex);
    indicesHW.setUsage(Usage.CpuOnly);
    weightsHW.setUsage(Usage.CpuOnly);
    box.setBuffer(weightsHW);
    box.setBuffer(indicesHW);
    // 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);
}
Also used : Geometry(com.jme3.scene.Geometry) VertexBuffer(com.jme3.scene.VertexBuffer) DirectionalLight(com.jme3.light.DirectionalLight) Node(com.jme3.scene.Node) SkeletonControl(com.jme3.animation.SkeletonControl) Box(com.jme3.scene.shape.Box) FloatBuffer(java.nio.FloatBuffer) Skeleton(com.jme3.animation.Skeleton) Bone(com.jme3.animation.Bone) ByteBuffer(java.nio.ByteBuffer) AmbientLight(com.jme3.light.AmbientLight)

Example 20 with FloatBuffer

use of java.nio.FloatBuffer in project jmonkeyengine by jMonkeyEngine.

the class HelloOpenCL method testKernel.

private boolean testKernel(Context clContext, CommandQueue clQueue) {
    try {
        //create fill code
        String include = "#define TYPE float\n";
        Program program = clContext.createProgramFromSourceFilesWithInclude(assetManager, include, "jme3test/opencl/Blas.cl");
        program.build();
        Kernel kernel = program.createKernel("Fill");
        System.out.println("number of args: " + kernel.getArgCount());
        //fill buffer
        int size = 256 + 128;
        Buffer buffer = clContext.createBuffer(size * 4);
        float value = 5;
        Event event = kernel.Run1(clQueue, new com.jme3.opencl.Kernel.WorkSize(buffer.getSize() / 4), buffer, value);
        event.waitForFinished();
        //check if filled
        ByteBuffer buf = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        FloatBuffer buff = buf.asFloatBuffer();
        for (int i = 0; i < size; ++i) {
            float v = buff.get(i);
            assertEquals(value, v, "Buffer filled with the wrong value at index " + i);
        }
        buffer.unmap(clQueue, buf);
        //release
        buffer.release();
        kernel.release();
        program.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "kernel test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "kernel test failed with:", ex);
        return false;
    }
    return true;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

FloatBuffer (java.nio.FloatBuffer)287 ByteBuffer (java.nio.ByteBuffer)82 IntBuffer (java.nio.IntBuffer)43 ShortBuffer (java.nio.ShortBuffer)39 Vector3f (com.jme3.math.Vector3f)27 VertexBuffer (com.jme3.scene.VertexBuffer)27 DoubleBuffer (java.nio.DoubleBuffer)17 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)16 LongBuffer (java.nio.LongBuffer)10 Mesh (com.jme3.scene.Mesh)9 CharBuffer (java.nio.CharBuffer)9 FrameBuffer2D (androidx.media.filterfw.FrameBuffer2D)8 OutputPort (androidx.media.filterfw.OutputPort)7 Matrix4f (com.jme3.math.Matrix4f)7 Buffer (java.nio.Buffer)7 TempVars (com.jme3.util.TempVars)6 IOException (java.io.IOException)6 BufferOverflowException (java.nio.BufferOverflowException)6 BufferUnderflowException (java.nio.BufferUnderflowException)6 ArrayList (java.util.ArrayList)6