Search in sources :

Example 6 with VertexBuffer

use of com.jme3.scene.VertexBuffer 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 7 with VertexBuffer

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

the class MeshBuffers method getPositionsBuffer.

/**
     * @return positions buffer
     */
public VertexBuffer getPositionsBuffer() {
    VertexBuffer positionBuffer = new VertexBuffer(Type.Position);
    Vector3f[] data = verts.toArray(new Vector3f[verts.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 8 with VertexBuffer

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

the class MaterialContext method applyMaterial.

/**
     * Applies material to a given geometry.
     * 
     * @param geometry
     *            the geometry
     * @param geometriesOMA
     *            the geometries OMA
     * @param userDefinedUVCoordinates
     *            UV coords defined by user
     * @param blenderContext
     *            the blender context
     */
public void applyMaterial(Geometry geometry, Long geometriesOMA, Map<String, List<Vector2f>> userDefinedUVCoordinates, BlenderContext blenderContext) {
    Material material = null;
    if (shadeless) {
        material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        if (!transparent) {
            diffuseColor.a = 1;
        }
        material.setColor("Color", diffuseColor);
    } else {
        material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        material.setBoolean("UseMaterialColors", Boolean.TRUE);
        // setting the colors
        if (!transparent) {
            diffuseColor.a = 1;
        }
        material.setColor("Diffuse", diffuseColor);
        material.setColor("Specular", specularColor);
        material.setFloat("Shininess", shininess);
        material.setColor("Ambient", new ColorRGBA(ambientFactor, ambientFactor, ambientFactor, 1f));
    }
    // applying textures
    int textureIndex = 0;
    if (loadedTextures != null && loadedTextures.size() > 0) {
        if (loadedTextures.size() > TextureHelper.TEXCOORD_TYPES.length) {
            LOGGER.log(Level.WARNING, "The blender file has defined more than {0} different textures. JME supports only {0} UV mappings.", TextureHelper.TEXCOORD_TYPES.length);
        }
        for (CombinedTexture combinedTexture : loadedTextures) {
            if (textureIndex < TextureHelper.TEXCOORD_TYPES.length) {
                String usedUserUVSet = combinedTexture.flatten(geometry, geometriesOMA, userDefinedUVCoordinates, blenderContext);
                this.setTexture(material, combinedTexture.getMappingType(), combinedTexture.getResultTexture());
                List<Vector2f> uvs = combinedTexture.getResultUVS();
                if (uvs != null && uvs.size() > 0) {
                    VertexBuffer uvCoordsBuffer = new VertexBuffer(TextureHelper.TEXCOORD_TYPES[textureIndex++]);
                    uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()])));
                    geometry.getMesh().setBuffer(uvCoordsBuffer);
                }
                if (usedUserUVSet != null) {
                    userDefinedUVCoordinates = new HashMap<>(userDefinedUVCoordinates);
                    userDefinedUVCoordinates.remove(usedUserUVSet);
                }
            } else {
                LOGGER.log(Level.WARNING, "The texture could not be applied because JME only supports up to {0} different UV's.", TextureHelper.TEXCOORD_TYPES.length);
            }
        }
    }
    if (userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) {
        LOGGER.fine("Storing unused, user defined UV coordinates sets.");
        if (userDefinedUVCoordinates.size() > TextureHelper.TEXCOORD_TYPES.length) {
            LOGGER.log(Level.WARNING, "The blender file has defined more than {0} different UV coordinates for the mesh. JME supports only {0} UV coordinates buffers.", TextureHelper.TEXCOORD_TYPES.length);
        }
        for (Entry<String, List<Vector2f>> entry : userDefinedUVCoordinates.entrySet()) {
            if (textureIndex < TextureHelper.TEXCOORD_TYPES.length) {
                List<Vector2f> uvs = entry.getValue();
                VertexBuffer uvCoordsBuffer = new VertexBuffer(TextureHelper.TEXCOORD_TYPES[textureIndex++]);
                uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()])));
                geometry.getMesh().setBuffer(uvCoordsBuffer);
            } else {
                LOGGER.log(Level.WARNING, "The user's UV set named: '{0}' could not be stored because JME only supports up to {1} different UV's.", new Object[] { entry.getKey(), TextureHelper.TEXCOORD_TYPES.length });
            }
        }
    }
    // applying additional data
    material.setName(name);
    if (vertexColor) {
        material.setBoolean(shadeless ? "VertexColor" : "UseVertexColor", true);
    }
    material.getAdditionalRenderState().setFaceCullMode(faceCullMode != null ? faceCullMode : blenderContext.getBlenderKey().getFaceCullMode());
    if (transparent) {
        material.setTransparent(true);
        material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        geometry.setQueueBucket(Bucket.Transparent);
    }
    geometry.setMaterial(material);
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) VertexBuffer(com.jme3.scene.VertexBuffer) Vector2f(com.jme3.math.Vector2f) Material(com.jme3.material.Material) List(java.util.List) CombinedTexture(com.jme3.scene.plugins.blender.textures.CombinedTexture)

