[Solved] Problem with applying texture to a model

hi ,

everyone



I have a a very simple model which does not have any textures applied to it.



what i am trying to do  is to put in a texture once the model has been loaded.



However for sum reason , it is appearing as white  ,



Please help , I have been trying this from past 6 hrs …  :frowning:



if you want i can post the model and the pic used as tex …





Here is a test case reproducing this behaviour





import com.jme.app.SimpleGame;
import com.jme.scene.Node;
import com.jme.scene.state.LightState;
import com.jme.scene.state.TextureState;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.TextureManager;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.system.DisplaySystem;
import com.jme.image.Texture;
import com.grandmaster.Game.skins.SkinManager;

import java.net.URL;

public class TestLoadModel extends SimpleGame {
    public static void main(String[] args) {
          new TestLoadModel().start();
    }

    protected void simpleInitGame() {
       SkinManager.getDefaultManager().LoadDefaultData();

       URL tmpPath =TestLoadModel.class.getClassLoader().getResource("data/models/jme/pawn.jme");
       URL skinPath =TestLoadModel.class.getClassLoader().getResource("data/textures/board/Wooden/dark.jpg");
       Node tmp = LoadModel(tmpPath,skinPath);
       rootNode.attachChild(tmp);
      
     
    }

     private Node LoadModel(URL Path,URL SkinPath ) {
         //Note only jme models loaded
        try {
            Node base;
            Node tmp;

            base = new Node();

           // Box trial;
            tmp = (Node) BinaryImporter.getInstance().load(Path.openStream());
           // trial = new Box(this.getName() + Math.random(), new Vector3f(0,0,0),3,3,4);
            //tmp = new Node();
            //tmp.attachChild(trial);
            //we need to check type...but for now a quick fix
             base.attachChild(tmp);
             tmp.setLocalScale(10.0f);
             tmp.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.PI / 2, new Vector3f(-1, 0, 0)));
             tmp.setLocalTranslation(0,0,0);
            //tmp.setRenderState(SkinManager.getDefaultManager().getTextureInfo(SkinEnum.Tex_ChessBoardBorder).getTextureState());
            // base.setRenderState(SkinManager.getDefaultManager().getTextureInfo(SkinEnum.Tex_ChessBoardBlackSide).getTextureState());
            //tmp.setLightCombineMode(LightState.OFF);
            TextureState tex = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
                 tex.setEnabled(true);
                 tex.setTexture(
                         TextureManager.loadTexture(
                                 SkinPath, Texture.MM_LINEAR,Texture.FM_LINEAR));

             tmp.setRenderState(tex);
            tmp.updateRenderState();
            return tmp;
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }
    }
}



[SOLVED]  :D--> See a few post below for details...

Sounds like your missing a light in your scene.

forgive me  if i am wrong but …Isnt a light always on in the simpleGame…???

Have checked out the few tests …



like TestCapsule.



They do not initialize any light explicitly…



but still get lit up…  :?

You're likely to be right, I havn't any source in front of me. I was going off the symtom of a white object, which is usually related to lighting.



Perhaps someone else could chime in on this one…

Looks like everything is correct loading-wise. Do you see the texture when applied to the box (the one that is commented out now)?

If yes, I'd assume that there is something funky in your .jme file. Where does that file come from? How did you make a .jme file out of it?

No , i do not see the textures being applied…



now about how i made the jme model…



i had a blend file , which i exported to 3ds and then converted it into the jme file… using model loader…



points to be noted 1) the blend file did not seem to have been textured…since i didnt make this and not beeing to familiar with it…



i just exported the whole thing to 3ds…  …



btw…it would be a great help if somebody could help me with the modelling part… since i am currenly using programmer art  :stuck_out_tongue: picked up form various places online… this is just a prototype …i intend to look into the art part later


AgentX said:

points to be noted 1) the blend file did not *seem* to have been textured...since i didnt make this and not beeing to familiar with it....

