Reputation: 21
So I've been trying to play around with Java 3D and, recently, I've been playing around with importing outside 3D models into a program. At this point, I can get the model into the program as an OBJ file, but for whatever reason, the program won't load the corresponding material file and I don't know if the problem is my coding or if the file just wasn't exported properly.
This is the code I wrote:
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import com.sun.j3d.loaders.objectfile.*;
import com.sun.j3d.loaders.Scene;
import java.awt.Color;
import javax.vecmath.*;
public class ModelLoadingTest {
public static void main(String[] args) {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup scene = new BranchGroup();
ObjectFile loader = new ObjectFile(ObjectFile.LOAD_ALL);
loader.setFlags(ObjectFile.RESIZE);
Scene modelScene = null;
try{
modelScene = loader.load("paintedcar.obj");
}
catch(Exception e){
}
DirectionalLight lighting = new DirectionalLight(new Color3f(Color.WHITE), new Vector3f(0f, 0f, -1f));
lighting.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 1.0), 100));
scene.addChild(modelScene.getSceneGroup());
scene.addChild(lighting);
universe.addBranchGraph(scene);
universe.getViewingPlatform().setNominalViewingTransform();
}
}
If it helps, the models I'm testing with were made in Maya and exported as Wavefront files.
Upvotes: 2
Views: 514
Reputation: 130
... You are not loading the texture what so ever in your code. The matirl file is not coded in a obj file, you need to import it as a texture, You can do this the same as you would for a sphere except when you assign the texture to the mesh. When you assign it you need to use
"mesh name".setAppearance("your Appearance name");
for example
model.setAppearance(ap);
Upvotes: 1