Loading 3ds Models from Blender

From JPCT
Jump to: navigation, search

Loading a 3ds file from Blender is fairly straight forward. Below is a quick model which I made and did a simple texture on. If the image texture window is used during texturing the coordinates and name will be exported. The name needs to be 8 characters with three for the extension or some characters will be cut off and it will be harder to load into Jpct. It's a good idea to select all the vertices and hit cntrl-t after making your model in order to export triangles. I have exported quads and they have worked so far, however. I switch to object mode before I export. Then Hit File->export->3ds. I named this character bman and texture is called bman.jpg. Bman1.jpg

Now we can load it into a modified version of the Hello World program. Notice that the texture is loaded first. Also notice the loadmodel function which does a bunch of things, which are pretty much standard I guess, except for the rotations which are used to orient the blender model in the same way it was in Blender.


import com.threed.jpct.*;
import javax.swing.*;

public class HelloWorldSoftware {
    private String[] textures = {"bman"};
    private String thingName = "bman";
    private int thingScale = 1;//end 
    private World world;
    private FrameBuffer buffer;
    private Object3D thing;
    private JFrame frame;

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

    public HelloWorldSoftware() throws Exception {
        frame = new JFrame("Blender Model Loading");
        frame.setSize(350, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        world = new World();
        world.setAmbientLight(150, 150, 150);
        for (int i = 0; i < textures.length; ++i) {
            TextureManager.getInstance().addTexture(textures[i] + ".jpg", new Texture("res/" + textures[i] + ".jpg"));
        }
        thing = loadModel("res/" + thingName + ".3ds", thingScale);
        thing.build();
        world.addObject(thing);
        world.getCamera().setPosition(0, 0, -20);
        world.getCamera().lookAt(thing.getTransformedCenter());
    }

    private void loop() throws Exception {
        buffer = new FrameBuffer(350, 300, FrameBuffer.SAMPLINGMODE_NORMAL);
        while (frame.isShowing()) {
            SimpleVector place = new SimpleVector();
            buffer.clear(java.awt.Color.BLUE);
            world.renderScene(buffer);
            world.draw(buffer);
            buffer.update();
            buffer.display(frame.getGraphics());
            Thread.sleep(10);
        }
        buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
        buffer.dispose();
        frame.dispose();
        System.exit(0);
    }

    private Object3D loadModel(String filename, float scale) {
        Object3D[] model = Loader.load3DS(filename, scale);
        Object3D o3d = new Object3D(0);
        Object3D temp = null;
        for (int i = 0; i < model.length; i++) {
            temp = model[i];
            temp.setCenter(SimpleVector.ORIGIN);
            temp.rotateX((float)( -.5*Math.PI));
            temp.rotateMesh();
            temp.setRotationMatrix(new Matrix());
            o3d = Object3D.mergeObjects(o3d, temp);
            o3d.build();
        }
        return o3d;
    }
}