Search in sources :

Example 51 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project jmonkeyengine by jMonkeyEngine.

the class MeshBuffers method getNormalsBuffer.

/**
     * @return normals buffer
     */
public VertexBuffer getNormalsBuffer() {
    VertexBuffer positionBuffer = new VertexBuffer(Type.Normal);
    Vector3f[] data = normals.toArray(new Vector3f[normals.size()]);
    positionBuffer.setupData(Usage.Static, 3, Format.Float, BufferUtils.createFloatBuffer(data));
    return positionBuffer;
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f)

Example 52 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project jmonkeyengine by jMonkeyEngine.

the class ParticlePointMesh method updateParticleData.

@Override
public void updateParticleData(Particle[] particles, Camera cam, Matrix3f inverseRotation) {
    VertexBuffer pvb = getBuffer(VertexBuffer.Type.Position);
    FloatBuffer positions = (FloatBuffer) pvb.getData();
    VertexBuffer cvb = getBuffer(VertexBuffer.Type.Color);
    ByteBuffer colors = (ByteBuffer) cvb.getData();
    VertexBuffer svb = getBuffer(VertexBuffer.Type.Size);
    FloatBuffer sizes = (FloatBuffer) svb.getData();
    VertexBuffer tvb = getBuffer(VertexBuffer.Type.TexCoord);
    FloatBuffer texcoords = (FloatBuffer) tvb.getData();
    float sizeScale = emitter.getWorldScale().x;
    // update data in vertex buffers
    positions.rewind();
    colors.rewind();
    sizes.rewind();
    texcoords.rewind();
    for (int i = 0; i < particles.length; i++) {
        Particle p = particles[i];
        positions.put(p.position.x).put(p.position.y).put(p.position.z);
        sizes.put(p.size * sizeScale);
        colors.putInt(p.color.asIntABGR());
        int imgX = p.imageIndex % imagesX;
        int imgY = (p.imageIndex - imgX) / imagesY;
        float startX = ((float) imgX) / imagesX;
        float startY = ((float) imgY) / imagesY;
        float endX = startX + (1f / imagesX);
        float endY = startY + (1f / imagesY);
        texcoords.put(startX).put(startY).put(endX).put(endY);
    }
    positions.flip();
    colors.flip();
    sizes.flip();
    texcoords.flip();
    // force renderer to re-send data to GPU
    pvb.updateData(positions);
    cvb.updateData(colors);
    svb.updateData(sizes);
    tvb.updateData(texcoords);
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 53 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project jmonkeyengine by jMonkeyEngine.

the class ParticleTriMesh method updateParticleData.

@Override
public void updateParticleData(Particle[] particles, Camera cam, Matrix3f inverseRotation) {
    //        System.arraycopy(particles, 0, particlesCopy, 0, particlesCopy.length);
    //        comparator.setCamera(cam);
    //        Arrays.sort(particlesCopy, comparator);
    //        SortUtil.qsort(particlesCopy, comparator);
    //        SortUtil.msort(particles, particlesCopy, comparator);
    //        particles = particlesCopy;
    VertexBuffer pvb = getBuffer(VertexBuffer.Type.Position);
    FloatBuffer positions = (FloatBuffer) pvb.getData();
    VertexBuffer cvb = getBuffer(VertexBuffer.Type.Color);
    ByteBuffer colors = (ByteBuffer) cvb.getData();
    VertexBuffer tvb = getBuffer(VertexBuffer.Type.TexCoord);
    FloatBuffer texcoords = (FloatBuffer) tvb.getData();
    Vector3f camUp = cam.getUp();
    Vector3f camLeft = cam.getLeft();
    Vector3f camDir = cam.getDirection();
    inverseRotation.multLocal(camUp);
    inverseRotation.multLocal(camLeft);
    inverseRotation.multLocal(camDir);
    boolean facingVelocity = emitter.isFacingVelocity();
    Vector3f up = new Vector3f(), left = new Vector3f();
    if (!facingVelocity) {
        up.set(camUp);
        left.set(camLeft);
    }
    // update data in vertex buffers
    positions.clear();
    colors.clear();
    texcoords.clear();
    Vector3f faceNormal = emitter.getFaceNormal();
    for (int i = 0; i < particles.length; i++) {
        Particle p = particles[i];
        boolean dead = p.life == 0;
        if (dead) {
            positions.put(0).put(0).put(0);
            positions.put(0).put(0).put(0);
            positions.put(0).put(0).put(0);
            positions.put(0).put(0).put(0);
            continue;
        }
        if (facingVelocity) {
            left.set(p.velocity).normalizeLocal();
            camDir.cross(left, up);
            up.multLocal(p.size);
            left.multLocal(p.size);
        } else if (faceNormal != null) {
            up.set(faceNormal).crossLocal(Vector3f.UNIT_X);
            faceNormal.cross(up, left);
            up.multLocal(p.size);
            left.multLocal(p.size);
            if (p.angle != 0) {
                TempVars vars = TempVars.get();
                vars.vect1.set(faceNormal).normalizeLocal();
                vars.quat1.fromAngleNormalAxis(p.angle, vars.vect1);
                vars.quat1.multLocal(left);
                vars.quat1.multLocal(up);
                vars.release();
            }
        } else if (p.angle != 0) {
            float cos = FastMath.cos(p.angle) * p.size;
            float sin = FastMath.sin(p.angle) * p.size;
            left.x = camLeft.x * cos + camUp.x * sin;
            left.y = camLeft.y * cos + camUp.y * sin;
            left.z = camLeft.z * cos + camUp.z * sin;
            up.x = camLeft.x * -sin + camUp.x * cos;
            up.y = camLeft.y * -sin + camUp.y * cos;
            up.z = camLeft.z * -sin + camUp.z * cos;
        } else {
            up.set(camUp);
            left.set(camLeft);
            up.multLocal(p.size);
            left.multLocal(p.size);
        }
        positions.put(p.position.x + left.x + up.x).put(p.position.y + left.y + up.y).put(p.position.z + left.z + up.z);
        positions.put(p.position.x - left.x + up.x).put(p.position.y - left.y + up.y).put(p.position.z - left.z + up.z);
        positions.put(p.position.x + left.x - up.x).put(p.position.y + left.y - up.y).put(p.position.z + left.z - up.z);
        positions.put(p.position.x - left.x - up.x).put(p.position.y - left.y - up.y).put(p.position.z - left.z - up.z);
        if (uniqueTexCoords) {
            int imgX = p.imageIndex % imagesX;
            int imgY = (p.imageIndex - imgX) / imagesY;
            float startX = ((float) imgX) / imagesX;
            float startY = ((float) imgY) / imagesY;
            float endX = startX + (1f / imagesX);
            float endY = startY + (1f / imagesY);
            texcoords.put(startX).put(endY);
            texcoords.put(endX).put(endY);
            texcoords.put(startX).put(startY);
            texcoords.put(endX).put(startY);
        }
        int abgr = p.color.asIntABGR();
        colors.putInt(abgr);
        colors.putInt(abgr);
        colors.putInt(abgr);
        colors.putInt(abgr);
    }
    positions.clear();
    colors.clear();
    if (!uniqueTexCoords)
        texcoords.clear();
    else {
        texcoords.clear();
        tvb.updateData(texcoords);
    }
    // force renderer to re-send data to GPU
    pvb.updateData(positions);
    cvb.updateData(colors);
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) TempVars(com.jme3.util.TempVars) ByteBuffer(java.nio.ByteBuffer)

Example 54 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project jmonkeyengine by jMonkeyEngine.

the class SkeletonInterBoneWire method updateGeometry.

/**
     * The method updates the geometry according to the poitions of the bones.
     */
public void updateGeometry() {
    VertexBuffer vb = this.getBuffer(Type.Position);
    FloatBuffer posBuf = this.getFloatBuffer(Type.Position);
    posBuf.clear();
    for (int i = 0; i < skeleton.getBoneCount(); ++i) {
        Bone bone = skeleton.getBone(i);
        Vector3f parentTail = bone.getModelSpacePosition().add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(boneLengths.get(i))));
        for (Bone child : bone.getChildren()) {
            Vector3f childHead = child.getModelSpacePosition();
            Vector3f v = childHead.subtract(parentTail);
            float pointDelta = v.length() / POINT_AMOUNT;
            v.normalizeLocal().multLocal(pointDelta);
            Vector3f pointPosition = parentTail.clone();
            for (int j = 0; j < POINT_AMOUNT; ++j) {
                posBuf.put(pointPosition.getX()).put(pointPosition.getY()).put(pointPosition.getZ());
                pointPosition.addLocal(v);
            }
        }
    }
    posBuf.flip();
    vb.updateData(posBuf);
    this.updateBound();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) Bone(com.jme3.animation.Bone)

