⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 load3ds.java

📁 包括了JAVA3D(全世界都能看到的网络三维动画)的源代码部分! 很多基础但是却很精彩的例子!有什么不明白的也可以和我交流MSN:guorui0728@hotmail.com
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            {
                normal.add( otherFace.normal );
                noofNormals++;
            }
        }

        if ( noofNormals != 1 )
        {
            normal.x /= noofNormals;
            normal.y /= noofNormals;
            normal.z /= noofNormals;
        }

        normal.normalize();

        return normal;
    }


    //
    // Read in the 2D texture coordinates - note these are
    // only valid if planar mapping was used in 3DS.
    //

    void processTextureCoordinates( RandomAccessFile in )
                throws IOException
    {
        int     vertexCount = readUnsignedShort( in );
        int     i;

        if ( vertexCount != noofVertices )
        {
            System.out.println( "** Number of texture vertices = #model vertices" );
            return;
        }

        if ( verbosity > 1 )
        {
            System.out.println( "    Texture coordinates: #" + vertexCount );
        }

        textureCoords = new Point2f[vertexCount];

        for ( i = 0; i < vertexCount; i++ )
        {
            textureCoords[i] = new Point2f( readFloat( in ), readFloat( in ) );
            //System.out.println( "==     " + textureCoords[i] );
        }
    }


    //
    // Read in the definition of the ambient light.
    //

    void processAmbientLight( RandomAccessFile in )
                throws IOException
    {
        Color3f ambient = readColor( in );

        System.out.println( "Ambient Light: " + ambient );
    }


    //
    // Read in a colour, either in the 3 * float format or
    // the 3 * byte format.
    //

    Color3f readColor( RandomAccessFile in )
                throws IOException
    {
        int tag     = readUnsignedShort( in );
        int length  = readInt( in );

        switch ( tag )
        {
            case S3D_COLOR_F:
                return new Color3f( readFloat( in ), readFloat( in ), readFloat( in ) );

            case S3D_COLOR_24:
                return new Color3f( (float)in.readUnsignedByte() / 255,
                                    (float)in.readUnsignedByte() / 255,
                                    (float)in.readUnsignedByte() / 255 );

            default:
                throw new IOException( "COLOR_F/COLOR_24 expected" );
        }
    }


    //
    // Read in a float or int percentage and return it
    // as a number between 0.0 and 1.0
    //

    float readPercentage( RandomAccessFile in )
                throws IOException
    {
        int tag     = readUnsignedShort( in );
        int length  = readInt( in );

        switch ( tag )
        {
            case S3D_INT_PERCENTAGE:
                return (float)readUnsignedShort( in ) / 100;

            case S3D_FLOAT_PERCENTAGE:
                return readFloat( in );

            default:
                throw new IOException( "INT_PERCENTAGE/FLOAT_PERCENTAGE expected" );
        }
    }


    //
    // Read in material name.
    //

    String readMatName( RandomAccessFile in )
                throws IOException
    {
        int tag     = readUnsignedShort( in );
        int length  = readInt( in );

        return readName( in );
    }


    //
    // Read in the string used to specify a name in
    // many different chunks.
    //

    String readName( RandomAccessFile in )
                throws IOException
    {
        StringBuffer    buf = new StringBuffer();
        char            c;

        while ( (c = (char)in.readUnsignedByte()) != '\0' )
        {
            buf.append( c );
        }

        return buf.toString();
    }


    //
    // Read in an unsigned short (16 bits).
    //

    int readUnsignedShort( RandomAccessFile in )
            throws IOException
    {
        int num = in.readUnsignedShort();

        return ((num << 8) & 0xFF00) | ((num >> 8) & 0x00FF);
    }


    //
    // Read in a 32 bit integer (unsigned).
    //

    int readInt( RandomAccessFile in )
            throws IOException
    {
        int num = in.readInt();

        return ((num << 24) & 0xFF000000) |
               ((num << 8)  & 0x00FF0000) |
               ((num >> 8)  & 0x0000FF00) |
               ((num >> 24) & 0x000000FF);
    }


    //
    // Read in a 32 bit floating point number.
    //

    float readFloat( RandomAccessFile in )
            throws IOException
    {
        return Float.intBitsToFloat( readInt( in ) );
    }


    //
    // Internal data structure representing a polygon and
    // when constructed updates a list of those faces
    // sharing any given vertex.
    //

    class Face
    {
        int         a, b, c;
        Vector3f    normal  = null;
        int         group;

        public Face( int vertexA, int vertexB, int vertexC )
        {
            a       = vertexA;
            b       = vertexB;
            c       = vertexC;
            normal  = calculateFaceNormal( a, b, c );

            sharedFaces[a].addElement( this );
            sharedFaces[b].addElement( this );
            sharedFaces[c].addElement( this );
        }


        public Point3f a()
        {
            return vertices[a];
        }


        public Point3f b()
        {
            return vertices[b];
        }


        public Point3f c()
        {
            return vertices[c];
        }
    };


    //
    // Internal data structure for representing a surface
    // as a list of faces.
    //

    class Surface
    {
        Vector  faces = new Vector();

        public Surface()
        {
        }


        public void add( Face f )
        {
            faces.addElement( f );
        }


        public Enumeration faces()
        {
            return faces.elements();
        }


        public int noofFaces()
        {
            return faces.size();
        }
    }


    //
    // List of chunks contained in a .3DS file that we
    // are interested in.
    //


    // .3DS file magic number

    static final int S3D_M3DMAGIC 		= 0x4d4d;


    // Tag IDs

    static final int S3D_MMAGIC 		= 0x3d3d;
    static final int S3D_MESH_VERSION	= 0x0001;
    static final int S3D_M3D_VERSION		= 0x0002;

    static final int S3D_COLOR_F		= 0x0010;
    static final int S3D_COLOR_24		= 0x0011;
    static final int S3D_INT_PERCENTAGE	= 0x0030;
    static final int S3D_FLOAT_PERCENTAGE	= 0x0031;

    static final int S3D_MASTER_SCALE	= 0x0100;

    static final int S3D_BIT_MAP		= 0x1100;
    static final int S3D_USE_BIT_MAP		= 0x1101;
    static final int S3D_SOLID_BGND		= 0x1200;
    static final int S3D_USE_SOLID_BGND	= 0x1201;
    static final int S3D_V_GRADIENT		= 0x1300;
    static final int S3D_USE_V_GRADIENT	= 0x1301;

    static final int S3D_LO_SHADOW_BIAS	= 0x1400;
    static final int S3D_HI_SHADOW_BIAS	= 0x1410;
    static final int S3D_SHADOW_MAP_SIZE	= 0x1420;
    static final int S3D_SHADOW_SAMPLES	= 0x1430;
    static final int S3D_SHADOW_RANGE	= 0x1440;

    static final int S3D_AMBIENT_LIGHT	= 0x2100;

    static final int S3D_FOG			= 0x2200;
    static final int S3D_USE_FOG		= 0x2201;
    static final int S3D_FOG_BGND		= 0x2210;
    static final int S3D_DISTANCE_CUE	= 0x2300;
    static final int S3D_USE_DISTANCE_CUE	= 0x2301;
    static final int S3D_DCUE_BGND		= 0x2310;

    static final int S3D_DEFAULT_VIEW	= 0x3000;
    static final int S3D_VIEW_TOP		= 0x3010;
    static final int S3D_VIEW_BOTTOM		= 0x3020;
    static final int S3D_VIEW_LEFT		= 0x3030;
    static final int S3D_VIEW_RIGHT		= 0x3040;
    static final int S3D_VIEW_FRONT		= 0x3050;
    static final int S3D_VIEW_BACK		= 0x3060;
    static final int S3D_VIEW_USER		= 0x3070;
    static final int S3D_VIEW_CAMERA		= 0x3080;
    static final int S3D_VIEW_WINDOW		= 0x3090;

    static final int S3D_NAMED_OBJECT	= 0x4000;
    static final int S3D_OBJ_HIDDEN		= 0x4010;
    static final int S3D_OBJ_VIS_LOFTER	= 0x4011;
    static final int S3D_OBJ_DOESNT_CAST	= 0x4012;
    static final int S3D_OBJ_MATTE		= 0x4013;

    static final int S3D_N_TRI_OBJECT	= 0x4100;

    static final int S3D_POINT_ARRAY		= 0x4110;
    static final int S3D_POINT_FLAG_ARRAY	= 0x4111;
    static final int S3D_FACE_ARRAY		= 0x4120;
    static final int S3D_MSH_MAT_GROUP	= 0x4130;
    static final int S3D_TEX_VERTS		= 0x4140;
    static final int S3D_SMOOTH_GROUP	= 0x4150;
    static final int S3D_MESH_MATRIX		= 0x4160;

    static final int S3D_N_DIRECT_LIGHT	= 0x4600;
    static final int S3D_DL_SPOTLIGHT	= 0x4610;
    static final int S3D_DL_OFF		= 0x4620;
    static final int S3D_DL_SHADOWED		= 0x4630;

    static final int S3D_N_CAMERA		= 0x4700;


    // Material file Chunk IDs

    static final int S3D_MAT_ENTRY		= 0xafff;
    static final int S3D_MAT_NAME		= 0xa000;
    static final int S3D_MAT_AMBIENT		= 0xa010;
    static final int S3D_MAT_DIFFUSE		= 0xa020;
    static final int S3D_MAT_SPECULAR	= 0xa030;
    static final int S3D_MAT_SHININESS	= 0xa040;
    static final int S3D_MAT_SHININESS_STRENGTH = 0xa041;
    static final int S3D_MAT_TRANSPARENCY	= 0xa050;
    static final int S3D_MAT_WIRE       = 0xa085;
    static final int S3D_MAT_WIRESIZE   = 0xa087;
    static final int S3D_MAT_SELF_ILLUM	= 0xa080;
    static final int S3D_MAT_TWO_SIDE	= 0xa081;
    static final int S3D_MAT_DECAL		= 0xa082;
    static final int S3D_MAT_ADDITIVE	= 0xa083;

    static final int S3D_MAT_SHADING		= 0xa100;


    static final int S3D_MAT_TEXMAP		= 0xa200;
    static final int S3D_MAT_OPACMAP		= 0xa210;
    static final int S3D_MAT_REFLMAP		= 0xa220;
    static final int S3D_MAT_BUMPMAP		= 0xa230;

    static final int S3D_MAT_MAPNAME		= 0xa300;


    // Reverse engineered hierarchy information

    static final int S3D_HIERARCHY		= 0xb000;
    static final int S3D_HIERARCHY_NODE	= 0xb002;
    static final int S3D_HIERARCHY_LINK	= 0xb010;
    static final int S3D_INSTANCE_NAME      = 0xb011;
    static final int S3D_PIVOT              = 0xb013;
    static final int S3D_POS_TRACK_TAG      = 0xb020;
    static final int S3D_ROT_TRACK_TAG      = 0xb021;
    static final int S3D_SCL_TRACK_TAG      = 0xb022;
    static final int S3D_NODE_ID	        = 0xb030;
    static final int S3D_OBJECT_LINK_NULL   = 0xffff;


    // Dummy Chunk ID

    static final int S3D_DUMMY_CHUNK		= 0xffff;


    // These chunks are found in the .PRJ file (only as far as I know)

    static final int S3D_PROJECT_FILE 	= 0xc23d;
    static final int S3D_MAPPING_RETILE	= 0xc4b0;
    static final int S3D_MAPPING_CENTRE	= 0xc4c0;
    static final int S3D_MAPPING_SCALE 	= 0xc4d0;
    static final int S3D_MAPPING_ORIENTATION	= 0xc4e1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -