Search in sources :

Example 26 with Vector2f

use of com.jme3.math.Vector2f in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method calculateTangent.

/**
     * 
     * @param v Takes 3 vertices: root, right, bottom
     * @param t Takes 3 tex coords: root, right, bottom
     * @param tangent that will store the result
     * @return the tangent store
     */
public static Vector3f calculateTangent(Vector3f[] v, Vector2f[] t, Vector3f tangent, Vector3f binormal) {
    // y=0
    Vector3f edge1 = new Vector3f();
    // x=0
    Vector3f edge2 = new Vector3f();
    // y=0
    Vector2f edge1uv = new Vector2f();
    // x=0
    Vector2f edge2uv = new Vector2f();
    t[2].subtract(t[0], edge2uv);
    t[1].subtract(t[0], edge1uv);
    // - edge1uv.y*edge2uv.x;  = 0
    float det = edge1uv.x * edge2uv.y;
    boolean normalize = true;
    if (Math.abs(det) < 0.0000001f) {
        det = 1;
        normalize = true;
    }
    v[1].subtract(v[0], edge1);
    v[2].subtract(v[0], edge2);
    tangent.set(edge1);
    tangent.normalizeLocal();
    binormal.set(edge2);
    binormal.normalizeLocal();
    float factor = 1 / det;
    tangent.x = (edge2uv.y * edge1.x) * factor;
    tangent.y = 0;
    tangent.z = (edge2uv.y * edge1.z) * factor;
    if (normalize) {
        tangent.normalizeLocal();
    }
    binormal.x = 0;
    binormal.y = (edge1uv.x * edge2.y) * factor;
    binormal.z = (edge1uv.x * edge2.z) * factor;
    if (normalize) {
        binormal.normalizeLocal();
    }
    return tangent;
}
Also used : Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f)

Example 27 with Vector2f

use of com.jme3.math.Vector2f in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method writeTangentArray.

