Author Topic: loader3ds object3D setTexture  (Read 2480 times)

Offline yxbnnl

  • byte
  • *
  • Posts: 19
    • View Profile
loader3ds object3D setTexture
« on: March 04, 2009, 11:10:48 am »
Hi:
   I have read all  Texture defined in 3ds file and add all Texture in TextureManager.Now I want to add Texture to Object3D.But I dont know
how to select the correct Texture to Object3D.

      code:
import java.awt.Color;
import java.io.File;

import com.threed.jpct.*;

import javax.swing.*;


public class HelloWorldSoftware {

   private World world;
   private FrameBuffer buffer;
   private Object3D car3ds;
   private Object3D[] car;
   private JFrame frame;
   TextureManager  texMan  = null;
   public static void main(String[] args) throws Exception {
      new HelloWorldSoftware().loop();
   }

   public HelloWorldSoftware() throws Exception {
      //Config.saveMemory = true;
      frame=new JFrame("Hello world");
      frame.setSize(800, 600);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      
      world = new World();
      world.setAmbientLight(0, 255, 0);
      texMan=TextureManager.getInstance();
      char c=File.separatorChar;
      File dir=new File("brick_3ds");
      String[] files=dir.list();
         for (int i=0; i<files.length; i++) {
            String name=files;
            if (name.toLowerCase().endsWith(".jpg")) {
               texMan.addTexture(name, new Texture("brick_3ds/"+name));
            }
         }
      
      car=Loader.load3DS("brick_3ds/brick.3DS",0.2f);
      if(car.length != 0){
         car3ds = car[0];
         //car3ds.setTexture("plant");
         car3ds.setTransparency(2);
         car3ds.setCulling(Object3D.CULLING_DISABLED);
         car3ds.rotateX(-(float)Math.PI/2f);
         car3ds.rotateMesh();
         car3ds.setRotationMatrix(new Matrix());
         //car3ds.setAdditionalColor(new Color(100,100,100));
         car3ds.build();
      }
      world.addObject(car3ds);
      world.getCamera().setPosition(50, -50, -5);
      world.getCamera().lookAt(car3ds.getTransformedCenter());
   }

   private void loop() throws Exception {
      buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);

      while (frame.isShowing()) {
         car3ds.rotateY(0.01f);
         buffer.clear(java.awt.Color.white);
         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);
   }
}


Thank you

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11777
    • View Profile
    • http://www.jpct.net
Re: loader3ds object3D setTexture
« Reply #1 on: March 04, 2009, 11:59:20 am »
You don't! If the names in the Manager are correct, the loader will do this for you. That's what the liked thread your other thread was dealing with. Or from the Javadocs:

Quote
The loader will read the textures' names out of the 3DS-file and will add dummy textures with these names to the texture-manager (If the name is already in use, no new dummy texture will be added). So one may load some textures and add them to the manager with the appropiate names before loading the 3DS-file or replace the ones generated by the loader after loading is finished. If the materials used in the file are not using textures, small, single colored textures will be generated that represent the diffuse colors of the materials. This method is for usage in applets/for accessing files via an URL.

Offline fireside

  • double
  • *****
  • Posts: 605
    • View Profile
Re: loader3ds object3D setTexture
« Reply #2 on: March 04, 2009, 06:09:07 pm »
I had trouble with this too.  What I do is load the texture with the same name as what is in the 3ds file before I load the 3ds file. 
Code: [Select]
TextureManager.getInstance().addTexture("tree4" + ".png", new Texture(this.getClass().getClassLoader().getResourceAsStream("tree4" + ".png")));
The 3ds must use no more than 8 letters for the name with a 3 letter file type.

Then load the 3ds file after that, and the texture gets applied automatically.
« Last Edit: March 04, 2009, 06:13:10 pm by fireside »
click here->Fireside 7 Games<-

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: loader3ds object3D setTexture
« Reply #3 on: March 05, 2009, 12:11:29 am »
A couple of things you might check.

Firstly, you are assuming in your code that all the texture files in your model end in ".jpg".  Your code:
Code: [Select]
         for (int i=0; i<files.length; i++) {
            String name=files;
            if (name.toLowerCase().endsWith(".jpg")) {
               texMan.addTexture(name, new Texture("brick_3ds/"+name));
            }
Make sure this is correct.  If there are other image formats used by your model, like .bmp or .png, include them too.

Secondly, make sure the filenames are exactly the same as you see from the output.  For example, if the output says:
Code: [Select]
Processing new material 08 - Default!
Texture named STOP3.BMP added to TextureManager!
Processing new material 11 - Default!
Texture named DOOR2.BMP added to TextureManager!
Make sure the images that you are loading are named exactly "STOP3.BMP" and "DOOR2.BMP" (capital letters might make a difference).

Thirdly, make sure you increase the maximum visible polygons to a high enough number.  If the number is too low, the model might look distorted, or like it is textured correctly.  For example, if your output says:
Code: [Select]
Processing object from 3DS-file: kaput
Object 'kaput_jPCT0' created using 14832 polygons and 7869 vertices.
Processing object from 3DS-file: tampon
Object 'tampon_jPCT1' created using 25904 polygons and 13611 vertices.
Processing object from 3DS-file: camurluk
Object 'camurluk_jPCT2' created using 15728 polygons and 8237 vertices.
Processing object from 3DS-file: camurluk_a
Object 'camurluk_a_jPCT3' created using 11888 polygons and 6206 vertices.
You would add up all the polygons (which comes to 68352, in this example).  Then make sure AT LEAST this many polygons may be visible at once.  Place a line like this near the top of your program:
Code: [Select]
Config.maxPolysVisible = 70000; // Larger number than all the polygons in the model.
If none of these things help, please post a screenshot for your output, and possibly a link to the model, if you don't mind.