JOGL .x loader

OK, it’s not perfect, beautiful, and probably only works for some models (I haven’t tested it much yet), but at least for one model it works fine, so here it is.

xloader site

The demo should be able to load and display any static .x model, vertex colors aren’t used in the demo and only texture information is implemented for materials (the information is there, I just chose not to implement it for the demo). Let me know if it’s useful to anyway.

You beat me to it. Been working on one of these myself. Will have to try yours in the morning (coz its getting a bit late now!)

How far do you plan to take your loader, as I’ve stated skeleton code and animation is probably as far as I’d go, are you planning to write a 100% complete .x loader (such as support custom templates and such)?

BTW, do you have a .x models you could share for testing purposes, I’m having a hard time finding any on the web. I could convert one but I’m hoping to find one written by a different program in case I’m relying on formatting that may not be 100% reliable. The model provided in my demo was written with gameSpaceLight.

Nice work! Could we have a webstart demo to show us its beauty?

Kev

The formatting is what has given me the most grief really. I have the models from the DX9 SDK download, and I have some models exported from 3d modeller. Its been quite hard to make them compatible. I havent got as far as considering animation. I did find the Irrlicht engine to be a useful reference for a way to write a loader.

This page contains useful information about the .x file format and you may find it useful.

http://astronomy.swin.edu.au/~pbourke/geomformats/directx/

I hadn’t planned to setup a webstart demo since the important part (loading the model) isn’t a visual aspect of the program, but I can set that up.

Webstartable demo, also linked from the site but easier to find here.

I would just like to report that the webstart .xloader works without fault under Mac OS X 10.3.5, Java 1.4.2 Update 1.

Just a quick example of a different type of format for an X file to have. The difference is in how the Materials are referenced. Any suggestions on how to parse something like this welcome!

xof 0303txt 0032

Material LovelyGreenMaterial {
0.588235;0.850980;0.780392;1.000000;;
3.200000;
0.000000;0.000000;0.000000;;
0.000000;0.000000;0.000000;;
}

Frame Box01 {

FrameTransformMatrix {
1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000,0.000000,0.000000,0.000000,0.000000,1.000000;;
}

Mesh {
8;
-25.000000;0.000000;-25.000000;,
25.000000;0.000000;-25.000000;,
-25.000000;0.000000;25.000000;,
25.000000;0.000000;25.000000;,
-25.000000;50.000000;-25.000000;,
25.000000;50.000000;-25.000000;,
-25.000000;50.000000;25.000000;,
25.000000;50.000000;25.000000;;
12;
3;0,3,2;,
3;3,0,1;,
3;4,7,5;,
3;7,4,6;,
3;0,5,1;,
3;5,0,4;,
3;1,7,3;,
3;7,1,5;,
3;3,6,2;,
3;6,3,7;,
3;2,4,0;,
3;4,2,6;;

MeshMaterialList {
1;
12;
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0;
{ LovelyGreenMaterial }
}
}
}

I think just about any variable can be handled like that, where the actual template is defined in some arbitrary location, so you could have something like this…

xof 0303txt 0032

float Zero {
0.00000;
}

Material LovelyGreenMaterial {
0.588235;0.850980;0.780392;1.000000;;
3.200000;
{Zero};{Zero};{Zero};;
{Zero};{Zero};{Zero};;
}

I believe the above is considered OK for a .x file, basically it probably means any time I grab something I expect to be a variable, I should check to see if there is a { or } in it and if so assume it’s a variable reference.

To handle this within the current code of my loader, I would probably create a Vector in XModelData called templates, and whenever I ran into a template (such as Material) in the main parser I would call my parseMaterial function but pass it a null for the object it’d normally store things in (MeshObject in this case), when the object is about to store the new material it’d check if the MeshObject was null and if it was, it’d automatically stick the new object into the templates Vector.

Back to finding a { or } when I expect a variable, when this happens I can go through the list of template objects looking for one with a matching name, and if one isn’t found, I would then look through the rest of the data tree for a matching name. I know that for a variable to be referenced by a name like that it has to be defined in the file before it’s used, I’m not sure if it’s particular about heirarchy or if all variable names are supposed to be unique in the file. Anyway, I don’t think this would be a big issue to support this format feature, depending on the below stuff. The page I linked to for format information actually uses a similar object as an example (which I hadn’t noticed before today) so it’s probably pretty common to have some stuff defined globally.

Things to know before I try to implement this.

Can two different materials (most likely inside different Mesh or Frame sections) have the same name, or are the variable names intended to be unique?

Assuming names are unique, in a multi mesh object, should the second mesh that is defined be able to make reference to the first meshes materials by name, or should it only be able to reference those materials defined globally (such as the example posted above).

Can a variable be defined anywhere, or just globally or can a material be defined inside frame (or for example inside animationset)? I’m praying it’s global only.

Can any variable be a reference to a pre-defined variable (I believe yes, but I haven’t tested this out).

