Search in sources :

Example 71 with Geometry

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

the class TestChaseCamera method simpleInitApp.

public void simpleInitApp() {
    // Load a teapot model
    teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
    Material mat_tea = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
    teaGeom.setMaterial(mat_tea);
    rootNode.attachChild(teaGeom);
    // Load a floor model
    Material mat_ground = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat_ground.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
    Geometry ground = new Geometry("ground", new Quad(50, 50));
    ground.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
    ground.setLocalTranslation(-25, -1, 25);
    ground.setMaterial(mat_ground);
    rootNode.attachChild(ground);
    // Disable the default first-person cam!
    flyCam.setEnabled(false);
    // Enable a chase cam
    chaseCam = new ChaseCamera(cam, teaGeom, inputManager);
    //Uncomment this to invert the camera's vertical rotation Axis 
    //chaseCam.setInvertVerticalAxis(true);
    //Uncomment this to invert the camera's horizontal rotation Axis
    //chaseCam.setInvertHorizontalAxis(true);
    //Comment this to disable smooth camera motion
    chaseCam.setSmoothMotion(true);
    //Uncomment this to disable trailing of the camera 
    //WARNING, trailing only works with smooth motion enabled. It is true by default.
    //chaseCam.setTrailingEnabled(false);
    //Uncomment this to look 3 world units above the target
    //chaseCam.setLookAtOffset(Vector3f.UNIT_Y.mult(3));
    //Uncomment this to enable rotation when the middle mouse button is pressed (like Blender)
    //WARNING : setting this trigger disable the rotation on right and left mouse button click
    //chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
    //Uncomment this to set mutiple triggers to enable rotation of the cam
    //Here spade bar and middle mouse button
    //chaseCam.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE),new KeyTrigger(KeyInput.KEY_SPACE));
    //registering inputs for target's movement
    registerInput();
}
Also used : Geometry(com.jme3.scene.Geometry) Quad(com.jme3.scene.shape.Quad) Quaternion(com.jme3.math.Quaternion) ChaseCamera(com.jme3.input.ChaseCamera) Material(com.jme3.material.Material)

Example 72 with Geometry

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

the class BatchNode method updateSubBatch.

protected void updateSubBatch(Geometry bg) {
    Batch batch = batchesByGeom.get(bg);
    if (batch != null) {
        Mesh mesh = batch.geometry.getMesh();
        Mesh origMesh = bg.getMesh();
        VertexBuffer pvb = mesh.getBuffer(VertexBuffer.Type.Position);
        FloatBuffer posBuf = (FloatBuffer) pvb.getData();
        VertexBuffer nvb = mesh.getBuffer(VertexBuffer.Type.Normal);
        FloatBuffer normBuf = (FloatBuffer) nvb.getData();
        VertexBuffer opvb = origMesh.getBuffer(VertexBuffer.Type.Position);
        FloatBuffer oposBuf = (FloatBuffer) opvb.getData();
        VertexBuffer onvb = origMesh.getBuffer(VertexBuffer.Type.Normal);
        FloatBuffer onormBuf = (FloatBuffer) onvb.getData();
        Matrix4f transformMat = getTransformMatrix(bg);
        if (mesh.getBuffer(VertexBuffer.Type.Tangent) != null) {
            VertexBuffer tvb = mesh.getBuffer(VertexBuffer.Type.Tangent);
            FloatBuffer tanBuf = (FloatBuffer) tvb.getData();
            VertexBuffer otvb = origMesh.getBuffer(VertexBuffer.Type.Tangent);
            FloatBuffer otanBuf = (FloatBuffer) otvb.getData();
            doTransformsTangents(oposBuf, onormBuf, otanBuf, posBuf, normBuf, tanBuf, bg.startIndex, bg.startIndex + bg.getVertexCount(), transformMat);
            tvb.updateData(tanBuf);
        } else {
            doTransforms(oposBuf, onormBuf, posBuf, normBuf, bg.startIndex, bg.startIndex + bg.getVertexCount(), transformMat);
        }
        pvb.updateData(posBuf);
        nvb.updateData(normBuf);
        batch.geometry.updateModelBound();
    }
}
Also used : Matrix4f(com.jme3.math.Matrix4f) FloatBuffer(java.nio.FloatBuffer)

Example 73 with Geometry

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

the class BatchNode method mergeGeometries.

/**
     * Merges all geometries in the collection into
     * the output mesh. Does not take into account materials.
     *
     * @param geometries
     * @param outMesh
     */
