Load model with textures

hi,



I got a code here on the site of loading textures with models, but a message appears saying "Unable to locate url". But this url is correct!



loading the model is working correctly, only the textures that are not loaded …



I'll post my code and q error message:


static Spatial criaModelo (String urlModelo, String textura[])
    {
        
        Texture texturas[] = criaTextura (textura);

           for (int i=0; i<texturas.length;i++)
           {
               if (texturas[i] != null)
               {
                  //JOptionPane.showMessageDialog(null,i,"file binary", JOptionPane.INFORMATION_MESSAGE);
                   File textures = new File( texturas[i].getImageLocation() );
                   SimpleResourceLocator location=null;
                    try {
                        try {
                            location = new SimpleResourceLocator(textures.toURI().toURL());
                        } catch (MalformedURLException ex) {
                            Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } catch (URISyntaxException ex) {
                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, location );
               }
           }

        
           FormatConverter CONVERTER  = null;

           String modelFormat    = urlModelo.substring(urlModelo.lastIndexOf(".") + 1, urlModelo.length());


                     if (modelFormat.equalsIgnoreCase("3ds"))
                           CONVERTER = new MaxToJme();
         else
                        {
                            if (modelFormat.equalsIgnoreCase("obj"))
                               CONVERTER = new ObjToJme();
                            else
                            {
                                if (modelFormat.equalsIgnoreCase("x3d"))
                                {
                                    try
                                    {
                                       CONVERTER = new X3dToJme();
                                    }
                                     catch (Exception ex)
                                    {
                                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                 }
                              }
                         }



           Spatial Modelo=null;
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           FileInputStream rawIn = null;        


           //JOptionPane.showMessageDialog(null,"-1!","file binary", JOptionPane.INFORMATION_MESSAGE);
            try {
                rawIn = new FileInputStream(urlModelo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                //JOptionPane.showMessageDialog(null,"0!","file binary", JOptionPane.INFORMATION_MESSAGE);
                CONVERTER.convert(rawIn, outStream);
                //JOptionPane.showMessageDialog(null,"passou!","file binary", JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rawIn.close();
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
                ByteArrayInputStream convertedIn =   new ByteArrayInputStream(outStream.toByteArray());
            try {
                Modelo = (Spatial) BinaryImporter.getInstance().load(convertedIn);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
                Modelo.setModelBound(new BoundingSphere());
                Modelo.updateWorldBound();
                Modelo.updateModelBound();
                Modelo.updateRenderState();
                Modelo.updateGeometricState( 0, true );

                //JOptionPane.showMessageDialog(null,"1","file binary", JOptionPane.INFORMATION_MESSAGE);

             export(Modelo,urlModelo);

            // JOptionPane.showMessageDialog(null,"2!","file binary", JOptionPane.INFORMATION_MESSAGE);

            return Modelo;
    }

  static Texture textura (String url)
    {
        Texture textura = TextureManager.loadTexture(url,
            Texture.MinificationFilter.BilinearNearestMipMap,
            Texture.MagnificationFilter.Bilinear);

        return textura;
    }

         static Texture[] criaTextura (String urlTexturas[])
    {
             Texture texturas[] = new Texture [urlTexturas.length];
             for (int i=0;i<urlTexturas.length;i++)
                 if (urlTexturas[i] != null)
                     texturas[i] = textura(urlTexturas[i]);
                 else
                     break;

             return texturas;
    }



error message:

08/05/2010 21:48:26 com.jme.input.joystick.DummyJoystickInput <init>
INFO: Joystick support is disabled
08/05/2010 21:48:26 com.jme.system.lwjgl.LWJGLDisplaySystem <init>
INFO: LWJGL Display System created.
08/05/2010 21:48:28 com.jme.renderer.lwjgl.LWJGLRenderer <init>
INFO: LWJGLRenderer created. W: 800 H: 700        Version: 2.1.0
08/05/2010 21:48:28 com.jme.renderer.AbstractCamera <init>
INFO: Camera created.
08/05/2010 21:48:28 com.jme.scene.Node <init>
INFO: Node created.
08/05/2010 21:48:28 com.jme.scene.Node <init>
INFO: Node created.
08/05/2010 21:49:51 com.jme.util.resource.ResourceLocatorTool locateResource
WARNING: Unable to locate: D:Meus documentosNetBeansProjectsVisao3DbuildclassesModelossalasof

Hm when i was working with JME2 I had to create a SimpleREssourceLocator, whenever i was logina model, so it was able to find the textures



OBJ:  (special since u set some parameters)



private void precacheOBJ(String modelpath, String internalname) throws IOException {
        URL objFile = Thread.currentThread().getContextClassLoader().getResource(modelpath);
        ObjToJme converter=new ObjToJme();
        if(!servermode){
           int split = modelpath.lastIndexOf("/");
           String parentpath = modelpath.substring(0,split+1);
           URL parentdirectory = Thread.currentThread().getContextClassLoader().getResource(parentpath);
            System.out.println(parentdirectory);
             converter.setProperty("texdir",parentdirectory);
             converter.setProperty("mtllib",parentdirectory);
        }
        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        converter.convert(objFile.openStream(),BO);
        Node r = new Node();
        r.attachChild((Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray())));
        cachedtrimeshes.put(internalname,r);
   }




private void precache3ds(String modelpath, String internalname) throws IOException, URISyntaxException {
      URL objFile = Thread.currentThread().getContextClassLoader().getResource(modelpath);
      
      
      MaxToJme converter=new MaxToJme();
        if(!servermode){
           int split = modelpath.lastIndexOf("/");
           String parentpath = modelpath.substring(0,split+1);
           URL parentdirectory = Thread.currentThread().getContextClassLoader().getResource(parentpath);
           ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE,new SimpleResourceLocator(parentdirectory));
        }
        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        converter.convert(objFile.openStream(),BO);
        Node r = new Node();
        Spatial child = (Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
        Quaternion rotation = new Quaternion();
        rotation.fromAngles(FastMath.DEG_TO_RAD * -90,0,0);
        child.setLocalRotation(rotation);
        r.attachChild(child);
        cachedtrimeshes.put(internalname,r);
   }



Hope this points you in the right direction :)

it's worked!





but I had to change this part: "URL ParentDirectory"



My code is as follows:


 static Spatial criaModelo (String urlModelo)
    {

        //if(!servermode){
           int split = urlModelo.lastIndexOf("\");
           String parentpath = urlModelo.substring(0,split+1);
                URL parentdirectory = null;

                try
                {
                    parentdirectory = new File(parentpath).toURI().toURL();
                }
                catch (Exception e)
                {

                }
               
              //  JOptionPane.showMessageDialog(null,parentdirectory,"file binary", JOptionPane.INFORMATION_MESSAGE);
            try
            {
                ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, new SimpleResourceLocator(parentdirectory));
            }
            catch (URISyntaxException ex)
            {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
        //}


           FormatConverter CONVERTER  = null;

           String modelFormat    = urlModelo.substring(urlModelo.lastIndexOf(".") + 1, urlModelo.length());


                     if (modelFormat.equalsIgnoreCase("3ds"))
                           CONVERTER = new MaxToJme();
         else
                        {
                            if (modelFormat.equalsIgnoreCase("obj"))
                               CONVERTER = new ObjToJme();
                            else
                            {
                                if (modelFormat.equalsIgnoreCase("x3d"))
                                {
                                    try
                                    {
                                       CONVERTER = new X3dToJme();
                                    }
                                     catch (Exception ex)
                                    {
                                        Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
                                    }
                                 }
                              }
                         }



           Spatial Modelo=null;
           ByteArrayOutputStream outStream = new ByteArrayOutputStream();
           FileInputStream rawIn = null;        


           //JOptionPane.showMessageDialog(null,"-1!","file binary", JOptionPane.INFORMATION_MESSAGE);
            try {
                rawIn = new FileInputStream(urlModelo);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                //JOptionPane.showMessageDialog(null,"0!","file binary", JOptionPane.INFORMATION_MESSAGE);
                CONVERTER.convert(rawIn, outStream);
                //JOptionPane.showMessageDialog(null,"passou!","file binary", JOptionPane.INFORMATION_MESSAGE);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                rawIn.close();
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
                ByteArrayInputStream convertedIn =   new ByteArrayInputStream(outStream.toByteArray());
            try {
                Modelo = (Spatial) BinaryImporter.getInstance().load(convertedIn);
            } catch (IOException ex) {
                Logger.getLogger(ModeloBinario.class.getName()).log(Level.SEVERE, null, ex);
            }
                Modelo.setModelBound(new BoundingSphere());
                Modelo.updateWorldBound();
                Modelo.updateModelBound();
                Modelo.updateRenderState();
                Modelo.updateGeometricState( 0, true );

                //JOptionPane.showMessageDialog(null,"1","file binary", JOptionPane.INFORMATION_MESSAGE);

             export(Modelo,urlModelo);

            // JOptionPane.showMessageDialog(null,"2!","file binary", JOptionPane.INFORMATION_MESSAGE);

            return Modelo;
    }



thanks for your help!

problem solved!

:D