public FloatBuffer[] writeTangentArray(FloatBuffer normalBuffer, FloatBuffer tangentStore, FloatBuffer binormalStore, FloatBuffer textureBuffer, Vector3f scale) {
    if (!isLoaded()) {
        throw new NullPointerException();
    }
    if (tangentStore != null) {
        if (tangentStore.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        tangentStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    tangentStore.rewind();
    if (binormalStore != null) {
        if (binormalStore.remaining() < getWidth() * getHeight() * 3) {
            throw new BufferUnderflowException();
        }
    } else {
        binormalStore = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    }
    binormalStore.rewind();
    Vector3f normal = new Vector3f();
    Vector3f tangent = new Vector3f();
    Vector3f binormal = new Vector3f();
    for (int r = 0; r < getHeight(); r++) {
        for (int c = 0; c < getWidth(); c++) {
            int idx = (r * getWidth() + c) * 3;
            normal.set(normalBuffer.get(idx), normalBuffer.get(idx + 1), normalBuffer.get(idx + 2));
            tangent.set(normal.cross(new Vector3f(0, 0, 1)));
            binormal.set(new Vector3f(1, 0, 0).cross(normal));
            // save the tangent
            BufferUtils.setInBuffer(tangent.normalizeLocal(), tangentStore, (r * getWidth() + c));
            // save the binormal
            BufferUtils.setInBuffer(binormal.normalizeLocal(), binormalStore, (r * getWidth() + c));
        }
    }
    /*        for (int r = 0; r < getHeight(); r++) {
            for (int c = 0; c < getWidth(); c++) {

                int texIdx = ((getHeight() - 1 - r) * getWidth() + c) * 2; // pull from the end
                int texIdxAbove = ((getHeight() - 1 - (r - 1)) * getWidth() + c) * 2; // pull from the end
                int texIdxNext = ((getHeight() - 1 - (r + 1)) * getWidth() + c) * 2; // pull from the end

                v1.set(c, getValue(c, r), r);
                t1.set(textureBuffer.get(texIdx), textureBuffer.get(texIdx + 1));

                // below
                if (r == getHeight()-1) { // last row
                    v3.set(c, getValue(c, r), r + 1);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdxAbove);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdxAbove + 1);
                    v += textureBuffer.get(texIdx + 1);
                    t3.set(u, v);
                } else {
                    v3.set(c, getValue(c, r + 1), r + 1);
                    t3.set(textureBuffer.get(texIdxNext), textureBuffer.get(texIdxNext + 1));
                }
                
                //right
                if (c == getWidth()-1) { // last column
                    v2.set(c + 1, getValue(c, r), r);
                    float u = textureBuffer.get(texIdx) - textureBuffer.get(texIdx - 2);
                    u += textureBuffer.get(texIdx);
                    float v = textureBuffer.get(texIdx + 1) - textureBuffer.get(texIdx - 1);
                    v += textureBuffer.get(texIdx - 1);
                    t2.set(u, v);
                } else {
                    v2.set(c + 1, getValue(c + 1, r), r); // one to the right
                    t2.set(textureBuffer.get(texIdx + 2), textureBuffer.get(texIdx + 3));
                }

                calculateTangent(new Vector3f[]{v1.mult(scale), v2.mult(scale), v3.mult(scale)}, new Vector2f[]{t1, t2, t3}, tangent, binormal);
                BufferUtils.setInBuffer(tangent, tangentStore, (r * getWidth() + c)); // save the tangent
                BufferUtils.setInBuffer(binormal, binormalStore, (r * getWidth() + c)); // save the binormal
            }
        }
        */
    return new FloatBuffer[] { tangentStore, binormalStore };
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 28 with Vector2f

use of com.jme3.math.Vector2f in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method createMesh.

public Mesh createMesh(Vector3f scale, Vector2f tcScale, Vector2f tcOffset, float offsetAmount, int totalSize, boolean center, int lod, boolean rightLod, boolean topLod, boolean leftLod, boolean bottomLod) {
    FloatBuffer pb = writeVertexArray(null, scale, center);
    FloatBuffer texb = writeTexCoordArray(null, tcOffset, tcScale, offsetAmount, totalSize);
    FloatBuffer nb = writeNormalArray(null, scale);
    Buffer ib;
    IndexBuffer idxB = writeIndexArrayLodDiff(lod, rightLod, topLod, leftLod, bottomLod, totalSize);
    if (idxB.getBuffer() instanceof IntBuffer)
        ib = (IntBuffer) idxB.getBuffer();
    else
        ib = (ShortBuffer) idxB.getBuffer();
    FloatBuffer bb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    FloatBuffer tanb = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 3);
    writeTangentArray(nb, tanb, bb, texb, scale);
    Mesh m = new Mesh();
    m.setMode(Mode.TriangleStrip);
    m.setBuffer(Type.Position, 3, pb);
    m.setBuffer(Type.Normal, 3, nb);
    m.setBuffer(Type.Tangent, 3, tanb);
    m.setBuffer(Type.Binormal, 3, bb);
    m.setBuffer(Type.TexCoord, 2, texb);
    if (ib instanceof IntBuffer)
        m.setBuffer(Type.Index, 3, (IntBuffer) ib);
    else if (ib instanceof ShortBuffer)
        m.setBuffer(Type.Index, 3, (ShortBuffer) ib);
    m.setStatic();
    m.updateBound();
    return m;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) IntBuffer(java.nio.IntBuffer) Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 29 with Vector2f

use of com.jme3.math.Vector2f in project jmonkeyengine by jMonkeyEngine.

the class LODGeomap method getTriangleAtPoint.

/**
     * Get the triangle that the point is on.
     * 
     * @param x coordinate in local space to the geomap
     * @param z coordinate in local space to the geomap
     * @return triangle in local space to the geomap
     */
protected Triangle getTriangleAtPoint(float x, float z) {
    Triangle[] triangles = getGridTrianglesAtPoint(x, z);
    if (triangles == null) {
        //System.out.println("x,z: " + x + "," + z);
        return null;
    }
    Vector2f point = new Vector2f(x, z);
    Vector2f t1 = new Vector2f(triangles[0].get1().x, triangles[0].get1().z);
    Vector2f t2 = new Vector2f(triangles[0].get2().x, triangles[0].get2().z);
    Vector2f t3 = new Vector2f(triangles[0].get3().x, triangles[0].get3().z);
    if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
        return triangles[0];
    }
    t1.set(triangles[1].get1().x, triangles[1].get1().z);
    t1.set(triangles[1].get2().x, triangles[1].get2().z);
    t1.set(triangles[1].get3().x, triangles[1].get3().z);
    if (0 != FastMath.pointInsideTriangle(t1, t2, t3, point)) {
        return triangles[1];
    }
    return null;
}
Also used : Vector2f(com.jme3.math.Vector2f) Triangle(com.jme3.math.Triangle)

Example 30 with Vector2f

use of com.jme3.math.Vector2f in project jmonkeyengine by jMonkeyEngine.

the class GeoMap method writeTexCoordArray.

public FloatBuffer writeTexCoordArray(FloatBuffer store, Vector2f offset, Vector2f scale) {
    if (store != null) {
        if (store.remaining() < getWidth() * getHeight() * 2)
            throw new BufferUnderflowException();
    } else {
        store = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 2);
    }
    if (offset == null)
        offset = new Vector2f();
    Vector2f tcStore = new Vector2f();
    for (int y = 0; y < getHeight(); y++) {
        for (int x = 0; x < getWidth(); x++) {
            getUV(x, y, tcStore);
            store.put(offset.x + tcStore.x * scale.x);
            store.put(offset.y + tcStore.y * scale.y);
        }
    }
    return store;
}
Also used : Vector2f(com.jme3.math.Vector2f) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

Vector2f (com.jme3.math.Vector2f)80 Vector3f (com.jme3.math.Vector3f)38 Geometry (com.jme3.scene.Geometry)22 Material (com.jme3.material.Material)20 Box (com.jme3.scene.shape.Box)17 ArrayList (java.util.ArrayList)14 Texture (com.jme3.texture.Texture)9 List (java.util.List)9 Node (com.jme3.scene.Node)8 Sphere (com.jme3.scene.shape.Sphere)8 FloatBuffer (java.nio.FloatBuffer)8 InputCapsule (com.jme3.export.InputCapsule)7 Spatial (com.jme3.scene.Spatial)7 DirectionalLight (com.jme3.light.DirectionalLight)6 Mesh (com.jme3.scene.Mesh)6 AmbientLight (com.jme3.light.AmbientLight)5 Quad (com.jme3.scene.shape.Quad)5 BoundingBox (com.jme3.bounding.BoundingBox)4 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)4 CollisionResult (com.jme3.collision.CollisionResult)4