|
import com.jme.scene.*;
|
import com.jme.math.*;
|
import com.jme.scene.state.MaterialState;
|
import com.jme.scene.state.TextureState;
|
import com.jme.renderer.ColorRGBA;
|
|
import javax.vecmath.*;
|
//import java.awt.Color;
|
//import java.awt.color.ColorSpace;
|
|
class cJME
|
{
|
static boolean gennormals = false; // can be done through UI
|
static boolean trim = true;
|
static boolean stripify = false; // true;
|
static boolean reduction34 = false;
|
static boolean genUV = false;
|
static boolean mergeAttributes = true;
|
|
Matrix4d tempmat = new Matrix4d();
|
Matrix4d tempmatR = new Matrix4d();
|
Matrix4d tempmatS = new Matrix4d();
|
Matrix4d tempmatT = new Matrix4d();
|
Quat4d quat = new Quat4d();
|
javax.vecmath.Vector3d trans = new javax.vecmath.Vector3d();
|
|
Object3D Read(Spatial s)
|
{
|
// System.out.println("Parsing: " + s);
|
|
if (s == null)
|
return new Object3D("NULL");
|
|
if (s instanceof Node)
|
return Read((Node)s);
|
if (s instanceof com.jme.scene.TriMesh)
|
return Read((com.jme.scene.TriMesh)s);
|
//if (s instanceof SharedNode)
|
//return Read(((SharedNode)s).getChild());
|
|
System.out.println("Unknown node class : " + s.getClass().getSimpleName());
|
|
return new Object3D(s.getClass().getName());
|
}
|
|
public int count = 0;
|
|
void ReadTransform(Object3D c, Spatial g)
|
{
|
tempmat.setIdentity();
|
tempmatR.setIdentity();
|
tempmatS.setIdentity();
|
tempmatT.setIdentity();
|
|
Quaternion q = g.getLocalRotation();
|
quat.set(q.x, q.y, q.z, q.w);
|
com.jme.math.Vector3f s = g.getLocalScale();
|
com.jme.math.Vector3f t = g.getLocalTranslation();
|
trans.set(t.x, t.y, t.z);
|
|
tempmatR.setRotation(quat);
|
//tempmat.setScale(s.x);
|
tempmatS.setElement(0,0, s.x);
|
tempmatS.setElement(1,1, s.y);
|
tempmatS.setElement(2,2, s.z);
|
tempmatT.setTranslation(trans);
|
|
// System.out.println("R = " + quat);
|
// System.out.println("S = " + s);
|
// System.out.println("T = " + trans);
|
|
switch(count%6)
|
{
|
case 0:
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatS);
|
// System.out.println("TRS");
|
break;
|
|
case 1:
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatR);
|
// System.out.println("TSR");
|
break;
|
|
case 2:
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatT);
|
// System.out.println("RST");
|
break;
|
|
case 3:
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatS);
|
// System.out.println("RTS");
|
break;
|
|
case 4:
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatT);
|
tempmat.mul(tempmatR);
|
// System.out.println("STR");
|
break;
|
|
case 5:
|
tempmat.mul(tempmatS);
|
tempmat.mul(tempmatR);
|
tempmat.mul(tempmatT);
|
// System.out.println("SRT");
|
break;
|
}
|
|
if (c.toParent == null)
|
{
|
c.toParent = LA.newMatrix();
|
c.fromParent = LA.newMatrix();
|
}
|
|
if (count < 6)
|
{
|
FillMatrix(tempmat, c.toParent);
|
LA.matInvert(c.toParent, c.fromParent);
|
}
|
else
|
{
|
FillMatrix(tempmat, c.fromParent);
|
LA.matInvert(c.toParent, c.toParent);
|
}
|
}
|
|
void ReadMaterial(Object3D o, Spatial s)
|
{
|
//Object texture = s.getRenderState(com.jme.scene.state.RenderState.RS_TEXTURE);
|
o.material = Read((MaterialState)s.getRenderState(com.jme.scene.state.RenderState.RS_MATERIAL));
|
}
|
|
void ReadTexture(Object3D o, Spatial s)
|
{
|
o.GetTextures().name = ":"; //
|
Read((TextureState)s.getRenderState(com.jme.scene.state.RenderState.RS_TEXTURE));
|
}
|
|
void ReadNode(Object3D c, Spatial g)
|
{
|
ReadTransform(c, g);
|
ReadMaterial(c, g);
|
ReadTexture(c, g);
|
}
|
|
Object3D Read(Node g)
|
{
|
Composite c = new Composite(g.getClass().getName());
|
|
ReadNode(c, g);
|
|
for (int i=0; i<g.getQuantity(); i++)
|
{
|
c.addChild(Read(g.getChild(i)));
|
}
|
|
return c;
|
}
|
|
float[] buffer = new float[4];
|
|
cMaterial Read(MaterialState mat)
|
{
|
if (mat == null)
|
return null;
|
|
cMaterial cmat = new cMaterial();
|
|
ColorRGBA col = mat.getDiffuse();
|
buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = col.a;
|
//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;
|
|
col = mat.getSpecular();
|
buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = col.a;
|
|
cmat.specular = buffer[0]+0.001f;
|
// No way to get the metalness
|
|
col = mat.getAmbient();
|
buffer[0] = col.r; buffer[1] = col.g; buffer[2] = col.b; buffer[3] = col.a;
|
|
cmat.ambient = buffer[0]+0.001f;
|
|
cmat.shininess = /*1/*/(Math.max(mat.getShininess(),0)+0.001f);
|
|
return cmat;
|
}
|
|
cTexture Read(TextureState tex)
|
{
|
if (tex == null)
|
return null;
|
|
if( tex.getTexture() != null)
|
{
|
System.out.println(tex.getTexture().getImageLocation());
|
System.out.println(tex.getTexture().getImage());
|
System.out.println(tex.getTexture().getTextureKey());
|
}
|
else
|
System.out.println("NO TEXTURE");
|
|
cTexture ctex = null; // new cTexture();
|
|
return ctex;
|
}
|
|
Vertex v = new Vertex(true);
|
|
Object3D Read(com.jme.scene.TriMesh g)
|
{
|
Object3D result = new Object3D(g.getClass().getSimpleName());
|
|
ReadNode(result, g);
|
|
Vertex.normalmode = true;
|
//Vertex.normalmode = cJME.gennormals;
|
|
int tris = g.getTriangleCount();
|
|
System.out.println("#triangles = " + tris);
|
//int faces = g.getVertexCount()/3;
|
//int verts = g.getVertexCount();
|
int faces = tris;
|
int verts = tris*3;
|
|
BoundaryRep bRep = new BoundaryRep(verts, faces);
|
|
boolean cache = true;
|
|
if (!cache)
|
bRep.Trim();
|
|
//float[] coords = bRep.getRawVertices();
|
//float[] normals = bRep.getRawNormals();
|
//float[] uvmap = bRep.getRawUVMap();
|
|
//assert (coords != null);
|
//assert (normals != null);
|
|
/*
|
com.jme.math.Vector3f[] vertices = new com.jme.math.Vector3f[verts];
|
|
for (int i=0; i<verts; i++)
|
{
|
vertices[i] = new com.jme.math.Vector3f();
|
}
|
|
g.getMeshAsTrianglesVertices(0, vertices);
|
*/
|
|
com.jme.math.Triangle[] triangles = new com.jme.math.Triangle[tris];
|
|
for (int i=0; i<tris; i++)
|
{
|
triangles[i] = new com.jme.math.Triangle(
|
new com.jme.math.Vector3f(),
|
new com.jme.math.Vector3f(),
|
new com.jme.math.Vector3f());
|
}
|
|
g.getMeshAsTriangles(0, triangles); // jME1
|
//g.getMeshAsTriangles(/*0,*/ triangles); // jME2
|
|
//tsa.getNormals(normals);
|
|
//float[][] textures = new float[1][];
|
//textures[0] = uvmap;
|
|
//tsa.getTextureCoordinates(textures);
|
|
// jME1
|
java.nio.FloatBuffer texcoords = g.getTextureBuffer(0,0);
|
//TexCoords texcoords = g.getTextureBuffer(0,0); //g.getTextureCoords(0); //.coords;
|
java.nio.FloatBuffer normals = g.getNormalBuffer(0);
|
|
// jME2
|
// java.nio.FloatBuffer texcoords = // g.getTextureBuffer(0,0);
|
// /*TexCoords texcoords =*/ g.getTextureCoords(0).coords;
|
// java.nio.FloatBuffer normals = g.getNormalBuffer(); // 0);
|
|
//System.out.println("#texcoords = " + texcoords.capacity());
|
//System.out.println("#normals = " + normals.capacity());
|
|
if(texcoords != null && texcoords.capacity() != tris*6)
|
texcoords = null;
|
if(normals != null && normals.capacity() != verts*3)
|
normals = null;
|
|
if (cache)
|
{
|
// bbox
|
float minx=Float.MAX_VALUE,miny=Float.MAX_VALUE,minz=Float.MAX_VALUE;
|
float maxx=Float.MIN_VALUE,maxy=Float.MIN_VALUE,maxz=Float.MIN_VALUE;
|
for (int i=0; i<verts; i++)
|
{
|
com.jme.math.Vector3f jv = triangles[i/3].get(i%3);
|
if (minx > jv.x)
|
minx = jv.x;
|
if (miny > jv.y)
|
miny = jv.y;
|
if (minz > jv.z)
|
minz = jv.z;
|
if (maxx < jv.x)
|
maxx = jv.x;
|
if (maxy < jv.y)
|
maxy = jv.y;
|
if (maxz < jv.z)
|
maxz = jv.z;
|
}
|
|
float orgx = (minx+maxx)/2;
|
float orgy = miny;
|
float orgz = (minz+maxz)/2;
|
|
// Vertices
|
int i3 = 0;
|
int i2 = 0;
|
for (int i=0; i<verts; i++, i3+=3, i2+=2)
|
{
|
com.jme.math.Vector3f jv = triangles[i/3].get(i%3);
|
v.set(jv.x-orgx, jv.y-orgy, jv.z-orgz);
|
//v.set(jv.x - 94, jv.y - 90, jv.z + 540);
|
if (normals != null)
|
v.norm.set(normals.get(i3), normals.get(i3+1), normals.get(i3+2));
|
if (texcoords != null)
|
{
|
v.s = texcoords.get(i2);
|
v.t = texcoords.get(i2+1);
|
}
|
Vertex in = bRep.GetCache(v);
|
if (in == null)
|
{
|
v.index = -1;
|
bRep.Remember(new Vertex(v));
|
}
|
else
|
{
|
bRep.AddVertex(in);
|
}
|
}
|
|
LA.matTranslate(result.fromParent, -orgx,-orgy,-orgz);
|
LA.matTranslate(result.toParent, orgx,orgy,orgz);
|
}
|
|
// 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 == verts;
|
|
if (cache)
|
bRep.Trim(trim, //true, true, true); //
|
normals==null || gennormals,false,stripify,genUV);
|
|
result.bRep = bRep;
|
|
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.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();
|
|
return bRep;
|
}
|
|
static BoundaryRep Read(TriangleFanArray tsa)
|
{
|
BoundaryRep bRep = new BoundaryRep();
|
|
return bRep;
|
}
|
|
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.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();
|
|
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;
|
}
|
*/
|
|
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);
|
}
|
}
|