Example 55 with VertexBuffer

use of com.jme3.scene.VertexBuffer in project jmonkeyengine by jMonkeyEngine.

the class SkeletonPoints method updateGeometry.

/**
     * The method updates the geometry according to the positions of the bones.
     */
public void updateGeometry() {
    VertexBuffer vb = this.getBuffer(Type.Position);
    FloatBuffer posBuf = this.getFloatBuffer(Type.Position);
    posBuf.clear();
    for (int i = 0; i < skeleton.getBoneCount(); ++i) {
        Bone bone = skeleton.getBone(i);
        Vector3f head = bone.getModelSpacePosition();
        posBuf.put(head.getX()).put(head.getY()).put(head.getZ());
        if (boneLengths != null) {
            Vector3f tail = head.add(bone.getModelSpaceRotation().mult(Vector3f.UNIT_Y.mult(boneLengths.get(i))));
            posBuf.put(tail.getX()).put(tail.getY()).put(tail.getZ());
        }
    }
    posBuf.flip();
    vb.updateData(posBuf);
    this.updateBound();
}
Also used : VertexBuffer(com.jme3.scene.VertexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) Bone(com.jme3.animation.Bone)

Aggregations

VertexBuffer (com.jme3.scene.VertexBuffer)45 FloatBuffer (java.nio.FloatBuffer)43 Vector3f (com.jme3.math.Vector3f)20 ByteBuffer (java.nio.ByteBuffer)12 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)10 ShortBuffer (java.nio.ShortBuffer)9 IntBuffer (java.nio.IntBuffer)8 Mesh (com.jme3.scene.Mesh)7 Geometry (com.jme3.scene.Geometry)6 Buffer (java.nio.Buffer)6 ColorRGBA (com.jme3.math.ColorRGBA)5 Vector2f (com.jme3.math.Vector2f)5 ArrayList (java.util.ArrayList)5 Bone (com.jme3.animation.Bone)4 Material (com.jme3.material.Material)4 Matrix4f (com.jme3.math.Matrix4f)4 Type (com.jme3.scene.VertexBuffer.Type)3 Texture (com.jme3.texture.Texture)3 TempVars (com.jme3.util.TempVars)3 HashMap (java.util.HashMap)3