[Solved] Adding textures to nodes using class results in "java fatal error"?

Hello!



I'm working on this school project using jme to create a game. My teacher want me to have as little as possible in the SimplePhysicsGame class.



I use this code to add nodes to the root node:


for(Node n :LevelParser.getInstance().getFloors()) {
         rootNode.attachChild(n);  
      }
      
      for(Node n:LevelParser.getInstance().getDynamicNodes()) {
         rootNode.attachChild(n);
      }
      
      for(Node n :LevelParser.getInstance().getTargets()) {
          rootNode.attachChild(n);
      }



...and it works like a charm!

Then i created this class to add some textures to the nodes in the lists:

public class NodeDressup {

   static NodeDressup nodeDressup = null;

   private NodeDressup() {

   }

   public static NodeDressup getInstance() {
      if (nodeDressup == null) {
         nodeDressup = new NodeDressup();
      }
      return nodeDressup;
   }

   public TextureState getConcrete() {
      try {
         URL texture = new URL(new File("resources/textures/").toURI()
               .toURL()
               + "concrete.jpg");
         TextureState ts = GameLoop.getInstance().getTextureState();
         Texture t0 = TextureManager.loadTexture(texture,
               Texture.MinificationFilter.Trilinear,
               Texture.MagnificationFilter.Bilinear);
         t0.setWrap(Texture.WrapMode.Repeat);
         ts.setTexture(t0);
         return ts;
      } catch (MalformedURLException e) {
         return null;
      }
   }

   public TextureState getWoody() {
      try {
         URL texture = new URL(new File("resources/textures/").toURI()
               .toURL()
               + "wood.jpg");
         TextureState ts = GameLoop.getInstance().getTextureState();
         Texture t0 = TextureManager.loadTexture(texture,
               Texture.MinificationFilter.Trilinear,
               Texture.MagnificationFilter.Bilinear);
         t0.setWrap(Texture.WrapMode.Repeat);
         ts.setTexture(t0);
         return ts;
      } catch (MalformedURLException e) {
         return null;
      }
   }

   public TextureState getMetal() {
      try {
         URL texture = new URL(new File("resources/textures/").toURI()
               .toURL()
               + "metal.jpg");
         TextureState ts = GameLoop.getInstance().getTextureState();
         Texture t0 = TextureManager.loadTexture(texture,
               Texture.MinificationFilter.Trilinear,
               Texture.MagnificationFilter.Bilinear);
         t0.setWrap(Texture.WrapMode.Repeat);
         ts.setTexture(t0);
         return ts;
      } catch (MalformedURLException e) {
         return null;
      }
   }
}



I also have this function in my SimplePhysicsGame class:

public TextureState getTextureState() {
      TextureState a = display.getRenderer().createTextureState();
      return a;
   }



But when i try to apply a texture to a node any of the functions in node dressup, for example:

n.setRenderState(NodeDressup.getInstance().getWoody());


Java crashes and prints this error to the console:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x776f5be2, pid=5236, tid=5900
#
# JRE version: 6.0_18-b07
# Java VM: Java HotSpot(TM) Client VM (16.0-b13 mixed mode, sharing windows-x86 )



Any ideas?

Do you do these calls from the update() method? If your code is not running on the render thread it will cause problems (like when its called from the AWT thread through a swing button press).

Im calling these functions from the class that extends simplePhysicGame:



http://code.google.com/p/tda366-block-removal-3d/source/browse/trunk/BlockRemoval3D/src/core/GameLoop.java





Or if you have cvs installed, checkout: http://tda366-block-removal-3d.googlecode.com/svn/trunk/



don't know whats wrong :frowning:

No, what I mean is not in which class the method is but which thread calls n.setRenderState(NodeDressup.getInstance().getWoody());?

Because this can only be called in the render thread, meaning the thread that calls simpleInitGame() and simpleUpdate().



If you want to call this from anywhere (like a button in a window) and still have it happen in the render thread you will have to "wrap it up" in a Callable and submit it to the GameTaskQueueManager like this:



GameTaskQueueManager.getManager().update(new Callable<Void>() {
  public Void call() throws Exception {
      n.setRenderState(NodeDressup.getInstance().getWoody());
      return null;
  }
} );

Im just calling "n.setRenderState(NodeDressup.getInstance().getWoody());" from simpleInitGame(). Can't do that?

As said simpleInitGame() is called from the render thread so you should be able to use it there. Cant tell whats wrong there right now. Its strange that it dies cold like this, maybe the renderer doesnt like the format of the jpg files somehow?

normen said:

As said simpleInitGame() is called from the render thread so you should be able to use it there. Cant tell whats wrong there right now. Its strange that it dies cold like this, maybe the renderer doesnt like the format of the jpg files somehow?


Changed texture file format to png and it worked like a charm!

Thanks!

Interesting… I just ran across your post.  I have been using the JME2 snapshot for a game I've been working on and haven't had any troubles until I tried to get things going on a laptop I had floating around.



As soon as I did that I had a similar crash in the driver. 



After narrowing things down I found it seemed to be related to using jpg textures in the '.obj' (wavefront?) model loader.  When I changed the textures to ".png" things behave much better.  (Though I still swear I had a crash with png, but I might be mistaken – my ide might have been out of date).



The graphics card with issues ia s Intel GMA 4500MHD (embedded into the laptop).  My primary development box has an NVidia card in it which I have had absolutely no issues with.






I always make my textures power of two since I am used to problems if they are not.

normen said:

I always make my textures power of two since I am used to problems if they are not.


Seems to be prudent.  I believe I will follow your lead!