Texture error while loading lens flares

Hello,

I wanna add lens flares to my scene. But whenever I try to do so, I keep getting a null pointer on the line indicated wit -->

        // Setup the lensflare textures.
        TextureState[] tex = new TextureState[4];
        tex[0] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[0].setTexture(
                TextureManager.loadTexture(
                SkyDome.class.getClassLoader().getResource(
                "images/flare1.png"),
                Texture.MM_LINEAR_LINEAR,
                Texture.FM_LINEAR,
                Image.RGBA8888,
                1.0f,
                true));
        tex[0].setEnabled(true);
 -->     tex[0].apply();
       
        tex[1] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[1].setTexture(
                TextureManager.loadTexture(
                SkyDome.class.getClassLoader().getResource(
                "images/flare2.png"),
                Texture.MM_LINEAR_LINEAR,
                Texture.FM_LINEAR));
        tex[1].setEnabled(true);
        tex[1].apply();
       
        tex[2] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[2].setTexture(
                TextureManager.loadTexture(
                SkyDome.class.getClassLoader().getResource(
                "images/flare3.png"),
                Texture.MM_LINEAR_LINEAR,
                Texture.FM_LINEAR));
        tex[2].setEnabled(true);
        tex[2].apply();
       
        tex[3] = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
        tex[3].setTexture(
                TextureManager.loadTexture(
                SkyDome.class.getClassLoader().getResource(
                "images/flare4.png"),
                Texture.MM_LINEAR_LINEAR,
                Texture.FM_LINEAR));
        tex[3].setEnabled(true);
        tex[3].apply();
       
        flare = LensFlareFactory.createBasicLensFlare("flare", tex);
       
        flare.setIntensity(0.5f);

SeySayux said:

I keep getting a null pointer on the line indicated wit -->


        tex[0].setEnabled(true);
 -->     tex[0].apply();



That's (almost) impossible! Could rebuild your project, then review/post the stacktrace? AFAIK there's very little chance that tex[0] is fine for setEnabled(), but the very next line, it is null. Unless you have overridden Texture.setEnabled() with something nasty. Maybe you have missed a line, or several, from your stacktrace because System.err was interrupted by System.out, or someting?

apply() makes GL calls; you're not in a GL context, therefore you get crashes.

You shouldn't need to call apply at all, it is automatically called when the states is needed to be applied when rendering.

Remove all apply() calls and you should be fine, just make sure to call updateRenderState on your spatial at the end.

Sorry, Momoko_Fan, but that doesn't really relate to the problem, which wasn't crashing due to threading issues, but a NullPointerException. Also, how do you know that the code is not run in a GL context? This is about LensFlare, and last time I looked at it, TestLensFlare had the textures loaded in exactly that way - the apply() calls are there to load the textures into the graphics card, I'm not entirely sure if that is only a pre-caching sort of thing, or even neccessary.

The NullPointerException is due to the thread being out of context. It is entirely possible that he is using StandardGame (as many others) and so he is likley to be out of the context.

The apply calls should only be made by the renderer to apply the state into GL; doc for RenderState.apply():

This function is defined in the RenderState that is actually used by the Renderer. It contains the code that, when executed, applies the render state for the given render system. This should only be called internally and not by users directly.

I'm using standardgame, and this stuff is located in a gamestate, which is afaik in the opengl thread. Whenever I do not use apply, the lens flares won't show up.



I did not override texture stuff.



I'll try what TestLensFlares gives, and I'll post the stack trace.

TestLensFlare doesn't seem to use tex.apply(). It works with testlensflare, but not with my app  :?

I'm using standardgame, and this stuff is located in a gamestate, which is afaik in the opengl thread

Only the render and update methods are guaranteed to run in the gl thread.

Whenever I do not use apply, the lens flares won't show up.

But apply causes a crash? How is that possible to use it then?
Momoko_Fan said:

Whenever I do not use apply, the lens flares won't show up.

But apply causes a crash? How is that possible to use it then?

I believe that I still know the difference between seeing lens flares and not seeing lens flares, right? I don't use apply (commented it out) and no lens flares appeared.

Well, as Momoko has said, it is very likely you are messing up with GL and that is the cause of the null pointer.



Post a stack trace of the error so we can help further.

Just because it's an NPE does not mean it is not a threading error.  In fact, if it is an NPE anywhere inside of the jME code, in the majority of cases it is a GL thread problem.

its probably coz u r not in the GL thread while calling apply(). apply() preloads the texture u just assigned, and this has be done in the GL thread~

So that means i'll need to start messing with callables…

If you are using StandardGame and are not using them, then that explains a LOT!  :-o

Callables solve it…



I had the same problem and it works now