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

From JPCT
Jump to: navigation, search
(Loading a 3ds file from Blender)
Line 5: Line 5:
 
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.
 
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.
  
 +
<code>
  
<code>
 
 
import com.threed.jpct.*;
 
import com.threed.jpct.*;
 
import javax.swing.*;
 
import javax.swing.*;
  
/**
 
Modified from Egon's HelloWorld program
 
*/
 
 
public class HelloWorldSoftware {
 
public class HelloWorldSoftware {
//world info
 
 
 
     private String[] textures = {"bman"};
 
     private String[] textures = {"bman"};
 
     private String thingName = "bman";
 
     private String thingName = "bman";
Line 29: Line 24:
  
 
     public HelloWorldSoftware() throws Exception {
 
     public HelloWorldSoftware() throws Exception {
 
 
         frame = new JFrame("Blender Model Loading");
 
         frame = new JFrame("Blender Model Loading");
 
         frame.setSize(350, 300);
 
         frame.setSize(350, 300);
Line 39: Line 33:
 
             TextureManager.getInstance().addTexture(textures[i] + ".jpg", new Texture("res/" + textures[i] + ".jpg"));
 
             TextureManager.getInstance().addTexture(textures[i] + ".jpg", new Texture("res/" + textures[i] + ".jpg"));
 
         }
 
         }
 
 
         thing = loadModel("res/" + thingName + ".3ds", thingScale);
 
         thing = loadModel("res/" + thingName + ".3ds", thingScale);
 
         thing.build();
 
         thing.build();
Line 49: Line 42:
 
     private void loop() throws Exception {
 
     private void loop() throws Exception {
 
         buffer = new FrameBuffer(350, 300, FrameBuffer.SAMPLINGMODE_NORMAL);
 
         buffer = new FrameBuffer(350, 300, FrameBuffer.SAMPLINGMODE_NORMAL);
 
 
         while (frame.isShowing()) {
 
         while (frame.isShowing()) {
 
             SimpleVector place = new SimpleVector();
 
             SimpleVector place = new SimpleVector();
Line 67: Line 59:
 
     private Object3D loadModel(String filename, float scale) {
 
     private Object3D loadModel(String filename, float scale) {
 
         Object3D[] model = Loader.load3DS(filename, scale);
 
         Object3D[] model = Loader.load3DS(filename, scale);
 
 
         Object3D o3d = new Object3D(0);
 
         Object3D o3d = new Object3D(0);
 
 
         Object3D temp = null;
 
         Object3D temp = null;
 
 
         for (int i = 0; i < model.length; i++) {
 
         for (int i = 0; i < model.length; i++) {
 
             temp = model[i];
 
             temp = model[i];
Line 80: Line 69:
 
             temp.setRotationMatrix(new Matrix());
 
             temp.setRotationMatrix(new Matrix());
 
             o3d = Object3D.mergeObjects(o3d, temp);
 
             o3d = Object3D.mergeObjects(o3d, temp);
 
 
         }
 
         }
 
         return o3d;
 
         return o3d;
 
     }
 
     }
 
 
}
 
}
 
</code>
 
</code>

Revision as of 18:30, 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.*;

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.rotateY((float)Math.PI);
           temp.rotateMesh();
           temp.setRotationMatrix(new Matrix());
           o3d = Object3D.mergeObjects(o3d, temp);
       }
       return o3d;
   }

}