Project

General

Profile

« Previous | Next » 

Revision fc46d8ef

Added by Leszek Koltunski about 6 hours ago

get rid of java.nio stuff from Mesh reading/writing

View differences:

src/main/java/org/distorted/library/mesh/MeshBase.kt
33 33
import org.distorted.library.uniformblock.UniformBlockAssociation
34 34
import org.distorted.library.uniformblock.UniformBlockCenter
35 35

  
36
import java.nio.ByteBuffer
37 36
import java.nio.ByteOrder
38 37

  
39 38
import kotlin.math.min
......
911 910
        GLES.glBindBuffer(GLES.GL_ARRAY_BUFFER, 0)
912 911
    }
913 912

  
913
    ///////////////////////////////////////////////////////////////////////////////////////////////////
914
    // for writing FloatArrays. Big-endian.
915
    fun FloatArray.toByteArrayBE(): ByteArray
916
    {
917
        val result = ByteArray(size*4)
918
        var i = 0
919

  
920
        for (f in this)
921
        {
922
            val bits = f.toBits()
923
            result[i++] = (bits shr 24 and 0xFF).toByte()
924
            result[i++] = (bits shr 16 and 0xFF).toByte()
925
            result[i++] = (bits shr  8 and 0xFF).toByte()
926
            result[i++] = (bits        and 0xFF).toByte()
927
        }
928

  
929
        return result
930
    }
931

  
932
    ///////////////////////////////////////////////////////////////////////////////////////////////////
933
    // meshes are big-endian
934
    fun ByteArray.getIntAt(offset: Int): Int
935
    {
936
        val v0 = (this[offset+3].toInt() and 0xFF)
937
        val v1 = (this[offset+2].toInt() and 0xFF)
938
        val v2 = (this[offset+1].toInt() and 0xFF)
939
        val v3 = (this[offset  ].toInt() and 0xFF)
940

  
941
        return v0 or (v1 shl 8) or (v2 shl 16) or (v3 shl 24)
942
    }
943
    
944
    ///////////////////////////////////////////////////////////////////////////////////////////////////
945
    fun ByteArray.getFloatAt(offset: Int): Float
946
    {
947
        return Float.fromBits(getIntAt(offset))
948
    }
949

  
950
    ///////////////////////////////////////////////////////////////////////////////////////////////////
951
    fun ByteArray.readFloatArray(offsetFloats: Int, count: Int): FloatArray
952
    {
953
        val result = FloatArray(count)
954
        var byteOffset = 4*offsetFloats
955

  
956
        for (i in 0 until count)
957
        {
958
            result[i] = getFloatAt(byteOffset)
959
            byteOffset += 4
960
        }
961
        return result
962
    }
963

  
914 964
    ///////////////////////////////////////////////////////////////////////////////////////////////////
915 965
    // Return the number of bytes read.
916 966
    fun read(reader: FileReader): Int
......
926 976
        try
