material.setEmission(new float[]{ .0f, .0f, .0f, 1f });
material.setSpecular(new float[]{ 0.9f, 0.9f, 0.9f, 1f }); // almost white: very reflective
material.setShininess(110);// 0=no shine, 127=max shine
//Group used to move to the screen center and to put the mesh group in
MTComponent group1 = new MTComponent(mtApplication);
//Create a group and set the light for the whole mesh group ->better for performance than setting light to more comps
final MTComponent meshGroup = new MTComponent(mtApplication, "Mesh group");
meshGroup.setLight(light);
//Desired position for the meshes to appear at
Vector3D destinationPosition = new Vector3D(mtApplication.width/2, mtApplication.height/2, 50);
//Desired scale for the meshes
float destinationScale = mtApplication.width*0.85f;
//Load the meshes with the ModelImporterFactory (A file can contain more than 1 mesh)
//Loads 3ds model
MTTriangleMesh[] meshes = ModelImporterFactory.loadModel(mtApplication, modelsPath + "jazz_Obj" + MTApplication.separator + "honda_jazz.obj", 180, true, false );
//Get the biggest mesh in the group to use as a reference for setting the position/scale
final MTTriangleMesh biggestMesh = this.getBiggestMesh(meshes);
Vector3D translationToScreenCenter = new Vector3D(destinationPosition);
translationToScreenCenter.subtractLocal(biggestMesh.getCenterPointGlobal());
Vector3D scalingPoint = new Vector3D(biggestMesh.getCenterPointGlobal());
float biggestWidth = biggestMesh.getWidthXY(TransformSpace.GLOBAL);
float scale = destinationScale/biggestWidth;
//Move the group the the desired position
group1.scaleGlobal(scale, scale, scale, scalingPoint);
group1.translateGlobal(translationToScreenCenter);
this.getCanvas().addChild(group1);
group1.addChild(meshGroup);
//Inverts the normals, if they are calculated pointing inside of the mesh instead of outside
boolean invertNormals = true;
for (int i = 0; i < meshes.length; i++) {
MTTriangleMesh mesh = meshes[i];
meshGroup.addChild(mesh);
mesh.unregisterAllInputProcessors(); //Clear previously registered input processors
mesh.setPickable(true);
if (invertNormals){
Vector3D[] normals = mesh.getGeometryInfo().getNormals();
for (int j = 0; j < normals.length; j++) {
Vector3D vector3d = normals[j];
vector3d.scaleLocal(-1);
}
mesh.getGeometryInfo().setNormals(mesh.getGeometryInfo().getNormals(), mesh.isUseDirectGL(), mesh.isUseVBOs());
}
//If the mesh has more than 20 vertices, use a display list for faster rendering
if (mesh.getVertexCount() > 20)
mesh.generateAndUseDisplayLists();
//Set the material to the mesh (determines the reaction to the lightning)
if (mesh.getMaterial() == null)
mesh.setMaterial(material);
mesh.setDrawNormals(false);
}
//Register arcball gesture manipulation to the whole mesh-group
meshGroup.setComposite(true); //-> Group gets picked instead of its children
meshGroup.registerInputProcessor(new ArcballProcessor(mtApplication, biggestMesh));
meshGroup.addGestureListener(ArcballProcessor.class, new IGestureEventListener(){
//@Override
public boolean processGestureEvent(MTGestureEvent ge) {
ArcBallGestureEvent aEvt = (ArcBallGestureEvent)ge;
meshGroup.transform(aEvt.getTransformationMatrix());
return false;
}
});
meshGroup.registerInputProcessor(new ScaleProcessor(mtApplication));
meshGroup.addGestureListener(ScaleProcessor.class, new IGestureEventListener(){
//@Override
public boolean processGestureEvent(MTGestureEvent ge) {
ScaleEvent se = (ScaleEvent)ge;
meshGroup.scaleGlobal(se.getScaleFactorX(), se.getScaleFactorY(), se.getScaleFactorX(), biggestMesh.getCenterPointGlobal());
return false;
}
});
meshGroup.registerInputProcessor(new RotateProcessor(mtApplication));
meshGroup.addGestureListener(RotateProcessor.class, new DefaultRotateAction());
}