Search in sources :

Example 1 with IntMesh

use of gaiasky.util.gdx.mesh.IntMesh in project gaiasky by langurmonkey.

the class BillboardSpriteRenderSystem method init.

private void init(float w, float h) {
    // Init comparator
    comp = new DistToCameraComparator<>();
    // Init vertices
    float[] vertices = new float[20];
    fillVertices(vertices, w, h);
    mesh = new IntMesh(true, 4, 6, new VertexAttribute[] { new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0") });
    mesh.setVertices(vertices, 0, vertices.length);
    mesh.getIndicesBuffer().position(0);
    mesh.getIndicesBuffer().limit(6);
    int[] indices = new int[] { 0, 1, 2, 0, 2, 3 };
    mesh.setIndices(indices);
    aux = new Vector3();
}
Also used : VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Vector3(com.badlogic.gdx.math.Vector3) IntMesh(gaiasky.util.gdx.mesh.IntMesh)

Example 2 with IntMesh

use of gaiasky.util.gdx.mesh.IntMesh in project gaiasky by langurmonkey.

the class BillboardStarRenderSystem method init.

private void init(String tex0, float w, float h) {
    setStarTexture(tex0);
    // Init comparator
    comp = new DistToCameraComparator<>();
    // Init vertices
    float[] vertices = new float[20];
    fillVertices(vertices, w, h);
    // We won't need indices if we use GL_TRIANGLE_FAN to draw our quad
    // TRIANGLE_FAN will draw the vertices in this order: 0, 1, 2; 0, 2, 3
    mesh = new IntMesh(true, 4, 6, new VertexAttribute[] { new VertexAttribute(Usage.Position, 2, ExtShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.ColorPacked, 4, ExtShaderProgram.COLOR_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ExtShaderProgram.TEXCOORD_ATTRIBUTE + "0") });
    mesh.setVertices(vertices, 0, vertices.length);
    mesh.getIndicesBuffer().position(0);
    mesh.getIndicesBuffer().limit(6);
    int[] indices = new int[] { 0, 1, 2, 0, 2, 3 };
    mesh.setIndices(indices);
    aux = new Vector3();
    EventManager.instance.subscribe(this, Event.STAR_TEXTURE_IDX_CMD);
}
Also used : VertexAttribute(com.badlogic.gdx.graphics.VertexAttribute) Vector3(com.badlogic.gdx.math.Vector3) IntMesh(gaiasky.util.gdx.mesh.IntMesh)

Example 3 with IntMesh

use of gaiasky.util.gdx.mesh.IntMesh 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 4 with IntMesh

use of gaiasky.util.gdx.mesh.IntMesh in project gaiasky by langurmonkey.

the class IntModel method convertMesh.

protected void convertMesh(IntModelMesh modelMesh) {
    int numIndices = 0;
    for (IntModelMeshPart part : modelMesh.parts) {
        numIndices += part.indices.length;
    }
    VertexAttributes attributes = new VertexAttributes(modelMesh.attributes);
    int numVertices = modelMesh.vertices.length / (attributes.vertexSize / 4);
    IntMesh mesh = new IntMesh(true, numVertices, numIndices, attributes);
    meshes.add(mesh);
    disposables.add(mesh);
    BufferUtils.copy(modelMesh.vertices, mesh.getVerticesBuffer(), modelMesh.vertices.length, 0);
    int offset = 0;
    mesh.getIndicesBuffer().clear();
    for (IntModelMeshPart part : modelMesh.parts) {
        IntMeshPart meshPart = new IntMeshPart();
        meshPart.id = part.id;
        meshPart.primitiveType = part.primitiveType;
        meshPart.offset = offset;
        meshPart.size = part.indices.length;
        meshPart.mesh = mesh;
        mesh.getIndicesBuffer().put(part.indices);
        offset += meshPart.size;
        meshParts.add(meshPart);
    }
    mesh.getIndicesBuffer().position(0);
    for (IntMeshPart part : meshParts) part.update();
}
Also used : IntModelMeshPart(gaiasky.util.gdx.model.data.IntModelMeshPart) VertexAttributes(com.badlogic.gdx.graphics.VertexAttributes) IntMesh(gaiasky.util.gdx.mesh.IntMesh)

Example 5 with IntMesh

use of gaiasky.util.gdx.mesh.IntMesh in project gaiasky by langurmonkey.

the class IntIntMeshBuilder method end.

/**
 * End building the mesh and returns the mesh
 */
public IntMesh end() {
    if (this.attributes == null)
        throw new RuntimeException("Call begin() first");
    endpart();
    final IntMesh mesh = new IntMesh(true, vertices.size / stride, indices.size, attributes);
    mesh.setVertices(vertices.items, 0, vertices.size);
    mesh.setIndices(indices.items, 0, indices.size);
    for (IntMeshPart p : parts) p.mesh = mesh;
    parts.clear();
    attributes = null;
    vertices.clear();
    indices.clear();
    return mesh;
}
Also used : IntMeshPart(gaiasky.util.gdx.model.IntMeshPart) IntMesh(gaiasky.util.gdx.mesh.IntMesh)

Aggregations

IntMesh (gaiasky.util.gdx.mesh.IntMesh)14 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)9 Vector3 (com.badlogic.gdx.math.Vector3)2 Pixmap (com.badlogic.gdx.graphics.Pixmap)1 Texture (com.badlogic.gdx.graphics.Texture)1 VertexAttributes (com.badlogic.gdx.graphics.VertexAttributes)1 PixmapTextureData (com.badlogic.gdx.graphics.glutils.PixmapTextureData)1 OwnObjLoader (gaiasky.util.gdx.loader.OwnObjLoader)1 IntMeshPart (gaiasky.util.gdx.model.IntMeshPart)1 IntModelMeshPart (gaiasky.util.gdx.model.data.IntModelMeshPart)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