927 977
        {
928 978
            reader.read(buffer)
929
            val byteBuf = ByteBuffer.wrap(buffer)
979
            version = buffer.getIntAt(0)
930 980

  
931
            version = byteBuf.getInt(0)
932

  
933
            if (version==1||version==2)
981
            if( version==2 )
934 982
            {
935
                numVertices = byteBuf.getInt(4)
936
                numTex = byteBuf.getInt(8)
937
                numEff = byteBuf.getInt(12)
983
                numVertices = buffer.getIntAt(4)
984
                numTex = buffer.getIntAt(8)
985
                numEff = buffer.getIntAt(12)
986

  
987
                if( numVertices<=0 || numEff<=0 || numTex<=0 )
988
                {
989
                    DistortedLibrary.logMessage("MeshBase: Error: numVertices: $numVertices numEff: $numEff numTex: $numTex")
990
                    return 0
991
                }
938 992
            }
939 993
            else
940 994
            {
......
951 1005
        //////////////////////////////////////////////////////////////////////////////////////////
952 1006
        // read contents of all texture and effect components
953 1007
        //////////////////////////////////////////////////////////////////////////////////////////
954
        if (numVertices>0&&numEff>0&&numTex>0)
955
        {
956
            val size = numEff+TEX_COMP_SIZE*numTex
957
            val tmp = FloatArray(size)
958
            mVertAttribs1 = FloatArray(VERT1_ATTRIBS*numVertices)
959
            mVertAttribs2 = FloatArray(VERT2_ATTRIBS*numVertices)
960

  
961
            // version 1 had extra 3 floats (the 'inflate' vector) in its vert1 array
962
            val vert1InFile = if (version==2) VERT1_ATTRIBS else VERT1_ATTRIBS+3
963
            // ... and there was no center.
964
            val centerSize = if (version==2) 3*numEff else 0
1008
        val size = numEff+TEX_COMP_SIZE*numTex
1009
        buffer = ByteArray(BYTES_PER_FLOAT*(size + 3*numEff + numVertices*(VERT1_ATTRIBS+VERT2_ATTRIBS)) )
965 1010

  
966
            buffer = ByteArray(BYTES_PER_FLOAT*(size+centerSize+numVertices*(vert1InFile+VERT2_ATTRIBS)))
967

  
968
            try
969
            {
970
                reader.read(buffer)
971
            }
972
            catch (e: Exception)
973
            {
974
                DistortedLibrary.logMessage("MeshBase: Exception2 trying to read file: $e")
975
                return 0
976
            }
977

  
978
            val byteBuf = ByteBuffer.wrap(buffer)
979
            val floatBuf = byteBuf.asFloatBuffer()
1011
        try
1012
        {
1013
            reader.read(buffer)
1014
        }
1015
        catch (e: Exception)
1016
        {
1017
            DistortedLibrary.logMessage("MeshBase: Exception2 trying to read file: $e")
1018
            return 0
1019
        }
980 1020

  
981
            floatBuf[tmp, 0, size]
1021
        var tex: TexComponent
1022
        var index: Int
1023
        var x: Float
1024
        var y: Float
1025
        var z: Float
1026
        var w: Float
982 1027

  
983
            var tex: TexComponent
984
            var index: Int
985
            var x: Float
986
            var y: Float
987
            var z: Float
988
            var w: Float
1028
        var texComp = 0
1029
        while (texComp<numTex)
1030
        {
1031
            index = buffer.getIntAt(TEX_COMP_SIZE*texComp)
989 1032

  
990
            var texComp = 0
991
            while (texComp<numTex)
992
            {
993
                index = tmp[TEX_COMP_SIZE*texComp].toInt()
1033
            x = buffer.getFloatAt(TEX_COMP_SIZE*texComp+1)
1034
            y = buffer.getFloatAt(TEX_COMP_SIZE*texComp+2)
1035
            z = buffer.getFloatAt(TEX_COMP_SIZE*texComp+3)
1036
            w = buffer.getFloatAt(TEX_COMP_SIZE*texComp+4)
994 1037

  
995
                x = tmp[TEX_COMP_SIZE*texComp+1]
996
                y = tmp[TEX_COMP_SIZE*texComp+2]
997
                z = tmp[TEX_COMP_SIZE*texComp+3]
998
                w = tmp[TEX_COMP_SIZE*texComp+4]
1038
            tex = TexComponent(index)
1039
            tex.setMap(Static4D(x, y, z, w))
999 1040

  
1000
                tex = TexComponent(index)
1001
                tex.setMap(Static4D(x, y, z, w))
1041
            mTexComponent.add(tex)
1042
            texComp++
1043
        }
1002 1044

  
1003
                mTexComponent.add(tex)
1004
                texComp++
1005
            }
1045
        for (effComp in 0 until numEff)
1046
        {
1047
            index = buffer.getIntAt(TEX_COMP_SIZE*texComp+effComp)
1048
            mEffComponent.add(index)
1049
        }
1006 1050

  
1007
            for (effComp in 0 until numEff)
1008
            {
1009
                index = tmp[TEX_COMP_SIZE*texComp+effComp].toInt()
1010
                mEffComponent.add(index)
1011
            }
1051
        //////////////////////////////////////////////////////////////////////////////////////////
1052
        // read vert1 array, vert2 array and the component centers.
1053
        //////////////////////////////////////////////////////////////////////////////////////////
1054
        var start = size
1055
        var count = 3*numEff
1056
        val centers = buffer.readFloatArray(start,count )
1057
        start += count
1058
        count = VERT1_ATTRIBS*numVertices
1059
        mVertAttribs1 = buffer.readFloatArray( start,count )
1060
        start += count
1061
        count = VERT2_ATTRIBS*numVertices
1062
        mVertAttribs2 = buffer.readFloatArray( start,count )
1012 1063

  
1013
            //////////////////////////////////////////////////////////////////////////////////////////
1014
            // read vert1 array, vert2 array and (if version>1) the component centers.
1015
            //////////////////////////////////////////////////////////////////////////////////////////
1016
            when (version)
1064
        if (useCenters)
1065
        {
1066
            var eff = 0
1067
            while (eff<numEff)
1017 1068
            {
1018
                1 ->
1019
                {
1020
                    index = floatBuf.position()
1021

  
1022
                    var vert = 0
1023
                    while (vert<numVertices)
1024
                    {
1025
                        mVertAttribs1!![VERT1_ATTRIBS*vert+POS_ATTRIB  ] = floatBuf[index+POS_ATTRIB  ]
1026
                        mVertAttribs1!![VERT1_ATTRIBS*vert+POS_ATTRIB+1] = floatBuf[index+POS_ATTRIB+1]
1027
                        mVertAttribs1!![VERT1_ATTRIBS*vert+POS_ATTRIB+2] = floatBuf[index+POS_ATTRIB+2]
1028
                        mVertAttribs1!![VERT1_ATTRIBS*vert+NOR_ATTRIB  ] = floatBuf[index+NOR_ATTRIB  ]
1029
                        mVertAttribs1!![VERT1_ATTRIBS*vert+NOR_ATTRIB+1] = floatBuf[index+NOR_ATTRIB+1]
1030
                        mVertAttribs1!![VERT1_ATTRIBS*vert+NOR_ATTRIB+2] = floatBuf[index+NOR_ATTRIB+2]
1031
                        index += vert1InFile
1032
                        vert++
1033
                    }
1034
                    floatBuf.position(index)
1035
                }
1036

  
1037
                2 ->
1038
                {
1039
                    val centers = FloatArray(3*numEff)
1040
                    floatBuf[centers, 0, 3*numEff]
1041

  
1042
                    if (useCenters)
1043
                    {
1044
                        var eff = 0
1045
                        while (eff<numEff)
1046
                        {
1047
                            mUBC!!.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2])
1048
                            eff++
1049
                        }
1050
                    }
1051

  
1052
                    floatBuf[mVertAttribs1, 0, VERT1_ATTRIBS*numVertices]
1053
                }
1054

  
1055
                else ->
1056
                {
1057
                    DistortedLibrary.logMessage("MeshBase: Error: unknown mesh file version "+String.format("0x%08X", version))
1058
                    return 0
1059
                }
1069
                mUBC!!.setEffectCenterNow(eff, centers[3*eff], centers[3*eff+1], centers[3*eff+2])
1070
                eff++
1060 1071
            }
1061

  
1062
            floatBuf[mVertAttribs2, 0, VERT2_ATTRIBS*numVertices]
1063 1072
        }
1064 1073

  
1065 1074
        return BYTES_PER_FLOAT*(4+numEff+TEX_COMP_SIZE*numTex+VERT1_ATTRIBS*numVertices+VERT2_ATTRIBS*numVertices)
......
1130 1139
            writer.writeFloat(centers[4*i+2])
1131 1140
        }
1132 1141

  
1133
        val vertBuf1 = ByteBuffer.allocate(VERT1_SIZE*numVertices)
1134
        vertBuf1.asFloatBuffer().put(mVertAttribs1)
1135
        val vertBuf2 = ByteBuffer.allocate(VERT2_SIZE*numVertices)
1136
        vertBuf2.asFloatBuffer().put(mVertAttribs2)
1137

  
1138
        writer.write(vertBuf1.array())
1139
        writer.write(vertBuf2.array())
1142
        writer.write( mVertAttribs1!!.toByteArrayBE() )
1143
        writer.write( mVertAttribs2!!.toByteArrayBE() )
1140 1144
    }
1141 1145

  
1142 1146
    ///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff