Search in sources :

Example 36 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method generatePrefilteredEnvMap.

/**
     * Generates the prefiltered env map (used for image based specular
     * lighting) With the GGX/Shlick brdf
     * {@link EnvMapUtils#getSphericalHarmonicsCoefficents(com.jme3.texture.TextureCubeMap)}
     * Note that the output cube map is in RGBA8 format.
     *
     * @param sourceEnvMap
     * @param targetMapSize the size of the irradiance map to generate
     * @param store
     * @param fixSeamsMethod the method to fix seams
     * @return The irradiance cube map for the given coefficients
     */
public static TextureCubeMap generatePrefilteredEnvMap(TextureCubeMap sourceEnvMap, int targetMapSize, FixSeamsMethod fixSeamsMethod, TextureCubeMap store) {
    TextureCubeMap pem = store;
    if (pem == null) {
        pem = new TextureCubeMap(targetMapSize, targetMapSize, Image.Format.RGB16F);
        pem.setMagFilter(Texture.MagFilter.Bilinear);
        pem.setMinFilter(Texture.MinFilter.Trilinear);
        pem.getImage().setColorSpace(ColorSpace.Linear);
    }
    int nbMipMap = (int) (Math.log(targetMapSize) / Math.log(2) - 1);
    CubeMapWrapper sourceWrapper = new CubeMapWrapper(sourceEnvMap);
    CubeMapWrapper targetWrapper = new CubeMapWrapper(pem);
    targetWrapper.initMipMaps(nbMipMap);
    Vector3f texelVect = new Vector3f();
    Vector3f color = new Vector3f();
    ColorRGBA outColor = new ColorRGBA();
    for (int mipLevel = 0; mipLevel < nbMipMap; mipLevel++) {
        System.err.println("mip level " + mipLevel);
        float roughness = getRoughnessFromMip(mipLevel, nbMipMap);
        int nbSamples = getSampleFromMip(mipLevel, nbMipMap);
        int targetMipMapSize = (int) pow(2, nbMipMap + 1 - mipLevel);
        for (int face = 0; face < 6; face++) {
            System.err.println("face " + face);
            for (int y = 0; y < targetMipMapSize; y++) {
                for (int x = 0; x < targetMipMapSize; x++) {
                    color.set(0, 0, 0);
                    getVectorFromCubemapFaceTexCoord(x, y, targetMipMapSize, face, texelVect, FixSeamsMethod.Wrap);
                    prefilterEnvMapTexel(sourceWrapper, roughness, texelVect, nbSamples, color);
                    outColor.set(color.x, color.y, color.z, 1.0f);
                    // System.err.println("coords " + x + "," + y);
                    targetWrapper.setPixel(x, y, face, mipLevel, outColor);
                }
            }
        }
    }
    return pem;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) Vector3f(com.jme3.math.Vector3f)

Example 37 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method makeCubeMap.

/**
     * Creates a cube map from 6 images
     *
     * @param leftImg the west side image, also called negative x (negX) or left
     * image
     * @param rightImg the east side image, also called positive x (posX) or
     * right image
     * @param downImg the bottom side image, also called negative y (negY) or
     * down image
     * @param upImg the up side image, also called positive y (posY) or up image
     * @param backImg the south side image, also called positive z (posZ) or
     * back image
     * @param frontImg the north side image, also called negative z (negZ) or
     * front image
     * @param format the format of the image
     * @return a cube map
     */
public static TextureCubeMap makeCubeMap(Image rightImg, Image leftImg, Image upImg, Image downImg, Image backImg, Image frontImg, Image.Format format) {
    Image cubeImage = new Image(format, leftImg.getWidth(), leftImg.getHeight(), null, ColorSpace.Linear);
    cubeImage.addData(rightImg.getData(0));
    cubeImage.addData(leftImg.getData(0));
    cubeImage.addData(upImg.getData(0));
    cubeImage.addData(downImg.getData(0));
    cubeImage.addData(backImg.getData(0));
    cubeImage.addData(frontImg.getData(0));
    if (leftImg.getEfficentData() != null) {
        // also consilidate efficient data
        ArrayList<Object> efficientData = new ArrayList<Object>(6);
        efficientData.add(rightImg.getEfficentData());
        efficientData.add(leftImg.getEfficentData());
        efficientData.add(upImg.getEfficentData());
        efficientData.add(downImg.getEfficentData());
        efficientData.add(backImg.getEfficentData());
        efficientData.add(frontImg.getEfficentData());
        cubeImage.setEfficentData(efficientData);
    }
    TextureCubeMap cubeMap = new TextureCubeMap(cubeImage);
    cubeMap.setAnisotropicFilter(0);
    cubeMap.setMagFilter(Texture.MagFilter.Bilinear);
    cubeMap.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
    cubeMap.setWrap(Texture.WrapMode.EdgeClamp);
    return cubeMap;
}
Also used : TextureCubeMap(com.jme3.texture.TextureCubeMap) ArrayList(java.util.ArrayList) Image(com.jme3.texture.Image)