Example 9 with VertexBuffer

use of com.jme3.scene.VertexBuffer 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 10 with VertexBuffer

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

the class Cylinder method updateGeometry.

/**
     * Rebuilds the cylinder based on a new set of parameters.
     *
     * @param axisSamples the number of samples along the axis.
     * @param radialSamples the number of samples around the radial.
     * @param radius the radius of the bottom of the cylinder.
     * @param radius2 the radius of the top of the cylinder.
     * @param height the cylinder's height.
     * @param closed should the cylinder have top and bottom surfaces.
     * @param inverted is the cylinder is meant to be viewed from the inside.
     */
public void updateGeometry(int axisSamples, int radialSamples, float radius, float radius2, float height, boolean closed, boolean inverted) {
    this.axisSamples = axisSamples;
    this.radialSamples = radialSamples;
    this.radius = radius;
    this.radius2 = radius2;
    this.height = height;
    this.closed = closed;
    this.inverted = inverted;
    //        VertexBuffer pvb = getBuffer(Type.Position);
    //        VertexBuffer nvb = getBuffer(Type.Normal);
    //        VertexBuffer tvb = getBuffer(Type.TexCoord);
    axisSamples += (closed ? 2 : 0);
    // Vertices
    int vertCount = axisSamples * (radialSamples + 1) + (closed ? 2 : 0);
    setBuffer(Type.Position, 3, createVector3Buffer(getFloatBuffer(Type.Position), vertCount));
    // Normals
    setBuffer(Type.Normal, 3, createVector3Buffer(getFloatBuffer(Type.Normal), vertCount));
    // Texture co-ordinates
    setBuffer(Type.TexCoord, 2, createVector2Buffer(vertCount));
    int triCount = ((closed ? 2 : 0) + 2 * (axisSamples - 1)) * radialSamples;
    setBuffer(Type.Index, 3, createShortBuffer(getShortBuffer(Type.Index), 3 * triCount));
    // generate geometry
    float inverseRadial = 1.0f / radialSamples;
    float inverseAxisLess = 1.0f / (closed ? axisSamples - 3 : axisSamples - 1);
    float inverseAxisLessTexture = 1.0f / (axisSamples - 1);
    float halfHeight = 0.5f * height;
    // Generate points on the unit circle to be used in computing the mesh
    // points on a cylinder slice.
    float[] sin = new float[radialSamples + 1];
    float[] cos = new float[radialSamples + 1];
    for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
        float angle = FastMath.TWO_PI * inverseRadial * radialCount;
        cos[radialCount] = FastMath.cos(angle);
        sin[radialCount] = FastMath.sin(angle);
    }
    sin[radialSamples] = sin[0];
    cos[radialSamples] = cos[0];
    // calculate normals
    Vector3f[] vNormals = null;
    Vector3f vNormal = Vector3f.UNIT_Z;
    if ((height != 0.0f) && (radius != radius2)) {
        vNormals = new Vector3f[radialSamples];
        Vector3f vHeight = Vector3f.UNIT_Z.mult(height);
        Vector3f vRadial = new Vector3f();
        for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
            vRadial.set(cos[radialCount], sin[radialCount], 0.0f);
            Vector3f vRadius = vRadial.mult(radius);
            Vector3f vRadius2 = vRadial.mult(radius2);
            Vector3f vMantle = vHeight.subtract(vRadius2.subtract(vRadius));
            Vector3f vTangent = vRadial.cross(Vector3f.UNIT_Z);
            vNormals[radialCount] = vMantle.cross(vTangent).normalize();
        }
    }
    FloatBuffer nb = getFloatBuffer(Type.Normal);
    FloatBuffer pb = getFloatBuffer(Type.Position);
    FloatBuffer tb = getFloatBuffer(Type.TexCoord);
    // generate the cylinder itself
    Vector3f tempNormal = new Vector3f();
    for (int axisCount = 0, i = 0; axisCount < axisSamples; axisCount++, i++) {
        float axisFraction;
        float axisFractionTexture;
        int topBottom = 0;
        if (!closed) {
            // in [0,1]
            axisFraction = axisCount * inverseAxisLess;
            axisFractionTexture = axisFraction;
        } else {
            if (axisCount == 0) {
                // bottom
                topBottom = -1;
                axisFraction = 0;
                axisFractionTexture = inverseAxisLessTexture;
            } else if (axisCount == axisSamples - 1) {
                // top
                topBottom = 1;
                axisFraction = 1;
                axisFractionTexture = 1 - inverseAxisLessTexture;
            } else {
                axisFraction = (axisCount - 1) * inverseAxisLess;
                axisFractionTexture = axisCount * inverseAxisLessTexture;
            }
        }
        // compute center of slice
        float z = -halfHeight + height * axisFraction;
        Vector3f sliceCenter = new Vector3f(0, 0, z);
        // compute slice vertices with duplication at end point
        int save = i;
        for (int radialCount = 0; radialCount < radialSamples; radialCount++, i++) {
            // in [0,1)
            float radialFraction = radialCount * inverseRadial;
            tempNormal.set(cos[radialCount], sin[radialCount], 0.0f);
            if (vNormals != null) {
                vNormal = vNormals[radialCount];
            } else if (radius == radius2) {
                vNormal = tempNormal;
            }
            if (topBottom == 0) {
                if (!inverted)
                    nb.put(vNormal.x).put(vNormal.y).put(vNormal.z);
                else
                    nb.put(-vNormal.x).put(-vNormal.y).put(-vNormal.z);
            } else {
                nb.put(0).put(0).put(topBottom * (inverted ? -1 : 1));
            }
            tempNormal.multLocal((radius - radius2) * axisFraction + radius2).addLocal(sliceCenter);
            pb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
            tb.put((inverted ? 1 - radialFraction : radialFraction)).put(axisFractionTexture);
        }
        BufferUtils.copyInternalVector3(pb, save, i);
        BufferUtils.copyInternalVector3(nb, save, i);
        tb.put((inverted ? 0.0f : 1.0f)).put(axisFractionTexture);
    }
    if (closed) {
        // bottom center
        pb.put(0).put(0).put(-halfHeight);
        nb.put(0).put(0).put(-1 * (inverted ? -1 : 1));
        tb.put(0.5f).put(0);
        // top center
        pb.put(0).put(0).put(halfHeight);
        nb.put(0).put(0).put(1 * (inverted ? -1 : 1));
        tb.put(0.5f).put(1);
    }
    IndexBuffer ib = getIndexBuffer();
    int index = 0;
    // Connectivity
    for (int axisCount = 0, axisStart = 0; axisCount < axisSamples - 1; axisCount++) {
        int i0 = axisStart;
        int i1 = i0 + 1;
        axisStart += radialSamples + 1;
        int i2 = axisStart;
        int i3 = i2 + 1;
        for (int i = 0; i < radialSamples; i++) {
            if (closed && axisCount == 0) {
                if (!inverted) {
                    ib.put(index++, i0++);
                    ib.put(index++, vertCount - 2);
                    ib.put(index++, i1++);
                } else {
                    ib.put(index++, i0++);
                    ib.put(index++, i1++);
                    ib.put(index++, vertCount - 2);
                }
            } else if (closed && axisCount == axisSamples - 2) {
                ib.put(index++, i2++);
                ib.put(index++, inverted ? vertCount - 1 : i3++);
                ib.put(index++, inverted ? i3++ : vertCount - 1);
            } else {
                ib.put(index++, i0++);
                ib.put(index++, inverted ? i2 : i1);
                ib.put(index++, inverted ? i1 : i2);
                ib.put(index++, i1++);
                ib.put(index++, inverted ? i2++ : i3++);
                ib.put(index++, inverted ? i3++ : i2++);
            }
        }
    }
    updateBound();
    setStatic();
}
Also used : IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer)

Aggregations

FloatBuffer (java.nio.FloatBuffer)42 VertexBuffer (com.jme3.scene.VertexBuffer)40 Vector3f (com.jme3.math.Vector3f)17 ByteBuffer (java.nio.ByteBuffer)12 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 IntBuffer (java.nio.IntBuffer)8 Buffer (java.nio.Buffer)6 Geometry (com.jme3.scene.Geometry)5 Mesh (com.jme3.scene.Mesh)5 Bone (com.jme3.animation.Bone)4 Matrix4f (com.jme3.math.Matrix4f)4 Vector2f (com.jme3.math.Vector2f)4 ArrayList (java.util.ArrayList)4 Material (com.jme3.material.Material)3 ColorRGBA (com.jme3.math.ColorRGBA)3 Type (com.jme3.scene.VertexBuffer.Type)3 TempVars (com.jme3.util.TempVars)3 HashMap (java.util.HashMap)3 BoundingBox (com.jme3.bounding.BoundingBox)2