• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JavaFX 3D load model

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to create a 3D model in Blender or download one and import it into JavaFX.  I can't seem to find any detailed tutorials.  I found this website:  http://www.interactivemesh.org/models/jfx3dimporter.html and downloaded the files but I have no idea how to use them.  I found this Youtube video:  
but he doesn't explain how to install it or where to put the files.
I am using IntelliJ Idea with Maven.
 
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to try using that jar, try looking into configuring Maven to use local jar files. You can drop the jar somewhere in your project and should be able to get Maven to use it as a local dependency.
 
Travis Kern
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see in the video he has the file in a folder named 'lib' so I put it there.
I added this to my pom.xml file:

In the Maven panel on the right it's listed as a dependency.
But when I use the class ObjModelImporter in my code it says "Cannot resolve symbol 'ObjModelImporter'".
He doesn't have any import statement for it.

I found these instructions.

I tried step 1 by typing this command in the terminal:

mvn install:install-file -Dfile="D:\My Documents\Programming\Java\LoadBuilder\lib\jimObjModelImporterJ
FX.jar" -DgroupId=com.interactivemesh -DartifactId=jimObjModelImporterJFX -Dversion=1.0 -Dpackaging=jar

but I got a build error:

[ERROR] Unknown lifecycle phase ".interactivemesh". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-
group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: pre-clean, clean, post-clean, validate, initialize, generate-
sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-r
esources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-inte
gration-test, verify, install, deploy, pre-site, site, post-site, site-deploy. ->

Not sure what to do.
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The import must've been auto added when he used autocomplete? I guess the import is just above the blank line at the top when he scrolled up later.

The step 1 at that link is doing something entirely different: putting the jar into your a local Maven repository. That could work too but it's a bit overkill here.

-You can refer to jars locally on your file system (what xml using the /lib directory should be doing)
-You can put a jar in your local repository cache (step 1 link, the mvn install stuff)
-Maven can pull jars from remote repositories like Maven Central (I doubt this jar is there)

I don't think I'd bother with the local repo cache method.

Where is your /lib directory located? At the top of the project where pom.xml is?

Which IDE are you using? If the jar is added correctly, you should be able to find the jar somewhere in your project's references. You can even browse it and should be able to find that class you need to verify that the jar contains it.

If you're using Eclipse, try right clicking the project folder, and select Maven->Update (or something like that). I prefer IntelliJ so Eclipse is harder to remember but I think you need to do an update like that manually in Eclipse to get Maven to see changes.
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hopefully one of the others will stop by with some Maven ideas, most of my experience is with Ant/Gradle. But I think it might just be that you need to force that Maven Update.

IntelliJ + Gradle warns you and gives you a little refresh button that it's hard to miss.
 
Travis Kern
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the lib folder is at the top at the same level as the pom.xml.

I am using IntelliJ.

I added this import statement:


IntelliJ gave me some kind of error and suggested it add  "requires something...blah...blah so I clicked on it and now I don't get the 'Can't resolve symbol' error.  However, The model still doesn't load.  I get this error message:
ObjModelImporter read(URL url) : url is null !

I have this method which is the same as the video:


and the call:


box.obj is just a simple box created in Blender and exported as a .obj file and placed in the resources folder.
This is the output from the print statement:
file:/D:/My%20Documents/Programming/Java/LoadBuilder/target/classes/box.obj

 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new error is a good sign, now it should just need a valid model path.

For the new error, I think the problem is probably where you have the obj file. We could just switch out to using an absolute disk path for your purposes (usually easier than messing with resources in the jar like this), but let's see if we can get the bundled approach working first.

Is the obj file inside the same package as the class this code is in? (Meaning basically, the same folder structure.)
From your last line, it looks like it's at the top level or 'default' package.

The JavaDoc for this is perhaps not the clearest, but always worth checking first, in this case for Class.getResource():
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String-

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:
       modified_package_name/name

   Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').



You don't have a leading /, so the 2nd case applies.

In the last line, "this object" is the Class. For your code, that's just the same Class this code is in. So "modified_package_name" should be the package name of the class. And "name" is the resource name you passed in, "box.obj".

So long story short, you need to get box.obj into the same directory path as your main class. But since it's a resource and not code, it should be under resources, not java. (I know, packages are kind of a strange concept.)

