Search in sources :

Example 1 with Vector4f

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

the class MultiPassLightingLogic method render.

@Override
public void render(RenderManager renderManager, Shader shader, Geometry geometry, LightList lights, int lastTexUnit) {
    Renderer r = renderManager.getRenderer();
    Uniform lightDir = shader.getUniform("g_LightDirection");
    Uniform lightColor = shader.getUniform("g_LightColor");
    Uniform lightPos = shader.getUniform("g_LightPosition");
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    boolean isFirstLight = true;
    boolean isSecondLight = false;
    getAmbientColor(lights, false, ambientLightColor);
    for (int i = 0; i < lights.size(); i++) {
        Light l = lights.get(i);
        if (l instanceof AmbientLight) {
            continue;
        }
        if (isFirstLight) {
            // set ambient color for first light only
            ambientColor.setValue(VarType.Vector4, ambientLightColor);
            isFirstLight = false;
            isSecondLight = true;
        } else if (isSecondLight) {
            ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
            // apply additive blending for 2nd and future lights
            r.applyRenderState(ADDITIVE_LIGHT);
            isSecondLight = false;
        }
        TempVars vars = TempVars.get();
        Quaternion tmpLightDirection = vars.quat1;
        Quaternion tmpLightPosition = vars.quat2;
        ColorRGBA tmpLightColor = vars.color;
        Vector4f tmpVec = vars.vect4f1;
        ColorRGBA color = l.getColor();
        tmpLightColor.set(color);
        tmpLightColor.a = l.getType().getId();
        lightColor.setValue(VarType.Vector4, tmpLightColor);
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //FIXME : there is an inconstency here due to backward
                //compatibility of the lighting shader.
                //The directional light direction is passed in the
                //LightPosition uniform. The lighting shader needs to be
                //reworked though in order to fix this.
                tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                //We transform the spot direction in view space here to save 5 varying later in the lighting shader
                //one vec4 less and a vec4 that becomes a vec3
                //the downside is that spotAngleCos decoding happens now in the frag shader.
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0);
                renderManager.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Probe:
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
        vars.release();
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
    if (isFirstLight) {
        // Either there are no lights at all, or only ambient lights.
        // Render a dummy "normal light" so we can see the ambient color.
        ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, false, ambientLightColor));
        lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha);
        lightPos.setValue(VarType.Vector4, NULL_DIR_LIGHT);
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Uniform(com.jme3.shader.Uniform) TempVars(com.jme3.util.TempVars) SpotLight(com.jme3.light.SpotLight) ColorRGBA(com.jme3.math.ColorRGBA) Vector4f(com.jme3.math.Vector4f) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Renderer(com.jme3.renderer.Renderer) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight)

Example 2 with Vector4f

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

the class IrUtils method toTangentsWithParity.

private static void toTangentsWithParity(IrVertex vertex) {
    if (vertex.tang != null && vertex.bitang != null) {
        float wCoord = vertex.norm.cross(vertex.tang).dot(vertex.bitang) < 0f ? -1f : 1f;
        vertex.tang4d = new Vector4f(vertex.tang.x, vertex.tang.y, vertex.tang.z, wCoord);
        vertex.tang = null;
        vertex.bitang = null;
    }
}
Also used : Vector4f(com.jme3.math.Vector4f)

Example 3 with Vector4f

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

the class CurvesTemporalMesh method loadNurbSurface.

/**
     * This method loads the NURBS curve or surface.
     * @param nurb
     *            the NURBS data structure
     * @throws BlenderFileException
     *             an exception is thrown when problems with reading occur
     */
