de.javagl.obj.ObjData Java Examples

The following examples show how to use de.javagl.obj.ObjData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObjGltfAssetCreatorV1.java    From JglTF with MIT License 5 votes vote down vote up
/**
 * Create a byte buffer containing the data of the indices for the
 * given OBJ. The face vertex indices of the given OBJ will be 
 * extracted (assuming that it contains only triangles), converted
 * to the given indices component type (which is a GL constant like
 * GL_SHORT), and a byte buffer containing these indices will be returned.
 * 
 * @param obj The OBJ
 * @param indicesComponentType The indices component type
 * @return The byte buffer
 */
private static ByteBuffer createIndicesByteBuffer(
    ReadableObj obj, int indicesComponentType)
{
    int numVerticesPerFace = 3;
    IntBuffer objIndices = 
        ObjData.getFaceVertexIndices(obj, numVerticesPerFace);
    int indicesComponentSize =
        Accessors.getNumBytesForAccessorComponentType(
            indicesComponentType);
    ByteBuffer indicesByteBuffer = 
        IntBuffers.convertToByteBuffer(objIndices, indicesComponentSize);
    return indicesByteBuffer;
}
 
Example #2
Source File: ObjGltfAssetCreatorV2.java    From JglTF with MIT License 5 votes vote down vote up
/**
 * Create a byte buffer containing the data of the indices for the
 * given OBJ. The face vertex indices of the given OBJ will be 
 * extracted (assuming that it contains only triangles), converted
 * to the given indices component type (which is a GL constant like
 * GL_SHORT), and a byte buffer containing these indices will be returned.
 * 
 * @param obj The OBJ
 * @param indicesComponentType The indices component type
 * @return The byte buffer
 */
private static ByteBuffer createIndicesByteBuffer(
    ReadableObj obj, int indicesComponentType)
{
    int numVerticesPerFace = 3;
    IntBuffer objIndices = 
        ObjData.getFaceVertexIndices(obj, numVerticesPerFace);
    int indicesComponentSize =
        Accessors.getNumBytesForAccessorComponentType(
            indicesComponentType);
    ByteBuffer indicesByteBuffer = 
        IntBuffers.convertToByteBuffer(objIndices, indicesComponentSize);
    return indicesByteBuffer;
}
 
Example #3
Source File: ObjGltfAssetCreatorV1.java    From JglTF with MIT License 4 votes vote down vote up
/**
 * Create the {@link MeshPrimitive} from the given OBJ data.
 * 
 * @param part The OBJ
 * @param partName A name that will be used as a basis to create the IDs of 
 * the {@link BufferView} and {@link Accessor} instances
 * @return The {@link MeshPrimitive}
 */
private MeshPrimitive createMeshPrimitive(ReadableObj part, String partName)
{
    MeshPrimitive meshPrimitive = 
        new MeshPrimitive();
    meshPrimitive.setMode(GltfConstants.GL_TRIANGLES);

    // Add the indices data from the OBJ to the buffer structure
    String indicesAccessorId = partName + "_indices"; 
    bufferStructureBuilder.createAccessorModel(
        indicesAccessorId, indicesComponentType, "SCALAR", 
        createIndicesByteBuffer(part, indicesComponentType));
    meshPrimitive.setIndices(indicesAccessorId);
    
    bufferStructureBuilder.createArrayElementBufferViewModel(
        partName + "_indices_bufferView");
    
    // Add the vertices (positions) from the OBJ to the buffer structure
    String positionsAccessorId = partName + "_positions";
    FloatBuffer objVertices = ObjData.getVertices(part);
    bufferStructureBuilder.createAccessorModel(
        positionsAccessorId, GltfConstants.GL_FLOAT, "VEC3", 
        Buffers.createByteBufferFrom(objVertices));
    meshPrimitive.addAttributes("POSITION", positionsAccessorId);

    // Add the texture coordinates from the OBJ to the buffer structure
    boolean flipY = true;
    FloatBuffer objTexCoords = ObjData.getTexCoords(part, 2, flipY);
    if (objTexCoords.capacity() > 0)
    {
        String texCoordsAccessorId = partName + "_texcoords0";
        bufferStructureBuilder.createAccessorModel(
            texCoordsAccessorId, GltfConstants.GL_FLOAT, "VEC2", 
            Buffers.createByteBufferFrom(objTexCoords));
        meshPrimitive.addAttributes("TEXCOORD_0", texCoordsAccessorId);
    }
    
    // Add the normals from the OBJ to the buffer structure
    FloatBuffer objNormals = ObjData.getNormals(part);
    if (objNormals.capacity() > 0)
    {
        ObjNormals.normalize(objNormals);
        String normalsAccessorId = partName + "_normals";
        bufferStructureBuilder.createAccessorModel(
            normalsAccessorId, GltfConstants.GL_FLOAT, "VEC3", 
            Buffers.createByteBufferFrom(objNormals));
        meshPrimitive.addAttributes("NORMAL", normalsAccessorId);
    }

    bufferStructureBuilder.createArrayBufferViewModel(
        partName + "_attributes_bufferView");
    
    return meshPrimitive;
}
 
