Search in sources :

Example 41 with Structure

use of com.jme3.scene.plugins.blender.file.Structure in project jmonkeyengine by jMonkeyEngine.

the class CurvesTemporalMesh method loadBezierCurve.

/**
     * The methods loads the bezier curve from the given structure.
     * @param nurbStructure
     *            the structure containing a single curve definition
     * @param materialIndex
     *            the index of this segment's material
     * @throws BlenderFileException
     *             an exception is thrown when problems with reading occur
     */
private void loadBezierCurve(Structure nurbStructure, int materialIndex) throws BlenderFileException {
    Pointer pBezierTriple = (Pointer) nurbStructure.getFieldValue("bezt");
    if (pBezierTriple.isNotNull()) {
        int resolution = ((Number) nurbStructure.getFieldValue("resolu")).intValue();
        boolean cyclic = (((Number) nurbStructure.getFieldValue("flagu")).intValue() & 0x01) != 0;
        boolean smooth = (((Number) nurbStructure.getFieldValue("flag")).intValue() & FLAG_SMOOTH) != 0;
        // creating the curve object
        BezierCurve bezierCurve = new BezierCurve(0, pBezierTriple.fetchData(), 3, blenderContext.getBlenderKey().isFixUpAxis());
        List<Vector3f> controlPoints = bezierCurve.getControlPoints();
        if (cyclic) {
            // copy the first three points at the end
            for (int i = 0; i < 3; ++i) {
                controlPoints.add(controlPoints.get(i));
            }
        }
        // removing the first and last handles
        controlPoints.remove(0);
        controlPoints.remove(controlPoints.size() - 1);
        // creating curve
        Curve curve = new Curve(new Spline(SplineType.Bezier, controlPoints, 0, false), resolution);
        FloatBuffer vertsBuffer = (FloatBuffer) curve.getBuffer(Type.Position).getData();
        beziers.add(new BezierLine(BufferUtils.getVector3Array(vertsBuffer), materialIndex, smooth, cyclic));
    }
}
Also used : Vector3f(com.jme3.math.Vector3f) Curve(com.jme3.scene.shape.Curve) Pointer(com.jme3.scene.plugins.blender.file.Pointer) FloatBuffer(java.nio.FloatBuffer) Spline(com.jme3.math.Spline)

Example 42 with Structure

use of com.jme3.scene.plugins.blender.file.Structure in project jmonkeyengine by jMonkeyEngine.

the class LightHelper method toLight.

public Light toLight(Structure structure, BlenderContext blenderContext) throws BlenderFileException {
    Light result = (Light) blenderContext.getLoadedFeature(structure.getOldMemoryAddress(), LoadedDataType.FEATURE);
    if (result != null) {
        return result;
    }
    Light light = null;
    int type = ((Number) structure.getFieldValue("type")).intValue();
    switch(type) {
        case // Lamp
        0:
            light = new PointLight();
            float distance = ((Number) structure.getFieldValue("dist")).floatValue();
            ((PointLight) light).setRadius(distance);
            break;
        case // Sun
        1:
            LOGGER.log(Level.WARNING, "'Sun' lamp is not supported in jMonkeyEngine. Using PointLight with radius = Float.MAX_VALUE.");
            light = new PointLight();
            ((PointLight) light).setRadius(Float.MAX_VALUE);
            break;
        case // Spot
        2:
            light = new SpotLight();
            // range
            ((SpotLight) light).setSpotRange(((Number) structure.getFieldValue("dist")).floatValue());
            // outer angle
            float outerAngle = ((Number) structure.getFieldValue("spotsize")).floatValue() * FastMath.DEG_TO_RAD * 0.5f;
            ((SpotLight) light).setSpotOuterAngle(outerAngle);
            // inner angle
            float spotblend = ((Number) structure.getFieldValue("spotblend")).floatValue();
            spotblend = FastMath.clamp(spotblend, 0, 1);
            float innerAngle = outerAngle * (1 - spotblend);
            ((SpotLight) light).setSpotInnerAngle(innerAngle);
            break;
        case // Hemi
        3:
            LOGGER.log(Level.WARNING, "'Hemi' lamp is not supported in jMonkeyEngine. Using DirectionalLight instead.");
        case // Area
        4:
            light = new DirectionalLight();
            break;
        default:
            throw new BlenderFileException("Unknown light source type: " + type);
    }
    float r = ((Number) structure.getFieldValue("r")).floatValue();
    float g = ((Number) structure.getFieldValue("g")).floatValue();
    float b = ((Number) structure.getFieldValue("b")).floatValue();
    light.setColor(new ColorRGBA(r, g, b, 1.0f));
    light.setName(structure.getName());
    return light;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) DirectionalLight(com.jme3.light.DirectionalLight) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) PointLight(com.jme3.light.PointLight) SpotLight(com.jme3.light.SpotLight)

Example 43 with Structure

use of com.jme3.scene.plugins.blender.file.Structure in project jmonkeyengine by jMonkeyEngine.

the class Point method loadAll.

/**
     * Loads all points of the mesh that do not belong to any edge.
     * @param meshStructure
     *            the mesh structure
     * @return a list of points
     * @throws BlenderFileException
     *             an exception is thrown when problems with file reading occur
     */