Example 38 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class PrefilteredEnvMapFaceGenerator method generatePrefilteredEnvMap.

/**
     * Generates the prefiltered env map (used for image based specular
     * lighting) With the GGX/Shlick brdf
     * {@link EnvMapUtils#getSphericalHarmonicsCoefficents(com.jme3.texture.TextureCubeMap)}
     * Note that the output cube map is in RGBA8 format.
     *
     * @param sourceEnvMap
     * @param targetMapSize the size of the irradiance map to generate
     * @param store
     * @param fixSeamsMethod the method to fix seams
     * @return The irradiance cube map for the given coefficients
     */
private TextureCubeMap generatePrefilteredEnvMap(TextureCubeMap sourceEnvMap, int targetMapSize, EnvMapUtils.FixSeamsMethod fixSeamsMethod, TextureCubeMap store) {
    TextureCubeMap pem = store;
    int nbMipMap = (int) (Math.log(targetMapSize) / Math.log(2) - 1);
    setEnd(nbMipMap);
    CubeMapWrapper sourceWrapper = new CubeMapWrapper(sourceEnvMap);
    CubeMapWrapper targetWrapper = new CubeMapWrapper(pem);
    Vector3f texelVect = new Vector3f();
    Vector3f color = new Vector3f();
    ColorRGBA outColor = new ColorRGBA();
    for (int mipLevel = 0; mipLevel < nbMipMap; mipLevel++) {
        float roughness = getRoughnessFromMip(mipLevel, nbMipMap);
        int nbSamples = getSampleFromMip(mipLevel, nbMipMap);
        int targetMipMapSize = (int) pow(2, nbMipMap + 1 - mipLevel);
        for (int y = 0; y < targetMipMapSize; y++) {
            for (int x = 0; x < targetMipMapSize; x++) {
                color.set(0, 0, 0);
                getVectorFromCubemapFaceTexCoord(x, y, targetMipMapSize, face, texelVect, EnvMapUtils.FixSeamsMethod.Wrap);
                prefilterEnvMapTexel(sourceWrapper, roughness, texelVect, nbSamples, color);
                outColor.set(Math.max(color.x, 0.0001f), Math.max(color.y, 0.0001f), Math.max(color.z, 0.0001f), 1);
                log.log(Level.FINE, "coords {0},{1}", new Object[] { x, y });
                targetWrapper.setPixel(x, y, face, mipLevel, outColor);
            }
        }
        progress();
    }
    return pem;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) TextureCubeMap(com.jme3.texture.TextureCubeMap) Vector3f(com.jme3.math.Vector3f) CubeMapWrapper(com.jme3.environment.util.CubeMapWrapper) EnvMapUtils.getHammersleyPoint(com.jme3.environment.util.EnvMapUtils.getHammersleyPoint)

Example 39 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class Image method write.

public void write(JmeExporter e) throws IOException {
    OutputCapsule capsule = e.getCapsule(this);
    capsule.write(format, "format", Format.RGBA8);
    capsule.write(width, "width", 0);
    capsule.write(height, "height", 0);
    capsule.write(depth, "depth", 0);
    capsule.write(mipMapSizes, "mipMapSizes", null);
    capsule.write(multiSamples, "multiSamples", 1);
    capsule.writeByteBufferArrayList(data, "data", null);
    capsule.write(colorSpace, "colorSpace", null);
}
Also used : OutputCapsule(com.jme3.export.OutputCapsule)

Example 40 with Format

use of com.jme3.scene.VertexBuffer.Format in project jmonkeyengine by jMonkeyEngine.

