Search in sources :

Example 11 with Type

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

the class MaterialHelper method getMaterials.

/**
     * This method returns the table of materials connected to the specified structure. The given structure can be of any type (ie. mesh or
     * curve) but needs to have 'mat' field/
     * 
     * @param structureWithMaterials
     *            the structure containing the mesh data
     * @param blenderContext
     *            the blender context
     * @return a list of vertices colors, each color belongs to a single vertex
     * @throws BlenderFileException
     *             this exception is thrown when the blend file structure is somehow invalid or corrupted
     */
public MaterialContext[] getMaterials(Structure structureWithMaterials, BlenderContext blenderContext) throws BlenderFileException {
    Pointer ppMaterials = (Pointer) structureWithMaterials.getFieldValue("mat");
    MaterialContext[] materials = null;
    if (ppMaterials.isNotNull()) {
        List<Structure> materialStructures = ppMaterials.fetchData();
        if (materialStructures != null && materialStructures.size() > 0) {
            MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
            materials = new MaterialContext[materialStructures.size()];
            int i = 0;
            for (Structure s : materialStructures) {
                materials[i++] = s == null ? null : materialHelper.toMaterialContext(s, blenderContext);
            }
        }
    }
    return materials;
}
Also used : Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure)

Example 12 with Type

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

the class UVCoordinatesGenerator method getBoundingSphere.

/**
     * This method returns the bounding sphere of the given geometries.
     * 
     * @param geometries
     *            the list of geometries
     * @return bounding sphere of the given geometries
     */
/* package */
static BoundingSphere getBoundingSphere(Geometry... geometries) {
    BoundingSphere result = null;
    for (Geometry geometry : geometries) {
        geometry.updateModelBound();
        BoundingVolume bv = geometry.getModelBound();
        if (bv instanceof BoundingBox) {
            BoundingBox bb = (BoundingBox) bv;
            float r = Math.max(bb.getXExtent(), bb.getYExtent());
            r = Math.max(r, bb.getZExtent());
            return new BoundingSphere(r, bb.getCenter());
        } else if (bv instanceof BoundingSphere) {
            return (BoundingSphere) bv;
        } else {
            throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName());
        }
    }
    return result;
}
Also used : Geometry(com.jme3.scene.Geometry) BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) BoundingVolume(com.jme3.bounding.BoundingVolume)

Example 13 with Type

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

the class UVCoordinatesGenerator method getBoundingBox.

/**
     * This method returns the bounding box of the given geometries.
     * 
     * @param geometries
     *            the list of geometries
     * @return bounding box of the given geometries
     */
public static BoundingBox getBoundingBox(Geometry... geometries) {
    BoundingBox result = null;
    for (Geometry geometry : geometries) {
        geometry.updateModelBound();
        BoundingVolume bv = geometry.getModelBound();
        if (bv instanceof BoundingBox) {
            return (BoundingBox) bv;
        } else if (bv instanceof BoundingSphere) {
            BoundingSphere bs = (BoundingSphere) bv;
            float r = bs.getRadius();
            return new BoundingBox(bs.getCenter(), r, r, r);
        } else {
            throw new IllegalStateException("Unknown bounding volume type: " + bv.getClass().getName());
        }
    }
    return result;
}
Also used : Geometry(com.jme3.scene.Geometry) BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) BoundingVolume(com.jme3.bounding.BoundingVolume)

Example 14 with Type

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

the class AbstractTextureBlender method blendHSV.

/**
     * The method that performs the ramp blending.
     * 
     * @param type
     *            the blend type
     * @param materialRGB
     *            the rgb value of the material, here the result is stored too
     * @param fac
     *            color affection factor
     * @param pixelColor
     *            the texture color
     * @param blenderContext
     *            the blender context
     */
