Search in sources :

Example 11 with Material

use of gaiasky.util.gdx.shader.Material in project gaiasky by langurmonkey.

the class VRContext method loadRenderModel.

private IntModel loadRenderModel(String name, String modelNumber, String manufacturer, VRControllerRole role) {
    if (models.containsKey(name))
        return models.get(name);
    // FIXME we load the models synchronously cause we are lazy
    IntModel model = null;
    if (false) {
        int error;
        PointerBuffer modelPointer = PointerBuffer.allocateDirect(1);
        while (true) {
            error = VRRenderModels.VRRenderModels_LoadRenderModel_Async(name, modelPointer);
            if (error != VR.EVRRenderModelError_VRRenderModelError_Loading)
                break;
        }
        if (error != VR.EVRRenderModelError_VRRenderModelError_None)
            return null;
        RenderModel renderModel = new RenderModel(modelPointer.getByteBuffer(RenderModel.SIZEOF));
        PointerBuffer texturePointer = PointerBuffer.allocateDirect(1);
        while (true) {
            error = VRRenderModels.VRRenderModels_LoadTexture_Async(renderModel.diffuseTextureId(), texturePointer);
            if (error != VR.EVRRenderModelError_VRRenderModelError_Loading)
                break;
        }
        if (error != VR.EVRRenderModelError_VRRenderModelError_None) {
            VRRenderModels.VRRenderModels_FreeRenderModel(renderModel);
            return null;
        }
        RenderModelTextureMap renderModelTexture = new RenderModelTextureMap(texturePointer.getByteBuffer(RenderModelTextureMap.SIZEOF));
        // convert to a Model
        IntMesh mesh = new IntMesh(true, renderModel.unVertexCount(), renderModel.unTriangleCount() * 3, new VertexAttribute[] { VertexAttribute.Position(), VertexAttribute.Normal(), VertexAttribute.TexCoords(0) });
        IntMeshPart meshPart = new IntMeshPart(name, mesh, 0, renderModel.unTriangleCount() * 3, GL20.GL_TRIANGLES);
        RenderModelVertex.Buffer vertices = renderModel.rVertexData();
        float[] packedVertices = new float[8 * renderModel.unVertexCount()];
        int i = 0;
        while (vertices.remaining() > 0) {
            RenderModelVertex v = vertices.get();
            packedVertices[i++] = v.vPosition().v(0);
            packedVertices[i++] = v.vPosition().v(1);
            packedVertices[i++] = v.vPosition().v(2);
            packedVertices[i++] = v.vNormal().v(0);
            packedVertices[i++] = v.vNormal().v(1);
            packedVertices[i++] = v.vNormal().v(2);
            packedVertices[i++] = v.rfTextureCoord().get(0);
            packedVertices[i++] = v.rfTextureCoord().get(1);
        }
        mesh.setVertices(packedVertices);
        // Shorts to Integers
        int[] indices = new int[renderModel.unTriangleCount() * 3];
        ShortBuffer sb = renderModel.IndexData();
        sb.rewind();
        int j = 0;
        while (sb.hasRemaining()) {
            short index = sb.get();
            indices[j++] = index;
        }
        mesh.setIndices(indices);
        Pixmap pixmap = new Pixmap(renderModelTexture.unWidth(), renderModelTexture.unHeight(), Format.RGBA8888);
        byte[] pixels = new byte[renderModelTexture.unWidth() * renderModelTexture.unHeight() * 4];
        renderModelTexture.rubTextureMapData(pixels.length).get(pixels);
        pixmap.getPixels().put(pixels);
        pixmap.getPixels().position(0);
        Texture texture = new Texture(new PixmapTextureData(pixmap, pixmap.getFormat(), true, true));
        Material material = new Material(new TextureAttribute(TextureAttribute.Diffuse, texture));
        model = new IntModel();
        model.meshes.add(mesh);
        model.meshParts.add(meshPart);
        model.materials.add(material);
        IntNode node = new IntNode();
        node.id = name;
        node.parts.add(new IntNodePart(meshPart, material));
        model.nodes.add(node);
        model.manageDisposable(mesh);
        model.manageDisposable(texture);
        VRRenderModels.VRRenderModels_FreeRenderModel(renderModel);
        VRRenderModels.VRRenderModels_FreeTexture(renderModelTexture);
    } else {
        OwnObjLoader ol = new OwnObjLoader();
        if (manufacturer == null || manufacturer.equalsIgnoreCase("Oculus")) {
            if (isControllerLeft(name, modelNumber, role)) {
                model = ol.loadModel(Settings.settings.data.dataFileHandle("models/controllers/oculus/oculus-left.obj"));
            } else if (isControllerRight(name, modelNumber, role)) {
                model = ol.loadModel(Settings.settings.data.dataFileHandle("models/controllers/oculus/oculus-right.obj"));
            } else {
                logger.info("WARN: Could not parse controller name - Manufacturer: " + manufacturer + ", Name: " + name + ", ModelNumber: " + modelNumber);
            }
        } else {
            // HTC
            if (isControllerRight(name, modelNumber, role) || isControllerLeft(name, modelNumber, role)) {
                model = ol.loadModel(Settings.settings.data.dataFileHandle("models/controllers/vive/vr_controller_vive.obj"));
            } else {
                logger.info("WARN: Could not parse controller name - Manufacturer: " + manufacturer + ", Name: " + name + ", ModelNumber: " + modelNumber);
            }
        }
        // Load default
        if (model == null) {
            logger.info("WARN: Could not find suitable controller model, using default...");
            model = ol.loadModel(Settings.settings.data.dataFileHandle("models/controllers/vive/vr_controller_vive.obj"));
        }
    }
    models.put(name, model);
    return model;
}
Also used : PointerBuffer(org.lwjgl.PointerBuffer) PixmapTextureData(com.badlogic.gdx.graphics.glutils.PixmapTextureData) Material(gaiasky.util.gdx.shader.Material) OwnObjLoader(gaiasky.util.gdx.loader.OwnObjLoader) Texture(com.badlogic.gdx.graphics.Texture) ShortBuffer(java.nio.ShortBuffer) Pixmap(com.badlogic.gdx.graphics.Pixmap) IntMesh(gaiasky.util.gdx.mesh.IntMesh) TextureAttribute(gaiasky.util.gdx.shader.attribute.TextureAttribute)