Just a note but gamespacelight didn’t like it when I tried my { Zero } example with the transform matrix. That could mean that it’s not necessary to watch out for variables referenced by name all the time, or that you can’t define a variable like I did using a primative type.

Hey!

great thing! :slight_smile:

I hope you’ve found your way to load animation, bones, weights… :slight_smile:

Probably is the hardest part (I am not a coder)

You have sample character animated x models at psionic3d.co.uk
Totally free (there are other per pay, of course)

Also, you have an x model in Irrlicht main package (but beware, it has a keyframe every frame, which is not usual, -nor clever for a game- , it is so as was though for a quick game, in md2 format, as you know, md2 it’s not dealed with bones keyframes) at irrlicht.sf.net

The irrlicht engine author made an x loading code for it. You may ask him for help about it, he’s a very gentle person. Or at least, you may see the code he used for it, as a clue.

Please…please…make it in a license of the type that could jME java game engine use it… I’m an artist (worked already in some companies) planning to develop a game with a great java coder friend of mine… We can’t make a big game as we’re only two, but we plan to do that little with quality…

The x boned and weighted format would be really great to have…Most packages have export of x, via free plugins. (Maya, Max, XSI(I have this), Character Fx, Lightwave, Hash AM(comercial plugin), Blender, and many others have it…)

I think it’d benefit jME and any Java engine a lot…

BTW, the dwarf included in…(a bit embarrased about it) … Irrlicht engine… I made it. It was made as a joke for some friends that wanted to code a mini game in some days, southpark-like characters in medieval times…a joke. (and a really quick/dirty made)

So , the model is a bit crappy, but served Niko (Irrlicht author) for his testing and coding.

I have also helped a lot with conversions, explanations, tutorials, in those open source engine forums, about the x format, how to make the exports (artist side, only) , and many, but many 3d related questions, as I have been several years, and made this somewhat my profession to make a living of it. And I like to think that I can sometimes if time allows(lately it doe snot allow so much, sadly), give some help.

I helped making the x exporter for Blender (and now it works) , and with the testing/art side help for the Urgh engine, that also made a great work to allow import of x files into his engine.

is the most spread way to import animated character files in 3d packages. And it works in any platform, mac, linux, win…

  1. I had to tweak a demo code to make it compile.

public class Main {
 public static void main( String[] args ) {
    try {
      JFrame testFrame = new JFrame(); // **1** added JFrame variable
    ....


public class RenderingCanvas implements      GLEventListener, KeyListener {      
  Object gllock = new Object();   //**2** added instance
  GLCanvas canvas;
   ...

Then few comments about the source code.

  1. Using Vector, not ArrayList
    XModel has Vector instance, not ArrayList. Is it really necessary to use a java1.1 Vector instance here. It’s a implicitly synchronized list.

      public class XModelData {
         ... 
         public Vector FrameModelList = new Vector();
         ...
      }

Same applies to FrameModel class.

  1. loader is lowercased class name
    “com.gufbal.dan.xLoader.loader” is lowercased. Probably should be “com.gufbal.dan.xLoader.Loader” name.

I hadn’t been checking my yahoo mail lately (oops) and didn’t notice people were paying attention to this thread again.

My use of Vector was a matter of being familiar with it, and it’s ‘safe’, and despite being synchronized I never felt that it was making a big impact on speed.

On the package name, I’m not sure why I didn’t cap it, I read suns naming convention thingy sometime around 1997 or 1998 and while I always tried to use the conventions, I um, probably haven’t ever read it again since the first time (not to mention I’m damn sloppy anyway).

Probably the most important thing is, I have not made any improvements, changes, or what not to this chunk of code in at least 6 months, and since I’ve actually stopped developing games in java (despite now being the best time ever to be doing it thanks to java-gaming.org and a few others) it’s not likely to get much else done to it by me. The code is free to use, and if anyone wants to take it over in a more official capacity I can set it up so that they have access to the website that it’s currently hosted on, or change the pages to point to their own web site of choice. To insure a prompt response mail me dan -AT- gufbal com . I’m off to go make note of this on the website.

I’ve updated the loader, I no longer have a demo available for it, jogl makes my new computer angry so a demo will most likely use LWJGL or JME (over LWJGL) if I write one.

This version should handle the inlined materials without a problem, posted by jacko so long ago, as well as inlined meshes (and inlined frames… necessary for animation). This version also reads in the AnimationSet, AnimationKey and Animation portion of the files. Skeletal animation is possible, however the .x format is a little vague on what a bone is, basically every frame seems to be considered a bone (even a static model will have a frame… presumably just a main bone to the dx engine).

Anyway, thought I’d mention the update here, to my knowledge no one is actually using this for anything (including myself haha) but possibly someone has used it for reference purposes. A small note, loader.java has been changed to Loader.java, but I haven’t bothered to change my Vectors to arraylists…