@SuppressWarnings("unchecked")
private void loadNurbSurface(Structure nurb, int materialIndex) throws BlenderFileException {
    // loading the knots
    List<Float>[] knots = new List[2];
    Pointer[] pKnots = new Pointer[] { (Pointer) nurb.getFieldValue("knotsu"), (Pointer) nurb.getFieldValue("knotsv") };
    for (int i = 0; i < knots.length; ++i) {
        if (pKnots[i].isNotNull()) {
            FileBlockHeader fileBlockHeader = blenderContext.getFileBlock(pKnots[i].getOldMemoryAddress());
            BlenderInputStream blenderInputStream = blenderContext.getInputStream();
            blenderInputStream.setPosition(fileBlockHeader.getBlockPosition());
            int knotsAmount = fileBlockHeader.getCount() * fileBlockHeader.getSize() / 4;
            knots[i] = new ArrayList<Float>(knotsAmount);
            for (int j = 0; j < knotsAmount; ++j) {
                knots[i].add(Float.valueOf(blenderInputStream.readFloat()));
            }
        }
    }
    // loading the flags and orders (basis functions degrees)
    int flag = ((Number) nurb.getFieldValue("flag")).intValue();
    boolean smooth = (flag & FLAG_SMOOTH) != 0;
    int flagU = ((Number) nurb.getFieldValue("flagu")).intValue();
    int flagV = ((Number) nurb.getFieldValue("flagv")).intValue();
    int orderU = ((Number) nurb.getFieldValue("orderu")).intValue();
    int orderV = ((Number) nurb.getFieldValue("orderv")).intValue();
    // loading control points and their weights
    int pntsU = ((Number) nurb.getFieldValue("pntsu")).intValue();
    int pntsV = ((Number) nurb.getFieldValue("pntsv")).intValue();
    List<Structure> bPoints = ((Pointer) nurb.getFieldValue("bp")).fetchData();
    List<List<Vector4f>> controlPoints = new ArrayList<List<Vector4f>>(pntsV);
    for (int i = 0; i < pntsV; ++i) {
        List<Vector4f> uControlPoints = new ArrayList<Vector4f>(pntsU);
        for (int j = 0; j < pntsU; ++j) {
            DynamicArray<Float> vec = (DynamicArray<Float>) bPoints.get(j + i * pntsU).getFieldValue("vec");
            if (blenderContext.getBlenderKey().isFixUpAxis()) {
                uControlPoints.add(new Vector4f(vec.get(0).floatValue(), vec.get(2).floatValue(), -vec.get(1).floatValue(), vec.get(3).floatValue()));
            } else {
                uControlPoints.add(new Vector4f(vec.get(0).floatValue(), vec.get(1).floatValue(), vec.get(2).floatValue(), vec.get(3).floatValue()));
            }
        }
        if ((flagU & 0x01) != 0) {
            for (int k = 0; k < orderU - 1; ++k) {
                uControlPoints.add(uControlPoints.get(k));
            }
        }
        controlPoints.add(uControlPoints);
    }
    if ((flagV & 0x01) != 0) {
        for (int k = 0; k < orderV - 1; ++k) {
            controlPoints.add(controlPoints.get(k));
        }
    }
    int originalVerticesAmount = vertices.size();
    int resolu = ((Number) nurb.getFieldValue("resolu")).intValue();
    if (knots[1] == null) {
        // creating the NURB curve
        Curve curve = new Curve(new Spline(controlPoints.get(0), knots[0]), resolu);
        FloatBuffer vertsBuffer = (FloatBuffer) curve.getBuffer(Type.Position).getData();
        beziers.add(new BezierLine(BufferUtils.getVector3Array(vertsBuffer), materialIndex, smooth, false));
    } else {
        // creating the NURB surface
        int resolv = ((Number) nurb.getFieldValue("resolv")).intValue();
        int uSegments = resolu * controlPoints.get(0).size() - 1;
        int vSegments = resolv * controlPoints.size() - 1;
        Surface nurbSurface = Surface.createNurbsSurface(controlPoints, knots, uSegments, vSegments, orderU, orderV, smooth);
        FloatBuffer vertsBuffer = (FloatBuffer) nurbSurface.getBuffer(Type.Position).getData();
        vertices.addAll(Arrays.asList(BufferUtils.getVector3Array(vertsBuffer)));
        FloatBuffer normalsBuffer = (FloatBuffer) nurbSurface.getBuffer(Type.Normal).getData();
        normals.addAll(Arrays.asList(BufferUtils.getVector3Array(normalsBuffer)));
        IndexBuffer indexBuffer = nurbSurface.getIndexBuffer();
        for (int i = 0; i < indexBuffer.size(); i += 3) {
            int index1 = indexBuffer.get(i) + originalVerticesAmount;
            int index2 = indexBuffer.get(i + 1) + originalVerticesAmount;
            int index3 = indexBuffer.get(i + 2) + originalVerticesAmount;
            faces.add(new Face(new Integer[] { index1, index2, index3 }, smooth, materialIndex, null, null, this));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Pointer(com.jme3.scene.plugins.blender.file.Pointer) FloatBuffer(java.nio.FloatBuffer) Spline(com.jme3.math.Spline) Surface(com.jme3.scene.shape.Surface) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Vector4f(com.jme3.math.Vector4f) ArrayList(java.util.ArrayList) List(java.util.List) BlenderInputStream(com.jme3.scene.plugins.blender.file.BlenderInputStream) Structure(com.jme3.scene.plugins.blender.file.Structure) Face(com.jme3.scene.plugins.blender.meshes.Face) FileBlockHeader(com.jme3.scene.plugins.blender.file.FileBlockHeader) Curve(com.jme3.scene.shape.Curve) DynamicArray(com.jme3.scene.plugins.blender.file.DynamicArray)

Example 4 with Vector4f

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

the class EnvMapUtils method importanceSampleGGX.

public static Vector3f importanceSampleGGX(Vector4f xi, float a2, Vector3f normal, Vector3f store, TempVars vars) {
    if (store == null) {
        store = new Vector3f();
    }
    float cosTheta = sqrt((1f - xi.x) / (1f + (a2 - 1f) * xi.x));
    float sinTheta = sqrt(1f - cosTheta * cosTheta);
    //xi.z is cos(phi)
    float sinThetaCosPhi = sinTheta * xi.z;
    //xi.w is sin(phi)
    float sinThetaSinPhi = sinTheta * xi.w;
    Vector3f upVector = Vector3f.UNIT_X;
    if (abs(normal.z) < 0.999) {
        upVector = Vector3f.UNIT_Y;
    }
    Vector3f tangentX = vars.vect3.set(upVector).crossLocal(normal).normalizeLocal();
    Vector3f tangentY = vars.vect4.set(normal).crossLocal(tangentX);
    // Tangent to world space
    tangentX.multLocal(sinThetaCosPhi);
    tangentY.multLocal(sinThetaSinPhi);
    vars.vect5.set(normal).multLocal(cosTheta);
    // Tangent to world space
    store.set(tangentX).addLocal(tangentY).addLocal(vars.vect5);
    return store;
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 5 with Vector4f

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

the class EnvMapUtils method prefilterEnvMapTexel.

private static Vector3f prefilterEnvMapTexel(CubeMapWrapper envMapReader, float roughness, Vector3f N, int numSamples, Vector3f store) {
    Vector3f prefilteredColor = store;
    float totalWeight = 0.0f;
    TempVars vars = TempVars.get();
    Vector4f Xi = vars.vect4f1;
    Vector3f H = vars.vect1;
    Vector3f tmp = vars.vect2;
    ColorRGBA c = vars.color;
    // a = roughness² and a2 = a²
    float a2 = roughness * roughness;
    a2 *= a2;
    a2 *= 10;
    for (int i = 0; i < numSamples; i++) {
        Xi = getHammersleyPoint(i, numSamples, Xi);
        H = importanceSampleGGX(Xi, a2, N, H, vars);
        H.normalizeLocal();
        tmp.set(H);
        float NoH = N.dot(tmp);
        Vector3f L = tmp.multLocal(NoH * 2).subtractLocal(N);
        float NoL = clamp(N.dot(L), 0.0f, 1.0f);
        if (NoL > 0) {
            envMapReader.getPixel(L, c);
            prefilteredColor.setX(prefilteredColor.x + c.r * NoL);
            prefilteredColor.setY(prefilteredColor.y + c.g * NoL);
            prefilteredColor.setZ(prefilteredColor.z + c.b * NoL);
            totalWeight += NoL;
        }
    }
    vars.release();
    return prefilteredColor.divideLocal(totalWeight);
}
Also used : Vector4f(com.jme3.math.Vector4f) ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Aggregations

Vector4f (com.jme3.math.Vector4f)6 Vector3f (com.jme3.math.Vector3f)5 TempVars (com.jme3.util.TempVars)5 ColorRGBA (com.jme3.math.ColorRGBA)3 DirectionalLight (com.jme3.light.DirectionalLight)2 Light (com.jme3.light.Light)2 PointLight (com.jme3.light.PointLight)2 SpotLight (com.jme3.light.SpotLight)2 Uniform (com.jme3.shader.Uniform)2 TextureKey (com.jme3.asset.TextureKey)1 BoundingSphere (com.jme3.bounding.BoundingSphere)1 AmbientLight (com.jme3.light.AmbientLight)1 Quaternion (com.jme3.math.Quaternion)1 Spline (com.jme3.math.Spline)1 Renderer (com.jme3.renderer.Renderer)1 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)1 BlenderInputStream (com.jme3.scene.plugins.blender.file.BlenderInputStream)1 DynamicArray (com.jme3.scene.plugins.blender.file.DynamicArray)1 FileBlockHeader (com.jme3.scene.plugins.blender.file.FileBlockHeader)1 Pointer (com.jme3.scene.plugins.blender.file.Pointer)1