private void mergeGeometries(Mesh outMesh, List<Geometry> geometries) {
    int[] compsForBuf = new int[VertexBuffer.Type.values().length];
    VertexBuffer.Format[] formatForBuf = new VertexBuffer.Format[compsForBuf.length];
    boolean[] normForBuf = new boolean[VertexBuffer.Type.values().length];
    int totalVerts = 0;
    int totalTris = 0;
    int totalLodLevels = 0;
    int maxWeights = -1;
    Mesh.Mode mode = null;
    float lineWidth = 1f;
    for (Geometry geom : geometries) {
        totalVerts += geom.getVertexCount();
        totalTris += geom.getTriangleCount();
        totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
        if (maxVertCount < geom.getVertexCount()) {
            maxVertCount = geom.getVertexCount();
        }
        Mesh.Mode listMode;
        //float listLineWidth = 1f;
        int components;
        switch(geom.getMesh().getMode()) {
            case Points:
                listMode = Mesh.Mode.Points;
                components = 1;
                break;
            case LineLoop:
            case LineStrip:
            case Lines:
                listMode = Mesh.Mode.Lines;
                //listLineWidth = geom.getMesh().getLineWidth();
                components = 2;
                break;
            case TriangleFan:
            case TriangleStrip:
            case Triangles:
                listMode = Mesh.Mode.Triangles;
                components = 3;
                break;
            default:
                throw new UnsupportedOperationException();
        }
        for (VertexBuffer vb : geom.getMesh().getBufferList().getArray()) {
            int currentCompsForBuf = compsForBuf[vb.getBufferType().ordinal()];
            if (vb.getBufferType() != VertexBuffer.Type.Index && currentCompsForBuf != 0 && currentCompsForBuf != vb.getNumComponents()) {
                throw new UnsupportedOperationException("The geometry " + geom + " buffer " + vb.getBufferType() + " has different number of components than the rest of the meshes " + "(this: " + vb.getNumComponents() + ", expected: " + currentCompsForBuf + ")");
            }
            compsForBuf[vb.getBufferType().ordinal()] = vb.getNumComponents();
            formatForBuf[vb.getBufferType().ordinal()] = vb.getFormat();
            normForBuf[vb.getBufferType().ordinal()] = vb.isNormalized();
        }
        maxWeights = Math.max(maxWeights, geom.getMesh().getMaxNumWeights());
        if (mode != null && mode != listMode) {
            throw new UnsupportedOperationException("Cannot combine different" + " primitive types: " + mode + " != " + listMode);
        }
        mode = listMode;
        //Not needed anymore as lineWidth is now in RenderState and will be taken into account when merging according to the material
        //            if (mode == Mesh.Mode.Lines) {
        //                if (lineWidth != 1f && listLineWidth != lineWidth) {
        //                    throw new UnsupportedOperationException("When using Mesh Line mode, cannot combine meshes with different line width "
        //                            + lineWidth + " != " + listLineWidth);
        //                }
        //                lineWidth = listLineWidth;
        //            }
        compsForBuf[VertexBuffer.Type.Index.ordinal()] = components;
    }
    outMesh.setMaxNumWeights(maxWeights);
    outMesh.setMode(mode);
    //outMesh.setLineWidth(lineWidth);
    if (totalVerts >= 65536) {
        // make sure we create an UnsignedInt buffer so we can fit all of the meshes
        formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedInt;
    } else {
        formatForBuf[VertexBuffer.Type.Index.ordinal()] = VertexBuffer.Format.UnsignedShort;
    }
    // generate output buffers based on retrieved info
    for (int i = 0; i < compsForBuf.length; i++) {
        if (compsForBuf[i] == 0) {
            continue;
        }
        Buffer data;
        if (i == VertexBuffer.Type.Index.ordinal()) {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
        } else {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
        }
        VertexBuffer vb = new VertexBuffer(VertexBuffer.Type.values()[i]);
        vb.setupData(VertexBuffer.Usage.Dynamic, compsForBuf[i], formatForBuf[i], data);
        vb.setNormalized(normForBuf[i]);
        outMesh.setBuffer(vb);
    }
    int globalVertIndex = 0;
    int globalTriIndex = 0;
    for (Geometry geom : geometries) {
        Mesh inMesh = geom.getMesh();
        if (!isBatch(geom)) {
            geom.associateWithGroupNode(this, globalVertIndex);
        }
        int geomVertCount = inMesh.getVertexCount();
        int geomTriCount = inMesh.getTriangleCount();
        for (int bufType = 0; bufType < compsForBuf.length; bufType++) {
            VertexBuffer inBuf = inMesh.getBuffer(VertexBuffer.Type.values()[bufType]);
            VertexBuffer outBuf = outMesh.getBuffer(VertexBuffer.Type.values()[bufType]);
            if (outBuf == null) {
                continue;
            }
            if (VertexBuffer.Type.Index.ordinal() == bufType) {
                int components = compsForBuf[bufType];
                IndexBuffer inIdx = inMesh.getIndicesAsList();
                IndexBuffer outIdx = outMesh.getIndexBuffer();
                for (int tri = 0; tri < geomTriCount; tri++) {
                    for (int comp = 0; comp < components; comp++) {
                        int idx = inIdx.get(tri * components + comp) + globalVertIndex;
                        outIdx.put((globalTriIndex + tri) * components + comp, idx);
                    }
                }
            } else if (VertexBuffer.Type.Position.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doCopyBuffer(inPos, globalVertIndex, outPos, 3);
            } else if (VertexBuffer.Type.Normal.ordinal() == bufType || VertexBuffer.Type.Tangent.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getData();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doCopyBuffer(inPos, globalVertIndex, outPos, compsForBuf[bufType]);
                if (VertexBuffer.Type.Tangent.ordinal() == bufType) {
                    useTangents = true;
                }
            } else {
                if (inBuf == null) {
                    throw new IllegalArgumentException("Geometry " + geom.getName() + " has no " + outBuf.getBufferType() + " buffer whereas other geoms have. all geometries should have the same types of buffers.\n Try to use GeometryBatchFactory.alignBuffer() on the BatchNode before batching");
                } else if (outBuf == null) {
                    throw new IllegalArgumentException("Geometry " + geom.getName() + " has a " + outBuf.getBufferType() + " buffer whereas other geoms don't. all geometries should have the same types of buffers.\n Try to use GeometryBatchFactory.alignBuffer() on the BatchNode before batching");
                } else {
                    inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
                }
            }
        }
        globalVertIndex += geomVertCount;
        globalTriIndex += geomTriCount;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Buffer(java.nio.Buffer) FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer)

Example 74 with Geometry

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

the class Geometry method computeWorldMatrix.

/**
     * Indicate that the transform of this spatial has changed and that
     * a refresh is required.
     */
// NOTE: Spatial has an identical implementation of this method,
// thus it was commented out.
//    @Override
//    protected void setTransformRefresh() {
//        refreshFlags |= RF_TRANSFORM;
//        setBoundRefresh();
//    }
/**
     * Recomputes the matrix returned by {@link Geometry#getWorldMatrix() }.
     * This will require a localized transform update for this geometry.
     */
public void computeWorldMatrix() {
    // Force a local update of the geometry's transform
    checkDoTransformUpdate();
    // Compute the cached world matrix
    cachedWorldMat.loadIdentity();
    cachedWorldMat.setRotationQuaternion(worldTransform.getRotation());
    cachedWorldMat.setTranslation(worldTransform.getTranslation());
    TempVars vars = TempVars.get();
    Matrix4f scaleMat = vars.tempMat4;
    scaleMat.loadIdentity();
    scaleMat.scale(worldTransform.getScale());
    cachedWorldMat.multLocal(scaleMat);
    vars.release();
}
Also used : Matrix4f(com.jme3.math.Matrix4f) TempVars(com.jme3.util.TempVars)

Example 75 with Geometry

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

the class SimpleBatchNode method getTransformMatrix.

@Override
protected Matrix4f getTransformMatrix(Geometry g) {
    // Compute the Local matrix for the geometry
    cachedLocalMat.loadIdentity();
    cachedLocalMat.setRotationQuaternion(g.localTransform.getRotation());
    cachedLocalMat.setTranslation(g.localTransform.getTranslation());
    TempVars vars = TempVars.get();
    Matrix4f scaleMat = vars.tempMat4;
    scaleMat.loadIdentity();
    scaleMat.scale(g.localTransform.getScale());
    cachedLocalMat.multLocal(scaleMat);
    vars.release();
    return cachedLocalMat;
}
Also used : Matrix4f(com.jme3.math.Matrix4f) TempVars(com.jme3.util.TempVars)

Aggregations

Geometry (com.jme3.scene.Geometry)246 Material (com.jme3.material.Material)175 Vector3f (com.jme3.math.Vector3f)116 Box (com.jme3.scene.shape.Box)92 DirectionalLight (com.jme3.light.DirectionalLight)56 Node (com.jme3.scene.Node)54 Sphere (com.jme3.scene.shape.Sphere)49 Spatial (com.jme3.scene.Spatial)41 Quaternion (com.jme3.math.Quaternion)39 Quad (com.jme3.scene.shape.Quad)33 Texture (com.jme3.texture.Texture)30 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)26 Mesh (com.jme3.scene.Mesh)25 KeyTrigger (com.jme3.input.controls.KeyTrigger)24 AmbientLight (com.jme3.light.AmbientLight)24 ColorRGBA (com.jme3.math.ColorRGBA)21 FilterPostProcessor (com.jme3.post.FilterPostProcessor)21 PointLight (com.jme3.light.PointLight)20 Vector2f (com.jme3.math.Vector2f)17 FloatBuffer (java.nio.FloatBuffer)17