Example 12 with Material

use of gaiasky.util.gdx.shader.Material in project gaiasky by langurmonkey.

the class IntModel method getMaterial.

/**
 * @param id The ID of the material to fetch.
 * @param ignoreCase whether to use case sensitivity when comparing the material id.
 * @return The {@link Material} with the specified id, or null if not available.
 */
public Material getMaterial(final String id, boolean ignoreCase) {
    final int n = materials.size;
    Material material;
    if (ignoreCase) {
        for (int i = 0; i < n; i++) if ((material = materials.get(i)).id.equalsIgnoreCase(id))
            return material;
    } else {
        for (int i = 0; i < n; i++) if ((material = materials.get(i)).id.equals(id))
            return material;
    }
    return null;
}
Also used : Material(gaiasky.util.gdx.shader.Material)

Example 13 with Material

use of gaiasky.util.gdx.shader.Material in project gaiasky by langurmonkey.

the class IntModel method convertMaterial.

protected Material convertMaterial(ModelMaterial mtl, TextureProvider textureProvider) {
    Material result = new Material();
    result.id = mtl.id;
    if (mtl.ambient != null)
        result.set(new ColorAttribute(ColorAttribute.Ambient, mtl.ambient));
    if (mtl.diffuse != null)
        result.set(new ColorAttribute(ColorAttribute.Diffuse, mtl.diffuse));
    if (mtl.specular != null)
        result.set(new ColorAttribute(ColorAttribute.Specular, mtl.specular));
    if (mtl.emissive != null)
        result.set(new ColorAttribute(ColorAttribute.Emissive, mtl.emissive));
    if (mtl.reflection != null)
        result.set(new ColorAttribute(ColorAttribute.Reflection, mtl.reflection));
    if (mtl.shininess > 0f)
        result.set(new FloatAttribute(FloatAttribute.Shininess, mtl.shininess));
    if (mtl.opacity != 1.f)
        result.set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, mtl.opacity));
    ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
    // FIXME uvScaling/uvTranslation totally ignored
    if (mtl.textures != null) {
        for (ModelTexture tex : mtl.textures) {
            Texture texture;
            if (textures.containsKey(tex.fileName)) {
                texture = textures.get(tex.fileName);
            } else {
                texture = textureProvider.load(tex.fileName);
                textures.put(tex.fileName, texture);
                disposables.add(texture);
            }
            TextureDescriptor descriptor = new TextureDescriptor(texture);
            descriptor.minFilter = texture.getMinFilter();
            descriptor.magFilter = texture.getMagFilter();
            descriptor.uWrap = texture.getUWrap();
            descriptor.vWrap = texture.getVWrap();
            float offsetU = tex.uvTranslation == null ? 0f : tex.uvTranslation.x;
            float offsetV = tex.uvTranslation == null ? 0f : tex.uvTranslation.y;
            float scaleU = tex.uvScaling == null ? 1f : tex.uvScaling.x;
            float scaleV = tex.uvScaling == null ? 1f : tex.uvScaling.y;
            switch(tex.usage) {
                case ModelTexture.USAGE_DIFFUSE:
                    result.set(new TextureAttribute(TextureAttribute.Diffuse, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_SPECULAR:
                    result.set(new TextureAttribute(TextureAttribute.Specular, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_BUMP:
                    result.set(new TextureAttribute(TextureAttribute.Bump, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_NORMAL:
                    result.set(new TextureAttribute(TextureAttribute.Normal, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_AMBIENT:
                    result.set(new TextureAttribute(TextureAttribute.Ambient, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_EMISSIVE:
                    result.set(new TextureAttribute(TextureAttribute.Emissive, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_REFLECTION:
                    result.set(new TextureAttribute(TextureAttribute.Reflection, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_SHININESS:
                    result.set(new TextureAttribute(TextureAttribute.Roughness, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
            }
        }
    }
    return result;
}
Also used : Material(gaiasky.util.gdx.shader.Material) TextureDescriptor(com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor) Texture(com.badlogic.gdx.graphics.Texture)

Example 14 with Material

use of gaiasky.util.gdx.shader.Material in project gaiasky by langurmonkey.

the class IntModelInstance method getMaterial.

/**
 * @param id The ID of the material to fetch.
 * @param ignoreCase whether to use case sensitivity when comparing the material id.
 * @return The {@link Material} with the specified id, or null if not available.
 */
public Material getMaterial(final String id, boolean ignoreCase) {
    final int n = materials.size;
    Material material;
    if (ignoreCase) {
        for (int i = 0; i < n; i++) if ((material = materials.get(i)).id.equalsIgnoreCase(id))
            return material;
    } else {
        for (int i = 0; i < n; i++) if ((material = materials.get(i)).id.equals(id))
            return material;
    }
    return null;
}
Also used : Material(gaiasky.util.gdx.shader.Material)

Example 15 with Material

use of gaiasky.util.gdx.shader.Material in project gaiasky by langurmonkey.

the class ModelCache method getModel.

public Pair<IntModel, Map<String, Material>> getModel(String shape, Map<String, Object> params, Bits attributes, int primitiveType) {
    String key = getKey(shape, params, attributes);
    IntModel model = null;
    Map<String, Material> materials = new HashMap<>();
    Material mat;
    if (modelCache.containsKey(key)) {
        model = modelCache.get(key);
        mat = model.materials.first();
    } else {
        mat = new Material();
        switch(shape) {
            case "sphere":
                int quality = ((Long) params.get("quality")).intValue();
                float diameter = params.containsKey("diameter") ? ((Double) params.get("diameter")).floatValue() : 1f;
                Boolean flip = params.containsKey("flip") ? (Boolean) params.get("flip") : false;
                model = mb.createSphere(diameter, diameter, diameter, quality, quality, flip, primitiveType, mat, attributes);
                modelCache.put(key, model);
                break;
            case "icosphere":
                int recursion = ((Long) params.get("recursion")).intValue();
                diameter = params.containsKey("diameter") ? ((Double) params.get("diameter")).floatValue() : 1f;
                flip = params.containsKey("flip") ? (Boolean) params.get("flip") : false;
                model = mb.createIcoSphere(diameter / 2, recursion, flip, false, primitiveType, mat, attributes);
                modelCache.put(key, model);
                break;
            case "octahedronsphere":
                int divisions = ((Long) params.get("divisions")).intValue();
                diameter = params.containsKey("diameter") ? ((Double) params.get("diameter")).floatValue() : 1f;
                flip = params.containsKey("flip") ? (Boolean) params.get("flip") : false;
                model = mb.createOctahedronSphere(diameter / 2, divisions, flip, false, primitiveType, mat, attributes);
                modelCache.put(key, model);
                break;
            case "plane":
            case "patch":
                int divisionsU = ((Long) params.get("divisionsu")).intValue();
                int divisionsV = ((Long) params.get("divisionsv")).intValue();
                float side = ((Double) params.get("side")).floatValue();
                model = mb.createPlane(side, divisionsU, divisionsV, primitiveType, mat, attributes);
                modelCache.put(key, model);
                break;
            case "disc":
                // Prepare model
                float diameter2 = (params.containsKey("diameter") ? ((Double) params.get("diameter")).floatValue() : 1f) / 2f;
                // Initialize disc model
                // TOP VERTICES
                VertexInfo vt00 = new VertexInfo();
                vt00.setPos(-diameter2, 0, -diameter2);
                vt00.setNor(0, 1, 0);
                vt00.setUV(0, 0);
                VertexInfo vt01 = new VertexInfo();
                vt01.setPos(diameter2, 0, -diameter2);
                vt01.setNor(0, 1, 0);
                vt01.setUV(0, 1);
                VertexInfo vt11 = new VertexInfo();
                vt11.setPos(diameter2, 0, diameter2);
                vt11.setNor(0, 1, 0);
                vt11.setUV(1, 1);
                VertexInfo vt10 = new VertexInfo();
                vt10.setPos(-diameter2, 0, diameter2);
                vt10.setNor(0, 1, 0);
                vt10.setUV(1, 0);
                // BOTTOM VERTICES
                VertexInfo vb00 = new VertexInfo();
                vb00.setPos(-diameter2, 0, -diameter2);
                vb00.setNor(0, 1, 0);
                vb00.setUV(0, 0);
                VertexInfo vb01 = new VertexInfo();
                vb01.setPos(diameter2, 0, -diameter2);
                vb01.setNor(0, 1, 0);
                vb01.setUV(0, 1);
                VertexInfo vb11 = new VertexInfo();
                vb11.setPos(diameter2, 0, diameter2);
                vb11.setNor(0, 1, 0);
                vb11.setUV(1, 1);
                VertexInfo vb10 = new VertexInfo();
                vb10.setPos(-diameter2, 0, diameter2);
                vb10.setNor(0, 1, 0);
                vb10.setUV(1, 0);
                mb.begin();
                mb.part("up", primitiveType, attributes, mat).rect(vt00, vt01, vt11, vt10);
                mb.part("down", primitiveType, attributes, mat).rect(vb00, vb10, vb11, vb01);
                model = mb.end();
                break;
            case "twofacedbillboard":
                // Prepare model
                diameter2 = (params.containsKey("diameter") ? ((Double) params.get("diameter")).floatValue() : 1f) / 2f;
                // Initialize disc model
                // TOP VERTICES
                vt00 = new VertexInfo();
                vt00.setPos(-diameter2, -diameter2, 0);
                vt00.setNor(0, 1, 0);
                vt00.setUV(1, 0);
                vt01 = new VertexInfo();
                vt01.setPos(-diameter2, diameter2, 0);
                vt01.setNor(0, 1, 0);
                vt01.setUV(1, 1);
                vt11 = new VertexInfo();
                vt11.setPos(diameter2, diameter2, 0);
                vt11.setNor(0, 1, 0);
                vt11.setUV(0, 1);
                vt10 = new VertexInfo();
                vt10.setPos(diameter2, -diameter2, 0);
                vt10.setNor(0, 1, 0);
                vt10.setUV(0, 0);
                // BOTTOM VERTICES
                vb00 = new VertexInfo();
                vb00.setPos(-diameter2, -diameter2, 0);
                vb00.setNor(0, 1, 0);
                vb00.setUV(1, 0);
                vb01 = new VertexInfo();
                vb01.setPos(-diameter2, diameter2, 0);
                vb01.setNor(0, 1, 0);
                vb01.setUV(1, 1);
                vb11 = new VertexInfo();
                vb11.setPos(diameter2, diameter2, 0);
                vb11.setNor(0, 1, 0);
                vb11.setUV(0, 1);
                vb10 = new VertexInfo();
                vb10.setPos(diameter2, -diameter2, 0);
                vb10.setNor(0, 1, 0);
                vb10.setUV(0, 0);
                mb.begin();
                mb.part("up", primitiveType, attributes, mat).rect(vt00, vt01, vt11, vt10);
                mb.part("down", primitiveType, attributes, mat).rect(vb00, vb10, vb11, vb01);
                model = mb.end();
                break;
            case "cylinder":
                // Use builder
                float width = ((Double) params.get("width")).floatValue();
                float height = ((Double) params.get("height")).floatValue();
                float depth = ((Double) params.get("depth")).floatValue();
                divisions = ((Long) params.get("divisions")).intValue();
                flip = params.containsKey("flip") ? (Boolean) params.get("flip") : false;
                model = mb.createCylinder(width, height, depth, divisions, flip, primitiveType, mat, attributes);
                break;
            case "ring":
                // Sphere with cylinder
                Material ringMat = new Material();
                materials.put("ring", ringMat);
                quality = ((Long) params.get("quality")).intValue();
                divisions = ((Long) params.get("divisions")).intValue();
                float innerRad = ((Double) params.get("innerradius")).floatValue();
                float outerRad = ((Double) params.get("outerradius")).floatValue();
                boolean sph = params.containsKey("sphere-in-ring") ? (Boolean) params.get("sphere-in-ring") : true;
                if (sph) {
                    model = ModelCache.cache.mb.createSphereRing(1, quality, quality, innerRad, outerRad, divisions, primitiveType, mat, ringMat, attributes);
                } else {
                    model = ModelCache.cache.mb.createRing(1, quality, quality, innerRad, outerRad, divisions, primitiveType, mat, ringMat, attributes);
                }
                break;
            case "cone":
                width = ((Double) params.get("width")).floatValue();
                height = ((Double) params.get("height")).floatValue();
                depth = ((Double) params.get("depth")).floatValue();
                divisions = ((Long) params.get("divisions")).intValue();
                int hDivisions = 0;
                if (params.containsKey("hdivisions")) {
                    hDivisions = ((Long) params.get("hdivisions")).intValue();
                }
                if (hDivisions == 0)
                    model = mb.createCone(width, height, depth, divisions, primitiveType, mat, attributes);
                else
                    model = mb.createCone(width, height, depth, divisions, hDivisions, primitiveType, mat, attributes);
                break;
            case "cube":
            case "box":
                if (params.containsKey("width")) {
                    width = ((Double) params.get("width")).floatValue();
                    height = ((Double) params.get("height")).floatValue();
                    depth = ((Double) params.get("depth")).floatValue();
                } else {
                    width = ((Double) params.get("size")).floatValue();
                    height = width;
                    depth = width;
                }
                model = mb.createBox(width, height, depth, mat, attributes);
                break;
        }
    }
    materials.put("base", mat);
    return new Pair<>(model, materials);
}
Also used : HashMap(java.util.HashMap) VertexInfo(gaiasky.util.gdx.IntMeshPartBuilder.VertexInfo) Material(gaiasky.util.gdx.shader.Material) IntModel(gaiasky.util.gdx.model.IntModel)

Aggregations

Material (gaiasky.util.gdx.shader.Material)22 IntModel (gaiasky.util.gdx.model.IntModel)7 IntModelInstance (gaiasky.util.gdx.model.IntModelInstance)7 Texture (com.badlogic.gdx.graphics.Texture)6 BlendingAttribute (gaiasky.util.gdx.shader.attribute.BlendingAttribute)6 Matrix4 (com.badlogic.gdx.math.Matrix4)5 ColorAttribute (gaiasky.util.gdx.shader.attribute.ColorAttribute)5 ModelComponent (gaiasky.scenegraph.component.ModelComponent)4 Environment (gaiasky.util.gdx.shader.Environment)4 FloatAttribute (gaiasky.util.gdx.shader.attribute.FloatAttribute)4 TextureAttribute (gaiasky.util.gdx.shader.attribute.TextureAttribute)4 Map (java.util.Map)4 DirectionalLight (com.badlogic.gdx.graphics.g3d.environment.DirectionalLight)2 IntModelBuilder (gaiasky.util.gdx.IntModelBuilder)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)1 TextureDescriptor (com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor)1 PixmapTextureData (com.badlogic.gdx.graphics.glutils.PixmapTextureData)1 ExtendViewport (com.badlogic.gdx.utils.viewport.ExtendViewport)1 IntMeshPartBuilder (gaiasky.util.gdx.IntMeshPartBuilder)1