Skip to Main Content

import 3D model using javafx

elattarAug 14 2016 — edited Aug 21 2016

I'm currently working on importing a 3D model (obj file) using JavaFX.

first, i read the obj file and save its content in float arrays (vertices, normals, textures) and integer array for (faces).

for example:

In vertices the file contains:

v 3.227124 -0.065127 -1.000000

v 3.227124 -0.065127 1.000000

i save them in array like that: [3.227124,-0.065127,-1.000000,3.227124,-0.065127,1.000000].

and same for textures and normals.

while for faces: the file contains:

f 509/413/144 91/405/139 94/409/143

f 508/410/3 95/408/142 96/407/141

i save them in integer array as [509,413,144,91,405,139,94,409,143,508,410,3,95,408,142,96,407,141]

Now, i used javafx to draw the model using trianglemshes, but unfortunately the scene is empty and nothing is drawn.

would anybody help me to solve this problem and suggest for me a solution or links for a tutorial or a book.

This is the Code:

public class TriangleMeshes extends Application
{
  
private PerspectiveCamera camera;
  
private final double sceneWidth = 1000;
  
private final double sceneHeight = 700;
  
private double scenex, sceney = 0;
  
private double fixedXAngle, fixedYAngle = 0;
  
private final DoubleProperty angleX = new SimpleDoubleProperty(0);
  
private final DoubleProperty angleY = new SimpleDoubleProperty(0);
  
private double anchorAngleX = 0;
  
private double anchorAngleY = 0;

  
@Override
  
public void start(Stage primaryStage) throws Exception
  
{
  
// Build the Scene and Camera
  
Group sceneRoot = new Group();
  
Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight);
  
//scene.setFill(Color.BLACK);

  
PerspectiveCamera camera = new PerspectiveCamera(true);
  camera
.setNearClip(0.1);
  camera
.setFarClip(10000.0);
  camera
.setTranslateZ(-1000);
  scene
.setCamera(camera);

  primaryStage
.setTitle("Model 3D Object");
  primaryStage
.setScene(scene);
  primaryStage
.show();

  
// Create the object with a color and add it to the Scene
  
Group obj = buildObject(Color.RED, false, false);
  obj
.setRotationAxis(Rotate.Y_AXIS);
  obj
.setRotate(45);
  obj
.setTranslateX(-100);

  
Group objectGroup = new Group(obj);

  
// Add a Mouse Handler for Rotation
  
Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
  
Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
  objectGroup
.getTransforms().addAll(xRotate,yRotate);

  
// Use Binding so your rotation doesn't have to be recreated
  xRotate
.angleProperty().bind(angleX);
  yRotate
.angleProperty().bind(angleY);

  
// Tracking mouse movements only when a button is pressed
  scene
.setOnMousePressed(event -> {
  scenex
= event.getSceneX();
  sceney
= event.getSceneY();
  anchorAngleX
= angleX.get();
  anchorAngleY
= angleY.get();
  
});

  
// change angle when the button is pressed
  scene
.setOnMouseDragged(event -> {
  angleX
.set(anchorAngleX - (scenex - event.getSceneY()));
  angleY
.set(anchorAngleY + sceney - event.getSceneX());
  
});

  sceneRoot
.getChildren().addAll(objectGroup);
  
}
  
private Group buildObject(Color color, boolean ambient, boolean fill) throws IOException
  
{
  
// Create object from ObjLoader class to create model containing vertices, normals, textures, and faces
  
ObjLoader ol = new ObjLoader();
  
Model m = ol.loadModel("stall");
  
final TriangleMesh mesh = new TriangleMesh();

  mesh
.getPoints().addAll(m.getVertices()); // [3.227124,-0.065127,-1.000000,3.227124,-0.065127,1.000000, ...]
  mesh
.getTexCoords().addAll(m.getTextures());
  mesh
.getFaces().addAll(m.getFaces()); // [509,413,144,91,405, ...]
  
//mesh.getNormals().addAll(m.getNormals());

  
MeshView meshView = new MeshView(mesh);
  meshView
.setDrawMode(DrawMode.LINE);
  meshView
.setCullFace(CullFace.BACK);

  
Group objGroup = new Group();
  objGroup
.getChildren().add(meshView);

  
if(null != color)
  
{
  
PhongMaterial material = new PhongMaterial(color);
  meshView
.setMaterial(material);
  
}
  
if(ambient)
  
{
  
AmbientLight light = new AmbientLight(Color.WHITE);
  light
.getScope().add(meshView);
  objGroup
.getChildren().add(light);
  
}
  
if(fill)
  
{
  meshView
.setDrawMode(DrawMode.FILL);
  
}

  
return objGroup;
  
}

  
public static void main(String[] args)
  
{
  launch
(args);
  
}

}

Thanks in advance.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Sep 18 2016
Added on Aug 14 2016
1 comment
1,655 views