public static List<Point> loadAll(Structure meshStructure) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Loading all points that do not belong to any edge from mesh: {0}", meshStructure.getName());
    List<Point> result = new ArrayList<Point>();
    Pointer pMEdge = (Pointer) meshStructure.getFieldValue("medge");
    if (pMEdge.isNotNull()) {
        int count = ((Number) meshStructure.getFieldValue("totvert")).intValue();
        Set<Integer> unusedVertices = new HashSet<Integer>(count);
        for (int i = 0; i < count; ++i) {
            unusedVertices.add(i);
        }
        List<Structure> edges = pMEdge.fetchData();
        for (Structure edge : edges) {
            unusedVertices.remove(((Number) edge.getFieldValue("v1")).intValue());
            unusedVertices.remove(((Number) edge.getFieldValue("v2")).intValue());
        }
        for (Integer unusedIndex : unusedVertices) {
            result.add(new Point(unusedIndex));
        }
    }
    LOGGER.log(Level.FINE, "Loaded {0} points.", result.size());
    return result;
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) HashSet(java.util.HashSet)

Example 44 with Structure

use of com.jme3.scene.plugins.blender.file.Structure in project jmonkeyengine by jMonkeyEngine.

the class ArmatureModifier method buildBones.

private void buildBones(Long armatureObjectOMA, Structure boneStructure, Bone parent, List<Bone> result, Long spatialOMA, BlenderContext blenderContext) throws BlenderFileException {
    BoneContext bc = new BoneContext(armatureObjectOMA, boneStructure, blenderContext);
    bc.buildBone(result, spatialOMA, blenderContext);
}
Also used : BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext)

Example 45 with Structure

use of com.jme3.scene.plugins.blender.file.Structure in project jmonkeyengine by jMonkeyEngine.

the class MaskModifier method apply.

@Override
public void apply(Node node, BlenderContext blenderContext) {
    if (invalid) {
        LOGGER.log(Level.WARNING, "Mirror modifier is invalid! Cannot be applied to: {0}", node.getName());
    } else {
        TemporalMesh temporalMesh = this.getTemporalMesh(node);
        if (temporalMesh != null) {
            List<String> vertexGroupsToRemove = new ArrayList<String>();
            if (vertexGroupName != null) {
                vertexGroupsToRemove.add(vertexGroupName);
            } else if (pArmatureObject != null && pArmatureObject.isNotNull()) {
                try {
                    Structure armatureObject = pArmatureObject.fetchData().get(0);
                    Structure armatureStructure = ((Pointer) armatureObject.getFieldValue("data")).fetchData().get(0);
                    List<Structure> bonebase = ((Structure) armatureStructure.getFieldValue("bonebase")).evaluateListBase();
                    vertexGroupsToRemove.addAll(this.readBoneNames(bonebase));
                } catch (BlenderFileException e) {
                    LOGGER.log(Level.SEVERE, "Cannot load armature object for the mask modifier. Cause: {0}", e.getLocalizedMessage());
                    LOGGER.log(Level.SEVERE, "Mask modifier will NOT be applied to node named: {0}", node.getName());
                }
            } else {
                // if the mesh has no vertex groups then remove all verts
                // if the mesh has at least one vertex group - then do nothing
                // I have no idea why we should do that, but blender works this way
                Set<String> vertexGroupNames = new HashSet<String>();
                for (Map<String, Float> groups : temporalMesh.getVertexGroups()) {
                    vertexGroupNames.addAll(groups.keySet());
                }
                if (vertexGroupNames.size() == 0 && !invertMask || vertexGroupNames.size() > 0 && invertMask) {
                    temporalMesh.clear();
                }
            }
            if (vertexGroupsToRemove.size() > 0) {
                List<Integer> vertsToBeRemoved = new ArrayList<Integer>();
                for (int i = 0; i < temporalMesh.getVertexCount(); ++i) {
                    Map<String, Float> vertexGroups = temporalMesh.getVertexGroups().get(i);
                    boolean hasVertexGroup = false;
                    if (vertexGroups != null) {
                        for (String groupName : vertexGroupsToRemove) {
                            Float weight = vertexGroups.get(groupName);
                            if (weight != null && weight > 0) {
                                hasVertexGroup = true;
                                break;
                            }
                        }
                    }
                    if (!hasVertexGroup && !invertMask || hasVertexGroup && invertMask) {
                        vertsToBeRemoved.add(i);
                    }
                }
                Collections.reverse(vertsToBeRemoved);
                for (Integer vertexIndex : vertsToBeRemoved) {
                    this.removeVertexAt(vertexIndex, temporalMesh);
                }
            }
        } else {
            LOGGER.log(Level.WARNING, "Cannot find temporal mesh for node: {0}. The modifier will NOT be applied!", node);
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) BlenderFileException(com.jme3.scene.plugins.blender.file.BlenderFileException) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Point(com.jme3.scene.plugins.blender.meshes.Point) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) ArrayList(java.util.ArrayList) List(java.util.List) Structure(com.jme3.scene.plugins.blender.file.Structure) Map(java.util.Map)

Aggregations

Structure (com.jme3.scene.plugins.blender.file.Structure)34 Pointer (com.jme3.scene.plugins.blender.file.Pointer)30 ArrayList (java.util.ArrayList)16 Vector3f (com.jme3.math.Vector3f)8 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)7 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)7 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)7 List (java.util.List)6 Map (java.util.Map)6 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 ObjectHelper (com.jme3.scene.plugins.blender.objects.ObjectHelper)5 HashMap (java.util.HashMap)5 ColorRGBA (com.jme3.math.ColorRGBA)4 Node (com.jme3.scene.Node)4 Texture (com.jme3.texture.Texture)4 CameraNode (com.jme3.scene.CameraNode)3 LightNode (com.jme3.scene.LightNode)3 Spatial (com.jme3.scene.Spatial)3 AnimationHelper (com.jme3.scene.plugins.blender.animations.AnimationHelper)3 ConstIpo (com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo)3