protected void blendHSV(int type, float[] materialRGB, float fac, float[] pixelColor, BlenderContext blenderContext) {
    float oneMinusFactor = 1.0f - fac;
    MaterialHelper materialHelper = blenderContext.getHelper(MaterialHelper.class);
    switch(type) {
        case MTEX_BLEND_HUE:
            {
                // FIXME: not working well for image textures (works fine for generated textures)
                float[] colorTransformResult = new float[3];
                materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colorTransformResult);
                if (colorTransformResult[0] != 0.0f) {
                    float colH = colorTransformResult[0];
                    materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], colorTransformResult);
                    materialHelper.hsvToRgb(colH, colorTransformResult[1], colorTransformResult[2], colorTransformResult);
                    materialRGB[0] = oneMinusFactor * materialRGB[0] + fac * colorTransformResult[0];
                    materialRGB[1] = oneMinusFactor * materialRGB[1] + fac * colorTransformResult[1];
                    materialRGB[2] = oneMinusFactor * materialRGB[2] + fac * colorTransformResult[2];
                }
                break;
            }
        case MTEX_BLEND_SAT:
            {
                float[] colorTransformResult = new float[3];
                materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], colorTransformResult);
                float h = colorTransformResult[0];
                float s = colorTransformResult[1];
                float v = colorTransformResult[2];
                if (s != 0.0f) {
                    materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colorTransformResult);
                    materialHelper.hsvToRgb(h, oneMinusFactor * s + fac * colorTransformResult[1], v, materialRGB);
                }
                break;
            }
        case MTEX_BLEND_VAL:
            {
                float[] rgbToHsv = new float[3];
                float[] colToHsv = new float[3];
                materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], rgbToHsv);
                materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colToHsv);
                materialHelper.hsvToRgb(rgbToHsv[0], rgbToHsv[1], oneMinusFactor * rgbToHsv[2] + fac * colToHsv[2], materialRGB);
                break;
            }
        case MTEX_BLEND_COLOR:
            {
                // FIXME: not working well for image textures (works fine for generated textures)
                float[] rgbToHsv = new float[3];
                float[] colToHsv = new float[3];
                materialHelper.rgbToHsv(pixelColor[0], pixelColor[1], pixelColor[2], colToHsv);
                if (colToHsv[2] != 0) {
                    materialHelper.rgbToHsv(materialRGB[0], materialRGB[1], materialRGB[2], rgbToHsv);
                    materialHelper.hsvToRgb(colToHsv[0], colToHsv[1], rgbToHsv[2], rgbToHsv);
                    materialRGB[0] = oneMinusFactor * materialRGB[0] + fac * rgbToHsv[0];
                    materialRGB[1] = oneMinusFactor * materialRGB[1] + fac * rgbToHsv[1];
                    materialRGB[2] = oneMinusFactor * materialRGB[2] + fac * rgbToHsv[2];
                }
                break;
            }
        default:
            throw new IllegalStateException("Unknown ramp type: " + type);
    }
}
Also used : MaterialHelper(com.jme3.scene.plugins.blender.materials.MaterialHelper)

Example 15 with Type

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

the class HelloOpenCL method testImages.

