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

📄 load3ds.java

📁 在java3d中导入.3ds文件的java程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                        ca.setShadeModel( ColoringAttributes.SHADE_FLAT );
                        break;

                    case 3:         // Phong
                    default:
                        style = PolygonAttributes.POLYGON_FILL;
                        ca.setShadeModel( ColoringAttributes.NICEST );
                        break;

                }

                if ( verbosity > 2 )
                {
                    System.out.println( "== Shading: " + mode + ", style = " + style );
                }

                pa.setPolygonMode( style );
                mat.setPolygonAttributes( pa );
                mat.setColoringAttributes( ca );
                break;

            case S3D_MAT_WIRE:          // Another way of enforcing wireframe
                PolygonAttributes   pat = mat.getPolygonAttributes();
                pat.setPolygonMode( PolygonAttributes.POLYGON_LINE );

                if ( verbosity > 2 )
                {
                    System.out.println( "== Wireframe" );
                }
                break;

            case S3D_MAT_WIRESIZE:      // Wireframe line width
                float               width   = readFloat( in );
                LineAttributes      la      = mat.getLineAttributes();

                if ( la == null )
                {
                    la = new LineAttributes();
                }

                la.setLineWidth( width );
                mat.setLineAttributes( la );

                if ( verbosity > 2 )
                {
                    System.out.println( "== Wire width: " + width );
                }
                break;

            case S3D_MAT_TWO_SIDE:      // Face culling
                PolygonAttributes   pat2 = mat.getPolygonAttributes();
                pat2.setCullFace( PolygonAttributes.CULL_NONE );

                if ( verbosity > 2 )
                {
                    System.out.println( "== Two sided" );
                }
                break;

           case S3D_MAT_TEXMAP:         // Image for texture map
                float   matPercent  = readPercentage( in ) * 100;
                String  imageName   = path + readMatName( in );

                System.out.println( "Loading texture map '" + imageName + "' (" +
                                    matPercent + "%)" );

                if ( new java.io.File( imageName ).exists() == false )
                {
                    System.out.println( "** Can't find image '" + imageName + "'" );
		            imageName = null;
                }
                else
                {
	                Texture textureMap = new TextureLoader( imageName, component ).getTexture();
	                if ( textureMap == null )
	                {
	                    System.out.println( "** Texturing has been disabled" );
	                }

	                if ( verbosity > 1 )
	                {
	                    System.out.println( "== Texturing: " + textureMap.getEnable() );
    	                System.out.println( "== MipMapMode: " + textureMap.getMipMapMode() );
    	            }

                    //textureMap.setMinFilter( Texture.BASE_LEVEL_POINT );
                    //textureMap.setMagFilter( Texture.BASE_LEVEL_POINT );
	                mat.setTexture( textureMap );

	                TextureAttributes   texA = mat.getTextureAttributes();

	                if ( texA == null )
	                {
	                    texA = new TextureAttributes();
	                }

	                mat.setTextureAttributes( texA );
	            }
                break;

            case S3D_TEX_VERTS:         // 2D Texture coordinates
                processTextureCoordinates( in );
                break;

            case S3D_NAMED_OBJECT:      // Start of 3DS object
                if ( surfacesCreated == false )
                {
                    createUnsmoothedFaces();
                    prepareForNewObject();
                }

                String objectName = readName( in );

                if ( hiddenObject( objectName ) )
                {
                    skipChunk( in, length - objectName.length() - 1 );
                    System.out.println( "(Skipping hidden object '" + objectName + "')" );
                    break;
                }

                System.out.println( "Processing object '" + objectName + "'" );

                object = new SharedGroup();
                shape  = new Shape3D();
                processChunk( in );

                if ( verbosity > 1 )
                {
                    System.out.println( "== Adding shape to transform group" );
                }

                object.addChild( shape );

                if ( verbosity > 1 )
                {
                    System.out.println( "== Adding object to list of shared objects" );
                }

                objectTable.put( objectName, object );
                break;

            case S3D_POINT_ARRAY:           // Vertex list
                processPointArray( in );
                break;

            case S3D_FACE_ARRAY:            // Face list
                processFaceArray( in );
                break;

            case S3D_MSH_MAT_GROUP:         // Materials used by object
                processMaterial( in );
                break;

            case S3D_SMOOTH_GROUP:          // List of surfaces
                processSmoothGroup( in );
                prepareForNewObject();
                break;

            case S3D_MESH_MATRIX:           // Object transform
                processMeshMatrix( in );
                break;

            case S3D_POINT_FLAG_ARRAY:      // Not much use to us
            case S3D_PIVOT:                 // Unused
                skipChunk( in, length );
                break;

            default:
                if ( verbosity > 0 )
                {
                    System.out.println( "TAG: 0x" + Integer.toHexString( tag ) +
                                        "  LEN: " + length );
                }
                skipChunk( in, length );
                break;
        }
    }


    //
    // Skip over the current chunk, i.e. we are not interested in it.
    //

    void skipChunk( DataInputStream in, int length )
                throws IOException
    {
        int bytesToSkip = length - 6;

        if ( bytesToSkip > 0 )
        {
            in.skipBytes( bytesToSkip );
        }
    }


    //
    // S3D_OBJ_HIDDEN doesn't seem to be set by 3DSMax, so
    // objects that have names beginning with '$' are taken
    // as hidden.
    //

    boolean hiddenObject( String name )
    {
        return name.charAt( 0 ) == '$';
    }


    //
    // Defines an instance of an object.
    //

    void processLink( DataInputStream in )
                throws IOException
    {
        nodeName = readName( in );

        int     flags1  = readUnsignedShort( in );
        int     flags2  = readUnsignedShort( in );
        int     dummy   = readUnsignedShort( in );

        if ( verbosity > 2 )
        {
            System.out.println( "== Link for object '" + nodeName + "': 0x" +
                                Integer.toHexString( flags1 ) + ", 0x" +
                                Integer.toHexString( flags2 ) + ", 0x" +
                                Integer.toHexString( dummy ) );
        }
    }


    //
    // Processed out of curiosity!
    //

    void processNodeID( DataInputStream in )
                throws IOException
    {
        int id = readUnsignedShort( in );

        if ( verbosity > 1 )
        {
            System.out.println( "== NodeID: " + id );
        }
    }


    //
    // Lookup an object by <name>.
    //

    SharedGroup findObject( String name )
    {
        return (SharedGroup)objectTable.get( name );
    }


    //
    // Take the last position specified in this keyframe list
    // as the initial position of the object.
    //

    void processPosTrackTag( DataInputStream in )
                throws IOException
    {
        int dummy, keys, i;

        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        keys  = readUnsignedShort( in );
        dummy = readUnsignedShort( in );

        for ( i = 0; i < keys; i++ )
        {
            dummy = readUnsignedShort( in );
            dummy = readInt( in );

            //
            // Reverse the Y and Z coordinates, negate Z coordinates
            //

            float   x = readFloat( in ), y = readFloat( in ), z = readFloat( in );

            translation = new Vector3f( x, z, -y );
//            translation = new Vector3f( x, y, z );

            if ( verbosity > 0 )
            {
                System.out.println( "    Position: " + translation );
            }
        }
    }


    //
    // Take the last orientation specified in this keyframe list
    // as the initial orientation of the object.
    //

    void processRotTrackTag( DataInputStream in )
                throws IOException
    {
        int dummy, keys, i;

        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        keys  = readUnsignedShort( in );
        dummy = readUnsignedShort( in );

        for ( i = 0; i < keys; i++ )
        {
            dummy = readUnsignedShort( in );
            dummy = readInt( in );
            float   rot = readFloat( in );
            float   x = readFloat( in );
            float   y = readFloat( in );
            float   z = readFloat( in );

            //
            // Convert the orientation between 3DS and
            // Java3D coordinate systems.
            //

            AxisAngle4f axes    = new AxisAngle4f( x, y, z, -rot );
            Matrix4f    m       = new Matrix4f();
            Matrix4f    rm      = new Matrix4f( );

            m.set( axes );
            rm.rotX( (float)-Math.PI / 2 );
            orientation = new Matrix4f();
            orientation.mul( rm, m );

            if ( verbosity > 0 )
            {
                System.out.println( "    Rotation: " + orientation );
            }
        }
    }


    //
    // Take the last scale specified in this keyframe list
    // as the initial scale of the object.  Also take this
    // as the queue to finish instancing the object.
    //

    void processSclTrackTag( DataInputStream in )
                throws IOException
    {
        int dummy, keys, i;

        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        dummy = readUnsignedShort( in );
        keys = readUnsignedShort( in );
        dummy = readUnsignedShort( in );

        for ( i = 0; i < keys; i++ )
        {
            dummy = readUnsignedShort( in );
            dummy = readInt( in );


            //
            // Reverse the Y and Z coordinates
            //

            float   x = readFloat( in ), y = readFloat( in ), z = readFloat( in );

            scale = new Vector3f( x, z, y );

            if ( verbosity > 0 )
            {
                System.out.println( "    Scale   : " + scale );
            }
        }

        if ( hiddenObject( nodeName ) )
        {
            return;
        }

        Matrix4f        m           = new Matrix4f();
        m.set( orientation );
        m.setTranslation( translation );

        Transform3D     transform   = new Transform3D( m );
        TransformGroup  instance    = new TransformGroup( transform );
        SharedGroup     shared      = null;

        if ( instanceName == null )     // Use the node name.
        {
            System.out.println( "Instancing '" + nodeName + "'" );
            instanceTable.put( nodeName, instance );

⌨️ 快捷键说明

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