Search in sources :

Example 46 with Geometry

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

the class Sphere method setGeometryData.

/**
     * builds the vertices based on the radius, radial and zSamples.
     */
private void setGeometryData() {
    // allocate vertices
    vertCount = (zSamples - 2) * (radialSamples + 1) + 2;
    FloatBuffer posBuf = BufferUtils.createVector3Buffer(vertCount);
    // allocate normals if requested
    FloatBuffer normBuf = BufferUtils.createVector3Buffer(vertCount);
    // allocate texture coordinates
    FloatBuffer texBuf = BufferUtils.createVector2Buffer(vertCount);
    setBuffer(Type.Position, 3, posBuf);
    setBuffer(Type.Normal, 3, normBuf);
    setBuffer(Type.TexCoord, 2, texBuf);
    // generate geometry
    float fInvRS = 1.0f / radialSamples;
    float fZFactor = 2.0f / (zSamples - 1);
    // Generate points on the unit circle to be used in computing the mesh
    // points on a sphere slice.
    float[] afSin = new float[(radialSamples + 1)];
    float[] afCos = new float[(radialSamples + 1)];
    for (int iR = 0; iR < radialSamples; iR++) {
        float fAngle = FastMath.TWO_PI * fInvRS * iR;
        afCos[iR] = FastMath.cos(fAngle);
        afSin[iR] = FastMath.sin(fAngle);
    }
    afSin[radialSamples] = afSin[0];
    afCos[radialSamples] = afCos[0];
    TempVars vars = TempVars.get();
    Vector3f tempVa = vars.vect1;
    Vector3f tempVb = vars.vect2;
    Vector3f tempVc = vars.vect3;
    // generate the sphere itself
    int i = 0;
    for (int iZ = 1; iZ < (zSamples - 1); iZ++) {
        // in (-pi/2, pi/2)
        float fAFraction = FastMath.HALF_PI * (-1.0f + fZFactor * iZ);
        float fZFraction;
        if (useEvenSlices) {
            // in (-1, 1)
            fZFraction = -1.0f + fZFactor * iZ;
        } else {
            // in (-1,1)
            fZFraction = FastMath.sin(fAFraction);
        }
        float fZ = radius * fZFraction;
        // compute center of slice
        Vector3f kSliceCenter = tempVb.set(Vector3f.ZERO);
        kSliceCenter.z += fZ;
        // compute radius of slice
        float fSliceRadius = FastMath.sqrt(FastMath.abs(radius * radius - fZ * fZ));
        // compute slice vertices with duplication at end point
        Vector3f kNormal;
        int iSave = i;
        for (int iR = 0; iR < radialSamples; iR++) {
            // in [0,1)
            float fRadialFraction = iR * fInvRS;
            Vector3f kRadial = tempVc.set(afCos[iR], afSin[iR], 0);
            kRadial.mult(fSliceRadius, tempVa);
            posBuf.put(kSliceCenter.x + tempVa.x).put(kSliceCenter.y + tempVa.y).put(kSliceCenter.z + tempVa.z);
            BufferUtils.populateFromBuffer(tempVa, posBuf, i);
            kNormal = tempVa;
            kNormal.normalizeLocal();
            if (// allow interior texture vs. exterior
            !interior) {
                normBuf.put(kNormal.x).put(kNormal.y).put(kNormal.z);
            } else {
                normBuf.put(-kNormal.x).put(-kNormal.y).put(-kNormal.z);
            }
            if (textureMode == TextureMode.Original) {
                texBuf.put(fRadialFraction).put(0.5f * (fZFraction + 1.0f));
            } else if (textureMode == TextureMode.Projected) {
                texBuf.put(fRadialFraction).put(FastMath.INV_PI * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
            } else if (textureMode == TextureMode.Polar) {
                float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
                float u = r * afCos[iR] + 0.5f;
                float v = r * afSin[iR] + 0.5f;
                texBuf.put(u).put(v);
            }
            i++;
        }
        BufferUtils.copyInternalVector3(posBuf, iSave, i);
        BufferUtils.copyInternalVector3(normBuf, iSave, i);
        if (textureMode == TextureMode.Original) {
            texBuf.put(1.0f).put(0.5f * (fZFraction + 1.0f));
        } else if (textureMode == TextureMode.Projected) {
            texBuf.put(1.0f).put(FastMath.INV_PI * (FastMath.HALF_PI + FastMath.asin(fZFraction)));
        } else if (textureMode == TextureMode.Polar) {
            float r = (FastMath.HALF_PI - FastMath.abs(fAFraction)) / FastMath.PI;
            texBuf.put(r + 0.5f).put(0.5f);
        }
        i++;
    }
    vars.release();
    // south pole
    posBuf.position(i * 3);
    posBuf.put(0f).put(0f).put(-radius);
    normBuf.position(i * 3);
    if (!interior) {
        // allow for inner
        normBuf.put(0).put(0).put(-1);
    } else // texture orientation
    // later.
    {
        normBuf.put(0).put(0).put(1);
    }
    texBuf.position(i * 2);
    if (textureMode == TextureMode.Polar) {
        texBuf.put(0.5f).put(0.5f);
    } else {
        texBuf.put(0.5f).put(0.0f);
    }
    i++;
    // north pole
    posBuf.put(0).put(0).put(radius);
    if (!interior) {
        normBuf.put(0).put(0).put(1);
    } else {
        normBuf.put(0).put(0).put(-1);
    }
    if (textureMode == TextureMode.Polar) {
        texBuf.put(0.5f).put(0.5f);
    } else {
        texBuf.put(0.5f).put(1.0f);
    }
    updateBound();
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) TempVars(com.jme3.util.TempVars)

Example 47 with Geometry

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

the class InstancedGeometry method updateInstances.

public void updateInstances() {
    FloatBuffer fb = (FloatBuffer) transformInstanceData.getData();
    fb.limit(fb.capacity());
    fb.position(0);
    TempVars vars = TempVars.get();
    {
        float[] temp = vars.matrixWrite;
        for (int i = 0; i < firstUnusedIndex; i++) {
            Geometry geom = geometries[i];
            if (geom == null) {
                geom = geometries[firstUnusedIndex - 1];
                if (geom == null) {
                    throw new AssertionError();
                }
                swap(i, firstUnusedIndex - 1);
                while (geometries[firstUnusedIndex - 1] == null) {
                    firstUnusedIndex--;
                }
            }
            Matrix4f worldMatrix = geom.getWorldMatrix();
            updateInstance(worldMatrix, temp, 0, vars.tempMat3, vars.quat1);
            fb.put(temp);
        }
    }
    vars.release();
    fb.flip();
    if (fb.limit() / INSTANCE_SIZE != firstUnusedIndex) {
        throw new AssertionError();
    }
    transformInstanceData.updateData(fb);
}
Also used : Geometry(com.jme3.scene.Geometry) Matrix4f(com.jme3.math.Matrix4f) FloatBuffer(java.nio.FloatBuffer) TempVars(com.jme3.util.TempVars)

Example 48 with Geometry

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

the class TestColorApp method simpleInitApp.

@Override
public void simpleInitApp() {
    // Lights
    DirectionalLight sun = new DirectionalLight();
    Vector3f sunPosition = new Vector3f(1, -1, 1);
    sun.setDirection(sunPosition);
    sun.setColor(new ColorRGBA(1f, 1f, 1f, 1f));
    rootNode.addLight(sun);
    //DirectionalLightShadowFilter sun_renderer = new DirectionalLightShadowFilter(assetManager, 2048, 4);
    DirectionalLightShadowRenderer sun_renderer = new DirectionalLightShadowRenderer(assetManager, 2048, 1);
    sun_renderer.setLight(sun);
    viewPort.addProcessor(sun_renderer);
    //        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
    //        fpp.addFilter(sun_renderer);
    //        viewPort.addProcessor(fpp);
    rootNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
    // Camera
    viewPort.setBackgroundColor(new ColorRGBA(.6f, .6f, .6f, 1f));
    ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);
    // Objects
    // Ground Object
    final Geometry groundBoxWhite = new Geometry("Box", new Box(7.5f, 7.5f, .25f));
    float[] f = { -FastMath.PI / 2, 3 * FastMath.PI / 2, 0f };
    groundBoxWhite.setLocalRotation(new Quaternion(f));
    groundBoxWhite.move(7.5f, -.75f, 7.5f);
    final Material groundMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    groundMaterial.setColor("Diffuse", new ColorRGBA(.9f, .9f, .9f, .9f));
    groundBoxWhite.setMaterial(groundMaterial);
    groundBoxWhite.addControl(chaseCam);
    rootNode.attachChild(groundBoxWhite);
    // Planter
    Geometry planterBox = new Geometry("Box", new Box(.5f, .5f, .5f));
    final Material planterMaterial = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    planterMaterial.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg"));
    planterBox.setMaterial(groundMaterial);
    planterBox.setLocalTranslation(10, 0, 9);
    rootNode.attachChild(planterBox);
    // Action!
    inputManager.addMapping("on", new KeyTrigger(KeyInput.KEY_Z));
    inputManager.addMapping("off", new KeyTrigger(KeyInput.KEY_X));
    inputManager.addListener(new AnalogListener() {

        @Override
        public void onAnalog(String s, float v, float v1) {
            if (s.equals("on")) {
                groundBoxWhite.setMaterial(planterMaterial);
            }
            if (s.equals("off")) {
                groundBoxWhite.setMaterial(groundMaterial);
            }
        }
    }, "on", "off");
    inputEnabled = true;
}
Also used : Quaternion(com.jme3.math.Quaternion) KeyTrigger(com.jme3.input.controls.KeyTrigger) ChaseCamera(com.jme3.input.ChaseCamera) Box(com.jme3.scene.shape.Box) Material(com.jme3.material.Material) Geometry(com.jme3.scene.Geometry) ColorRGBA(com.jme3.math.ColorRGBA) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) DirectionalLightShadowRenderer(com.jme3.shadow.DirectionalLightShadowRenderer) AnalogListener(com.jme3.input.controls.AnalogListener)

