Author Topic: help loading an md2 model in a java applet  (Read 3456 times)

Offline newjavadev

  • byte
  • *
  • Posts: 3
    • View Profile
help loading an md2 model in a java applet
« on: September 02, 2011, 02:40:28 pm »
hi, i'm not an entirely new java coder, but i'm fairly new to using jpct.

i pretty much understand how to use the Object3D, and i'm pretty good at just editing code however i can't get around to loading an object via java applet. :(

help is very appreciated, thank you so much.

btw i already know how to load lwjgl applets on my web and stuff like that, i just need a basic java code to load a md2 model because i'm stumped. i'm not very good yet, but i learn off tutorials and help! haha  thanks again in advance.
« Last Edit: September 02, 2011, 02:52:10 pm by newjavadev »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: help loading an md2 model in a java applet
« Reply #1 on: September 02, 2011, 11:47:48 pm »
I don't get the exact question...you are using the Loader-class to load an md2. It's actually pretty simple. Was that your question?

Offline newjavadev

  • byte
  • *
  • Posts: 3
    • View Profile
Re: help loading an md2 model in a java applet
« Reply #2 on: September 03, 2011, 04:08:31 am »
oh, i'm sorry my grammar isn't very good

what i'm trying to do is render an md2 model in a java applet on my website. now after reading all of your excellent tutorials and examples, i have managed to do the following:
load an md2 model via java application
load a 3d box via java applet (your example)

what i want is to be able to get my program i found that loads a md2 model in a java application turned into loading a md2 model via java applet.

basically, i want to know how to transform the code below so it will work as a java applet instead of an application. because when i try to tinker it, it gives me a ton of different errors i don't understand how to fix. :-(

Code: [Select]
import com.threed.jpct.*;
import javax.swing.*;
public class lawltest {
    private String[] textures = {"body"};
    private String thingName = "body";
    private int thingScale = 1;
    private World world;
    private FrameBuffer buffer;
    private Object3D thing;
    private TextureManager tm = TextureManager.getInstance();

    public static void main(String[] args) throws Exception {
        new lawltest().loop();
    }

    public lawltest() throws Exception {
        world = new World();
        world.setAmbientLight(150, 150, 150);
        for (int i = 0; i < textures.length; ++i) {
            tm.addTexture(textures[i] + ".png", new Texture("res/" + textures[i] + ".png"));
        }
        TextureInfo ti=new TextureInfo(tm.getTextureID(textures[0] + ".png"));
        thing = loadModel("res/" + thingName + ".md2", thingScale);
        thing.setTexture(ti);
       
        world.addObject(thing);
        world.getCamera().moveCamera(Camera.CAMERA_MOVEOUT,100);
        world.getCamera().lookAt(thing.getTransformedCenter());
    }

    private void loop() throws Exception {
        buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_GL_AA_2X);
        buffer.enableRenderer(IRenderer.RENDERER_OPENGL);
        buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
        while (!org.lwjgl.opengl.Display.isCloseRequested()) {
            buffer.clear(java.awt.Color.BLUE);
            world.renderScene( buffer );
            world.draw( buffer );
            buffer.update();
            buffer.displayGLOnly();
            thing.rotateY(0.02f);
            thing.rotateX(0.01f);
           
            Thread.sleep(10);
        }
        buffer.dispose();
        System.exit(0);
    }

    private Object3D loadModel(String filename, float scale) {
        Object3D temp = Loader.loadMD2(filename, scale);
        temp.build();
        return temp;
    }
}
« Last Edit: September 03, 2011, 11:01:01 am by newjavadev »

Offline qcrist

  • byte
  • *
  • Posts: 29
    • View Profile
Re: help loading an md2 model in a java applet
« Reply #3 on: September 03, 2011, 07:26:38 pm »
Here is basic applet code:
Everything else is the same as the normal window.


Code: [Select]
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.World;