Example #4
Source File: ObjGltfAssetCreatorV2.java    From JglTF with MIT License 4 votes vote down vote up
/**
 * Create the {@link MeshPrimitive} from the given OBJ data.
 * 
 * @param part The OBJ
 * @param partName A name that will be used as a basis to create the IDs of 
 * the {@link BufferView} and {@link Accessor} instances
 * @return The {@link MeshPrimitive}
 */
private MeshPrimitive createMeshPrimitive(ReadableObj part, String partName)
{
    MeshPrimitive meshPrimitive = 
        new MeshPrimitive();
    meshPrimitive.setMode(GltfConstants.GL_TRIANGLES);

    // Add the indices data from the OBJ to the buffer structure
    int indicesAccessorIndex = 
        bufferStructureBuilder.getNumAccessorModels();
    bufferStructureBuilder.createAccessorModel(
        "indicesAccessor_" + indicesAccessorIndex,
        indicesComponentType, "SCALAR", 
        createIndicesByteBuffer(part, indicesComponentType));
    meshPrimitive.setIndices(indicesAccessorIndex);
    
    bufferStructureBuilder.createArrayElementBufferViewModel(
        partName + "_indices_bufferView");
    
    // Add the vertices (positions) from the OBJ to the buffer structure
    int positionsAccessorIndex = 
        bufferStructureBuilder.getNumAccessorModels();
    FloatBuffer objVertices = ObjData.getVertices(part);
    bufferStructureBuilder.createAccessorModel(
        "positionsAccessor_" + positionsAccessorIndex,
        GltfConstants.GL_FLOAT, "VEC3", 
        Buffers.createByteBufferFrom(objVertices));
    meshPrimitive.addAttributes("POSITION", positionsAccessorIndex);

    // Add the texture coordinates from the OBJ to the buffer structure
    boolean flipY = true;
    FloatBuffer objTexCoords = ObjData.getTexCoords(part, 2, flipY);
    if (objTexCoords.capacity() > 0)
    {
        int texCoordsAccessorIndex = 
            bufferStructureBuilder.getNumAccessorModels();
        bufferStructureBuilder.createAccessorModel(
            "texCoordsAccessor_" + texCoordsAccessorIndex, 
            GltfConstants.GL_FLOAT, "VEC2", 
            Buffers.createByteBufferFrom(objTexCoords));
        meshPrimitive.addAttributes("TEXCOORD_0", texCoordsAccessorIndex);
    }
    
    // Add the normals from the OBJ to the buffer structure
    FloatBuffer objNormals = ObjData.getNormals(part);
    if (objNormals.capacity() > 0)
    {
        ObjNormals.normalize(objNormals);
        int normalsAccessorIndex = 
            bufferStructureBuilder.getNumAccessorModels();
        bufferStructureBuilder.createAccessorModel(
            "normalsAccessor_" + normalsAccessorIndex, 
            GltfConstants.GL_FLOAT, "VEC3", 
            Buffers.createByteBufferFrom(objNormals));
        meshPrimitive.addAttributes("NORMAL", normalsAccessorIndex);
    }

    bufferStructureBuilder.createArrayBufferViewModel(
        partName + "_attributes_bufferView");
    
    return meshPrimitive;
}