Nicest MD2 loader ever :)

I got fed up with those complex and ugly MD2 load examples, so I wrote my own.
Loads MD2 file + animation and plays it at given frame rate.

Just specify file path, animation options and a jogl GL handle and you’re off.

It was too big to post here, so get it here: http://www.furi.dk/source/Quake2Model.java


..clip..
// Open buffered stream for speed            
BufferedInputStream bis = new BufferedInputStream(url.openStream());
// Determine file size (not nice, but could'nt get url->file->url->stream to work :)
int fileSize = 0;            
long skipped = 0;
byte[] skipbuf = new byte[65536];
while (skipped != -1) {
   skipped = bis.read(skipbuf);
   fileSize += skipped;
}
bis = new BufferedInputStream(url.openStream());     
data = new byte[fileSize];
bis.read(data, 0, fileSize);
bis.close();
..clip..

Maybe you could do it this way:


bis = new BufferedInputStream(url.openStream());
baos = new ByteArrayOutputStream();
data = new byte[1024 * 8];
while(true) {
  read = bis.read(data);
  if (read < 0) break;
  baos.write(data, 0, read);
}
data = baos.toByteArray();
..continue..

Nice, it worked like a charm :slight_smile:

// Gregof

Yes, that is a common newbie bug. From the InputStream.read(byte[] b, int off, int len) javadoc:

[quote]Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.
[/quote]
It’s common that the number of bytes read is less than “len” when reading from the net, or from a jar.

Ahh yeah. You’re correct. Been a while since I streamed anything over the net, but now I remember :slight_smile:

I updated the source with this change, as well as added an option to reset the animation.

I’ll probably do vertex normal calculation next with support for tangent/binormals to make bumpmappable from a GLSL shader :slight_smile:
Using vertex arrays instead of immidiate mode commands are also on the list.

Oh. An ofcourse animated interpolated bounding spheres to get best collision with the model. (As good as a bounding sphere can be that is :))

I am not good on 3D and OpenGL or even in JOGL. Do you have a ready-to-run example and some animated model file to try this out. I was just able to tip you on java io streaming, but its nothing to do with 3d :slight_smile:

Ive always thought a 3d model stuff is something out of my league, but browsing through your loader.java its actually a quite understandable and simple. Well, im sure there are a lot more behing the corner, but still…

What happens to the textures, I did not see any bindtexture commands. Or do MD model file even contain any texture file information?

Very minor “fix” or more like a syntax sugar fix.


public void draw(GL gl) {
  // Get first frame or iterpolated anim frame
  animFrame frame;
  // Animate? 
  if (animate) {
    createInterpolatedFrame();
    frame = interpolatedFrame;
  } else {
    frame = frames[startFrame];
  }
..continue..
}

Then few comments about the java coding style.

  • use FirstUpperProperCase for class names, even for nested classes:
    private class AnimFrame
    private class Face

  • java starting brackets to the line, not c/c++ style. This is Sun’s java style:
    private class AnimFrame {

    }
    private int readByte {

    }
    for(int i…) {

    }

The MD2 model has support for a skin list, but I never saw a model that included it.

I load the textures manually and sets them before drawing the model. I’ve uploaded my simple jogl app framework which contains everything you need to get started.
Including examples on pixel/vertex shaders, MD2 animation w. weapons change, render to texture and more

Get it here: http://www.furi.dk/source/glframework.zip

You’re right about the class names. But I can’t use the java bracket convention. I’ve been programming C/C++ all my life and it’s just not working for me :slight_smile:

Thanks for the suggestions.

[quote]But I can’t use the java bracket convention. I’ve been programming C/C++ all my life and it’s just not working for me :slight_smile:
[/quote]
Same here, but I’ve been programming many languages all my life and the first thing I ever do when receiving code Sun formatted is to change the ident! It’s so easier to read!

This worked well (first one I got to work). Great for newbies ;D Thanks!

Added complete JOGL demobox example with weapons change and texture load at http://www.furi.dk/java :slight_smile:

Hi,

I looking into loading MD2 files into my engine. I’m already using JOGL and do not plan on switching to Xith. I’d like to see the source code of your JOGL demo box, but the link isn’t good anymore.

Alex