private boolean testImages(Context clContext, CommandQueue clQueue) {
    try {
        //query supported formats
        for (MemoryAccess ma : MemoryAccess.values()) {
            for (Image.ImageType type : Image.ImageType.values()) {
                try {
                    System.out.println("Formats for " + ma + " and " + type + ": " + Arrays.toString(clContext.querySupportedFormats(ma, type)));
                } catch (UnsupportedOperationException e) {
                    LOG.warning(e.getLocalizedMessage());
                }
            }
        }
        //create an image
        Image.ImageFormat format = new Image.ImageFormat(Image.ImageChannelOrder.RGBA, Image.ImageChannelType.FLOAT);
        Image.ImageDescriptor descr = new Image.ImageDescriptor(Image.ImageType.IMAGE_2D, 1920, 1080, 0, 0);
        Image image = clContext.createImage(MemoryAccess.READ_WRITE, format, descr);
        System.out.println("image created");
        //check queries
        assertEquals(descr.type, image.getImageType(), "Wrong image type");
        assertEquals(format, image.getImageFormat(), "Wrong image format");
        assertEquals(descr.width, image.getWidth(), "Wrong width");
        assertEquals(descr.height, image.getHeight(), "Wrong height");
        //fill with red and blue
        ColorRGBA color1 = ColorRGBA.Red;
        ColorRGBA color2 = ColorRGBA.Blue;
        Event e1 = image.fillAsync(clQueue, new long[] { 0, 0, 0 }, new long[] { descr.width / 2, descr.height, 1 }, color1);
        Event e2 = image.fillAsync(clQueue, new long[] { descr.width / 2, 0, 0 }, new long[] { descr.width / 2, descr.height, 1 }, color2);
        e1.waitForFinished();
        e2.waitForFinished();
        //copy to a buffer
        Buffer buffer = clContext.createBuffer(4 * 4 * 500 * 1024);
        Event e3 = image.copyToBufferAsync(clQueue, buffer, new long[] { 10, 10, 0 }, new long[] { 500, 1024, 1 }, 0);
        e3.release();
        //this buffer must be completely red
        ByteBuffer map1 = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        FloatBuffer map1F = map1.asFloatBuffer();
        map1F.rewind();
        for (int x = 0; x < 500; ++x) {
            for (int y = 0; y < 1024; ++y) {
                float r = map1F.get();
                float g = map1F.get();
                float b = map1F.get();
                float a = map1F.get();
                assertEquals(1, r, "Wrong red component");
                assertEquals(0, g, "Wrong green component");
                assertEquals(0, b, "Wrong blue component");
                assertEquals(1, a, "Wrong alpha component");
            }
        }
        buffer.unmap(clQueue, map1);
        //create a second image
        format = new Image.ImageFormat(Image.ImageChannelOrder.RGBA, Image.ImageChannelType.FLOAT);
        descr = new Image.ImageDescriptor(Image.ImageType.IMAGE_2D, 512, 512, 0, 0);
        Image image2 = clContext.createImage(MemoryAccess.READ_WRITE, format, descr);
        //copy an area of image1 to image2
        image.copyTo(clQueue, image2, new long[] { 1000, 20, 0 }, new long[] { 0, 0, 0 }, new long[] { 512, 512, 1 });
        //this area should be completely blue
        Image.ImageMapping map2 = image2.map(clQueue, new long[] { 0, 0, 0 }, new long[] { 512, 512, 1 }, MappingAccess.MAP_READ_WRITE);
        FloatBuffer map2F = map2.buffer.asFloatBuffer();
        for (int y = 0; y < 512; ++y) {
            for (int x = 0; x < 512; ++x) {
                long index = 4 * x + y * (map2.rowPitch / 4);
                map2F.position((int) index);
                float r = map2F.get();
                float g = map2F.get();
                float b = map2F.get();
                float a = map2F.get();
                assertEquals(0, r, "Wrong red component");
                assertEquals(0, g, "Wrong green component");
                assertEquals(1, b, "Wrong blue component");
                assertEquals(1, a, "Wrong alpha component");
            }
        }
        image2.unmap(clQueue, map2);
        //release
        image.release();
        image2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "image test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "image test failed with:", ex);
        return false;
    }
    return true;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ColorRGBA(com.jme3.math.ColorRGBA)

Aggregations

ArrayList (java.util.ArrayList)18 Structure (com.jme3.scene.plugins.blender.file.Structure)12 Vector3f (com.jme3.math.Vector3f)11 Pointer (com.jme3.scene.plugins.blender.file.Pointer)10 IOException (java.io.IOException)10 List (java.util.List)9 ColorRGBA (com.jme3.math.ColorRGBA)8 Texture (com.jme3.texture.Texture)8 Geometry (com.jme3.scene.Geometry)6 Image (com.jme3.texture.Image)6 BoundingBox (com.jme3.bounding.BoundingBox)5 BoundingSphere (com.jme3.bounding.BoundingSphere)5 Light (com.jme3.light.Light)5 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 Uniform (com.jme3.shader.Uniform)5 Material (com.jme3.material.Material)4 Quaternion (com.jme3.math.Quaternion)4 Transform (com.jme3.math.Transform)4 Mesh (com.jme3.scene.Mesh)4 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)4