Example 49 with Geometry

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

the class TestConeVSFrustum method simpleInitApp.

@Override
public void simpleInitApp() {
    viewPort.setBackgroundColor(ColorRGBA.DarkGray);
    frustumCam = cam.clone();
    frustumCam.setFrustumFar(25);
    Vector3f[] points = new Vector3f[8];
    for (int i = 0; i < 8; i++) {
        points[i] = new Vector3f();
    }
    ShadowUtil.updateFrustumPoints2(frustumCam, points);
    WireFrustum frustumShape = new WireFrustum(points);
    Geometry frustum = new Geometry("frustum", frustumShape);
    frustum.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
    rootNode.attachChild(frustum);
    rootNode.addLight(new DirectionalLight());
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(0.2f));
    rootNode.addLight(al);
    Grid grid = new Grid(50, 50, 5);
    Geometry gridGeom = new Geometry("grid", grid);
    gridGeom.setMaterial(new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"));
    gridGeom.getMaterial().setColor("Color", ColorRGBA.Gray);
    rootNode.attachChild(gridGeom);
    gridGeom.setLocalTranslation(-125, -25, -125);
    //        flyCam.setMoveSpeed(30);
    //        flyCam.setDragToRotate(true);
    //        cam.setLocation(new Vector3f(56.182674f, 19.037334f, 7.093905f));
    //        cam.setRotation(new Quaternion(0.0816657f, -0.82228005f, 0.12213967f, 0.5497892f));
    spotLight = new SpotLight();
    spotLight.setSpotRange(25);
    spotLight.setSpotOuterAngle(10 * FastMath.DEG_TO_RAD);
    float radius = FastMath.tan(spotLight.getSpotOuterAngle()) * spotLight.getSpotRange();
    Cylinder cylinder = new Cylinder(5, 16, 0, radius, spotLight.getSpotRange(), true, false);
    geom = new Geometry("light", cylinder);
    geom.setMaterial(new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"));
    geom.getMaterial().setColor("Diffuse", ColorRGBA.White);
    geom.getMaterial().setColor("Ambient", ColorRGBA.DarkGray);
    geom.getMaterial().setBoolean("UseMaterialColors", true);
    final LightNode ln = new LightNode("lb", spotLight);
    ln.attachChild(geom);
    geom.setLocalTranslation(0, -spotLight.getSpotRange() / 2f, 0);
    geom.rotate(-FastMath.HALF_PI, 0, 0);
    rootNode.attachChild(ln);
    //        ln.rotate(FastMath.QUARTER_PI, 0, 0);
    //      ln.setLocalTranslation(0, 0, -16);
    inputManager.addMapping("click", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
    inputManager.addMapping("shift", new KeyTrigger(KeyInput.KEY_LSHIFT), new KeyTrigger(KeyInput.KEY_RSHIFT));
    inputManager.addMapping("middleClick", new MouseButtonTrigger(MouseInput.BUTTON_MIDDLE));
    inputManager.addMapping("up", new MouseAxisTrigger(MouseInput.AXIS_Y, false));
    inputManager.addMapping("down", new MouseAxisTrigger(MouseInput.AXIS_Y, true));
    inputManager.addMapping("left", new MouseAxisTrigger(MouseInput.AXIS_X, true));
    inputManager.addMapping("right", new MouseAxisTrigger(MouseInput.AXIS_X, false));
    final Node camTarget = new Node("CamTarget");
    rootNode.attachChild(camTarget);
    ChaseCameraAppState chaser = new ChaseCameraAppState();
    chaser.setTarget(camTarget);
    chaser.setMaxDistance(150);
    chaser.setDefaultDistance(70);
    chaser.setDefaultHorizontalRotation(FastMath.HALF_PI);
    chaser.setMinVerticalRotation(-FastMath.PI);
    chaser.setMaxVerticalRotation(FastMath.PI * 2);
    chaser.setToggleRotationTrigger(new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    stateManager.attach(chaser);
    flyCam.setEnabled(false);
    inputManager.addListener(new AnalogListener() {

        public void onAnalog(String name, float value, float tpf) {
            Spatial s = null;
            float mult = 1;
            if (moving) {
                s = ln;
            }
            if (panning) {
                s = camTarget;
                mult = -1;
            }
            if ((moving || panning) && s != null) {
                if (shift) {
                    if (name.equals("left")) {
                        tmp.set(cam.getDirection());
                        s.rotate(tmpQuat.fromAngleAxis(value, tmp));
                    }
                    if (name.equals("right")) {
                        tmp.set(cam.getDirection());
                        s.rotate(tmpQuat.fromAngleAxis(-value, tmp));
                    }
                } else {
                    value *= MOVE_SPEED * mult;
                    if (name.equals("up")) {
                        tmp.set(cam.getUp()).multLocal(value);
                        s.move(tmp);
                    }
                    if (name.equals("down")) {
                        tmp.set(cam.getUp()).multLocal(-value);
                        s.move(tmp);
                    }
                    if (name.equals("left")) {
                        tmp.set(cam.getLeft()).multLocal(value);
                        s.move(tmp);
                    }
                    if (name.equals("right")) {
                        tmp.set(cam.getLeft()).multLocal(-value);
                        s.move(tmp);
                    }
                }
            }
        }
    }, "up", "down", "left", "right");
    inputManager.addListener(new ActionListener() {

        public void onAction(String name, boolean isPressed, float tpf) {
            if (name.equals("click")) {
                if (isPressed) {
                    moving = true;
                } else {
                    moving = false;
                }
            }
            if (name.equals("middleClick")) {
                if (isPressed) {
                    panning = true;
                } else {
                    panning = false;
                }
            }
            if (name.equals("shift")) {
                if (isPressed) {
                    shift = true;
                } else {
                    shift = false;
                }
            }
        }
    }, "click", "middleClick", "shift");
    /**
         * An unshaded textured cube. // * Uses texture from jme3-test-data
         * library!
         */
    Box boxMesh = new Box(1f, 1f, 1f);
    boxGeo = new Geometry("A Textured Box", boxMesh);
    Material boxMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    Texture monkeyTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
    boxMat.setTexture("ColorMap", monkeyTex);
    boxGeo.setMaterial(boxMat);
    //        rootNode.attachChild(boxGeo);
    //
    //boxGeo2 = boxGeo.clone();
    //rootNode.attachChild(boxGeo2); 
    System.err.println("light " + spotLight.getPosition());
}
Also used : Grid(com.jme3.scene.debug.Grid) KeyTrigger(com.jme3.input.controls.KeyTrigger) MouseAxisTrigger(com.jme3.input.controls.MouseAxisTrigger) LightNode(com.jme3.scene.LightNode) Node(com.jme3.scene.Node) SpotLight(com.jme3.light.SpotLight) Texture(com.jme3.texture.Texture) LightNode(com.jme3.scene.LightNode) DirectionalLight(com.jme3.light.DirectionalLight) Material(com.jme3.material.Material) Box(com.jme3.scene.shape.Box) ChaseCameraAppState(com.jme3.app.ChaseCameraAppState) Geometry(com.jme3.scene.Geometry) Cylinder(com.jme3.scene.shape.Cylinder) ActionListener(com.jme3.input.controls.ActionListener) Spatial(com.jme3.scene.Spatial) WireFrustum(com.jme3.scene.debug.WireFrustum) AnalogListener(com.jme3.input.controls.AnalogListener) MouseButtonTrigger(com.jme3.input.controls.MouseButtonTrigger) AmbientLight(com.jme3.light.AmbientLight)

Example 50 with Geometry

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

the class TestDirectionalLightShadow method loadScene.

public void loadScene() {
    obj = new Spatial[2];
    // Setup first view
    mat = new Material[2];
    mat[0] = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
    mat[1] = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
    mat[1].setBoolean("UseMaterialColors", true);
    mat[1].setColor("Ambient", ColorRGBA.White);
    mat[1].setColor("Diffuse", ColorRGBA.White.clone());
    obj[0] = new Geometry("sphere", new Sphere(30, 30, 2));
    obj[0].setShadowMode(ShadowMode.CastAndReceive);
    obj[1] = new Geometry("cube", new Box(1.0f, 1.0f, 1.0f));
    obj[1].setShadowMode(ShadowMode.CastAndReceive);
    TangentBinormalGenerator.generate(obj[1]);
    TangentBinormalGenerator.generate(obj[0]);
    Spatial t = obj[0].clone(false);
    t.setLocalScale(10f);
    t.setMaterial(mat[1]);
    rootNode.attachChild(t);
    t.setLocalTranslation(0, 25, 0);
    for (int i = 0; i < 60; i++) {
        t = obj[FastMath.nextRandomInt(0, obj.length - 1)].clone(false);
        t.setLocalScale(FastMath.nextRandomFloat() * 10f);
        t.setMaterial(mat[FastMath.nextRandomInt(0, mat.length - 1)]);
        rootNode.attachChild(t);
        t.setLocalTranslation(FastMath.nextRandomFloat() * 200f, FastMath.nextRandomFloat() * 30f + 20, 30f * (i + 2f));
    }
    Box b = new Box(1000, 2, 1000);
    b.scaleTextureCoordinates(new Vector2f(10, 10));
    ground = new Geometry("soil", b);
    ground.setLocalTranslation(0, 10, 550);
    matGroundU = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    matGroundU.setColor("Color", ColorRGBA.Green);
    matGroundL = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
    Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
    grass.setWrap(WrapMode.Repeat);
    matGroundL.setTexture("DiffuseMap", grass);
    ground.setMaterial(matGroundL);
    ground.setShadowMode(ShadowMode.CastAndReceive);
    rootNode.attachChild(ground);
    l = new DirectionalLight();
    //l.setDirection(new Vector3f(0.5973172f, -0.16583486f, 0.7846725f).normalizeLocal());
    l.setDirection(new Vector3f(-1, -1, -1));
    rootNode.addLight(l);
    al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(0.02f));
    rootNode.addLight(al);
    Spatial sky = SkyFactory.createSky(assetManager, "Scenes/Beach/FullskiesSunset0068.dds", false);
    sky.setLocalScale(350);
    rootNode.attachChild(sky);
}
Also used : Geometry(com.jme3.scene.Geometry) Sphere(com.jme3.scene.shape.Sphere) Spatial(com.jme3.scene.Spatial) Vector2f(com.jme3.math.Vector2f) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Box(com.jme3.scene.shape.Box) Material(com.jme3.material.Material) Texture(com.jme3.texture.Texture) AmbientLight(com.jme3.light.AmbientLight)

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