I think that's where your problem is. Your model does not have any texture coordinates, so the texcoords default to (0,0) - which is the lower left (or upper left if you flip the texture at load time) corner of the texture image.
That way the entire model gets colored in the color of the texture's pixel at (0,0).
But that theory does not explain why you don't see the texture on the Box.

hmm…



but even if the model is colored…



shouldnt an explicit texture state override that???




If I am right the model is textured. But only with the upper/or maybe lower left pixel of your texture image. That's what I meant when I said that the texture coordinates for all vertices default to (0,0).

BTW, if you are not sure how texture coordinates work, now would be the perfect time to read up on that :slight_smile:

Yeah… thanks for the tip , i will probly read up on that soon…



one thing  though … is it possible to completely remove the texture info from the model…



at least as a stop gap measure… since i need to dynamically texture the object deepening upon the state…



any ideas… it will really appreciated

yippeee…problem solved…thanks a lot to the tip by heveee…





the problem specifically was with the source model…it did not have the texture coordinate specified at all !!!..



Some info which really helped … kinda  :stuck_out_tongue:



http://en.wikipedia.org/wiki/Texture_mapping

http://en.wikipedia.org/wiki/UV_mapping









now the way to go about getting models is somthing like this…


  1. get the model in what ever format … anything importable by Wings3d

    ( on a side note …tried blender…found it too daunting and un intutive …but maybe its just me  :D)




  2. Export it to Obj file … you get 2 file…the obj itself and a file name MTL file…


  3. get a prog called UVMapper Classic from here --> UVMapper.com - downloads


  4. Open the obj file in it…


  5. Now if u see some lines should be fine …or better yet go to edit --> New UV map and  select the type of mapping


  6. Save the file…





    now use the test code below to load the model



import com.jme.app.AbstractGame;
import com.jme.app.SimpleGame;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.scene.Controller;
import com.jme.scene.TriMesh;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.converters.ObjToJme;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HelloModelLoading extends SimpleGame {
   private static final Logger logger = Logger.getLogger(HelloModelLoading.class.getName());

    public static void main(String[] args) {
        HelloModelLoading app = new HelloModelLoading();
        if (args.length > 0) {
            app.setModelToLoad(args[0]);
        }
        app.setDialogBehaviour(AbstractGame.FIRSTRUN_OR_NOCONFIGFILE_SHOW_PROPS_DIALOG);
        app.start();
    }

    private URL modelToLoad = null;

    private void setModelToLoad(String string) {
        try {
            modelToLoad = (new File(string)).toURI().toURL();
        } catch (MalformedURLException e) {
        }
    }

    protected void simpleInitGame() {
        if (modelToLoad == null) {
            modelToLoad = HelloModelLoading.class.getClassLoader().getResource("data/models/obj/rook.obj");
            System.out.println(modelToLoad.toString());
        }
        try {
            ObjToJme C1 = new ObjToJme();
            ByteArrayOutputStream BO = new ByteArrayOutputStream();
            C1.setProperty("mtllib",modelToLoad); //This is importang
            C1.convert(new BufferedInputStream(modelToLoad.openStream()), BO);
            final TriMesh r1 = (TriMesh)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            rootNode.attachChild(r1);
            r1.setLocalTranslation(new Vector3f(0,0,-50));
            TextureState ts= display.getRenderer().createTextureState();
            ts.setEnabled( true );
            ts.setTexture( TextureManager.loadTexture( HelloModelLoading.class
                .getClassLoader().getResource(
                "jmetest/data/images/Monkey.jpg" ),
                Texture.MM_LINEAR, Texture.FM_LINEAR ) );
            r1.setRenderState(ts);
            r1.updateRenderState();
            r1.addController( new Controller() {
                        private static final long serialVersionUID = 1L;
                        public void update( float time ) {
                                r1.getLocalRotation().fromAngleNormalAxis( timer.getTimeInSeconds(), new Vector3f(1,1,0) );
                        }
                    } );
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Failed to load file", e);
        }
    }
}



This should load the model ....with our beloved monkey  ;) on it .

hope it helps someone avoid banging their heads on white textures  :lol:


regds
AgentX