Search in sources :

Example 1 with Dome

use of com.jme3.scene.shape.Dome in project jmonkeyengine by jMonkeyEngine.

the class CubeField method createPlayer.

private Node createPlayer() {
    Dome b = new Dome(Vector3f.ZERO, 10, 100, 1);
    Geometry playerMesh = new Geometry("Box", b);
    playerMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    playerMaterial.setColor("Color", ColorRGBA.Red);
    playerMesh.setMaterial(playerMaterial);
    playerMesh.setName("player");
    Box floor = new Box(100, 0, 100);
    Geometry floorMesh = new Geometry("Box", floor);
    Vector3f translation = Vector3f.ZERO.add(playerMesh.getLocalTranslation().getX(), playerMesh.getLocalTranslation().getY() - 1, 0);
    floorMesh.setLocalTranslation(translation);
    floorMaterial = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    floorMaterial.setColor("Color", ColorRGBA.LightGray);
    floorMesh.setMaterial(floorMaterial);
    floorMesh.setName("floor");
    Node playerNode = new Node();
    playerNode.attachChild(playerMesh);
    playerNode.attachChild(floorMesh);
    return playerNode;
}
Also used : Geometry(com.jme3.scene.Geometry) Dome(com.jme3.scene.shape.Dome) Vector3f(com.jme3.math.Vector3f) Node(com.jme3.scene.Node) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box)

Example 2 with Dome

use of com.jme3.scene.shape.Dome in project jmonkeyengine by jMonkeyEngine.

the class Dome method updateGeometry.

/**
     * Rebuilds the dome with a new set of parameters.
     * 
     * @param center the new center of the dome.
     * @param planes the number of planes along the Z-axis.
     * @param radialSamples the new number of radial samples of the dome.
     * @param radius the new radius of the dome.
     * @param insideView should the dome be set up to be viewed from the inside looking out.
     */
