📄 load3ds.java
字号:
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 );
}
else
{
System.out.println( "Instancing '" + instanceName + "' (->'" +
nodeName + "')" );
instanceTable.put( instanceName, instance );
}
shared = findObject( nodeName );
if ( shared == null )
{
throw new IOException( "Can't locate referenced object." );
}
Link link = new Link( shared );
instance.addChild( link );
root.addChild( instance );
instanceName = null;
}
//
// Processed out of curiosity.
//
void processMasterScale( RandomAccessFile in )
throws IOException
{
float scale = readFloat( in );
if ( verbosity > 2 )
{
System.out.println( "== Master scale: " + scale );
}
}
//
// Read in the list of vertices.
//
void processPointArray( RandomAccessFile in )
throws IOException
{
int i;
noofVertices = readUnsignedShort( in );
vertices = new Point3f[noofVertices];
sharedFaces = new Vector[noofVertices];
System.out.println( " " + noofVertices + " vertices." );
totalVertices += noofVertices;
for ( i = 0; i < noofVertices; i++ )
{
vertices[i] = new Point3f( readFloat( in ), readFloat( in ),
readFloat( in ) );
/*
// Reverse the Y and Z coordinates, negate Z coordinates
float temp = vertices[i].z;
vertices[i].z = -vertices[i].y;
vertices[i].y = temp;
*/
sharedFaces[i] = new Vector(); // Filled in processFaceArray()
}
}
//
// Read in the list of polygons and allocate a TriangleArray
// big enough to contain them. Don't fill it in yet though,
// we want to one surface at a time.
//
void processFaceArray( RandomAccessFile in )
throws IOException
{
int i;
int vertexFormat = GeometryArray.COORDINATES | GeometryArray.NORMALS;
if ( textureCoords != null )
{
if ( verbosity > 1 )
{
System.out.println( " Object is TEXTURED" );
}
vertexFormat |= GeometryArray.TEXTURE_COORDINATE_2;
}
noofFaces = readUnsignedShort( in );
faces = new Face[noofFaces];
geometry = new TriangleArray( noofFaces * 3, vertexFormat );
System.out.println( " " + noofFaces + " faces." );
totalPolygons += noofFaces;
for ( i = 0; i < noofFaces; i++ )
{
int a = readUnsignedShort( in );
int b = readUnsignedShort( in );
int c = readUnsignedShort( in );
int flags = readUnsignedShort( in );
faces[i] = new Face( a, b, c );
}
if ( verbosity > 1 )
{
System.out.println( "== Adding geometry to shape" );
}
shape.setGeometry( geometry );
}
//
// In 3DS a face is constructed by listing vertices in
// anti-clockwise order.
//
Vector3f calculateFaceNormal( int a, int b, int c )
{
Vector3f vertexA = new Vector3f( vertices[a].x, vertices[a].y,
vertices[a].z );
Vector3f vertexB = new Vector3f( vertices[b].x, vertices[b].y,
vertices[b].z );
Vector3f vertexC = new Vector3f( vertices[c].x, vertices[c].y,
vertices[c].z );
Vector3f normal = new Vector3f();
vertexB.sub( vertexA );
vertexC.sub( vertexA );
normal.cross( vertexB, vertexC );
normal.normalize();
return normal;
}
//
// Read in the transformation matrix and inverse transform
// the vertex coordinates so they are back where they were
// when 3DS initially created the object. They are
// transformed back again with the information found in
// the position, rotation and scale keyframe tracks.
//
void processMeshMatrix( RandomAccessFile in )
throws IOException
{
Matrix4f m = new Matrix4f();
int x, y;
for ( y = 0; y < 4; y++ )
{
for ( x = 0; x < 3; x++ )
{
if ( y == 3 )
{
m.setElement( x, y, readFloat( in ) );
}
else
{
m.setElement( x, y, readFloat( in ) );
}
}
}
m.setElement( 3, 3, 1.0f );
float transY = m.getElement( 1, 3 );
float transZ = m.getElement( 2, 3 );
/*
//
// Reverse the Y and Z coordinates, negate Z coordinates
//
// m.setElement( 1, 3, transZ );
// m.setElement( 2, 3, -transY );
*/
Transform3D t = new Transform3D( m );
if ( verbosity > 1 )
{
System.out.println( " Transform: " + t );
}
t.invert();
for ( x = 0; x < noofVertices; x++ )
{
t.transform( vertices[x] );
}
}
//
// Associate material with the object.
//
void processMaterial( RandomAccessFile in )
throws IOException
{
String name = readName( in );
Appearance m = (Appearance)mats.get( name );
if ( m == null )
{
System.err.println( "** Can't find referenced material" );
return;
}
System.out.println( " Attaching material '" + name + "'" );
if ( verbosity > 0 )
{
System.out.println( " " + m.getMaterial() );
}
shape.setAppearance( m );
int faceCount = readUnsignedShort( in );
int i;
for ( i = 0; i < faceCount; i++ )
{
int dummy = readUnsignedShort( in );
}
}
//
// Contains a list of smoothed surfaces. Construct each surface
// at a time, specifying vertex normals and texture coordinates
// (if present).
//
void processSmoothGroup( RandomAccessFile in )
throws IOException
{
double log2 = Math.log( 2 );
int i;
surfaces = new Surface[MAX_SURFACES];
for ( i = 0; i < MAX_SURFACES; i++ )
{
surfaces[i] = new Surface();
}
for ( i = 0; i < noofFaces; i++ )
{
int group = readInt( in );
long b = 0x1;
int index;
for ( index = 0; index < 32; index++ )
{
if ( (group & b) > 0 )
{
break;
}
}
//System.out.println( "== group " + Long.toHexString( group ) + ", index = " + index );
faces[i].group = (int)group;
surfaces[index].add( faces[i] );
}
int surface;
i = 0;
for ( surface = 0; surface < MAX_SURFACES; surface++ )
{
if ( verbosity > 1 && surfaces[surface].noofFaces() > 0 )
{
System.out.println( " Constructing surface " + surface );
}
Enumeration iter = surfaces[surface].faces();
while ( iter.hasMoreElements() )
{
Face f = (Face)iter.nextElement();
Vector3f normalA = calculateVertexNormal( f.a, f, surfaces[surface] );
Vector3f normalB = calculateVertexNormal( f.b, f, surfaces[surface] );
Vector3f normalC = calculateVertexNormal( f.c, f, surfaces[surface] );
//System.out.println( "Face [" + f.a + "] " + f.a() + ", [" + f.b + "] " +
// f.b() + ", [" + f.c + "] " + f.c() );
//System.out.println( "Norm " + normalA + ", " + normalB + ", " +
// normalC );
geometry.setCoordinate( i * 3, f.a() );
geometry.setCoordinate( (i * 3) + 1, f.b() );
geometry.setCoordinate( (i * 3) + 2, f.c() );
geometry.setNormal( i * 3, normalA );
geometry.setNormal( (i * 3) + 1, normalB );
geometry.setNormal( (i * 3) + 2, normalC );
if ( textureCoords != null )
{
//System.out.println( "== Tex Coords: " + textureCoords[f.a] +
// ", " + textureCoords[f.b] + ", " +
// textureCoords[f.c] );
geometry.setTextureCoordinate( i * 3, textureCoords[f.a] );
geometry.setTextureCoordinate( (i * 3) + 1, textureCoords[f.b] );
geometry.setTextureCoordinate( (i * 3) + 2, textureCoords[f.c] );
}
i++;
}
}
surfacesCreated = true;
}
//
// Fill the TriangleArray with the raw polygon information,
// don't calculate vertex normals but do give texture coordinates
// if present.
//
void createUnsmoothedFaces()
{
int i;
for ( i = 0; i < noofFaces; i++ )
{
Face f = faces[i];
//System.out.println( "Face [" + f.a + "], [" + f.b + "], [" + f.c + "]" );
geometry.setCoordinate( i * 3, f.a() );
geometry.setCoordinate( (i * 3) + 1, f.b() );
geometry.setCoordinate( (i * 3) + 2, f.c() );
geometry.setNormal( i * 3, f.normal );
geometry.setNormal( (i * 3) + 1, f.normal );
geometry.setNormal( (i * 3) + 2, f.normal );
if ( textureCoords != null )
{
//System.out.println( "== Tex Coords: " + textureCoords[f.a] +
// ", " + textureCoords[f.b] + ", " +
// textureCoords[f.c] );
geometry.setTextureCoordinate( i * 3, textureCoords[f.a] );
geometry.setTextureCoordinate( (i * 3) + 1, textureCoords[f.b] );
geometry.setTextureCoordinate( (i * 3) + 2, textureCoords[f.c] );
}
}
}
//
// Checks if <vertex> is used by <face>.
//
boolean sharesVertex( Face face, int vertex )
{
return face.a == vertex || face.b == vertex || face.c == vertex;
}
//
// Calculates a normalised vertex normal for <vertex> in
// <thisFace> within <surface>.
//
Vector3f calculateVertexNormal( int vertex, Face thisFace, Surface surface )
{
Enumeration otherFaces = surface.faces();
Vector3f normal = new Vector3f( thisFace.normal );
int noofNormals = 1;
while ( otherFaces.hasMoreElements() )
{
Face otherFace = (Face)otherFaces.nextElement();
if ( sharesVertex( otherFace, vertex ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -