Applet model loading problem

Disclaimer: Self taught everything, constructive criticism welcome flames of ‘L2coden00b’ less so :slight_smile: I have searched the forums for topics on writing applets, use of the texturemanager and done much googling on applets and URL/sockets to see if I was doing something wrong and have come up empty handed, you are my last line of inquiry.



Hope someone can help me out with this, 2 days of shouting at the screen has not helped. I have been playing with simpleJMEApplet and have run into a problem. I want to load a mesh at runtime via javascript call to a method inside my applet for use as a simple browser model viewer. The applet loads and runs just fine and the method can be called inside simpleAppletSetup() and will do exactly as I ask it loading both a model and texture from URLs fed to it and adding them to the scene. However when I attempt to call the method from javascript at runtime nothing loads. Playing around with commenting out lines of code I discovered that if I only load the model and not the texture (ignore the texture URL completely) the model loads with a pure white texture(or lack thereof) however when the texture manager call is uncommented out, the mesh never loads at all.



The method I call:

[java]public void loadMesh(String itemName, String model, String tex)

{

log.info("Loading item: " + itemName);



//get the URLs from the model/texture

try

{

texPath = new URL(baseURL, tex);

modelPath = new URL(baseURL, model);

} catch (MalformedURLException ex)

{

log.warning(ex.toString());

}



//Loader and texture state

OgreLoader meshLoader = new OgreLoader();

TextureState ts = getRenderer().createTextureState();



try

{

if (modelPath != null && texPath != null)

{

mesh = meshLoader.loadModel(modelPath);

mesh.setModelBound(new BoundingBox());

mesh.updateModelBound();



log.info("Texture manager loading "+texPath);



Texture texture = TextureManager.loadTexture(

texPath,

Texture.MinificationFilter.Trilinear,

Texture.MagnificationFilter.Bilinear,

1.0f,

false);



log.info(“Assigning texture to mesh”);



if (texture != null)

{

ts.setTexture(texture);

ts.setEnabled(true);

mesh.setRenderState(ts);

}



log.info(“Model, texture state prepped, adding to base node”);



//Link it

base.attachChild(mesh);

getRootNode().updateRenderState();

}

else

{

log.warning(“Model or texture URL was null”);

}

} catch (IOException ex)

{

log.warning(ex.toString());

} catch (ModelFormatException ex)

{

log.warning(ex.toString());

}

}[/java]



The output in console when the method is called in simpleAppletSetup():



INFO: Texture manager loading http://localhost/Mesh/Test.png

network: Cache entry not found [url: http://localhost/Mesh/Test.png, version: null]

network: Connecting http://localhost/Mesh/Test.png with proxy=DIRECT

18-Feb-2011 10:09:07 AEapplet loadMesh

INFO: Assigning texture to mesh

18-Feb-2011 10:09:07 AEapplet loadMesh

INFO: Model, texture state prepped, adding to base node

18-Feb-2011 10:09:07 com.jme.scene.Node attachChild

INFO: Child “Test” attached to this node “Base Node”

18-Feb-2011 10:09:07 AEapplet loadMesh



The output when called from the javascript in the browser:



INFO: Texture manager loading http://localhost/Mesh/Test.png

network: Cache entry not found [url: http://localhost/Mesh/Test.png, version: null]

network: Connecting http://localhost/Mesh/Test.png with proxy=DIRECT



No error, no nothing, it simply stops there and the applet still runs away happily sans my model.

Running latest java update, jme2, a wamp server for local testing but have also uploaded it to the web for testing and get the same result. All jars are signed and the applet launches using jnlp webstart.



Thanks for any help

It may be bad form to reply to ones own post, but since it was moved I no longer seem to be able to edit it for some reason.



Anywho I have solved my problem, after changing the catch clause of the try block I posted to catch any and all Exceptions not just the two above (durr) I was given the answer to my woes. Despite being signed and afaik being granted as many permissions as I can manage the applet was being prevented from writing temporary files. Apparently when the method is called inside simpleAppletSetup() the texture manager IS allowed to write temporary files, but when the method was called via javascript it was no longer permitted. I wrote a supplimentary method to grab the texture I wanted from a URL using ImageI/O and what I could glean from reading the texture managers source, the result is below and works just fine for my purposes.

I can’t tell gurantee it’s efficient or the ‘correct’ way to get around the problem I was experiencing, but in the off chance it is helpful to someone else in my position here is the method I wrote which returns a texture object you can then apply to a texture state.



[java]private Texture getTex(URL texURL)

{

if(texURL != null)

{

try

{

javax.imageio.ImageIO.setUseCache(false);

BufferedImage image = ImageIO.read(texURL);



byte data[] = (byte[]) image.getRaster().getDataElements(

0,

0,

image.getWidth(),

image.getHeight(),

null

);



ByteBuffer buf = BufferUtils.createByteBuffer(data.length);

buf.clear();

buf.put(data);

buf.flip();

Image texImage = new com.jme.image.Image();

texImage.setFormat(Image.Format.RGBA8);

texImage.setWidth(image.getWidth());

texImage.setHeight(image.getHeight());

texImage.setData(buf);



Texture2D texture = new Texture2D();

texture.setAnisotropicFilterPercent(0);

texture.setMinificationFilter(Texture.MinificationFilter.Trilinear);

texture.setMagnificationFilter(Texture.MagnificationFilter.Bilinear);

texture.setImage(texImage);



return texture;

}catch (Exception ex)

{

log.warning(ex.toString());

}



return TextureState.getDefaultTexture();

}

else

{

return TextureState.getDefaultTexture();

}

}[/java]



Hope it helps someone out there and maybe saves them 3 days of headaches…or maybe that was just me.



o/

I think you just missed the “edit topic” button that is further up than the normal “edit post” buttons.