Search in sources :

Example 1 with OwnObjLoader

use of gaiasky.util.gdx.loader.OwnObjLoader 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)

Aggregations

Pixmap (com.badlogic.gdx.graphics.Pixmap)1 Texture (com.badlogic.gdx.graphics.Texture)1 PixmapTextureData (com.badlogic.gdx.graphics.glutils.PixmapTextureData)1 OwnObjLoader (gaiasky.util.gdx.loader.OwnObjLoader)1 IntMesh (gaiasky.util.gdx.mesh.IntMesh)1 Material (gaiasky.util.gdx.shader.Material)1 TextureAttribute (gaiasky.util.gdx.shader.attribute.TextureAttribute)1 ShortBuffer (java.nio.ShortBuffer)1 PointerBuffer (org.lwjgl.PointerBuffer)1