Difference between revisions of "Loading 3ds Models from Blender"

From JPCT
Jump to: navigation, search
(Created page with '== Loading a 3ds file from Blender == 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 ...')
 
(Loading a 3ds file from Blender)
Line 1: Line 1:
 
== Loading a 3ds file from Blender ==
 
== Loading a 3ds file from Blender ==
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.
+
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.
 
[[File:Bman1.jpg]]
 
[[File: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.*;
 +
 +
/**
 +
Modified code from Egon's HelloWorld program
 +
*/
 +
public class HelloWorldSoftware {
 +
//world info
 +
 +
    private String[] textures = {"bman"};
 +
    private String thingName = "bman";
 +
    private int thingScale = 10;//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", 1);
 +
        thing.build();
 +
        thing.setScale(thingScale);
 +
        world.addObject(thing);
 +
        world.getCamera().setPosition(25, 25, -5);
 +
        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);
 +
            setCamera();
 +
            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.rotateY((float)Math.PI);
 +
            temp.rotateMesh();
 +
            temp.setRotationMatrix(new Matrix());
 +
            o3d = Object3D.mergeObjects(o3d, temp);
 +
 +
        }
 +
        return o3d;
 +
    }
 +
 +
 +
    public void setCamera() {
 +
        SimpleVector tc = thing.getTransformedCenter();
 +
        SimpleVector pos = new SimpleVector(tc);
 +
        SimpleVector zAxis = thing.getZAxis();
 +
        SimpleVector yAxis = thing.getYAxis();
 +
        zAxis.scalarMul(20f);
 +
        yAxis.scalarMul(-5f);
 +
        pos.add(zAxis);
 +
        pos.add(yAxis);
 +
        world.getCamera().setPosition(pos);
 +
        world.getCamera().lookAt(tc);
 +
    }
 +
 +
}
 +
"

Revision as of 13:10, 5 April 2009

Loading a 3ds file from Blender

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.*;

/** Modified code from Egon's HelloWorld program

*/

public class HelloWorldSoftware { //world info

   private String[] textures = {"bman"};
   private String thingName = "bman";
   private int thingScale = 10;//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", 1);
       thing.build();
       thing.setScale(thingScale);
       world.addObject(thing);
       world.getCamera().setPosition(25, 25, -5);
       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);
           setCamera();
           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.rotateY((float)Math.PI);
           temp.rotateMesh();
           temp.setRotationMatrix(new Matrix());
           o3d = Object3D.mergeObjects(o3d, temp);
       }
       return o3d;
   }


   public void setCamera() {
       SimpleVector tc = thing.getTransformedCenter();
       SimpleVector pos = new SimpleVector(tc);
       SimpleVector zAxis = thing.getZAxis();
       SimpleVector yAxis = thing.getYAxis();
       zAxis.scalarMul(20f);
       yAxis.scalarMul(-5f);
       pos.add(zAxis);
       pos.add(yAxis);
       world.getCamera().setPosition(pos);
       world.getCamera().lookAt(tc);
   }

} "