//import org.j3d.renderer.aviatrix3d.loader.*;
|
import org.j3d.aviatrix3d.*;
|
import javax.vecmath.*;
|
import java.awt.Color;
|
import java.awt.color.ColorSpace;
|
|
class cAviatrix
|
{
|
static Matrix4d tempmat = new Matrix4d();
|
|
static Object3D Read(Node s)
|
{
|
if (s == null)
|
return new Object3D("NULL");
|
|
if (s instanceof Group)
|
return Read((Group)s);
|
if (s instanceof SharedGroup)
|
return Read(((SharedGroup)s).getChild(0));
|
if (s instanceof Shape3D)
|
return Read((Shape3D)s);
|
if (s instanceof SharedNode)
|
return Read(((SharedNode)s).getChild());
|
|
System.out.println("Unknown node class : " + s.getClass().getName());
|
//new Exception().printStackTrace();
|
|
return new Object3D(s.getClass().getName());
|
}
|
|
static Object3D Read(Group g)
|
{
|
//if (g instanceof SharedGroup)
|
//return Read(((SharedGroup)g).getChild(0));
|
|
Composite c = new Composite(g.getClass().getName());
|
|
if (g instanceof TransformGroup)
|
{
|
((TransformGroup)g).getTransform(tempmat);
|
if (c.toParent == null)
|
{
|
c.toParent = LA.newMatrix();
|
c.fromParent = LA.newMatrix();
|
}
|
FillMatrix(tempmat, c.toParent);
|
LA.matInvert(c.toParent, c.fromParent);
|
}
|
|
for (int i=0; i<g.numChildren(); i++)
|
{
|
c.addChild(Read(g.getChild(i)));
|
}
|
|
return c;
|
}
|
|
static float[] buffer = new float[4];
|
|
static cMaterial Read(Material mat)
|
{
|
cMaterial cmat = new cMaterial();
|
|
mat.getDiffuseColor(buffer);
|
//Color col = new Color(buffer[0], buffer[1], buffer[2]);
|
//col.getColorComponents(ColorSpace.getInstance(ColorSpace.TYPE_HSV), buffer);
|
cColor.RGBtoHSB(buffer[0], buffer[1], buffer[2], buffer);
|
|
cmat.color = buffer[0]+0.001f;
|
cmat.modulation = buffer[1]+0.001f;
|
cmat.diffuse = buffer[2]+0.001f;
|
|
mat.getSpecularColor(buffer);
|
|
cmat.specular = buffer[0]+0.001f;
|
// No way to get the metalness
|
|
mat.getAmbientColor(buffer);
|
|
cmat.ambient = buffer[0]+0.001f;
|
|
cmat.lightarea = 1/(mat.getShininess()+0.001f);
|
|
return cmat;
|
}
|
|
static BoundaryRep Read(org.j3d.aviatrix3d.Geometry g)
|
{
|
BoundaryRep result = null;
|
|
Vertex.normalmode = true;
|
|
if (g instanceof TriangleStripArray)
|
result = Read((TriangleStripArray)g);
|
//else if (g instanceof TriangleFanArray)
|
//return Read((TriangleFanArray)g);
|
else if (g instanceof TriangleArray)
|
result = Read((TriangleArray)g);
|
else if (g != null)
|
System.out.println("geometry class = " + g.getClass().getName());
|
//assert false;
|
|
Vertex.normalmode = false;
|
|
return result;
|
}
|
|
static BoundaryRep Read(TriangleStripArray tsa)
|
{
|
//System.out.println("TriangleStripArray");
|
BoundaryRep bRep = new BoundaryRep(tsa.getValidVertexCount(),
|
tsa.getValidVertexCount() -
|
tsa.getValidStripCount()*2);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
|
tsa.getVertices(vertices);
|
float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
|
tsa.getNormals(normals);
|
float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
|
|
float[][] textures = new float[1][];
|
textures[0] = uvmap;
|
|
try
|
{
|
tsa.getTextureCoordinates(textures);
|
}
|
catch (Exception e)
|
{
|
System.out.println("MULTI-TEXTURE ???");
|
}
|
|
if (cache)
|
{
|
// Vertices
|
int i3 = 0, i2 = 0;
|
for (int i=0; i<tsa.getValidVertexCount(); i++, i3+=3, i2+=2)
|
{
|
//Vertex v = new Vertex(true);
|
v./*pos.*/set(vertices[i3], vertices[i3+1], vertices[i3+2]);
|
v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
|
//v.s = textures[0][i2];
|
//v.t = textures[0][i2+1];
|
v.s = uvmap[i2];
|
v.t = uvmap[i2+1];
|
Vertex in = bRep.GetCache(v);
|
if (true) // in == null)
|
{
|
v.index = -1;
|
Vertex out = new Vertex(v);
|
//bRep.AddVertex(out);
|
bRep.Remember(out);
|
}
|
else
|
{
|
bRep.AddVertex(in);
|
}
|
}
|
}
|
|
// Faces
|
int[] counts = new int[tsa.getValidStripCount()];
|
tsa.getStripCount(counts);
|
|
int indexF = 0;
|
int indexV = 0;
|
|
for (int i=0; i<tsa.getValidStripCount(); i++)
|
{
|
int count = counts[i];
|
|
assert count >= 3;
|
|
bRep.AddFace(indexV, indexV+1, indexV+2);
|
indexF++;
|
//bRep.faces.addElement(null);
|
//bRep.setFace(indexF++, indexV, indexV+1, indexV+2);
|
indexV += 3;
|
|
for (int j=3; j<count; j++, indexV++)
|
{
|
//bRep.faces.addElement(null);
|
if ((indexF % 2) == 0)
|
bRep.AddFace(indexV-2, indexV-1, indexV);
|
else
|
bRep.AddFace(indexV-2, indexV, indexV-1);
|
|
indexF++;
|
}
|
}
|
|
assert indexF == tsa.getValidVertexCount() - tsa.getValidStripCount()*2;
|
assert indexV == tsa.getValidVertexCount();
|
|
if (cache)
|
bRep.Trim(cJME.trim, cJME.gennormals, false, cJME.stripify, cJME.genUV);
|
|
return bRep;
|
}
|
|
static BoundaryRep Read(TriangleFanArray tsa)
|
{
|
BoundaryRep bRep = new BoundaryRep();
|
|
return bRep;
|
}
|
|
static Vertex v = new Vertex(true);
|
|
static BoundaryRep Read(TriangleArray tsa)
|
{
|
//System.out.println("TriangleArray");
|
int faces = tsa.getValidVertexCount()/3;
|
|
BoundaryRep bRep = new BoundaryRep(tsa.getValidVertexCount(),
|
faces);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
float[] vertices = bRep.getRawVertices(); // new float[tsa.getValidVertexCount()*3];
|
float[] normals = bRep.getRawNormals(); // new float[tsa.getValidVertexCount()*3];
|
float[] uvmap = bRep.getRawUVMap(); // new float[tsa.getValidVertexCount()*2];
|
|
assert (vertices != null);
|
assert (normals != null);
|
try
|
{
|
tsa.getVertices(vertices);
|
tsa.getNormals(normals);
|
|
float[][] textures = new float[1][];
|
textures[0] = uvmap;
|
|
tsa.getTextureCoordinates(textures);
|
}
|
catch (Exception e)
|
{
|
System.out.println("NO VERTEX!");
|
}
|
|
if (cache)
|
{
|
// Vertices
|
int i3 = 0;
|
int i2 = 0;
|
for (int i=0; i<tsa.getValidVertexCount(); i++, i3+=3, i2+=2)
|
{
|
//Vertex v = new Vertex();
|
v./*pos.*/set(vertices[i3], vertices[i3+1], vertices[i3+2]);
|
v.norm.set(normals[i3], normals[i3+1], normals[i3+2]);
|
v.s = uvmap[i2];
|
v.t = uvmap[i2+1];
|
Vertex in = bRep.GetCache(v);
|
if (in == null)
|
{
|
v.index = -1;
|
bRep.Remember(new Vertex(v));
|
}
|
else
|
{
|
bRep.AddVertex(in);
|
}
|
}
|
}
|
|
// Faces
|
int indexF = 0;
|
int indexV = 0;
|
|
for (int i=0; i<faces; i++)
|
{
|
//bRep.faces.addElement(null);
|
bRep.AddFace(indexV, indexV+1, indexV+2);
|
indexF++;
|
|
indexV += 3;
|
}
|
|
assert indexF == faces;
|
assert indexV == tsa.getValidVertexCount();
|
|
if (cache)
|
bRep.Trim(cJME.trim, cJME.gennormals, false, cJME.stripify, cJME.genUV);
|
|
return bRep;
|
}
|
|
static Object3D Read(Shape3D s)
|
{
|
Object3D o = new Object3D(s.getClass().getName());
|
|
Material mat = s.getAppearance().getMaterial();
|
|
if (mat != null)
|
o.material = Read(mat);
|
|
//System.out.println("Geometry = " + s.getGeometry());
|
|
o.bRep = Read(s.getGeometry());
|
|
return o;
|
}
|
|
static void FillMatrix(Matrix4d in, double[][] out)
|
{
|
for (int j=0; j<4; j++)
|
for (int i=0; i<4; i++)
|
out[i][j] = in.getElement(j,i);
|
}
|
}
|