Search in sources :

Example 6 with Torus

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

the class TestExpandingTorus method simpleInitApp.

@Override
public void simpleInitApp() {
    torus = new Torus(30, 10, .5f, 1f);
    geom = new Geometry("Torus", torus);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    geom.setMaterial(mat);
    rootNode.attachChild(geom);
}
Also used : Geometry(com.jme3.scene.Geometry) Torus(com.jme3.scene.shape.Torus) Material(com.jme3.material.Material)

Example 7 with Torus

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

the class PQTorus method updateGeometry.

/**
     * Rebuilds this torus based on a new set of parameters.
     * 
     * @param p the x/z oscillation.
     * @param q the y oscillation.
     * @param radius the radius of the PQTorus.
     * @param width the width of the torus.
     * @param steps the steps along the torus.
     * @param radialSamples radial samples for the torus.
     */
public void updateGeometry(float p, float q, float radius, float width, int steps, int radialSamples) {
    this.p = p;
    this.q = q;
    this.radius = radius;
    this.width = width;
    this.steps = steps;
    this.radialSamples = radialSamples;
    final float thetaStep = (FastMath.TWO_PI / steps);
    final float betaStep = (FastMath.TWO_PI / radialSamples);
    Vector3f[] torusPoints = new Vector3f[steps];
    // Allocate all of the required buffers
    int vertCount = radialSamples * steps;
    FloatBuffer fpb = createVector3Buffer(vertCount);
    FloatBuffer fnb = createVector3Buffer(vertCount);
    FloatBuffer ftb = createVector2Buffer(vertCount);
    Vector3f pointB, T, N, B;
    Vector3f tempNorm = new Vector3f();
    float r, x, y, z, theta = 0.0f, beta;
    int nvertex = 0;
    // Move along the length of the pq torus
    for (int i = 0; i < steps; i++) {
        theta += thetaStep;
        float circleFraction = ((float) i) / (float) steps;
        // Find the point on the torus
        r = (0.5f * (2.0f + FastMath.sin(q * theta)) * radius);
        x = (r * FastMath.cos(p * theta) * radius);
        y = (r * FastMath.sin(p * theta) * radius);
        z = (r * FastMath.cos(q * theta) * radius);
        torusPoints[i] = new Vector3f(x, y, z);
        // Now find a point slightly farther along the torus
        r = (0.5f * (2.0f + FastMath.sin(q * (theta + 0.01f))) * radius);
        x = (r * FastMath.cos(p * (theta + 0.01f)) * radius);
        y = (r * FastMath.sin(p * (theta + 0.01f)) * radius);
        z = (r * FastMath.cos(q * (theta + 0.01f)) * radius);
        pointB = new Vector3f(x, y, z);
        // Approximate the Frenet Frame
        T = pointB.subtract(torusPoints[i]);
        N = torusPoints[i].add(pointB);
        B = T.cross(N);
        N = B.cross(T);
        // Normalise the two vectors and then use them to create an oriented circle
        N = N.normalize();
        B = B.normalize();
        beta = 0.0f;
        for (int j = 0; j < radialSamples; j++, nvertex++) {
            beta += betaStep;
            float cx = FastMath.cos(beta) * width;
            float cy = FastMath.sin(beta) * width;
            float radialFraction = ((float) j) / radialSamples;
            tempNorm.x = (cx * N.x + cy * B.x);
            tempNorm.y = (cx * N.y + cy * B.y);
            tempNorm.z = (cx * N.z + cy * B.z);
            fnb.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
            tempNorm.addLocal(torusPoints[i]);
            fpb.put(tempNorm.x).put(tempNorm.y).put(tempNorm.z);
            ftb.put(radialFraction).put(circleFraction);
        }
    }
    // Update the indices data
    ShortBuffer sib = createShortBuffer(6 * vertCount);
    for (int i = 0; i < vertCount; i++) {
        sib.put(new short[] { (short) (i), (short) (i - radialSamples), (short) (i + 1), (short) (i + 1), (short) (i - radialSamples), (short) (i - radialSamples + 1) });
    }
    for (int i = 0, len = sib.capacity(); i < len; i++) {
        int ind = sib.get(i);
        if (ind < 0) {
            ind += vertCount;
            sib.put(i, (short) ind);
        } else if (ind >= vertCount) {
            ind -= vertCount;
            sib.put(i, (short) ind);
        }
    }
    sib.rewind();
    setBuffer(Type.Position, 3, fpb);
    setBuffer(Type.Normal, 3, fnb);
    setBuffer(Type.TexCoord, 2, ftb);
    setBuffer(Type.Index, 3, sib);
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer)

Aggregations

Geometry (com.jme3.scene.Geometry)5 Torus (com.jme3.scene.shape.Torus)5 Vector3f (com.jme3.math.Vector3f)4 Material (com.jme3.material.Material)3 Sphere (com.jme3.scene.shape.Sphere)3 DirectionalLight (com.jme3.light.DirectionalLight)2 PointLight (com.jme3.light.PointLight)2 FloatBuffer (java.nio.FloatBuffer)2 AudioNode (com.jme3.audio.AudioNode)1 Quaternion (com.jme3.math.Quaternion)1 LightNode (com.jme3.scene.LightNode)1 Node (com.jme3.scene.Node)1 Box (com.jme3.scene.shape.Box)1 ShortBuffer (java.nio.ShortBuffer)1