public void updateGeometry(Vector3f center, int planes, int radialSamples, float radius, boolean insideView) {
    this.insideView = insideView;
    this.center = center != null ? center : new Vector3f(0, 0, 0);
    this.planes = planes;
    this.radialSamples = radialSamples;
    this.radius = radius;
    int vertCount = ((planes - 1) * (radialSamples + 1)) + 1;
    // Allocate vertices, allocating one extra in each radial to get the
    // correct texture coordinates
    //        setVertexCount();
    //        setVertexBuffer(createVector3Buffer(getVertexCount()));
    // allocate normals
    //        setNormalBuffer(createVector3Buffer(getVertexCount()));
    // allocate texture coordinates
    //        getTextureCoords().set(0, new TexCoords(createVector2Buffer(getVertexCount())));
    FloatBuffer vb = BufferUtils.createVector3Buffer(vertCount);
    FloatBuffer nb = BufferUtils.createVector3Buffer(vertCount);
    FloatBuffer tb = BufferUtils.createVector2Buffer(vertCount);
    setBuffer(Type.Position, 3, vb);
    setBuffer(Type.Normal, 3, nb);
    setBuffer(Type.TexCoord, 2, tb);
    // generate geometry
    float fInvRS = 1.0f / radialSamples;
    float fYFactor = 1.0f / (planes - 1);
    // Generate points on the unit circle to be used in computing the mesh
    // points on a dome slice.
    float[] afSin = new float[(radialSamples)];
    float[] afCos = new float[(radialSamples)];
    for (int iR = 0; iR < radialSamples; iR++) {
        float fAngle = FastMath.TWO_PI * fInvRS * iR;
        afCos[iR] = FastMath.cos(fAngle);
        afSin[iR] = FastMath.sin(fAngle);
    }
    TempVars vars = TempVars.get();
    Vector3f tempVc = vars.vect3;
    Vector3f tempVb = vars.vect2;
    Vector3f tempVa = vars.vect1;
    // generate the dome itself
    int i = 0;
    for (int iY = 0; iY < (planes - 1); iY++, i++) {
        // in (0,1)
        float fYFraction = fYFactor * iY;
        float fY = radius * fYFraction;
        // compute center of slice
        Vector3f kSliceCenter = tempVb.set(center);
        kSliceCenter.y += fY;
        // compute radius of slice
        float fSliceRadius = FastMath.sqrt(FastMath.abs(radius * radius - fY * fY));
        // compute slice vertices
        Vector3f kNormal;
        int iSave = i;
        for (int iR = 0; iR < radialSamples; iR++, i++) {
            // in [0,1)
            float fRadialFraction = iR * fInvRS;
            Vector3f kRadial = tempVc.set(afCos[iR], 0, afSin[iR]);
            kRadial.mult(fSliceRadius, tempVa);
            vb.put(kSliceCenter.x + tempVa.x).put(kSliceCenter.y + tempVa.y).put(kSliceCenter.z + tempVa.z);
            BufferUtils.populateFromBuffer(tempVa, vb, i);
            kNormal = tempVa.subtractLocal(center);
            kNormal.normalizeLocal();
            if (!insideView) {
                nb.put(kNormal.x).put(kNormal.y).put(kNormal.z);
            } else {
                nb.put(-kNormal.x).put(-kNormal.y).put(-kNormal.z);
            }
            tb.put(fRadialFraction).put(fYFraction);
        }
        BufferUtils.copyInternalVector3(vb, iSave, i);
        BufferUtils.copyInternalVector3(nb, iSave, i);
        tb.put(1.0f).put(fYFraction);
    }
    vars.release();
    // pole
    vb.put(this.center.x).put(this.center.y + radius).put(this.center.z);
    nb.put(0).put(insideView ? -1 : 1).put(0);
    tb.put(0.5f).put(1.0f);
    // allocate connectivity
    int triCount = (planes - 2) * radialSamples * 2 + radialSamples;
    ShortBuffer ib = BufferUtils.createShortBuffer(3 * triCount);
    setBuffer(Type.Index, 3, ib);
    // generate connectivity
    int index = 0;
    // Generate only for middle planes
    for (int plane = 1; plane < (planes - 1); plane++) {
        int bottomPlaneStart = ((plane - 1) * (radialSamples + 1));
        int topPlaneStart = (plane * (radialSamples + 1));
        for (int sample = 0; sample < radialSamples; sample++, index += 6) {
            if (insideView) {
                ib.put((short) (bottomPlaneStart + sample));
                ib.put((short) (bottomPlaneStart + sample + 1));
                ib.put((short) (topPlaneStart + sample));
                ib.put((short) (bottomPlaneStart + sample + 1));
                ib.put((short) (topPlaneStart + sample + 1));
                ib.put((short) (topPlaneStart + sample));
            } else {
                ib.put((short) (bottomPlaneStart + sample));
                ib.put((short) (topPlaneStart + sample));
                ib.put((short) (bottomPlaneStart + sample + 1));
                ib.put((short) (bottomPlaneStart + sample + 1));
                ib.put((short) (topPlaneStart + sample));
                ib.put((short) (topPlaneStart + sample + 1));
            }
        }
    }
    // pole triangles
    int bottomPlaneStart = (planes - 2) * (radialSamples + 1);
    for (int samples = 0; samples < radialSamples; samples++, index += 3) {
        if (insideView) {
            ib.put((short) (bottomPlaneStart + samples));
            ib.put((short) (bottomPlaneStart + samples + 1));
            ib.put((short) (vertCount - 1));
        } else {
            ib.put((short) (bottomPlaneStart + samples));
            ib.put((short) (vertCount - 1));
            ib.put((short) (bottomPlaneStart + samples + 1));
        }
    }
    updateBound();
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) TempVars(com.jme3.util.TempVars) ShortBuffer(java.nio.ShortBuffer)

Aggregations

Vector3f (com.jme3.math.Vector3f)2 Material (com.jme3.material.Material)1 Geometry (com.jme3.scene.Geometry)1 Node (com.jme3.scene.Node)1 Box (com.jme3.scene.shape.Box)1 Dome (com.jme3.scene.shape.Dome)1 TempVars (com.jme3.util.TempVars)1 FloatBuffer (java.nio.FloatBuffer)1 ShortBuffer (java.nio.ShortBuffer)1