So your project structure would be:

src/main/java/whateverPackage/yourMainClassIsIn/YourMainClass.java
src/main/resources/whateverPackage/yourMainClassIsIn/box.obj

If you're still unsure and not getting it to work, posting complete code of your class might help clear up any confusion. Even better, with a screen capture of your project with the folders open showing the directory structure. Maven/Gradle project file structures are rather brutal to communicate without a visual, especially when we have to use placeholder names in examples.
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might help. If your package is com.coderanch, your main class is in Main3.java, your obj file should go the same place as the 3 files this shows at the bottom.

Hopefully this is clearer. If I'm not mistaken I believe it'll find the obj there.

 
Travis Kern
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added the '/' to the start of the path.  Now I don't get any errors and my program runs but I still don't see the model.  I can see the other models that the program creates programmatically.  I tried it with my own models and with downloaded obj models.
Image-1.jpg
File structure
File structure
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so you went the full package path route with the leading slash. I think that looks good.

My Java 3D experience is using different libraries than JFX's built in 3D, so I don't know how this API behaves, but I can give a few guesses. If you can post a more complete example I might be able to get it running locally to investigate.

It might be working just fine, but maybe the scale is off?

Sometimes it's really annoying to get things in front of the camera and at the right scale in 3D. It could be that the box is way too large and the camera is inside it, or it's way too small, or it's just outside the view frustum, behind the camera. From your camera translations, maybe the cams are inside the box or pointed in the wrong direction.

Another possibility - do you need to add a Material to the box like you did with the other objects? It might render fine with some default material if one isn't given, but maybe not.

I think you are close to getting it working but maybe just need to tweak one or more of those things.
 
Travis Kern
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I downloaded the scooter model from the tutorial and used it but I can't see it either.  I put a print statement in the load method and I can see all the MeshViews (parts of the scooter) printed so I know they are getting loaded.
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's not a camera/location thing like I guessed above, it might be failing to render due to lacking a material. That scooter obj file refers to a material file (you can open it up, it's just a text format):

mtllib Scooter-smgrps.mtl
v 0.0952185 0.268222 -0.3379495
v 0.0997055 0.266254 -0.3338705
v 0.0997055 0.263457 -0.3370615
v 0.0997055 0.268613 -0.3303425
...

(All the "v" lines are vertices, and then later in the file are "f" lines for faces! Just a fun fact about that format but something you'll want to learn about if you continue in 3D dev.)

Put that .mtl file in the same location as the scooter obj file. It's probably looking in the same directory for that material file, since his code doesn't load the mtl explicitly.

I would also play around with the camera's translation. If you move it the wrong direction, or don't move it back enough, it could just be pointed at nothing.
 
Travis Kern
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the objects are added to the Group named group3D which I can spin around with the mouse and look at in any direction.  I can also move the camera on the Z-axis with the scroll wheel and move the camera left , right, up, down with the arrow keys.
The mtl file is in the same location as the obj.
 
Travis Kern
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the code for the tutorial from Github and used it in a new project and it worked.  He is adding the model group to the project's main Scene but I am adding it to a SubScene which is added to a Pane in the main Scene.  I don't know if that's the cause.
 
Travis Kern
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used to see the size of it and it was really small, like about 1 unit.  The rest of my objects are between 100 and 600 units.  So I scaled it up using:

I still don't see it.

I also used

It shows it is added to group3D.

I also used this to make sure it was at the center:

It's at 0,0,0.
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you're seeing, it's difficult to troubleshoot 3D scenes in these situations.

At this point you might need to post a working example project including the resource files you're using. (Like a GitHub project, or something. Or at least complete source.) I don't think anyone here has JFX 3D experience (myself included) to make educated guesses as to what else might be wrong.
 
Travis Kern
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the problem.  My program allows the user to drag boxes onto a truck area.  I have a function that redraws all of these boxes.  This function is called at the start of the program.  Before it redraws them, it loops through all the objects in the scene and if it isn't a truck part it deletes it.  I forgot to add the obj model to the 'if' statement so it was putting it in the scene and deleting it right away.  I also had to scale it up by 100x.

Thanks for your help
 
Lou Hamers
Bartender
Posts: 322
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha, cool. No problem glad I could assist some.
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic