Search in sources :

Example 1 with Mode

use of com.jme3.scene.Mesh.Mode in project jmonkeyengine by jMonkeyEngine.

the class GeometryBatchFactory method mergeGeometries.

/**
     * Merges all geometries in the collection into
     * the output mesh. Creates a new material using the TextureAtlas.
     * 
     * @param geometries
     * @param outMesh
     */
public static void mergeGeometries(Collection<Geometry> geometries, Mesh outMesh) {
    int[] compsForBuf = new int[VertexBuffer.Type.values().length];
    Format[] formatForBuf = new Format[compsForBuf.length];
    boolean[] normForBuf = new boolean[VertexBuffer.Type.values().length];
    int totalVerts = 0;
    int totalTris = 0;
    int totalLodLevels = 0;
    int maxWeights = -1;
    Mode mode = null;
    for (Geometry geom : geometries) {
        totalVerts += geom.getVertexCount();
        totalTris += geom.getTriangleCount();
        totalLodLevels = Math.min(totalLodLevels, geom.getMesh().getNumLodLevels());
        Mode listMode;
        int components;
        switch(geom.getMesh().getMode()) {
            case Points:
                listMode = Mode.Points;
                components = 0;
                break;
            case LineLoop:
            case LineStrip:
            case Lines:
                listMode = Mode.Lines;
                components = 2;
                break;
            case TriangleFan:
            case TriangleStrip:
            case Triangles:
                listMode = 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() != 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;
        compsForBuf[Type.Index.ordinal()] = components;
    }
    outMesh.setMaxNumWeights(maxWeights);
    outMesh.setMode(mode);
    if (totalVerts >= 65536) {
        // make sure we create an UnsignedInt buffer so
        // we can fit all of the meshes
        formatForBuf[Type.Index.ordinal()] = Format.UnsignedInt;
    } else {
        formatForBuf[Type.Index.ordinal()] = 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 == Type.Index.ordinal()) {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalTris);
        } else {
            data = VertexBuffer.createBuffer(formatForBuf[i], compsForBuf[i], totalVerts);
        }
        VertexBuffer vb = new VertexBuffer(Type.values()[i]);
        vb.setupData(Usage.Static, 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();
        geom.computeWorldMatrix();
        Matrix4f worldMatrix = geom.getWorldMatrix();
        int geomVertCount = inMesh.getVertexCount();
        int geomTriCount = inMesh.getTriangleCount();
        for (int bufType = 0; bufType < compsForBuf.length; bufType++) {
            VertexBuffer inBuf = inMesh.getBuffer(Type.values()[bufType]);
            VertexBuffer outBuf = outMesh.getBuffer(Type.values()[bufType]);
            if (inBuf == null || outBuf == null) {
                continue;
            }
            if (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 (Type.Position.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doTransformVerts(inPos, globalVertIndex, outPos, worldMatrix);
            } else if (Type.Normal.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                doTransformNorms(inPos, globalVertIndex, outPos, worldMatrix);
            } else if (Type.Tangent.ordinal() == bufType) {
                FloatBuffer inPos = (FloatBuffer) inBuf.getDataReadOnly();
                FloatBuffer outPos = (FloatBuffer) outBuf.getData();
                int components = inBuf.getNumComponents();
                doTransformTangents(inPos, globalVertIndex, components, outPos, worldMatrix);
            } else {
                inBuf.copyElements(0, outBuf, globalVertIndex, geomVertCount);
            }
        }
        globalVertIndex += geomVertCount;
        globalTriIndex += geomTriCount;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) Mode(com.jme3.scene.Mesh.Mode) FloatBuffer(java.nio.FloatBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Matrix4f(com.jme3.math.Matrix4f) Format(com.jme3.scene.VertexBuffer.Format)

Example 2 with Mode

use of com.jme3.scene.Mesh.Mode in project jmonkeyengine by jMonkeyEngine.

the class PssmShadowRenderer method setFilterMode.

/**
     * Sets the filtering mode for shadow edges see {@link FilterMode} for more
     * info
     *
     * @param filterMode
     */
public final void setFilterMode(FilterMode filterMode) {
    if (filterMode == null) {
        throw new NullPointerException();
    }
    if (this.filterMode == filterMode) {
        return;
    }
    this.filterMode = filterMode;
    postshadowMat.setInt("FilterMode", filterMode.ordinal());
    postshadowMat.setFloat("PCFEdge", edgesThickness);
    if (compareMode == CompareMode.Hardware) {
        for (Texture2D shadowMap : shadowMaps) {
            if (filterMode == FilterMode.Bilinear) {
                shadowMap.setMagFilter(MagFilter.Bilinear);
                shadowMap.setMinFilter(MinFilter.BilinearNoMipMaps);
            } else {
                shadowMap.setMagFilter(MagFilter.Nearest);
                shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
            }
        }
    }
    applyFilterMode = true;
}
Also used : Texture2D(com.jme3.texture.Texture2D)

Example 3 with Mode

use of com.jme3.scene.Mesh.Mode in project jmonkeyengine by jMonkeyEngine.

the class ShadowUtil method addGeometriesInCamFrustumAndViewPortFromNode.

/**
     * Helper function to recursively collect the geometries for getLitGeometriesInViewPort function.
     * 
     * @param vpCamera the viewPort camera 
     * @param cameras the camera array to check geometries against, representing the light viewspace
     * @param scene the Node to traverse or geometry to possibly add
     * @param outputGeometryList the output list of all geometries that are in the camera frustum
     */
private static void addGeometriesInCamFrustumAndViewPortFromNode(Camera vpCamera, Camera[] cameras, Spatial scene, RenderQueue.ShadowMode mode, GeometryList outputGeometryList) {
    if (scene.getCullHint() == Spatial.CullHint.Always)
        return;
    boolean inFrustum = false;
    for (int j = 0; j < cameras.length && inFrustum == false; j++) {
        Camera camera = cameras[j];
        int planeState = camera.getPlaneState();
        camera.setPlaneState(0);
        inFrustum = camera.contains(scene.getWorldBound()) != Camera.FrustumIntersect.Outside && scene.checkCulling(vpCamera);
        camera.setPlaneState(planeState);
    }
    if (inFrustum) {
        if (scene instanceof Node) {
            Node node = (Node) scene;
            for (Spatial child : node.getChildren()) {
                addGeometriesInCamFrustumAndViewPortFromNode(vpCamera, cameras, child, mode, outputGeometryList);
            }
        } else if (scene instanceof Geometry) {
            if (checkShadowMode(scene.getShadowMode(), mode) && !((Geometry) scene).isGrouped()) {
                outputGeometryList.add((Geometry) scene);
            }
        }
    }
}
Also used : Geometry(com.jme3.scene.Geometry) Spatial(com.jme3.scene.Spatial) Node(com.jme3.scene.Node) Camera(com.jme3.renderer.Camera)

Example 4 with Mode

use of com.jme3.scene.Mesh.Mode in project jmonkeyengine by jMonkeyEngine.

the class AbstractShadowRenderer method setShadowCompareMode.

/**
     * Sets the shadow compare mode. See {@link CompareMode} for more info.
     *
     * @param compareMode the desired compare mode (not null)
     */
public final void setShadowCompareMode(CompareMode compareMode) {
    if (compareMode == null) {
        throw new IllegalArgumentException("Shadow compare mode cannot be null");
    }
    this.shadowCompareMode = compareMode;
    for (Texture2D shadowMap : shadowMaps) {
        if (compareMode == CompareMode.Hardware) {
            shadowMap.setShadowCompareMode(ShadowCompareMode.LessOrEqual);
            if (edgeFilteringMode == EdgeFilteringMode.Bilinear) {
                shadowMap.setMagFilter(MagFilter.Bilinear);
                shadowMap.setMinFilter(MinFilter.BilinearNoMipMaps);
            } else {
                shadowMap.setMagFilter(MagFilter.Nearest);
                shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
            }
        } else {
            shadowMap.setShadowCompareMode(ShadowCompareMode.Off);
            shadowMap.setMagFilter(MagFilter.Nearest);
            shadowMap.setMinFilter(MinFilter.NearestNoMipMaps);
        }
    }
    postshadowMat.setBoolean("HardwareShadows", compareMode == CompareMode.Hardware);
}
Also used : Texture2D(com.jme3.texture.Texture2D)

Example 5 with Mode

use of com.jme3.scene.Mesh.Mode in project jmonkeyengine by jMonkeyEngine.

the class VRAppState method stateAttached.

@Override
public void stateAttached(AppStateManager stateManager) {
    //To change body of generated methods, choose Tools | Templates.
    super.stateAttached(stateManager);
    if (settings == null) {
        settings = new AppSettings(true);
        logger.config("Using default settings.");
    } else {
        logger.config("Using given settings.");
    }
    // Attach VR environment to the application
    if (!environment.isInitialized()) {
        environment.initialize();
    }
    if (environment.isInitialized()) {
        environment.atttach(this, stateManager.getApplication());
    } else {
        logger.severe("Cannot attach VR environment to the VR app state as its not initialized.");
    }
    GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    if (environment.isInVR() && !environment.compositorAllowed()) {
        // "easy extended" mode
        // setup experimental JFrame on external device
        // first, find the VR device
        GraphicsDevice VRdev = null;
        GraphicsDevice[] devs = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        // pick the display that isn't the default one
        for (GraphicsDevice gd : devs) {
            if (gd != defDev) {
                VRdev = gd;
                break;
            }
        }
        // did we get the VR device?
        if (VRdev != null) {
            // set properties for VR acceleration
            try {
                java.awt.DisplayMode useDM = null;
                int max = 0;
                for (java.awt.DisplayMode dm : VRdev.getDisplayModes()) {
                    int check = dm.getHeight() + dm.getWidth() + dm.getRefreshRate() + dm.getBitDepth();
                    if (check > max) {
                        max = check;
                        useDM = dm;
                    }
                }
                // create a window for the VR device
                settings.setWidth(useDM.getWidth());
                settings.setHeight(useDM.getHeight());
                settings.setBitsPerPixel(useDM.getBitDepth());
                settings.setFrequency(useDM.getRefreshRate());
                settings.setSwapBuffers(true);
                // allow vsync on this display
                settings.setVSync(true);
                stateManager.getApplication().setSettings(settings);
                logger.config("Updated underlying application settings.");
                // make sure we are in the right display mode
                if (VRdev.getDisplayMode().equals(useDM) == false) {
                    VRdev.setDisplayMode(useDM);
                }
                return;
            } catch (Exception e) {
                logger.log(Level.SEVERE, e.getMessage(), e);
            }
        } else {
            logger.config("Cannot access to external screen.");
        }
    } else {
        if (!environment.isInVR()) {
            logger.config("Cannot switch to VR mode (VR disabled by user).");
        } else if (!environment.compositorAllowed()) {
            logger.warning("Cannot switch to VR mode (VR not supported).");
        }
    }
    if (!environment.isInVR()) {
        //FIXME: Handling GLFW workaround on MacOS
        boolean macOs = false;
        if (macOs) {
            // GLFW workaround on macs
            settings.setFrequency(defDev.getDisplayMode().getRefreshRate());
            settings.setDepthBits(24);
            settings.setVSync(true);
            // try and read resolution from file in local dir
            File resfile = new File("resolution.txt");
            if (resfile.exists()) {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(resfile));
                    settings.setWidth(Integer.parseInt(br.readLine()));
                    settings.setHeight(Integer.parseInt(br.readLine()));
                    try {
                        settings.setFullscreen(br.readLine().toLowerCase(Locale.ENGLISH).contains("full"));
                    } catch (Exception e) {
                        settings.setFullscreen(false);
                    }
                    br.close();
                } catch (Exception e) {
                    settings.setWidth(1280);
                    settings.setHeight(720);
                }
            } else {
                settings.setWidth(1280);
                settings.setHeight(720);
                settings.setFullscreen(false);
            }
            settings.setResizable(false);
        }
        settings.setSwapBuffers(true);
    } else {
        // use basic mirroring window, skip settings window
        settings.setSamples(1);
        settings.setWidth(xWin);
        settings.setHeight(yWin);
        settings.setBitsPerPixel(32);
        settings.setFrameRate(0);
        settings.setFrequency(environment.getVRHardware().getDisplayFrequency());
        settings.setFullscreen(false);
        // stop vsyncing on primary monitor!
        settings.setVSync(false);
        settings.setSwapBuffers(environment.isSwapBuffers());
    }
    // Updating application settings
    stateManager.getApplication().setSettings(settings);
    logger.config("Updated underlying application settings.");
}
Also used : GraphicsDevice(java.awt.GraphicsDevice) AppSettings(com.jme3.system.AppSettings) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Aggregations

Vector3f (com.jme3.math.Vector3f)6 Texture2D (com.jme3.texture.Texture2D)6 Bone (com.jme3.animation.Bone)5 Quaternion (com.jme3.math.Quaternion)4 AppSettings (com.jme3.system.AppSettings)3 AnimControl (com.jme3.animation.AnimControl)2 InputCapsule (com.jme3.export.InputCapsule)2 OutputCapsule (com.jme3.export.OutputCapsule)2 Material (com.jme3.material.Material)2 Spatial (com.jme3.scene.Spatial)2 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)2 Texture (com.jme3.texture.Texture)2 TempVars (com.jme3.util.TempVars)2 GraphicsDevice (java.awt.GraphicsDevice)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 Buffer (java.nio.Buffer)2 FloatBuffer (java.nio.FloatBuffer)2