public class jPCTApplet extends JApplet implements Runnable
{
private static final long serialVersionUID = 1L;

private FrameBuffer buffer = null;
private World world = null;
private boolean loop = true;

Canvas myCanvas;

public void init()
{
world = new World();
World.setDefaultThread( Thread.currentThread() );

buffer = new FrameBuffer( 600, 800, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY );
buffer.disableRenderer( IRenderer.RENDERER_SOFTWARE );
myCanvas = buffer.enableGLCanvasRenderer();

add( myCanvas, BorderLayout.CENTER);
myCanvas.setVisible( true );

new Thread(this).start();
}

@Override
public void paint( Graphics g )
{
buffer.clear(Color.black);

world.renderScene( buffer );
world.draw( buffer );
buffer.update();

buffer.displayGLOnly();
myCanvas.repaint();
}


@Override
public void run()
{
while (loop)
{
repaint();
try{
Thread.sleep(10);
}catch(Exception e){}
}
}

}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: help loading an md2 model in a java applet
« Reply #4 on: September 03, 2011, 08:25:43 pm »
And more on applets can be found in the wiki (including an example that loads some 3ds files): http://www.jpct.net/wiki/index.php/Applets

Personally, i would avoid using applets at all and use web start instead.

Offline newjavadev

  • byte
  • *
  • Posts: 3
    • View Profile
Re: help loading an md2 model in a java applet
« Reply #5 on: September 04, 2011, 05:57:22 am »
Maybe I'm just retarded? Or just a retarded beginner, but why the hell doesn't this work then?

Code: [Select]
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.IRenderer;
import com.threed.jpct.Loader;
import com.threed.jpct.Object3D;
import com.threed.jpct.World;

public class applettest extends JApplet implements Runnable
{
private static final long serialVersionUID = 1L;

private FrameBuffer buffer = null;
private World world = null;
private boolean loop = true;
private Object3D thing;
Canvas myCanvas;

public void init()
{
world = new World();
World.setDefaultThread( Thread.currentThread() );

thing = Loader.loadMD2("body.md2", 1);
thing.build();
world.addObject(thing);

buffer = new FrameBuffer( 600, 800, FrameBuffer.SAMPLINGMODE_HARDWARE_ONLY );
buffer.disableRenderer( IRenderer.RENDERER_SOFTWARE );
myCanvas = buffer.enableGLCanvasRenderer();

add( myCanvas, BorderLayout.CENTER);
myCanvas.setVisible( true );

new Thread(this).start();
}

@Override
public void paint( Graphics g )
{
buffer.clear(Color.black);

world.renderScene( buffer );
world.draw( buffer );
buffer.update();

buffer.displayGLOnly();
myCanvas.repaint();
}


@Override
public void run()
{
while (loop)
{
repaint();
try{
Thread.sleep(10);
}catch(Exception e){}
}
}
}

Error I receive:
Loading file body.md2
[ Sat Sep 03 20:55:00 PDT 2011 ] - ERROR: Couldn't read file body.md2
[ Sat Sep 03 20:55:00 PDT 2011 ] - ERROR: Not a valid MD2-file!
Magic number: -1
Version: -1
Skin width: -1
Skin height: -1
Frame size: -1
Number of skins: -1
Number of Vertices: -1
Number of Texture coordinates: -1
Number of triangles: -1
Number of GL-commands: -1
Number of Frames: -1
java.lang.NegativeArraySizeException
   at com.threed.jpct.Loader.loadMD2(Unknown Source)
   at com.threed.jpct.Loader.loadMD2(Unknown Source)
   at applettest.init(applettest.java:27)
   at sun.applet.AppletPanel.run(Unknown Source)
   at java.lang.Thread.run(Unknown Source)

The freakin model is there, so why is it having problems loading it? Thanks again for the help guys. :)

Offline qcrist

  • byte
  • *
  • Posts: 29
    • View Profile
Re: help loading an md2 model in a java applet
« Reply #6 on: September 04, 2011, 03:04:23 pm »
Make sure the file is in the same directory that you get when you run:

new File(".").getAbsolutePath()

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: help loading an md2 model in a java applet
« Reply #7 on: September 04, 2011, 07:49:31 pm »
Or put them into the jar and load them from there via an input stream. That's what the wiki example does that i linked to (not the one in the wiki text itself, but the one from the zip at the bottom of the page).