the class HDRLoader method load.

public Image load(InputStream in, boolean flipY) throws IOException {
    float gamma = -1f;
    float exposure = -1f;
    float[] colorcorr = new float[] { -1f, -1f, -1f };
    int width = -1, height = -1;
    boolean verifiedFormat = false;
    while (true) {
        String ln = readString(in);
        ln = ln.trim();
        if (ln.startsWith("#") || ln.equals("")) {
            if (ln.equals("#?RADIANCE") || ln.equals("#?RGBE"))
                verifiedFormat = true;
            // comment or empty statement
            continue;
        } else if (ln.startsWith("+") || ln.startsWith("-")) {
            // + or - mark image resolution and start of data
            String[] resData = ln.split("\\s");
            if (resData.length != 4) {
                throw new IOException("Invalid resolution string in HDR file");
            }
            if (!resData[0].equals("-Y") || !resData[2].equals("+X")) {
                logger.warning("Flipping/Rotating attributes ignored!");
            }
            //if (resData[0].endsWith("X")){
            // first width then height
            //    width = Integer.parseInt(resData[1]);
            //    height = Integer.parseInt(resData[3]);
            //}else{
            width = Integer.parseInt(resData[3]);
            height = Integer.parseInt(resData[1]);
            break;
        } else {
            // regular command
            int index = ln.indexOf("=");
            if (index < 1) {
                logger.log(Level.FINE, "Ignored string: {0}", ln);
                continue;
            }
            String var = ln.substring(0, index).trim().toLowerCase();
            String value = ln.substring(index + 1).trim().toLowerCase();
            if (var.equals("format")) {
                if (!value.equals("32-bit_rle_rgbe") && !value.equals("32-bit_rle_xyze")) {
                    throw new IOException("Unsupported format in HDR picture");
                }
            } else if (var.equals("exposure")) {
                exposure = Float.parseFloat(value);
            } else if (var.equals("gamma")) {
                gamma = Float.parseFloat(value);
            } else {
                logger.log(Level.WARNING, "HDR Command ignored: {0}", ln);
            }
        }
    }
    assert width != -1 && height != -1;
    if (!verifiedFormat)
        logger.warning("Unsure if specified image is Radiance HDR");
    // some HDR images can get pretty big
    System.gc();
    // each pixel times size of component times # of components
    Format pixelFormat;
    if (writeRGBE) {
        pixelFormat = Format.RGBA8;
    } else {
        pixelFormat = Format.RGB16F;
    }
    dataStore = BufferUtils.createByteBuffer(width * height * pixelFormat.getBitsPerPixel());
    int bytesPerPixel = pixelFormat.getBitsPerPixel() / 8;
    int scanLineBytes = bytesPerPixel * width;
    for (int y = height - 1; y >= 0; y--) {
        if (flipY)
            dataStore.position(scanLineBytes * y);
        decodeScanline(in, width);
    }
    in.close();
    dataStore.rewind();
    //HDR files color data is actually stored in linear space.
    return new Image(pixelFormat, width, height, dataStore, ColorSpace.Linear);
}
Also used : Format(com.jme3.texture.Image.Format) IOException(java.io.IOException) Image(com.jme3.texture.Image)

Aggregations

Image (com.jme3.texture.Image)20 ByteBuffer (java.nio.ByteBuffer)19 Format (com.jme3.texture.Image.Format)13 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 ColorRGBA (com.jme3.math.ColorRGBA)7 BufferedImage (java.awt.image.BufferedImage)7 TextureCubeMap (com.jme3.texture.TextureCubeMap)6 Vector3f (com.jme3.math.Vector3f)4 TexturePixel (com.jme3.scene.plugins.blender.textures.TexturePixel)4 PixelInputOutput (com.jme3.scene.plugins.blender.textures.io.PixelInputOutput)4 Texture2D (com.jme3.texture.Texture2D)4 DataInputStream (java.io.DataInputStream)4 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)3 LittleEndien (com.jme3.util.LittleEndien)3 InputStream (java.io.InputStream)3 TextureKey (com.jme3.asset.TextureKey)2 VRAPI (com.jme3.input.vr.VRAPI)2 MatParam (com.jme3.material.MatParam)2 Material (com.jme3.material.Material)2